Skip to content

Commit

Permalink
Merge pull request #40 from spectrocloud/PLT-552
Browse files Browse the repository at this point in the history
PLT-552: cluster context fix for VMware and AWS.
  • Loading branch information
nikchern authored Jul 4, 2023
2 parents 71972f1 + 821b4c1 commit 336d6bd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
25 changes: 12 additions & 13 deletions client/cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -183,7 +184,7 @@ func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1Spec
return nil, nil
}

func (h *V1Client) GetClusterKubeConfig(uid string) (string, error) {
func (h *V1Client) GetClusterKubeConfig(uid string, ClusterContext string) (string, error) {
if h.GetClusterKubeConfigFn != nil {
return h.GetClusterKubeConfigFn(uid)
}
Expand All @@ -192,20 +193,18 @@ func (h *V1Client) GetClusterKubeConfig(uid string) (string, error) {
return "", err
}

builder := new(strings.Builder)
var params *clusterC.V1SpectroClustersUIDKubeConfigParams
switch ClusterContext {
case "project":
params = clusterC.NewV1SpectroClustersUIDKubeConfigParamsWithContext(h.Ctx).WithUID(uid)
case "tenant":
params = clusterC.NewV1SpectroClustersUIDKubeConfigParams().WithUID(uid)
default:
return "", errors.New("invalid cluster scope specified")
}

params := clusterC.NewV1SpectroClustersUIDKubeConfigParamsWithContext(h.Ctx).WithUID(uid)
builder := new(strings.Builder)
_, err = client.V1SpectroClustersUIDKubeConfig(params, builder)
// handle tenant context here cluster may be a tenant cluster
if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
params := clusterC.NewV1SpectroClustersUIDKubeConfigParams().WithUID(uid)
_, err = client.V1SpectroClustersUIDKubeConfig(params, builder)
if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
return "", nil
} else if err != nil {
return "", err
}
}
if err != nil {
if herr.IsNotFound(err) {
return "", nil
Expand Down
11 changes: 9 additions & 2 deletions client/cluster_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"
)

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

params := clusterC.NewV1SpectroClustersAwsCreateParamsWithContext(h.Ctx).WithBody(cluster)
var params *clusterC.V1SpectroClustersAwsCreateParams
switch ClusterContext {
case "project":
params = clusterC.NewV1SpectroClustersAwsCreateParamsWithContext(h.Ctx).WithBody(cluster)
case "tenant":
params = clusterC.NewV1SpectroClustersAwsCreateParams().WithBody(cluster)
}

success, err := client.V1SpectroClustersAwsCreate(params)
if err != nil {
return "", err
Expand Down
27 changes: 15 additions & 12 deletions client/cluster_backup_config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package client

import (
"errors"

"github.com/spectrocloud/hapi/models"
clusterC "github.com/spectrocloud/hapi/spectrocluster/client/v1"

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

func (h *V1Client) GetClusterBackupConfig(uid string) (*models.V1ClusterBackup, error) {
func (h *V1Client) GetClusterBackupConfig(uid, ClusterContext string) (*models.V1ClusterBackup, error) {
if h.GetClusterBackupConfigFn != nil {
return h.GetClusterBackupConfigFn(uid)
}
Expand All @@ -15,7 +18,17 @@ func (h *V1Client) GetClusterBackupConfig(uid string) (*models.V1ClusterBackup,
return nil, err
}

params := clusterC.NewV1ClusterFeatureBackupGetParamsWithContext(h.Ctx).WithUID(uid)
var params *clusterC.V1ClusterFeatureBackupGetParams
switch ClusterContext {
case "project":
params = clusterC.NewV1ClusterFeatureBackupGetParamsWithContext(h.Ctx).WithUID(uid)
case "tenant":
params = clusterC.NewV1ClusterFeatureBackupGetParams().WithUID(uid)
default:
return nil, errors.New("invalid cluster scope specified")

}

success, err := client.V1ClusterFeatureBackupGet(params)
if err != nil {
if herr.IsNotFound(err) || herr.IsBackupNotConfigured(err) {
Expand Down Expand Up @@ -48,13 +61,3 @@ func (h *V1Client) UpdateClusterBackupConfig(uid string, config *models.V1Cluste
_, err = client.V1ClusterFeatureBackupUpdate(params)
return err
}

func (h *V1Client) ApplyClusterBackupConfig(uid string, config *models.V1ClusterBackupConfig) error {
if policy, err := h.GetClusterBackupConfig(uid); err != nil {
return err
} else if policy == nil {
return h.CreateClusterBackupConfig(uid, config)
} else {
return h.UpdateClusterBackupConfig(uid, config)
}
}
11 changes: 9 additions & 2 deletions client/cluster_vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Cluster

func (h *V1Client) CreateClusterVsphere(cluster *models.V1SpectroVsphereClusterEntity) (string, error) {
func (h *V1Client) CreateClusterVsphere(cluster *models.V1SpectroVsphereClusterEntity, ClusterContext string) (string, error) {
if h.CreateClusterVsphereFn != nil {
return h.CreateClusterVsphereFn(cluster)
}
Expand All @@ -17,7 +17,14 @@ func (h *V1Client) CreateClusterVsphere(cluster *models.V1SpectroVsphereClusterE
return "", err
}

params := clusterC.NewV1SpectroClustersVsphereCreateParamsWithContext(h.Ctx).WithBody(cluster)
var params *clusterC.V1SpectroClustersVsphereCreateParams
switch ClusterContext {
case "project":
params = clusterC.NewV1SpectroClustersVsphereCreateParamsWithContext(h.Ctx).WithBody(cluster)
case "tenant":
params = clusterC.NewV1SpectroClustersVsphereCreateParams().WithBody(cluster)
}

success, err := client.V1SpectroClustersVsphereCreate(params)
if err != nil {
return "", err
Expand Down

0 comments on commit 336d6bd

Please sign in to comment.