Skip to content

Commit

Permalink
PLT-721: cluster_context and context corrections with unittest fix fo…
Browse files Browse the repository at this point in the history
…r 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 <[email protected]>
  • Loading branch information
SivaanandM and nikchern authored Oct 10, 2023
1 parent 4cbd0df commit b1fcb2e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
7 changes: 7 additions & 0 deletions spectrocloud/cluster_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions spectrocloud/cluster_common_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
67 changes: 63 additions & 4 deletions spectrocloud/data_source_cluster_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
},
},
},
},
},
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion spectrocloud/provider_resource_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spectrocloud/resource_cluster_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func resourceAddonDeployment() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"cluster_context": {
"context": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions spectrocloud/resource_cluster_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b1fcb2e

Please sign in to comment.