Skip to content

Commit

Permalink
add create region endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>
  • Loading branch information
RealHarshThakur committed Jul 3, 2024
1 parent 37ce76c commit 9d7ab4a
Show file tree
Hide file tree
Showing 3 changed files with 96 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 @@ -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)
}

}

0 comments on commit 9d7ab4a

Please sign in to comment.