Skip to content

Commit 5997e07

Browse files
Check-in the karbon stubs (#14)
Move over karbon stubs from nutanix/terraform-provider-nutanix.
1 parent b5a2ebd commit 5997e07

10 files changed

+688
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Add the fc stubs from nutanix/terraform-provider-nutanix
1010
- Add the foundation stubs from nutanix/terraform-provider-nutanix
11+
- Add the karbon stubs from nutanix/terraform-provider-nutanix
1112

1213
### Changed
1314
- Updated the http client with the latest from github.com/nutanix/terraform-provider-nutanix

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ build:
77
test:
88
go test -v pkg/nutanix/fc/*.go
99
go test -v pkg/nutanix/foundation/*.go
10+
go test -v pkg/nutanix/karbon/*.go
1011
go test -v pkg/nutanix/v3/*.go
1112
go test -v pkg/nutanix/*.go

pkg/nutanix/karbon/karbon_api.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package karbon
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
client "github.com/nutanix-cloud-native/prism-go-client/pkg/nutanix"
8+
)
9+
10+
const (
11+
absolutePath = "karbon"
12+
userAgent = "nutanix"
13+
clientName = "karbon"
14+
)
15+
16+
// Client manages the V3 API
17+
type Client struct {
18+
client *client.Client
19+
Cluster ClusterService
20+
PrivateRegistry PrivateRegistryService
21+
Meta MetaService
22+
}
23+
24+
// NewKarbonAPIClient return a client to operate Karbon resources
25+
func NewKarbonAPIClient(credentials client.Credentials) (*Client, error) {
26+
var baseClient *client.Client
27+
28+
// check if all required fields are present. Else create an empty client
29+
if credentials.Username != "" && credentials.Password != "" && credentials.Endpoint != "" {
30+
c, err := client.NewClient(&credentials, userAgent, absolutePath, false)
31+
if err != nil {
32+
return nil, err
33+
}
34+
baseClient = c
35+
} else {
36+
errorMsg := fmt.Sprintf("karbon Client is missing. "+
37+
"Please provide required details - %s in provider configuration.", strings.Join(credentials.RequiredFields[clientName], ", "))
38+
baseClient = &client.Client{UserAgent: userAgent, ErrorMsg: errorMsg}
39+
}
40+
41+
f := &Client{
42+
client: baseClient,
43+
Cluster: ClusterOperations{
44+
client: baseClient,
45+
},
46+
PrivateRegistry: PrivateRegistryOperations{
47+
client: baseClient,
48+
},
49+
Meta: MetaOperations{
50+
client: baseClient,
51+
},
52+
}
53+
54+
return f, nil
55+
}

pkg/nutanix/karbon/karbon_api_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package karbon
2+
3+
import (
4+
"testing"
5+
6+
client "github.com/nutanix-cloud-native/prism-go-client/pkg/nutanix"
7+
)
8+
9+
func TestNewKarbonAPIClient(t *testing.T) {
10+
// verifies positive client creation
11+
cred := client.Credentials{
12+
URL: "foo.com",
13+
Username: "username",
14+
Password: "password",
15+
Port: "",
16+
Endpoint: "0.0.0.0",
17+
Insecure: true,
18+
FoundationEndpoint: "10.0.0.0",
19+
FoundationPort: "8000",
20+
RequiredFields: nil,
21+
}
22+
_, err := NewKarbonAPIClient(cred)
23+
if err != nil {
24+
t.Errorf(err.Error())
25+
}
26+
27+
// verify missing client scenario
28+
cred2 := client.Credentials{
29+
URL: "foo.com",
30+
Insecure: true,
31+
RequiredFields: map[string][]string{
32+
"karbon": {"username", "password", "endpoint"},
33+
},
34+
}
35+
v3Client2, err2 := NewKarbonAPIClient(cred2)
36+
if err2 != nil {
37+
t.Errorf(err2.Error())
38+
}
39+
40+
if v3Client2.client.ErrorMsg == "" {
41+
t.Errorf("NewKarbonAPIClient(%v) expected the base client in karbon client to have some error message", cred2)
42+
}
43+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package karbon
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
client "github.com/nutanix-cloud-native/prism-go-client/pkg/nutanix"
9+
)
10+
11+
// ClusterOperations ...
12+
type ClusterOperations struct {
13+
client *client.Client
14+
}
15+
16+
// Service ...
17+
type ClusterService interface {
18+
// karbon v2.1
19+
ListKarbonClusters() (*ClusterListIntentResponse, error)
20+
CreateKarbonCluster(createRequest *ClusterIntentInput) (*ClusterActionResponse, error)
21+
GetKarbonCluster(karbonClusterName string) (*ClusterIntentResponse, error)
22+
GetKarbonClusterNodePool(karbonClusterName string, nodePoolName string) (*ClusterNodePool, error)
23+
DeleteKarbonCluster(karbonClusterName string) (*ClusterActionResponse, error)
24+
GetKubeConfigForKarbonCluster(karbonClusterName string) (*ClusterKubeconfigResponse, error)
25+
GetSSHConfigForKarbonCluster(karbonClusterName string) (*ClusterSSHconfig, error)
26+
ScaleUpKarbonCluster(karbonClusterName, karbonNodepoolName string, scaleUpRequest *ClusterScaleUpIntentInput) (*ClusterActionResponse, error)
27+
ScaleDownKarbonCluster(karbonClusterName, karbonNodepoolName string, scaleDownRequest *ClusterScaleDownIntentInput) (*ClusterActionResponse, error)
28+
// registries
29+
ListPrivateRegistries(karbonClusterName string) (*PrivateRegistryListResponse, error)
30+
AddPrivateRegistry(karbonClusterName string, createRequest PrivateRegistryOperationIntentInput) (*PrivateRegistryResponse, error)
31+
DeletePrivateRegistry(karbonClusterName string, privateRegistryName string) (*PrivateRegistryOperationResponse, error)
32+
}
33+
34+
// karbon 2.1
35+
func (op ClusterOperations) ListKarbonClusters() (*ClusterListIntentResponse, error) {
36+
ctx := context.TODO()
37+
path := "/v1-beta.1/k8s/clusters"
38+
req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
39+
karbonClusterListIntentResponse := new(ClusterListIntentResponse)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
return karbonClusterListIntentResponse, op.client.Do(ctx, req, karbonClusterListIntentResponse)
45+
}
46+
47+
func (op ClusterOperations) CreateKarbonCluster(createRequest *ClusterIntentInput) (*ClusterActionResponse, error) {
48+
ctx := context.TODO()
49+
50+
path := "/v1/k8s/clusters"
51+
req, err := op.client.NewRequest(ctx, http.MethodPost, path, createRequest)
52+
karbonClusterActionResponse := new(ClusterActionResponse)
53+
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
return karbonClusterActionResponse, op.client.Do(ctx, req, karbonClusterActionResponse)
59+
}
60+
61+
func (op ClusterOperations) GetKarbonCluster(name string) (*ClusterIntentResponse, error) {
62+
ctx := context.TODO()
63+
64+
path := fmt.Sprintf("/v1/k8s/clusters/%s", name)
65+
req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
66+
karbonClusterIntentResponse := new(ClusterIntentResponse)
67+
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
return karbonClusterIntentResponse, op.client.Do(ctx, req, karbonClusterIntentResponse)
73+
}
74+
75+
func (op ClusterOperations) GetKarbonClusterNodePool(name string, nodePoolName string) (*ClusterNodePool, error) {
76+
ctx := context.TODO()
77+
78+
path := fmt.Sprintf("/v1-beta.1/k8s/clusters/%s/node-pools/%s", name, nodePoolName)
79+
80+
req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
81+
karbonClusterNodePool := new(ClusterNodePool)
82+
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
return karbonClusterNodePool, op.client.Do(ctx, req, karbonClusterNodePool)
88+
}
89+
90+
func (op ClusterOperations) DeleteKarbonCluster(name string) (*ClusterActionResponse, error) {
91+
ctx := context.TODO()
92+
93+
path := fmt.Sprintf("/v1/k8s/clusters/%s", name)
94+
95+
req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)
96+
karbonClusterActionResponse := new(ClusterActionResponse)
97+
98+
if err != nil {
99+
return nil, err
100+
}
101+
102+
return karbonClusterActionResponse, op.client.Do(ctx, req, karbonClusterActionResponse)
103+
}
104+
105+
func (op ClusterOperations) GetKubeConfigForKarbonCluster(name string) (*ClusterKubeconfigResponse, error) {
106+
ctx := context.TODO()
107+
108+
path := fmt.Sprintf("/v1/k8s/clusters/%s/kubeconfig", name)
109+
110+
req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
111+
karbonClusterKubeconfigResponse := new(ClusterKubeconfigResponse)
112+
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
return karbonClusterKubeconfigResponse, op.client.Do(ctx, req, karbonClusterKubeconfigResponse)
118+
}
119+
120+
func (op ClusterOperations) GetSSHConfigForKarbonCluster(name string) (*ClusterSSHconfig, error) {
121+
ctx := context.TODO()
122+
123+
path := fmt.Sprintf("/v1/k8s/clusters/%s/ssh", name)
124+
125+
req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
126+
karbonClusterSSHconfig := new(ClusterSSHconfig)
127+
128+
if err != nil {
129+
return nil, err
130+
}
131+
132+
return karbonClusterSSHconfig, op.client.Do(ctx, req, karbonClusterSSHconfig)
133+
}
134+
135+
func (op ClusterOperations) ListPrivateRegistries(karbonClusterName string) (*PrivateRegistryListResponse, error) {
136+
ctx := context.TODO()
137+
path := fmt.Sprintf("/v1-alpha.1/k8s/clusters/%s/registries", karbonClusterName)
138+
139+
req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
140+
karbonPrivateRegistryListResponse := new(PrivateRegistryListResponse)
141+
142+
if err != nil {
143+
return nil, err
144+
}
145+
146+
return karbonPrivateRegistryListResponse, op.client.Do(ctx, req, karbonPrivateRegistryListResponse)
147+
}
148+
149+
func (op ClusterOperations) AddPrivateRegistry(karbonClusterName string, createRequest PrivateRegistryOperationIntentInput) (*PrivateRegistryResponse, error) {
150+
ctx := context.TODO()
151+
path := fmt.Sprintf("/v1-alpha.1/k8s/clusters/%s/registries", karbonClusterName)
152+
153+
req, err := op.client.NewRequest(ctx, http.MethodPost, path, createRequest)
154+
karbonPrivateRegistryResponse := new(PrivateRegistryResponse)
155+
156+
if err != nil {
157+
return nil, err
158+
}
159+
160+
return karbonPrivateRegistryResponse, op.client.Do(ctx, req, karbonPrivateRegistryResponse)
161+
}
162+
163+
func (op ClusterOperations) DeletePrivateRegistry(karbonClusterName string, privateRegistryName string) (*PrivateRegistryOperationResponse, error) {
164+
ctx := context.TODO()
165+
path := fmt.Sprintf("/v1-alpha.1/k8s/clusters/%s/registries/%s", karbonClusterName, privateRegistryName)
166+
167+
req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)
168+
karbonPrivateRegistryOperationResponse := new(PrivateRegistryOperationResponse)
169+
170+
if err != nil {
171+
return nil, err
172+
}
173+
174+
return karbonPrivateRegistryOperationResponse, op.client.Do(ctx, req, karbonPrivateRegistryOperationResponse)
175+
}
176+
177+
func (op ClusterOperations) ScaleUpKarbonCluster(karbonClusterName, karbonNodepoolName string, scaleUpRequest *ClusterScaleUpIntentInput) (*ClusterActionResponse, error) {
178+
ctx := context.TODO()
179+
180+
path := fmt.Sprintf("/v1-alpha.1/k8s/clusters/%s/node-pools/%s/add-nodes", karbonClusterName, karbonNodepoolName)
181+
req, err := op.client.NewRequest(ctx, http.MethodPost, path, scaleUpRequest)
182+
karbonClusterActionResponse := new(ClusterActionResponse)
183+
184+
if err != nil {
185+
return nil, err
186+
}
187+
188+
return karbonClusterActionResponse, op.client.Do(ctx, req, karbonClusterActionResponse)
189+
}
190+
191+
func (op ClusterOperations) ScaleDownKarbonCluster(karbonClusterName, karbonNodepoolName string, scaleDownRequest *ClusterScaleDownIntentInput) (*ClusterActionResponse, error) {
192+
ctx := context.TODO()
193+
194+
path := fmt.Sprintf("/v1-alpha.1/k8s/clusters/%s/node-pools/%s/remove-nodes", karbonClusterName, karbonNodepoolName)
195+
req, err := op.client.NewRequest(ctx, http.MethodPost, path, scaleDownRequest)
196+
karbonClusterActionResponse := new(ClusterActionResponse)
197+
198+
if err != nil {
199+
return nil, err
200+
}
201+
202+
return karbonClusterActionResponse, op.client.Do(ctx, req, karbonClusterActionResponse)
203+
}

0 commit comments

Comments
 (0)