Skip to content

Commit

Permalink
Add network put update (#228)
Browse files Browse the repository at this point in the history
* update changelog & version in config

* Add PutUpdateNetwork

* update changelog

* bump minor version 3.14.0
  • Loading branch information
nvthongswansea authored Sep 25, 2023
1 parent 3dd5e32 commit 91edcef
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 91edcef

Please sign in to comment.