Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support enable secure-by-default #450

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/container/containerv2/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type vpc struct {
type VPCs interface {
ListVPCs(target ClusterTargetHeader) ([]VPCConfig, error)
SetOutboundTrafficProtection(string, bool, ClusterTargetHeader) error
EnableSecureByDefault(string, bool, ClusterTargetHeader) error
}

func newVPCsAPI(c *client.Client) VPCs {
Expand Down Expand Up @@ -63,3 +64,20 @@ func (v *vpc) SetOutboundTrafficProtection(clusterID string, enable bool, target

return err
}

type EnableSecureByDefaultClusterRequest struct {
Cluster string `json:"cluster" binding:"required"`
DisableOutboundTrafficProtection bool `json:"disableOutboundTrafficProtection,omitempty"`
}

// Enable Secure by Default
func (v *vpc) EnableSecureByDefault(clusterID string, enable bool, target ClusterTargetHeader) error {
request := EnableSecureByDefaultClusterRequest{
Cluster: clusterID,
DisableOutboundTrafficProtection: enable,
}

_, err := v.client.Post("/network/v2/secure-by-default/enable", request, nil, target.ToMap())

return err
}
42 changes: 42 additions & 0 deletions api/container/containerv2/vpcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,48 @@ var _ = Describe("VPCs", func() {
})
})

// Enable secure by default
Describe("Enable Secure by Default", func() {
Context("When EnableSecureByDefault is successful", func() {
BeforeEach(func() {
server = ghttp.NewServer()
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPost, "/network/v2/secure-by-default/enable"),
ghttp.VerifyJSON(`{"cluster":"testCluster","disableOutboundTrafficProtection":true}`),
ghttp.RespondWith(http.StatusOK, ""),
),
)
})

It("should return with 200 OK", func() {
target := ClusterTargetHeader{}

err := newVPCs(server.URL()).EnableSecureByDefault("testCluster", true, target)
Expect(err).NotTo(HaveOccurred())
})
})
Context("When EnableSecureByDefault is unsuccessful", func() {
BeforeEach(func() {
server = ghttp.NewServer()
server.SetAllowUnhandledRequests(true)
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPost, "/network/v2/secure-by-default/enable"),
ghttp.VerifyJSON(`{"cluster":"testCluster"}`),
ghttp.RespondWith(http.StatusInternalServerError, ""),
),
)
})

It("should return with 500 Internal server error", func() {
target := ClusterTargetHeader{}
err := newVPCs(server.URL()).EnableSecureByDefault("testCluster", false, target)
Expect(err).To(HaveOccurred())
})
})
})

})

func newVPCs(url string) VPCs {
Expand Down