|
| 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