diff --git a/docs/resources/cluster_aws.md b/docs/resources/cluster_aws.md index 40731de3..e310f16f 100644 --- a/docs/resources/cluster_aws.md +++ b/docs/resources/cluster_aws.md @@ -166,6 +166,7 @@ Required: Optional: +- `control_plane_lb` (String) Control plane load balancer type. Valid values are `Internet-facing` and `internal`. Defaults to `` (empty string). - `vpc_id` (String) diff --git a/docs/resources/virtual_cluster.md b/docs/resources/virtual_cluster.md index 7ec4bbb0..eca48d19 100644 --- a/docs/resources/virtual_cluster.md +++ b/docs/resources/virtual_cluster.md @@ -62,6 +62,7 @@ resource "spectrocloud_virtual_cluster" "cluster" { - `cluster_group_uid` (String) - `cluster_profile` (Block List) (see [below for nested schema](#nestedblock--cluster_profile)) - `cluster_rbac_binding` (Block List) (see [below for nested schema](#nestedblock--cluster_rbac_binding)) +- `context` (String) - `force_delete` (Boolean) If set to `true`, the cluster will be force deleted and user has to manually clean up the provisioned cloud resources. - `force_delete_delay` (Number) Delay duration in minutes to before invoking cluster force delete. Default and minimum is 20. - `host_cluster_uid` (String) diff --git a/spectrocloud/cluster_common_profiles.go b/spectrocloud/cluster_common_profiles.go index a087c76f..e367bd42 100644 --- a/spectrocloud/cluster_common_profiles.go +++ b/spectrocloud/cluster_common_profiles.go @@ -13,8 +13,7 @@ import ( "github.com/spectrocloud/terraform-provider-spectrocloud/types" ) -func toProfiles(c *client.V1Client, d *schema.ResourceData) ([]*models.V1SpectroClusterProfileEntity, error) { - clusterContext := d.Get("context").(string) +func toProfiles(c *client.V1Client, d *schema.ResourceData, clusterContext string) ([]*models.V1SpectroClusterProfileEntity, error) { return toProfilesCommon(c, d, d.Id(), clusterContext) } diff --git a/spectrocloud/data_source_registry_pack.go b/spectrocloud/data_source_registry_pack.go index 0fe46c83..bae9deb7 100644 --- a/spectrocloud/data_source_registry_pack.go +++ b/spectrocloud/data_source_registry_pack.go @@ -32,12 +32,12 @@ func dataSourceRegistryPackRead(_ context.Context, d *schema.ResourceData, m int c := m.(*client.V1Client) var diags diag.Diagnostics if v, ok := d.GetOk("name"); ok { - registry, err := c.GetPackRegistryByName(v.(string)) + registry, err := c.GetPackRegistryCommonByName(v.(string)) if err != nil { return diag.FromErr(err) } - d.SetId(registry.Metadata.UID) - if err := d.Set("name", registry.Metadata.Name); err != nil { + d.SetId(registry.UID) + if err := d.Set("name", registry.Name); err != nil { return diag.FromErr(err) } } diff --git a/spectrocloud/resource_cluster_aks.go b/spectrocloud/resource_cluster_aks.go index b7e16de8..ccc63575 100644 --- a/spectrocloud/resource_cluster_aks.go +++ b/spectrocloud/resource_cluster_aks.go @@ -454,7 +454,8 @@ func toAksCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro } } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_aws.go b/spectrocloud/resource_cluster_aws.go index cbec77d6..ae96e618 100644 --- a/spectrocloud/resource_cluster_aws.go +++ b/spectrocloud/resource_cluster_aws.go @@ -120,6 +120,14 @@ func resourceClusterAws() *schema.Resource { ForceNew: true, Optional: true, }, + "control_plane_lb": { + Type: schema.TypeString, + ForceNew: true, + Default: "", + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"", "Internet-facing", "internal"}, false), + Description: "Control plane load balancer type. Valid values are `Internet-facing` and `internal`. Defaults to `` (empty string).", + }, }, }, }, @@ -496,7 +504,8 @@ func toAwsCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro // gnarly, I know! =/ cloudConfig := d.Get("cloud_config").([]interface{})[0].(map[string]interface{}) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } @@ -511,9 +520,10 @@ func toAwsCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro Profiles: profiles, Policies: toPolicies(d), CloudConfig: &models.V1AwsClusterConfig{ - SSHKeyName: cloudConfig["ssh_key_name"].(string), - Region: types.Ptr(cloudConfig["region"].(string)), - VpcID: cloudConfig["vpc_id"].(string), + SSHKeyName: cloudConfig["ssh_key_name"].(string), + Region: types.Ptr(cloudConfig["region"].(string)), + VpcID: cloudConfig["vpc_id"].(string), + ControlPlaneLoadBalancer: cloudConfig["control_plane_lb"].(string), }, }, } diff --git a/spectrocloud/resource_cluster_azure.go b/spectrocloud/resource_cluster_azure.go index 3f16e47a..c1beb411 100644 --- a/spectrocloud/resource_cluster_azure.go +++ b/spectrocloud/resource_cluster_azure.go @@ -489,7 +489,8 @@ func toAzureCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spect cloudConfig := d.Get("cloud_config").([]interface{})[0].(map[string]interface{}) //clientSecret := strfmt.Password(d.Get("azure_client_secret").(string)) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_coxedge.go b/spectrocloud/resource_cluster_coxedge.go index 65ced376..3221a27c 100644 --- a/spectrocloud/resource_cluster_coxedge.go +++ b/spectrocloud/resource_cluster_coxedge.go @@ -674,7 +674,8 @@ func toCoxEdgeCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spe } } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_edge_native.go b/spectrocloud/resource_cluster_edge_native.go index 8c0950cd..2165e804 100644 --- a/spectrocloud/resource_cluster_edge_native.go +++ b/spectrocloud/resource_cluster_edge_native.go @@ -439,7 +439,8 @@ func toEdgeNativeCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1 } } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_edge_vsphere.go b/spectrocloud/resource_cluster_edge_vsphere.go index a60f7ba0..77b640de 100644 --- a/spectrocloud/resource_cluster_edge_vsphere.go +++ b/spectrocloud/resource_cluster_edge_vsphere.go @@ -494,7 +494,8 @@ func toEdgeVsphereCluster(c *client.V1Client, d *schema.ResourceData) (*models.V vip := cloudConfig["vip"].(string) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_eks.go b/spectrocloud/resource_cluster_eks.go index 1b32f4d5..56539037 100644 --- a/spectrocloud/resource_cluster_eks.go +++ b/spectrocloud/resource_cluster_eks.go @@ -666,7 +666,8 @@ func toEksCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro } } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_gcp.go b/spectrocloud/resource_cluster_gcp.go index 8b5c32c9..59cc725f 100644 --- a/spectrocloud/resource_cluster_gcp.go +++ b/spectrocloud/resource_cluster_gcp.go @@ -413,7 +413,8 @@ func toGcpCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro cloudConfig := d.Get("cloud_config").([]interface{})[0].(map[string]interface{}) //clientSecret := strfmt.Password(d.Get("gcp_client_secret").(string)) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_import.go b/spectrocloud/resource_cluster_import.go index ae5ebce0..4cea3983 100644 --- a/spectrocloud/resource_cluster_import.go +++ b/spectrocloud/resource_cluster_import.go @@ -207,7 +207,8 @@ func resourceCloudClusterUpdate(_ context.Context, d *schema.ResourceData, m int func toCloudClusterProfiles(c *client.V1Client, d *schema.ResourceData) (*models.V1SpectroClusterProfiles, error) { if profiles := d.Get("cluster_profile").([]interface{}); len(profiles) > 0 { - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_libvirt.go b/spectrocloud/resource_cluster_libvirt.go index ac6f9d8e..4eb71496 100644 --- a/spectrocloud/resource_cluster_libvirt.go +++ b/spectrocloud/resource_cluster_libvirt.go @@ -607,7 +607,8 @@ func toLibvirtCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spe return nil, err } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_maas.go b/spectrocloud/resource_cluster_maas.go index 59e41d80..87aa0db5 100644 --- a/spectrocloud/resource_cluster_maas.go +++ b/spectrocloud/resource_cluster_maas.go @@ -453,7 +453,8 @@ func toMaasCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectr cloudConfig := d.Get("cloud_config").([]interface{})[0].(map[string]interface{}) DomainVal := cloudConfig["domain"].(string) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_openstack.go b/spectrocloud/resource_cluster_openstack.go index 32899335..0786fd6b 100644 --- a/spectrocloud/resource_cluster_openstack.go +++ b/spectrocloud/resource_cluster_openstack.go @@ -271,7 +271,8 @@ func toOpenStackCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1S cloudConfig := d.Get("cloud_config").([]interface{})[0].(map[string]interface{}) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_tke.go b/spectrocloud/resource_cluster_tke.go index bd0257bc..e4293c77 100644 --- a/spectrocloud/resource_cluster_tke.go +++ b/spectrocloud/resource_cluster_tke.go @@ -426,7 +426,8 @@ func toTkeCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spectro sshKeyIds = append(sshKeyIds, cloudConfig["ssh_key_name"].(string)) } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_virtual.go b/spectrocloud/resource_cluster_virtual.go index 41ca8127..19474b26 100644 --- a/spectrocloud/resource_cluster_virtual.go +++ b/spectrocloud/resource_cluster_virtual.go @@ -36,6 +36,12 @@ func resourceClusterVirtual() *schema.Resource { Required: true, ForceNew: true, }, + "context": { + Type: schema.TypeString, + Optional: true, + Default: "project", + ValidateFunc: validation.StringInSlice([]string{"project", "cluster"}, false), + }, "tags": { Type: schema.TypeSet, Optional: true, @@ -368,7 +374,8 @@ func toVirtualCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spe kubernetesVersion = cloudConfig["k8s_version"].(string) } - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err } diff --git a/spectrocloud/resource_cluster_virtual_test.go b/spectrocloud/resource_cluster_virtual_test.go new file mode 100644 index 00000000..7c65cd3e --- /dev/null +++ b/spectrocloud/resource_cluster_virtual_test.go @@ -0,0 +1,70 @@ +package spectrocloud + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/spectrocloud/palette-sdk-go/client" + "github.com/stretchr/testify/assert" +) + +func prepareVirtualClusterTestData() *schema.ResourceData { + d := resourceClusterVirtual().TestResourceData() + + d.SetId("") + d.Set("name", "virtual-picard-2") + + // Cluster Profile for Virtual Cluster + cProfile := make([]map[string]interface{}, 0) + cProfile = append(cProfile, map[string]interface{}{ + "id": "virtual-basic-infra-profile-id", + }) + d.Set("cluster_profile", cProfile) + d.Set("host_cluster_uid", "host-cluster-id") + d.Set("cluster_group_uid", "group-cluster-id") + + // Cloud Config for Virtual Cluster + cloudConfig := make([]map[string]interface{}, 0) + vCloud := map[string]interface{}{ + "chart_name": "virtual-chart-name", + "chart_repo": "virtual-chart-repo", + "chart_version": "v1.0.0", + "chart_values": "default-values", + "k8s_version": "v1.20.0", + } + cloudConfig = append(cloudConfig, vCloud) + d.Set("cloud_config", cloudConfig) + + return d +} + +func TestToVirtualCluster(t *testing.T) { + assert := assert.New(t) + // Create a mock ResourceData object + d := prepareVirtualClusterTestData() + + // Mock the client + mockClient := &client.V1Client{} + + // Create a mock ResourceData for testing + vCluster, err := toVirtualCluster(mockClient, d) + assert.Nil(err) + + // Check the output against the expected values + + // Verifying cluster name attribute + assert.Equal(d.Get("name").(string), vCluster.Metadata.Name) + + // Verifying host cluster uid and cluster group uid attributes + assert.Equal(d.Get("host_cluster_uid").(string), vCluster.Spec.ClusterConfig.HostClusterConfig.HostCluster.UID) + assert.Equal(d.Get("cluster_group_uid").(string), vCluster.Spec.ClusterConfig.HostClusterConfig.ClusterGroup.UID) + + // Verifying cloud config attributes + val, _ := d.GetOk("cloud_config") + cloudConfig := val.([]interface{})[0].(map[string]interface{}) + assert.Equal(cloudConfig["chart_name"].(string), vCluster.Spec.CloudConfig.HelmRelease.Chart.Name) + assert.Equal(cloudConfig["chart_repo"].(string), vCluster.Spec.CloudConfig.HelmRelease.Chart.Repo) + assert.Equal(cloudConfig["chart_version"].(string), vCluster.Spec.CloudConfig.HelmRelease.Chart.Version) + assert.Equal(cloudConfig["chart_values"].(string), vCluster.Spec.CloudConfig.HelmRelease.Values) + assert.Equal(cloudConfig["k8s_version"].(string), vCluster.Spec.CloudConfig.KubernetesVersion) +} diff --git a/spectrocloud/resource_cluster_vsphere.go b/spectrocloud/resource_cluster_vsphere.go index 64e9a3b5..432398ab 100644 --- a/spectrocloud/resource_cluster_vsphere.go +++ b/spectrocloud/resource_cluster_vsphere.go @@ -678,7 +678,8 @@ func toVsphereCluster(c *client.V1Client, d *schema.ResourceData) (*models.V1Spe cloudConfig := d.Get("cloud_config").([]interface{})[0].(map[string]interface{}) //clientSecret := strfmt.Password(d.Get("azure_client_secret").(string)) - profiles, err := toProfiles(c, d) + clusterContext := d.Get("context").(string) + profiles, err := toProfiles(c, d, clusterContext) if err != nil { return nil, err }