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

Region connect/disconnect methods #197

Merged
merged 1 commit into from
Jul 3, 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
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)
}
}
Loading