Skip to content

Commit

Permalink
Release palette 4.4.a (#475)
Browse files Browse the repository at this point in the history
* PLT-1253:Added cluster profile support in terrafrom import. (#460)

* PLT-790:Added import support for eks cluster (#461)

* PLT-794 (#462)

* PLT-792:Added import for vsphere cluster (#463)

* PLT-792:Added import for vsphere cluster

* Fixed unitest

* PLT-800:Added support for cluster profile in terrafrom import. (#465)

* PLT-800:Added support for cluster profile in terrafrom import.

* lint fix

* PLT-896: Updated registry_uid as mandatory for packs data source (#457)

* PLT-896: Updated registry_uid as mandatoray for packs data source

* updated go mod

* setting up types back

* Revert "PLT-896: Updated registry_uid as mandatory for packs data source (#457)"

This reverts commit b561564.

* Merging main (#473)

* Fix for release job (#468)

* update goreleaser version (#469)

* PRM-911: Fix for release v20 (#470)

* Prm 911 (#471)

* fix for goreleaser (#472)

---------

Co-authored-by: Seema Durrani <[email protected]>

* incorporated review comments

* added comment on code

---------

Co-authored-by: Seema Durrani <[email protected]>
  • Loading branch information
SivaanandM and seemadurrani authored Jun 24, 2024
1 parent 2e6f516 commit 33f6f17
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 27 deletions.
38 changes: 38 additions & 0 deletions spectrocloud/cluster_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,41 @@ func updateAgentUpgradeSetting(c *client.V1Client, d *schema.ResourceData) error
}
return nil
}

// This function is called during import cluster from palette to set default terraform value
func flattenCommonAttributeForClusterImport(c *client.V1Client, d *schema.ResourceData) error {
clusterProfiles, err := flattenClusterProfileForImport(c, d)
if err != nil {
return err
}
err = d.Set("cluster_profile", clusterProfiles)
if err != nil {
return err
}

err = d.Set("apply_setting", "DownloadAndInstall")
if err != nil {
return err
}
err = d.Set("force_delete", false)
if err != nil {
return err
}
err = d.Set("force_delete_delay", 20)
if err != nil {
return err
}
err = d.Set("os_patch_on_boot", false)
if err != nil {
return err
}
err = d.Set("pause_agent_upgrades", "unlock")
if err != nil {
return err
}
err = d.Set("skip_completion", false)
if err != nil {
return err
}
return nil
}
6 changes: 6 additions & 0 deletions spectrocloud/resource_cluster_aks_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func resourceClusterAksImport(ctx context.Context, d *schema.ResourceData, m int
return nil, fmt.Errorf("could not read cluster for import: %v", diags)
}

// cluster profile and common default cluster attribute is get set here
err = flattenCommonAttributeForClusterImport(c, d)
if err != nil {
return nil, err
}

// Return the resource data. In most cases, this method is only used to
// import one resource at a time, so you should return the resource data
// in a slice with a single element.
Expand Down
6 changes: 6 additions & 0 deletions spectrocloud/resource_cluster_aws_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func resourceClusterAwsImport(ctx context.Context, d *schema.ResourceData, m int
return nil, fmt.Errorf("could not read cluster for import: %v", diags)
}

// cluster profile and common default cluster attribute is get set here
err = flattenCommonAttributeForClusterImport(c, d)
if err != nil {
return nil, err
}

// Return the resource data. In most cases, this method is only used to
// import one resource at a time, so you should return the resource data
// in a slice with a single element.
Expand Down
69 changes: 67 additions & 2 deletions spectrocloud/resource_cluster_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func resourceClusterAzure() *schema.Resource {
ReadContext: resourceClusterAzureRead,
UpdateContext: resourceClusterAzureUpdate,
DeleteContext: resourceClusterDelete,
Description: "Resource for managing Azure clusters in Spectro Cloud through Palette.",
Importer: &schema.ResourceImporter{
StateContext: resourceClusterAzureImport,
},
Description: "Resource for managing Azure clusters in Spectro Cloud through Palette.",

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Expand Down Expand Up @@ -386,15 +389,71 @@ func resourceClusterAzureRead(_ context.Context, d *schema.ResourceData, m inter
d.SetId("")
return diags
}

// verify cluster type
err = ValidateCloudType("spectrocloud_cluster_azure", cluster)
if err != nil {
return diag.FromErr(err)
}
diagnostics, done := readCommonFields(c, d, cluster)
if done {
return diagnostics
}

return flattenCloudConfigAzure(cluster.Spec.CloudConfigRef.UID, d, c)
}
func flattenClusterConfigsAzure(config *models.V1AzureCloudConfig) []interface{} {
if config == nil || config.Spec == nil || config.Spec.ClusterConfig == nil {
return make([]interface{}, 0)
}
m := make(map[string]interface{})
if config.Spec.ClusterConfig.SubscriptionID != nil {
m["subscription_id"] = config.Spec.ClusterConfig.SubscriptionID
}
if config.Spec.ClusterConfig.ResourceGroup != "" {
m["resource_group"] = config.Spec.ClusterConfig.ResourceGroup
}
if config.Spec.ClusterConfig.Location != nil {
m["region"] = config.Spec.ClusterConfig.Location
}
if config.Spec.ClusterConfig.SSHKey != nil {
m["ssh_key"] = config.Spec.ClusterConfig.SSHKey
}
if config.Spec.ClusterConfig.StorageAccountName != "" {
m["storage_account_name"] = config.Spec.ClusterConfig.StorageAccountName
}
if config.Spec.ClusterConfig.ContainerName != "" {
m["container_name"] = config.Spec.ClusterConfig.ContainerName
}
if config.Spec.ClusterConfig.VnetResourceGroup != "" {
m["network_resource_group"] = config.Spec.ClusterConfig.VnetResourceGroup
}
if config.Spec.ClusterConfig.VnetName != "" {
m["virtual_network_name"] = config.Spec.ClusterConfig.VnetName
}
if config.Spec.ClusterConfig.VnetCidrBlock != "" {
m["virtual_network_cidr_block"] = config.Spec.ClusterConfig.VnetCidrBlock
}
if config.Spec.ClusterConfig.VnetResourceGroup != "" && config.Spec.ClusterConfig.VnetName != "" && config.Spec.ClusterConfig.VnetCidrBlock != "" {
if config.Spec.ClusterConfig.ControlPlaneSubnet != nil {
cpSubnet := map[string]interface{}{
"name": config.Spec.ClusterConfig.ControlPlaneSubnet.Name,
"cidr_block": config.Spec.ClusterConfig.ControlPlaneSubnet.CidrBlock,
"security_group_name": config.Spec.ClusterConfig.ControlPlaneSubnet.SecurityGroupName,
}
m["control_plane_subnet"] = []interface{}{cpSubnet}
}
if config.Spec.ClusterConfig.WorkerSubnet != nil {
workerSubnet := map[string]interface{}{
"name": config.Spec.ClusterConfig.WorkerSubnet.Name,
"cidr_block": config.Spec.ClusterConfig.WorkerSubnet.CidrBlock,
"security_group_name": config.Spec.ClusterConfig.WorkerSubnet.SecurityGroupName,
}
m["worker_node_subnet"] = []interface{}{workerSubnet}
}
}

return []interface{}{m}
}
func flattenCloudConfigAzure(configUID string, d *schema.ResourceData, c *client.V1Client) diag.Diagnostics {
ClusterContext := d.Get("context").(string)
if err := d.Set("cloud_config_id", configUID); err != nil {
Expand All @@ -403,6 +462,12 @@ func flattenCloudConfigAzure(configUID string, d *schema.ResourceData, c *client
if config, err := c.GetCloudConfigAzure(configUID, ClusterContext); err != nil {
return diag.FromErr(err)
} else {
if err := d.Set("cloud_account_id", config.Spec.CloudAccountRef.UID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("cloud_config", flattenClusterConfigsAzure(config)); err != nil {
return diag.FromErr(err)
}
mp := flattenMachinePoolConfigsAzure(config.Spec.MachinePoolConfig)
mp, err := flattenNodeMaintenanceStatus(c, d, c.GetNodeStatusMapAzure, mp, configUID, ClusterContext)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions spectrocloud/resource_cluster_azure_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/palette-sdk-go/client"
)

func resourceClusterAzureImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
c := m.(*client.V1Client)
err := GetCommonCluster(d, c)
if err != nil {
return nil, err
}

diags := resourceClusterAzureRead(ctx, d, m)
if diags.HasError() {
return nil, fmt.Errorf("could not read cluster for import: %v", diags)
}

// cluster profile and common default cluster attribute is get set here
err = flattenCommonAttributeForClusterImport(c, d)
if err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}
97 changes: 97 additions & 0 deletions spectrocloud/resource_cluster_azure_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package spectrocloud

import (
"github.com/spectrocloud/gomi/pkg/ptr"
"sort"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -274,3 +276,98 @@ func TestFlattenMachinePoolConfigsAzure(t *testing.T) {
assert.Equal(t, machinePools[0].NodeRepaveInterval, actual["node_repave_interval"])
assert.Equal(t, machinePools[0].Size, actual["count"])
}

func TestFlattenClusterConfigsAzure(t *testing.T) {
t.Run("NilConfig", func(t *testing.T) {
result := flattenClusterConfigsAzure(nil)
assert.Equal(t, []interface{}{}, result)
})

t.Run("EmptyConfig", func(t *testing.T) {
config := &models.V1AzureCloudConfig{}
result := flattenClusterConfigsAzure(config)
assert.Equal(t, []interface{}{}, result)
})

t.Run("PartialConfig", func(t *testing.T) {
config := &models.V1AzureCloudConfig{
Spec: &models.V1AzureCloudConfigSpec{
ClusterConfig: &models.V1AzureClusterConfig{
SubscriptionID: types.Ptr("test-subscription-id"),
ResourceGroup: "test-resource-group",
Location: types.Ptr("test-location"),
SSHKey: types.Ptr("test-ssh-key"),
StorageAccountName: "test-storage-account",
ContainerName: "test-container",
VnetResourceGroup: "test-network-resource-group",
VnetName: "test-vnet",
VnetCidrBlock: "10.0.0.0/16",
ControlPlaneSubnet: &models.V1Subnet{Name: "cp-subnet", CidrBlock: "10.0.1.0/24", SecurityGroupName: "cp-sg"},
WorkerSubnet: &models.V1Subnet{Name: "worker-subnet", CidrBlock: "10.0.2.0/24", SecurityGroupName: "worker-sg"},
},
},
}

expected := []interface{}{
map[string]interface{}{
"subscription_id": ptr.StringPtr("test-subscription-id"),
"resource_group": "test-resource-group",
"region": ptr.StringPtr("test-location"),
"ssh_key": ptr.StringPtr("test-ssh-key"),
"storage_account_name": "test-storage-account",
"container_name": "test-container",
"network_resource_group": "test-network-resource-group",
"virtual_network_name": "test-vnet",
"virtual_network_cidr_block": "10.0.0.0/16",
"control_plane_subnet": []interface{}{
map[string]interface{}{
"name": "cp-subnet",
"cidr_block": "10.0.1.0/24",
"security_group_name": "cp-sg",
},
},
"worker_node_subnet": []interface{}{
map[string]interface{}{
"name": "worker-subnet",
"cidr_block": "10.0.2.0/24",
"security_group_name": "worker-sg",
},
},
},
}

result := flattenClusterConfigsAzure(config)
sortSliceOfMaps(expected)
sortSliceOfMaps(result)
assert.Equal(t, expected, result)
})

t.Run("MissingFields", func(t *testing.T) {
config := &models.V1AzureCloudConfig{
Spec: &models.V1AzureCloudConfigSpec{
ClusterConfig: &models.V1AzureClusterConfig{
ResourceGroup: "test-resource-group",
Location: types.Ptr("test-location"),
},
},
}

expected := []interface{}{
map[string]interface{}{
"resource_group": "test-resource-group",
"region": ptr.StringPtr("test-location"),
},
}

result := flattenClusterConfigsAzure(config)
assert.Equal(t, expected, result)
})
}

func sortSliceOfMaps(slice []interface{}) {
sort.SliceStable(slice, func(i, j int) bool {
mapI := slice[i].(map[string]interface{})
mapJ := slice[j].(map[string]interface{})
return mapI["name"].(string) < mapJ["name"].(string)
})
}
6 changes: 6 additions & 0 deletions spectrocloud/resource_cluster_edge_native_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func resourceClusterEdgeNativeImport(ctx context.Context, d *schema.ResourceData
return nil, fmt.Errorf("could not read cluster for import: %v", diags)
}

// cluster profile and common default cluster attribute is get set here
err = flattenCommonAttributeForClusterImport(c, d)
if err != nil {
return nil, err
}

// Return the resource data. In most cases, this method is only used to
// import one resource at a time, so you should return the resource data
// in a slice with a single element.
Expand Down
15 changes: 13 additions & 2 deletions spectrocloud/resource_cluster_eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func resourceClusterEks() *schema.Resource {
ReadContext: resourceClusterEksRead,
UpdateContext: resourceClusterEksUpdate,
DeleteContext: resourceClusterDelete,
Description: "Resource for managing EKS clusters in Spectro Cloud through Palette.",
Importer: &schema.ResourceImporter{
StateContext: resourceClusterEksImport,
},
Description: "Resource for managing EKS clusters in Spectro Cloud through Palette.",

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Expand Down Expand Up @@ -417,7 +420,9 @@ func resourceClusterEksRead(_ context.Context, d *schema.ResourceData, m interfa
if config, err = c.GetCloudConfigEks(configUID, ClusterContext); err != nil {
return diag.FromErr(err)
}

if err := d.Set("cloud_account_id", config.Spec.CloudAccountRef.UID); err != nil {
return diag.FromErr(err)
}
cloudConfigFlatten := flattenClusterConfigsEKS(config)
if err := d.Set("cloud_config", cloudConfigFlatten); err != nil {
return diag.FromErr(err)
Expand All @@ -438,6 +443,12 @@ func resourceClusterEksRead(_ context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(err)
}

// verify cluster type
err = ValidateCloudType("spectrocloud_cluster_eks", cluster)
if err != nil {
return diag.FromErr(err)
}

diagnostics, done := readCommonFields(c, d, cluster)
if done {
return diagnostics
Expand Down
32 changes: 32 additions & 0 deletions spectrocloud/resource_cluster_eks_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/palette-sdk-go/client"
)

func resourceClusterEksImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
c := m.(*client.V1Client)
err := GetCommonCluster(d, c)
if err != nil {
return nil, err
}

diags := resourceClusterEksRead(ctx, d, m)
if diags.HasError() {
return nil, fmt.Errorf("could not read cluster for import: %v", diags)
}

// cluster profile and common default cluster attribute is get set here
err = flattenCommonAttributeForClusterImport(c, d)
if err != nil {
return nil, err
}

// Return the resource data. In most cases, this method is only used to
// import one resource at a time, so you should return the resource data
// in a slice with a single element.
return []*schema.ResourceData{d}, nil
}
Loading

0 comments on commit 33f6f17

Please sign in to comment.