Skip to content

Commit

Permalink
Merge branch 'main' into PLT-678
Browse files Browse the repository at this point in the history
  • Loading branch information
nikchern committed Sep 22, 2023
2 parents 59002ba + 764987e commit 3e534a5
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 28 deletions.
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
22 changes: 18 additions & 4 deletions spectrocloud/cluster_common_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ 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)
}

func toAddonDeplProfiles(c *client.V1Client, d *schema.ResourceData) ([]*models.V1SpectroClusterProfileEntity, error) {
clusterUid := d.Get("cluster_uid").(string)
clusterContext := d.Get("cluster_context").(string)
clusterUid := ""
clusterContext := ""
// handling cluster attachment flow for cluster created outside terraform and attaching addon profile to it
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)
}

return toProfilesCommon(c, d, clusterUid, clusterContext)
}

Expand Down
29 changes: 26 additions & 3 deletions spectrocloud/cluster_node_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package spectrocloud

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spectrocloud/hapi/models"
"github.com/spectrocloud/palette-sdk-go/client"
"log"
"time"
)

var NodeMaintenanceLifecycleStates = []string{
Expand Down Expand Up @@ -84,7 +86,12 @@ func resourceNodeAction(c *client.V1Client, ctx context.Context, newMachinePool
func flattenNodeMaintenanceStatus(c *client.V1Client, d *schema.ResourceData, fn GetNodeStatusMap, mPools []interface{}, cloudConfigId string, ClusterContext string) ([]interface{}, error) {
_, n := d.GetChange("machine_pool")
nsMap := make(map[string]interface{})
for _, mp := range n.(*schema.Set).List() {
machinePoolsList, i, err := getMachinePoolList(n)
if err != nil {
return i, err
}

for _, mp := range machinePoolsList {
machinePool := mp.(map[string]interface{})
nsMap[machinePool["name"].(string)] = machinePool
}
Expand Down Expand Up @@ -117,3 +124,19 @@ func flattenNodeMaintenanceStatus(c *client.V1Client, d *schema.ResourceData, fn
}
return mPools, nil
}

func getMachinePoolList(n interface{}) ([]interface{}, []interface{}, error) {
var machinePoolsList []interface{}

// Check if n is of type *schema.Set
if set, ok := n.(*schema.Set); ok {
machinePoolsList = set.List()
} else if list, ok := n.([]interface{}); ok {
// If n is already a slice of interfaces
machinePoolsList = list
} else {
// Handle error: n is neither *schema.Set nor []interface{}
return nil, nil, fmt.Errorf("unexpected type for n: %T", n)
}
return machinePoolsList, nil, nil
}
47 changes: 47 additions & 0 deletions spectrocloud/cluster_node_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package spectrocloud

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
)

func TestGetMachinePoolList(t *testing.T) {
tests := []struct {
name string
input interface{}
want []interface{}
wantErr bool
}{
{
name: "Handle *schema.Set",
input: schema.NewSet(schema.HashString, []interface{}{"a", "b"}),
want: []interface{}{"a", "b"},
wantErr: false,
},
{
name: "Handle []interface{}",
input: []interface{}{"a", "b"},
want: []interface{}{"a", "b"},
wantErr: false,
},
{
name: "Handle unexpected type",
input: "unexpected",
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _, err := getMachinePoolList(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("getMachinePoolList() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.ElementsMatch(t, tt.want, got)
})
}
}
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 @@ -125,6 +125,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 @@ -501,7 +509,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 @@ -516,9 +525,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
Loading

0 comments on commit 3e534a5

Please sign in to comment.