Skip to content

Commit

Permalink
Merge pull request #15 from gm-data/master
Browse files Browse the repository at this point in the history
  • Loading branch information
skyscrapr authored Jul 31, 2023
2 parents ba0673a + 86017c8 commit 48c64d7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cloudability/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cloudability

import (
"encoding/json"
"strconv"
)

const containersEndpoint = "/containers/"

// ContainersEndpoint - Cloudability Containers Provisioning Endpoint
type ContainersEndpoint struct {
*v3Endpoint
}

// Containers - Cloudability ContainersProvisioning Endpoint
func (c *Client) Containers() *ContainersEndpoint {
return &ContainersEndpoint{newV3Endpoint(c, containersEndpoint)}
}

type Cluster struct {
ID int `json:"id,omitempty"`
ClusterName string `json:"clusterName"`
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
ClusterVersion string `json:"clusterVersion,omitempty"`
}

type clusterPayload struct {
ClusterName string `json:"clusterName"`
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
ClusterVersion string `json:"clusterVersion,omitempty"`
}

// GetContainer - Get an existing cluster by ID.
func (e ContainersEndpoint) GetContainer(id int) (*Cluster, error) {
var result v3Result[*Cluster]
err := e.get(e, "provisioning/"+strconv.Itoa(id), &result)
return result.Result, err
}

// NewContainers - Create a new Container Provisioning.
func (e *ContainersEndpoint) NewContainers(clusterProvisioning *Cluster) (*Cluster, error) {
clusterProvisioningPayload := new(clusterPayload)
jsonClusterProvisioning, _ := json.Marshal(clusterProvisioning)
json.Unmarshal(jsonClusterProvisioning, clusterProvisioningPayload)
var result v3Result[*Cluster]
err := e.post(e, "provisioning/", clusterProvisioningPayload, nil)
return result.Result, err
}

// UpdateContainers - Update an existing container by ID.
func (e *ContainersEndpoint) UpdateContainers(clusterProvisioning *Cluster) error {
payload := new(clusterPayload)
jsonContainersProvisioning, _ := json.Marshal(clusterProvisioning)
json.Unmarshal(jsonContainersProvisioning, payload)
return e.put(e, "provisioning/"+strconv.Itoa(clusterProvisioning.ID), payload)
}
66 changes: 66 additions & 0 deletions cloudability/containers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cloudability

import (
"net/url"
"testing"
)

func TestContainersProvisioningEndpoint(t *testing.T) {
testClient := NewClient("testapikey")
e := testClient.Containers()
if e.BaseURL.String() != apiV3URL {
t.Errorf("ContaintersProvisioningEndpoint BaseURL mismatch. Got %s. Expected %s", e.BaseURL.String(), apiV3URL)
}
if e.EndpointPath != containersEndpoint {
t.Errorf("ContaintersProvisioningEndpoint EndpointPath mismatch. Got %s. Expected %s", e.EndpointPath, containersEndpoint)
}
}

func TestGetCluster(t *testing.T) {
testServer := testAPI(t, "GET", "/containers/provisioning/1", nil)
defer testServer.Close()

testClient := NewClient("testapikey")
e := testClient.Containers()
e.BaseURL, _ = url.Parse(testServer.URL)
_, err := e.GetContainer(1)
if err != nil {
t.Fail()
}
}

func TestNewContainers(t *testing.T) {
cluster := &Cluster{
ClusterName: "test-cluster-name",
ClusterVersion: "test-cluster-version",
KubernetesVersion: "test-kubernetes-version",
}
testServer := testAPI(t, "POST", "/containers/provisioning", cluster)
defer testServer.Close()

testClient := NewClient("testapikey")
e := testClient.Containers()
e.BaseURL, _ = url.Parse(testServer.URL)
_, err := e.NewContainers(cluster)
if err != nil {
t.Fail()
}
}

func TestUpdateContainers(t *testing.T) {
cluster := &Cluster{
ClusterName: "test-cluster-name",
ClusterVersion: "test-cluster-version",
KubernetesVersion: "test-kubernetes-version",
}
testServer := testAPI(t, "PUT", "/containers/provisioning/0", cluster)
defer testServer.Close()

testClient := NewClient("testapikey")
e := testClient.Containers()
e.BaseURL, _ = url.Parse(testServer.URL)
err := e.UpdateContainers(cluster)
if err != nil {
t.Fail()
}
}

0 comments on commit 48c64d7

Please sign in to comment.