diff --git a/CHANGELOG.md b/CHANGELOG.md index bee8e8f..5b511df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 3.14.0 (Sept 25, 2023) + +FEATURES: +- Add network PUT update operation [#228](https://github.com/gridscale/gsclient-go/pull/228). + +## 3.13.0 (Jul 4, 2023) + +FEATURES: +- Support proxy protocol for loadbalancer backends [#227](https://github.com/gridscale/gsclient-go/pull/227) + ## 3.12.1 (May 19, 2023) BUG FIXES: diff --git a/config.go b/config.go index 243282a..da2309b 100644 --- a/config.go +++ b/config.go @@ -13,7 +13,7 @@ import ( const ( defaultMaxNumberOfRetries = 5 defaultDelayIntervalMilliSecs = 1000 - version = "3.12.1" + version = "3.14.0" defaultAPIURL = "https://api.gridscale.io" resourceActiveStatus = "active" requestDoneStatus = "done" diff --git a/network.go b/network.go index 2d4281f..41b2136 100644 --- a/network.go +++ b/network.go @@ -272,6 +272,32 @@ type NetworkUpdateRequest struct { DHCPReservedSubnet *[]string `json:"dhcp_reserved_subnet,omitempty"` } +// NetworkUpdatePutRequest represents a PUT request for updating a network. +type NetworkUpdatePutRequest struct { + // New name. + Name string `json:"name"` + + // L2Security. + L2Security bool `json:"l2security"` + + // List of labels. + Labels *[]string `json:"labels"` + + // Defines the information if dhcp is activated for this network or not. + DHCPActive *bool `json:"dhcp_active"` + + // The general IP Range configured for this network (/24 for private networks). + DHCPRange *string `json:"dhcp_range"` + + // The ip reserved and communicated by the dhcp service to be the default gateway. + DHCPGateway *string `json:"dhcp_gateway"` + + DHCPDNS *string `json:"dhcp_dns"` + + // Subrange within the ip range. + DHCPReservedSubnet *[]string `json:"dhcp_reserved_subnet"` +} + // PinnedServerList hold a list of pinned server with corresponding DCHP IP. type PinnedServerList struct { // List of server and it's assigned DHCP IP @@ -344,6 +370,22 @@ func (c *Client) UpdateNetwork(ctx context.Context, id string, body NetworkUpdat return r.execute(ctx, *c, nil) } +// PutUpdateNetwork updates a specific network based on given id. This method uses PUT mechanism, which means all fields +// must be provided, even if they are not changed. If you want to update only some fields, use UpdateNetwork method instead. +// +// See: https://gridscale.io/en//api-documentation/index.html#operation/updateNetwork +func (c *Client) PutUpdateNetwork(ctx context.Context, id string, body NetworkUpdatePutRequest) error { + if !isValidUUID(id) { + return errors.New("'id' is invalid") + } + r := gsRequest{ + uri: path.Join(apiNetworkBase, id), + method: http.MethodPatch, + body: body, + } + return r.execute(ctx, *c, nil) +} + // GetNetworkList gets a list of available networks. // // See: https://gridscale.io/en//api-documentation/index.html#operation/getNetworks diff --git a/network_test.go b/network_test.go index 12fa78b..18fa78b 100644 --- a/network_test.go +++ b/network_test.go @@ -114,6 +114,43 @@ func TestClient_UpdateNetwork(t *testing.T) { } } +func TestClient_PutUpdateNetwork(t *testing.T) { + server, client, mux := setupTestClient(true) + defer server.Close() + var isFailed bool + uri := path.Join(apiNetworkBase, dummyUUID) + mux.HandleFunc(uri, func(writer http.ResponseWriter, request *http.Request) { + writer.Header().Set(requestUUIDHeader, dummyRequestUUID) + if isFailed { + writer.WriteHeader(400) + } else { + if request.Method == http.MethodPatch { + fmt.Fprintf(writer, "") + } else if request.Method == http.MethodGet { + fmt.Fprint(writer, prepareNetworkHTTPGet("active")) + } + } + }) + for _, serverTest := range commonSuccessFailTestCases { + isFailed = serverTest.isFailed + for _, test := range uuidCommonTestCases { + err := client.PutUpdateNetwork( + emptyCtx, + test.testUUID, + NetworkUpdatePutRequest{ + Name: "test", + L2Security: false, + Labels: &[]string{"label"}, + }) + if test.isFailed || isFailed { + assert.NotNil(t, err) + } else { + assert.Nil(t, err, "PutUpdateNetwork returned an error %v", err) + } + } + } +} + func TestClient_DeleteNetwork(t *testing.T) { server, client, mux := setupTestClient(true) defer server.Close()