From b1fcb2ead139ff001a7576be6a4b1b30077e9774 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Tue, 10 Oct 2023 09:28:45 +0530 Subject: [PATCH] PLT-721: cluster_context and context corrections with unittest fix for addon profile. (#352) * Fixed toAddonDeplProfiles cluster_context and context corrections with unittest fix * PLT-721: Fixed Day2 operations on tenant resource's * PLT-721: Merge main branch. --------- Co-authored-by: nikolay-spectro --- spectrocloud/cluster_common.go | 7 ++ spectrocloud/cluster_common_profiles.go | 14 ++-- spectrocloud/data_source_cluster_profile.go | 67 +++++++++++++++++-- spectrocloud/provider_resource_schema_test.go | 2 +- spectrocloud/resource_cluster_attachment.go | 8 +-- spectrocloud/resource_cluster_import.go | 9 +++ 6 files changed, 91 insertions(+), 16 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..9f2d6afb 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) } @@ -153,6 +153,7 @@ func updateProfiles(c *client.V1Client, d *schema.ResourceData) error { Profiles: profiles, SpcApplySettings: settings, } + clusterContext := d.Get("context").(string) if err := c.UpdateClusterProfileValues(d.Id(), body); err != nil { return err } @@ -162,7 +163,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/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/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 { diff --git a/spectrocloud/resource_cluster_import.go b/spectrocloud/resource_cluster_import.go index 4cea3983..a06bc1e0 100644 --- a/spectrocloud/resource_cluster_import.go +++ b/spectrocloud/resource_cluster_import.go @@ -6,6 +6,8 @@ import ( "fmt" "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" @@ -39,6 +41,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,