Skip to content

Commit

Permalink
Merge pull request #38 from spectrocloud/context-ref
Browse files Browse the repository at this point in the history
Cluster context refactor draft.
  • Loading branch information
nikchern committed Jun 29, 2023
2 parents bd99db5 + 1bbdf70 commit b732211
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 101 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type V1Client struct {

// Cluster generic
GetClusterWithoutStatusFn func(string) (*models.V1SpectroCluster, error)
GetClusterFn func(uid string) (*models.V1SpectroCluster, error)
GetClusterFn func(scope string, uid string) (*models.V1SpectroCluster, error)
GetClusterKubeConfigFn func(uid string) (string, error)
GetClusterBackupConfigFn func(uid string) (*models.V1ClusterBackup, error)
GetClusterScanConfigFn func(uid string) (*models.V1ClusterComplianceScan, error)
Expand Down
49 changes: 24 additions & 25 deletions client/cluster.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package client

import (
"fmt"
"strings"

"github.com/spectrocloud/hapi/apiutil/transport"
hashboardC "github.com/spectrocloud/hapi/hashboard/client/v1"
"github.com/spectrocloud/hapi/models"
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"

"github.com/spectrocloud/palette-sdk-go/client/herr"
)

func (h *V1Client) DeleteCluster(uid string) error {
func (h *V1Client) DeleteCluster(scope string, uid string) error {
client, err := h.GetClusterClient()
if err != nil {
return err
}

cluster, err := h.GetCluster(uid)
cluster, err := h.GetCluster(scope, uid)
if err != nil || cluster == nil {
return err
}

var params *clusterC.V1SpectroClustersDeleteParams
switch cluster.Metadata.Annotations["scope"] {
switch scope {
case "project":
params = clusterC.NewV1SpectroClustersDeleteParamsWithContext(h.Ctx).WithUID(uid)
case "tenant":
Expand All @@ -33,19 +35,19 @@ func (h *V1Client) DeleteCluster(uid string) error {
return err
}

func (h *V1Client) ForceDeleteCluster(uid string, forceDelete bool) error {
func (h *V1Client) ForceDeleteCluster(scope string, uid string, forceDelete bool) error {
client, err := h.GetClusterClient()
if err != nil {
return err
}

cluster, err := h.GetCluster(uid)
cluster, err := h.GetCluster(scope, uid)
if err != nil || cluster == nil {
return err
}

var params *clusterC.V1SpectroClustersDeleteParams
switch cluster.Metadata.Annotations["scope"] {
switch scope {
case "project":
params = clusterC.NewV1SpectroClustersDeleteParamsWithContext(h.Ctx).WithUID(uid).WithForceDelete(&forceDelete)
case "tenant":
Expand All @@ -56,11 +58,11 @@ func (h *V1Client) ForceDeleteCluster(uid string, forceDelete bool) error {
return err
}

func (h *V1Client) GetCluster(uid string) (*models.V1SpectroCluster, error) {
func (h *V1Client) GetCluster(scope string, uid string) (*models.V1SpectroCluster, error) {
if h.GetClusterFn != nil {
return h.GetClusterFn(uid)
return h.GetClusterFn(scope, uid)
}
cluster, err := h.GetClusterWithoutStatus(uid)
cluster, err := h.GetClusterWithoutStatus(scope, uid)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,7 +138,7 @@ func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroClust
return clusters, nil
}

func (h *V1Client) GetClusterWithoutStatus(uid string) (*models.V1SpectroCluster, error) {
func (h *V1Client) GetClusterWithoutStatus(scope string, uid string) (*models.V1SpectroCluster, error) {
if h.GetClusterWithoutStatusFn != nil {
return h.GetClusterWithoutStatusFn(uid)
}
Expand All @@ -145,22 +147,19 @@ func (h *V1Client) GetClusterWithoutStatus(uid string) (*models.V1SpectroCluster
return nil, err
}

params := clusterC.NewV1SpectroClustersGetParamsWithContext(h.Ctx).WithUID(uid)
success, err := client.V1SpectroClustersGet(params)
// handle tenant context here cluster may be a tenant cluster
if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
params := clusterC.NewV1SpectroClustersGetParams().WithUID(uid)
success, err = client.V1SpectroClustersGet(params)
if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
return nil, nil
} else if err != nil {
return nil, err
}
//return nil, nil
var params *clusterC.V1SpectroClustersGetParams

switch scope {
case "project":
params = clusterC.NewV1SpectroClustersGetParamsWithContext(h.Ctx).WithUID(uid)
case "tenant":
params = clusterC.NewV1SpectroClustersGetParams().WithUID(uid)
default:
return nil, fmt.Errorf("invalid scope %s", scope)
}
if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
return nil, nil
} else if err != nil {

success, err := client.V1SpectroClustersGet(params)
if err != nil {
return nil, err
}

Expand Down
11 changes: 9 additions & 2 deletions client/cluster_aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import (
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"
)

func (h *V1Client) CreateClusterAks(cluster *models.V1SpectroAzureClusterEntity) (string, error) {
func (h *V1Client) CreateClusterAks(cluster *models.V1SpectroAzureClusterEntity, ClusterContext string) (string, error) {
client, err := h.GetClusterClient()
if err != nil {
return "", err
}

params := clusterC.NewV1SpectroClustersAksCreateParamsWithContext(h.Ctx).WithBody(cluster)
var params *clusterC.V1SpectroClustersAksCreateParams
switch ClusterContext {
case "project":
params = clusterC.NewV1SpectroClustersAksCreateParamsWithContext(h.Ctx).WithBody(cluster)
case "tenant":
params = clusterC.NewV1SpectroClustersAksCreateParams().WithBody(cluster)
}

success, err := client.V1SpectroClustersAksCreate(params.WithTimeout(90 * time.Second))
if err != nil {
return "", err
Expand Down
8 changes: 4 additions & 4 deletions client/cluster_location_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"
)

func (h *V1Client) GetClusterLocationConfig(uid string) (*models.V1ClusterLocation, error) {
if clusterStatus, err := h.GetClusterWithoutStatus(uid); err != nil {
func (h *V1Client) GetClusterLocationConfig(scope string, uid string) (*models.V1ClusterLocation, error) {
if clusterStatus, err := h.GetClusterWithoutStatus(scope, uid); err != nil {
return nil, err
} else if clusterStatus != nil && clusterStatus.Status != nil && clusterStatus.Status.Location != nil {
return clusterStatus.Status.Location, nil
Expand All @@ -28,8 +28,8 @@ func (h *V1Client) UpdateClusterLocationConfig(uid string, config *models.V1Spec
return err
}

func (h *V1Client) ApplyClusterLocationConfig(uid string, config *models.V1SpectroClusterLocationInputEntity) error {
if curentConfig, err := h.GetClusterLocationConfig(uid); err != nil {
func (h *V1Client) ApplyClusterLocationConfig(scope string, uid string, config *models.V1SpectroClusterLocationInputEntity) error {
if curentConfig, err := h.GetClusterLocationConfig(scope, uid); err != nil {
return err
} else if curentConfig == nil {
return h.UpdateClusterLocationConfig(uid, config)
Expand Down
14 changes: 8 additions & 6 deletions client/datavolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"
)

func (h *V1Client) CreateDataVolume(uid string, name string, body *models.V1VMAddVolumeEntity) (string, error) {
func (h *V1Client) CreateDataVolume(scope string, uid string, name string, body *models.V1VMAddVolumeEntity) (string, error) {
if h.CreateDataVolumeFn != nil {
return h.CreateDataVolumeFn(uid, name, body)
}
Expand All @@ -18,7 +18,7 @@ func (h *V1Client) CreateDataVolume(uid string, name string, body *models.V1VMAd
}

// get cluster
cluster, err := h.GetCluster(uid)
cluster, err := h.GetCluster(scope, uid)
if err != nil {
return "", err
}
Expand All @@ -29,7 +29,6 @@ func (h *V1Client) CreateDataVolume(uid string, name string, body *models.V1VMAd
}

// get cluster scope
scope := cluster.Metadata.Annotations["scope"]
var params *clusterC.V1SpectroClustersVMAddVolumeParams
switch scope {
case "project":
Expand All @@ -49,7 +48,7 @@ func (h *V1Client) CreateDataVolume(uid string, name string, body *models.V1VMAd
return volume.AuditUID, nil
}

func (h *V1Client) DeleteDataVolume(uid string, namespace string, name string, body *models.V1VMRemoveVolumeEntity) error {
func (h *V1Client) DeleteDataVolume(scope string, uid string, namespace string, name string, body *models.V1VMRemoveVolumeEntity) error {
if h.DeleteDataVolumeFn != nil {
return h.DeleteDataVolumeFn(uid, namespace, name, body)
}
Expand All @@ -59,12 +58,15 @@ func (h *V1Client) DeleteDataVolume(uid string, namespace string, name string, b
}

// get cluster
cluster, err := h.GetCluster(uid)
cluster, err := h.GetCluster(scope, uid)
if err != nil {
return err
}
if cluster == nil {
return fmt.Errorf("cluster not found for scope %s and uid %s", scope, uid)
}

// get cluster scope
scope := cluster.Metadata.Annotations["scope"]
var params *clusterC.V1SpectroClustersVMRemoveVolumeParams
switch scope {
case "project":
Expand Down
22 changes: 12 additions & 10 deletions client/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"
)

func (h *V1Client) CreateVirtualMachine(uid string, body *models.V1ClusterVirtualMachine) (*models.V1ClusterVirtualMachine, error) {
func (h *V1Client) CreateVirtualMachine(scope string, uid string, body *models.V1ClusterVirtualMachine) (*models.V1ClusterVirtualMachine, error) {
client, err := h.GetClusterClient()
if err != nil {
return nil, err
}

// get cluster
cluster, err := h.GetCluster(uid)
cluster, err := h.GetCluster(scope, uid)
if err != nil {
return nil, err
}
Expand All @@ -26,7 +26,6 @@ func (h *V1Client) CreateVirtualMachine(uid string, body *models.V1ClusterVirtua
}

// get cluster scope
scope := cluster.Metadata.Annotations["scope"]
var params *clusterC.V1SpectroClustersVMCreateParams
switch scope {
case "project":
Expand All @@ -46,11 +45,11 @@ func (h *V1Client) CreateVirtualMachine(uid string, body *models.V1ClusterVirtua
return vm.Payload, nil
}

func (h *V1Client) GetVirtualMachine(uid string, namespace string, name string) (*models.V1ClusterVirtualMachine, error) {
func (h *V1Client) GetVirtualMachine(scope string, uid string, namespace string, name string) (*models.V1ClusterVirtualMachine, error) {
if h.GetVirtualMachineFn != nil {
return h.GetVirtualMachineFn(uid)
}
vm, err := h.GetVirtualMachineWithoutStatus(uid, name, namespace)
vm, err := h.GetVirtualMachineWithoutStatus(scope, uid, name, namespace)
if err != nil {
return nil, err
}
Expand All @@ -64,8 +63,8 @@ func (h *V1Client) UpdateVirtualMachine(cluster *models.V1SpectroCluster, vmName
}

clusterUid := cluster.Metadata.UID
// get cluster scope
scope := cluster.Metadata.Annotations["scope"]
// get cluster scope
var params *clusterC.V1SpectroClustersVMUpdateParams

// switch cluster scope
Expand All @@ -81,7 +80,7 @@ func (h *V1Client) UpdateVirtualMachine(cluster *models.V1SpectroCluster, vmName
params = params.WithUID(clusterUid).WithBody(body).WithNamespace(body.Metadata.Namespace).WithVMName(vmName)

// check if vm exists, return error
exists, err := h.IsVMExists(clusterUid, vmName, body.Metadata.Namespace)
exists, err := h.IsVMExists(scope, clusterUid, vmName, body.Metadata.Namespace)
if err != nil {
return nil, err
}
Expand All @@ -99,19 +98,22 @@ func (h *V1Client) UpdateVirtualMachine(cluster *models.V1SpectroCluster, vmName
//return nil, nil
}

func (h *V1Client) DeleteVirtualMachine(uid string, namespace string, name string) error {
func (h *V1Client) DeleteVirtualMachine(scope string, uid string, namespace string, name string) error {
client, err := h.GetClusterClient()
if err != nil {
return err
}

// get cluster
cluster, err := h.GetCluster(uid)
cluster, err := h.GetCluster(scope, uid)
if err != nil {
return err
}
if cluster == nil {
return fmt.Errorf("cluster not found for scope %s and uid %s", scope, uid)
}

// get cluster scope
scope := cluster.Metadata.Annotations["scope"]
var params *clusterC.V1SpectroClustersVMDeleteParams
switch scope {
case "project":
Expand Down
16 changes: 9 additions & 7 deletions client/virtual_machine_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import (
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"
)

func (h *V1Client) CloneVirtualMachine(clusterUid string, cloneVMFromName string, vmName string, vmNamespace string) error {
func (h *V1Client) CloneVirtualMachine(scope string, clusterUid string, cloneVMFromName string, vmName string, vmNamespace string) error {
client, err := h.GetClusterClient()
if err != nil {
return err
}

// get cluster
cluster, err := h.GetCluster(clusterUid)
cluster, err := h.GetCluster(scope, clusterUid)
if err != nil {
return err
}
if cluster == nil {
return errors.New("cluster not found")
}

body := &models.V1SpectroClusterVMCloneEntity{
CloneName: &vmName,
}
if err != nil {
return err
}
// get cluster scope
scope := cluster.Metadata.Annotations["scope"]

var params *clusterC.V1SpectroClustersVMCloneParams
switch scope {
case "project":
Expand Down
Loading

0 comments on commit b732211

Please sign in to comment.