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

add create region endpoint #196

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 @@ -125,6 +125,7 @@ type Clienter interface {

// Regions
ListRegions() ([]Region, error)
CreateRegion(r *CreateRegionRequest) (*Region, error)

// Snapshots
// CreateSnapshot(name string, r *SnapshotConfig) (*Snapshot, error)
Expand Down Expand Up @@ -1005,6 +1006,17 @@ func (c *FakeClient) ListRegions() ([]Region, error) {
}, nil
}

// CreateRegion implemented in a fake way for automated tests
func (c *FakeClient) CreateRegion(r *CreateRegionRequest) (*Region, error) {
region := Region{
Code: r.Code,
Name: r.Code,
OutOfCapacity: false,
Country: r.CountryISOCode,
}
return &region, 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 @@ -33,6 +33,19 @@ type Feature struct {
PublicIPNodePools bool `json:"public_ip_node_pools"`
}

// CreateRegionRequest is the request to create a new region
type CreateRegionRequest struct {
Code string `json:"code"`
CountryISOCode string `json:"country_iso_code" `
Private bool `json:"private,omitempty"`
AccountIDs []string `json:"account_ids,omitempty"`
// Kubeconfig should be a base64 encoded kubeconfig content
Kubeconfig string `json:"kubeconfig"`
// ComputeSoftDeletionHours can only be configured for private regions.
ComputeSoftDeletionHours *int `json:"compute_soft_deletion_hours" `
Features map[string]bool `json:"features" `
}

// 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 @@ -101,3 +114,18 @@ func (c *Client) GetDefaultRegion() (*Region, error) {

return nil, errors.New("no default region found")
}

// CreateRegion is a function to create a region
func (c *Client) CreateRegion(r *CreateRegionRequest) (*Region, error) {
resp, err := c.SendPostRequest("/v2/regions", r)
if err != nil {
return nil, decodeError(err)
}

region := Region{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&region); err != nil {
return nil, err
}

return &region, nil
}
56 changes: 56 additions & 0 deletions region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,59 @@ func TestFindRegions(t *testing.T) {
t.Errorf("Expected %s, got %s", "New York 1", got.Name)
}
}

func TestCreateRegion(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/regions": `{
"code": "TEST1",
"country_iso_code": "US",
"private": false,
"account_ids": [],
"kubeconfig": "",
"compute_soft_deletion_hours": 24,
"features": {
"iaas": true,
"kubernetes": true,
"object_store": false,
"loadbalancer": false,
"dbaas": false,
"volume": true,
"paas": false,
"kfaas": false,
"public_ip_node_pools": false
}
}`,
})
defer server.Close()

createRegionRequest := &CreateRegionRequest{
Code: "TEST1",
CountryISOCode: "US",
Private: false,
AccountIDs: []string{},
Kubeconfig: "",
// ComputeSoftDeletionHours: utils.IntPtr(24),
Features: map[string]bool{
"iaas": true,
"kubernetes": true,
"object_store": false,
"loadbalancer": false,
"dbaas": false,
"volume": true,
"paas": false,
"kfaas": false,
"public_ip_node_pools": false,
},
}

got, err := client.CreateRegion(createRegionRequest)
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

if got.Code != "TEST1" {
t.Errorf("Expected %s, got %s", "TEST1", got.Code)
}

}
Loading