Skip to content

Commit

Permalink
connect/disconnect region endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <[email protected]>
  • Loading branch information
RealHarshThakur committed Jul 3, 2024
1 parent 9d7ab4a commit 20b629e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ type Clienter interface {
// Regions
ListRegions() ([]Region, error)
CreateRegion(r *CreateRegionRequest) (*Region, error)
ConnectRegion(r *ConnectRegionRequest) error
DisconnectRegion(r *DisconnectRegionRequest) error

// Snapshots
// CreateSnapshot(name string, r *SnapshotConfig) (*Snapshot, error)
Expand Down Expand Up @@ -1017,6 +1019,16 @@ func (c *FakeClient) CreateRegion(r *CreateRegionRequest) (*Region, error) {
return &region, nil
}

// ConnectRegion implemented in a fake way for automated tests
func (c *FakeClient) ConnectRegion(r *ConnectRegionRequest) error {
return nil
}

// DisconnectRegion implemented in a fake way for automated tests
func (c *FakeClient) DisconnectRegion(r *DisconnectRegionRequest) error {
return nil
}

// CreateSnapshot implemented in a fake way for automated tests
// func (c *FakeClient) CreateSnapshot(name string, r *SnapshotConfig) (*Snapshot, error) {
// snapshot := Snapshot{
Expand Down
28 changes: 28 additions & 0 deletions region.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ type CreateRegionRequest struct {
Features map[string]bool `json:"features" `
}

// DisconnectRegionRequest is the request to disconnect a region
type DisconnectRegionRequest struct {
Code string `json:"code"`
}

// ConnectRegionRequest is the request to connect a region
type ConnectRegionRequest struct {
Code string `json:"code"`
}

// ListRegions returns all load balancers owned by the calling API account
func (c *Client) ListRegions() ([]Region, error) {
resp, err := c.SendGetRequest("/v2/regions")
Expand Down Expand Up @@ -129,3 +139,21 @@ func (c *Client) CreateRegion(r *CreateRegionRequest) (*Region, error) {

return &region, nil
}

// ConnectRegion connects a region to CivoAPI
func (c *Client) ConnectRegion(r *ConnectRegionRequest) error {
_, err := c.SendPostRequest("/v2/regions/connect", r)
if err != nil {
return decodeError(err)
}
return nil
}

// DisconnectRegion disconnects a region to CivoAPI
func (c *Client) DisconnectRegion(r *DisconnectRegionRequest) error {
_, err := c.SendPostRequest("/v2/regions/disconnect", r)
if err != nil {
return decodeError(err)
}
return nil
}
32 changes: 32 additions & 0 deletions region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,35 @@ func TestCreateRegion(t *testing.T) {
}

}

func TestConnectRegion(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/regions/connect": `{code: "TEST1"}`,
})
defer server.Close()

connectRegionRequest := &ConnectRegionRequest{
Code: "TEST1",
}

err := client.ConnectRegion(connectRegionRequest)
if err != nil {
t.Errorf("Request returned an error: %s", err)
}
}

func TestDisconnectRegion(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/regions/disconnect": `{code: "TEST1"}`,
})
defer server.Close()

disconnectRegionRequest := &DisconnectRegionRequest{
Code: "TEST1",
}

err := client.DisconnectRegion(disconnectRegionRequest)
if err != nil {
t.Errorf("Request returned an error: %s", err)
}
}

0 comments on commit 20b629e

Please sign in to comment.