From 26105b5369a6e72b8a28e776e17a229e981ed572 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Sat, 7 Oct 2023 00:05:41 +0530 Subject: [PATCH 1/3] Fixed toAddonDeplProfiles cluster_context and context corrections with unittest fix --- spectrocloud/cluster_common.go | 7 +++++++ spectrocloud/cluster_common_profiles.go | 12 ++++++------ spectrocloud/provider_resource_schema_test.go | 2 +- spectrocloud/resource_cluster_attachment.go | 8 ++++---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/spectrocloud/cluster_common.go b/spectrocloud/cluster_common.go index 53e91b00..c4b997fa 100644 --- a/spectrocloud/cluster_common.go +++ b/spectrocloud/cluster_common.go @@ -92,3 +92,10 @@ func ValidationNodeRepaveIntervalForControlPlane(nodeRepaveInterval int) error { } return nil } + +func ValidateContext(context string) error { + if context != "project" && context != "tenant" { + return fmt.Errorf("invalid Context set - %s", context) + } + return nil +} diff --git a/spectrocloud/cluster_common_profiles.go b/spectrocloud/cluster_common_profiles.go index 7689f54b..9c201ce4 100644 --- a/spectrocloud/cluster_common_profiles.go +++ b/spectrocloud/cluster_common_profiles.go @@ -24,17 +24,17 @@ func toAddonDeplProfiles(c *client.V1Client, d *schema.ResourceData) ([]*models. if uid, ok := d.GetOk("cluster_uid"); ok && uid != nil { clusterUid = uid.(string) //d.Get("cluster_uid").(string) } - if ct, ok := d.GetOk("cluster_context"); ok && c != nil { - clusterContext = ct.(string) //d.Get("cluster_context").(string) - } // handling cluster day 2 addon profile operation flow if clusterUid == "" { clusterUid = d.Id() } - if clusterContext == "" { - clusterContext = d.Get("context").(string) + if ct, ok := d.GetOk("context"); ok && c != nil { + clusterContext = ct.(string) + } + err := ValidateContext(clusterContext) + if err != nil { + return nil, err } - return toProfilesCommon(c, d, clusterUid, clusterContext) } diff --git a/spectrocloud/provider_resource_schema_test.go b/spectrocloud/provider_resource_schema_test.go index 4e764d5e..01cd3079 100644 --- a/spectrocloud/provider_resource_schema_test.go +++ b/spectrocloud/provider_resource_schema_test.go @@ -181,7 +181,7 @@ func prepareAddonDeploymentTestData(id string) *schema.ResourceData { // Set the cluster_uid, cluster_context, and apply_setting fields d.Set("cluster_uid", "cluster-123") - d.Set("cluster_context", "tenant") + d.Set("context", "tenant") d.Set("apply_setting", "test-setting") // Set up the cluster_profile field diff --git a/spectrocloud/resource_cluster_attachment.go b/spectrocloud/resource_cluster_attachment.go index 5c7fe0c1..11a830ed 100644 --- a/spectrocloud/resource_cluster_attachment.go +++ b/spectrocloud/resource_cluster_attachment.go @@ -36,7 +36,7 @@ func resourceAddonDeployment() *schema.Resource { Type: schema.TypeString, Required: true, }, - "cluster_context": { + "context": { Type: schema.TypeString, Required: true, }, @@ -65,7 +65,7 @@ func resourceAddonDeploymentCreate(ctx context.Context, d *schema.ResourceData, var diags diag.Diagnostics clusterUid := d.Get("cluster_uid").(string) - clusterScope := d.Get("cluster_context").(string) + clusterScope := d.Get("context").(string) cluster, err := c.GetCluster(clusterScope, clusterUid) if err != nil && cluster == nil { @@ -141,7 +141,7 @@ func resourceAddonDeploymentRead(_ context.Context, d *schema.ResourceData, m in var diags diag.Diagnostics clusterUid := d.Get("cluster_uid").(string) - clusterScope := d.Get("cluster_context").(string) + clusterScope := d.Get("context").(string) cluster, err := c.GetCluster(clusterScope, clusterUid) if err != nil { return diag.FromErr(err) @@ -163,7 +163,7 @@ func resourceAddonDeploymentUpdate(ctx context.Context, d *schema.ResourceData, c := m.(*client.V1Client) clusterUid := d.Get("cluster_uid").(string) - clusterScope := d.Get("cluster_context").(string) + clusterScope := d.Get("context").(string) cluster, err := c.GetCluster(clusterScope, clusterUid) if err != nil && cluster == nil { From f4369a1feef840b9c556a6131d7ea874ce7283fc Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Sat, 7 Oct 2023 00:20:08 +0530 Subject: [PATCH 2/3] PLT-721: Fixed Day2 operations on tenant resource's --- spectrocloud/cluster_common_profiles.go | 8 ++++++-- spectrocloud/resource_cluster_import.go | 13 +++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/spectrocloud/cluster_common_profiles.go b/spectrocloud/cluster_common_profiles.go index 9c201ce4..d9441c3f 100644 --- a/spectrocloud/cluster_common_profiles.go +++ b/spectrocloud/cluster_common_profiles.go @@ -141,6 +141,10 @@ func setPackManifests(pack *models.V1PackValuesEntity, p map[string]interface{}, func updateProfiles(c *client.V1Client, d *schema.ResourceData) error { log.Printf("Updating profiles") + var clusterContext string + if ct, ok := d.GetOk("context"); ok && c != nil { + clusterContext = ct.(string) + } profiles, err := toAddonDeplProfiles(c, d) if err != nil { return err @@ -153,7 +157,8 @@ func updateProfiles(c *client.V1Client, d *schema.ResourceData) error { Profiles: profiles, SpcApplySettings: settings, } - if err := c.UpdateClusterProfileValues(d.Id(), body); err != nil { + clusterContext = d.Get("context").(string) + if err := c.UpdateClusterProfileValues(d.Id(), clusterContext, body); err != nil { return err } @@ -162,7 +167,6 @@ func updateProfiles(c *client.V1Client, d *schema.ResourceData) error { } ctx := context.Background() - clusterContext := d.Get("context").(string) if err := waitForProfileDownload(ctx, c, clusterContext, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil { return err } diff --git a/spectrocloud/resource_cluster_import.go b/spectrocloud/resource_cluster_import.go index 4cea3983..f5e6d6d1 100644 --- a/spectrocloud/resource_cluster_import.go +++ b/spectrocloud/resource_cluster_import.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" @@ -39,6 +40,13 @@ func resourceClusterImport() *schema.Resource { Required: true, ForceNew: true, }, + "context": { + Type: schema.TypeString, + Optional: true, + Default: "project", + ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false), + Description: "The context of the cluster. Can be `project` or `tenant`. Default is `project`.", + }, "tags": { Type: schema.TypeSet, Optional: true, @@ -103,7 +111,7 @@ func resourceCloudClusterImport(ctx context.Context, d *schema.ResourceData, m i return diag.FromErr(err) } if profiles != nil { - if err := c.UpdateClusterProfileValues(uid, profiles); err != nil { + if err := c.UpdateClusterProfileValues(uid, ClusterContext, profiles); err != nil { return diag.FromErr(err) } } @@ -198,7 +206,8 @@ func resourceCloudClusterUpdate(_ context.Context, d *schema.ResourceData, m int if err != nil { return diag.FromErr(err) } - err = c.UpdateClusterProfileValues(d.Id(), profiles) + ClusterContext := d.Get("context").(string) + err = c.UpdateClusterProfileValues(d.Id(), ClusterContext, profiles) if err != nil { return diag.FromErr(err) } From 4afd60afd20d03d689394f4228cd60636268020e Mon Sep 17 00:00:00 2001 From: nikolay-spectro Date: Mon, 9 Oct 2023 20:55:04 -0700 Subject: [PATCH 3/3] PLT-721: Merge main branch. --- spectrocloud/cluster_common_profiles.go | 8 +-- spectrocloud/data_source_cluster_profile.go | 67 +++++++++++++++++++-- spectrocloud/resource_cluster_import.go | 8 +-- 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/spectrocloud/cluster_common_profiles.go b/spectrocloud/cluster_common_profiles.go index d9441c3f..9f2d6afb 100644 --- a/spectrocloud/cluster_common_profiles.go +++ b/spectrocloud/cluster_common_profiles.go @@ -141,10 +141,6 @@ func setPackManifests(pack *models.V1PackValuesEntity, p map[string]interface{}, func updateProfiles(c *client.V1Client, d *schema.ResourceData) error { log.Printf("Updating profiles") - var clusterContext string - if ct, ok := d.GetOk("context"); ok && c != nil { - clusterContext = ct.(string) - } profiles, err := toAddonDeplProfiles(c, d) if err != nil { return err @@ -157,8 +153,8 @@ func updateProfiles(c *client.V1Client, d *schema.ResourceData) error { Profiles: profiles, SpcApplySettings: settings, } - clusterContext = d.Get("context").(string) - if err := c.UpdateClusterProfileValues(d.Id(), clusterContext, body); err != nil { + clusterContext := d.Get("context").(string) + if err := c.UpdateClusterProfileValues(d.Id(), body); err != nil { return err } diff --git a/spectrocloud/data_source_cluster_profile.go b/spectrocloud/data_source_cluster_profile.go index 297d5426..0f29e069 100644 --- a/spectrocloud/data_source_cluster_profile.go +++ b/spectrocloud/data_source_cluster_profile.go @@ -3,14 +3,13 @@ package spectrocloud import ( "context" "fmt" + "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/spectrocloud/hapi/models" "github.com/spectrocloud/palette-sdk-go/client" - "github.com/spectrocloud/terraform-provider-spectrocloud/spectrocloud/schemas" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func dataSourceClusterProfile() *schema.Resource { @@ -41,7 +40,67 @@ func dataSourceClusterProfile() *schema.Resource { Computed: true, ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant", "system"}, false), }, - "pack": schemas.PackSchema(), + "pack": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "type": { + Type: schema.TypeString, + Optional: true, + Default: "spectro", + }, + "registry_uid": { + Type: schema.TypeString, + Optional: true, + }, + "uid": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "manifest": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "uid": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "content": { + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return strings.TrimSpace(old) == strings.TrimSpace(new) + }, + }, + }, + }, + }, + "tag": { + Type: schema.TypeString, + Optional: true, + }, + "values": { + Type: schema.TypeString, + Optional: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + // UI strips the trailing newline on save + return strings.TrimSpace(old) == strings.TrimSpace(new) + }, + }, + }, + }, + }, }, } } diff --git a/spectrocloud/resource_cluster_import.go b/spectrocloud/resource_cluster_import.go index f5e6d6d1..a06bc1e0 100644 --- a/spectrocloud/resource_cluster_import.go +++ b/spectrocloud/resource_cluster_import.go @@ -4,9 +4,10 @@ import ( "context" "errors" "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "time" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/spectrocloud/terraform-provider-spectrocloud/spectrocloud/schemas" @@ -111,7 +112,7 @@ func resourceCloudClusterImport(ctx context.Context, d *schema.ResourceData, m i return diag.FromErr(err) } if profiles != nil { - if err := c.UpdateClusterProfileValues(uid, ClusterContext, profiles); err != nil { + if err := c.UpdateClusterProfileValues(uid, profiles); err != nil { return diag.FromErr(err) } } @@ -206,8 +207,7 @@ func resourceCloudClusterUpdate(_ context.Context, d *schema.ResourceData, m int if err != nil { return diag.FromErr(err) } - ClusterContext := d.Get("context").(string) - err = c.UpdateClusterProfileValues(d.Id(), ClusterContext, profiles) + err = c.UpdateClusterProfileValues(d.Id(), profiles) if err != nil { return diag.FromErr(err) }