Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-685: Fixing context for virtual cluster. #346

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/cluster_aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
1 change: 1 addition & 0 deletions docs/resources/virtual_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions spectrocloud/cluster_common_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions spectrocloud/data_source_registry_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 14 additions & 4 deletions spectrocloud/resource_cluster_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).",
},
},
},
},
Expand Down Expand Up @@ -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
}
Expand All @@ -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),
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_coxedge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_edge_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_edge_vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_maas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_tke.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion spectrocloud/resource_cluster_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
70 changes: 70 additions & 0 deletions spectrocloud/resource_cluster_virtual_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion spectrocloud/resource_cluster_vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down