-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLT-795:Added import support for GKE & GCP cluster. (#459)
* PLT-795:Added import support for GCP(IAAS) type cluster. * added validtion * added cluster profile support in import * added cluster profile import support in gcp
- Loading branch information
1 parent
a9a3c6d
commit a9d784a
Showing
8 changed files
with
408 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package spectrocloud | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/spectrocloud/palette-sdk-go/client" | ||
) | ||
|
||
func resourceClusterGcpImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { | ||
c := m.(*client.V1Client) | ||
err := GetCommonCluster(d, c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
diags := resourceClusterGcpRead(ctx, d, m) | ||
if diags.HasError() { | ||
return nil, fmt.Errorf("could not read cluster for import: %v", diags) | ||
} | ||
|
||
clusterProfiles, err := flattenClusterProfileForImport(c, d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := d.Set("cluster_profile", clusterProfiles); err != nil { | ||
return nil, fmt.Errorf("could not read cluster for import: %v", diags) | ||
} | ||
|
||
// Return the resource data. In most cases, this method is only used to | ||
// import one resource at a time, so you should return the resource data | ||
// in a slice with a single element. | ||
return []*schema.ResourceData{d}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package spectrocloud | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/spectrocloud/gomi/pkg/ptr" | ||
"github.com/spectrocloud/hapi/models" | ||
"github.com/spectrocloud/terraform-provider-spectrocloud/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestToMachinePoolGcp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]interface{} | ||
expectedOutput *models.V1GcpMachinePoolConfigEntity | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Control Plane", | ||
input: map[string]interface{}{ | ||
"control_plane": true, | ||
"control_plane_as_worker": true, | ||
"azs": schema.NewSet(schema.HashString, []interface{}{"us-central1-a"}), | ||
"instance_type": "n1-standard-1", | ||
"disk_size_gb": 50, | ||
"name": "example-name", | ||
"count": 3, | ||
"node_repave_interval": 0, | ||
}, | ||
expectedOutput: &models.V1GcpMachinePoolConfigEntity{ | ||
CloudConfig: &models.V1GcpMachinePoolCloudConfigEntity{ | ||
Azs: []string{"us-central1-a"}, | ||
InstanceType: types.Ptr("n1-standard-1"), | ||
RootDeviceSize: int64(50), | ||
}, | ||
PoolConfig: &models.V1MachinePoolConfigEntity{ | ||
AdditionalLabels: map[string]string{}, | ||
Taints: nil, | ||
IsControlPlane: true, | ||
Labels: []string{"master"}, | ||
Name: types.Ptr("example-name"), | ||
Size: types.Ptr(int32(3)), | ||
UpdateStrategy: &models.V1UpdateStrategy{ | ||
Type: "RollingUpdateScaleOut", | ||
}, | ||
UseControlPlaneAsWorker: true, | ||
}, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Node Repave Interval Error", | ||
input: map[string]interface{}{ | ||
"control_plane": true, | ||
"control_plane_as_worker": false, | ||
"azs": schema.NewSet(schema.HashString, []interface{}{"us-central1-a"}), | ||
"instance_type": "n1-standard-2", | ||
"disk_size_gb": 100, | ||
"name": "example-name-2", | ||
"count": 2, | ||
"node_repave_interval": -1, | ||
}, | ||
expectedOutput: &models.V1GcpMachinePoolConfigEntity{ | ||
CloudConfig: &models.V1GcpMachinePoolCloudConfigEntity{ | ||
Azs: []string{"us-central1-a"}, | ||
InstanceType: types.Ptr("n1-standard-2"), | ||
RootDeviceSize: int64(100), | ||
}, | ||
PoolConfig: &models.V1MachinePoolConfigEntity{ | ||
AdditionalLabels: map[string]string{"example": "label"}, | ||
Taints: []*models.V1Taint{}, | ||
IsControlPlane: true, | ||
Labels: []string{"master"}, | ||
Name: types.Ptr("example-name-2"), | ||
Size: types.Ptr(int32(2)), | ||
UpdateStrategy: &models.V1UpdateStrategy{ | ||
Type: "RollingUpdate", | ||
}, | ||
UseControlPlaneAsWorker: false, | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
output, err := toMachinePoolGcp(tt.input) | ||
|
||
if tt.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expectedOutput, output) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFlattenMachinePoolConfigsGcp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []*models.V1GcpMachinePoolConfig | ||
expectedOutput []interface{} | ||
}{ | ||
{ | ||
name: "Single Machine Pool", | ||
input: []*models.V1GcpMachinePoolConfig{ | ||
{ | ||
AdditionalLabels: map[string]string{"label1": "value1", "label2": "value2"}, | ||
Taints: []*models.V1Taint{{Key: "taint1", Value: "value1", Effect: "NoSchedule"}}, | ||
IsControlPlane: ptr.BoolPtr(true), | ||
UseControlPlaneAsWorker: true, | ||
Name: "machine-pool-1", | ||
Size: int32(3), | ||
UpdateStrategy: &models.V1UpdateStrategy{Type: "RollingUpdate"}, | ||
InstanceType: types.Ptr("n1-standard-4"), | ||
RootDeviceSize: int64(100), | ||
Azs: []string{"us-west1-a", "us-west1-b"}, | ||
NodeRepaveInterval: 0, | ||
}, | ||
}, | ||
expectedOutput: []interface{}{ | ||
map[string]interface{}{ | ||
"additional_labels": map[string]string{ | ||
"label1": "value1", | ||
"label2": "value2", | ||
}, | ||
"taints": []interface{}{ | ||
map[string]interface{}{ | ||
"key": "taint1", | ||
"value": "value1", | ||
"effect": "NoSchedule", | ||
}, | ||
}, | ||
"control_plane": true, | ||
"control_plane_as_worker": true, | ||
"name": "machine-pool-1", | ||
"count": 3, | ||
"update_strategy": "RollingUpdate", | ||
"instance_type": "n1-standard-4", | ||
"disk_size_gb": 100, | ||
"azs": []string{"us-west1-a", "us-west1-b"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
output := flattenMachinePoolConfigsGcp(tt.input) | ||
assert.Equal(t, tt.expectedOutput, output) | ||
}) | ||
} | ||
} | ||
|
||
func TestFlattenClusterConfigsGcp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *models.V1GcpCloudConfig | ||
expectedOutput []interface{} | ||
}{ | ||
{ | ||
name: "Valid Cloud Config", | ||
input: &models.V1GcpCloudConfig{ | ||
Spec: &models.V1GcpCloudConfigSpec{ | ||
ClusterConfig: &models.V1GcpClusterConfig{ | ||
Project: ptr.StringPtr("my-project"), | ||
Network: "my-network", | ||
Region: ptr.StringPtr("us-west1"), | ||
}, | ||
}, | ||
}, | ||
expectedOutput: []interface{}{ | ||
map[string]interface{}{ | ||
"project": ptr.StringPtr("my-project"), | ||
"network": "my-network", | ||
"region": "us-west1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Nil Cloud Config", | ||
input: nil, | ||
expectedOutput: []interface{}{}, | ||
}, | ||
{ | ||
name: "Empty Cluster Config", | ||
input: &models.V1GcpCloudConfig{}, | ||
expectedOutput: []interface{}{}, | ||
}, | ||
{ | ||
name: "Empty Cluster Config Spec", | ||
input: &models.V1GcpCloudConfig{Spec: &models.V1GcpCloudConfigSpec{}}, | ||
expectedOutput: []interface{}{}, | ||
}, | ||
{ | ||
name: "Missing Fields in Cluster Config", | ||
input: &models.V1GcpCloudConfig{ | ||
Spec: &models.V1GcpCloudConfigSpec{ | ||
ClusterConfig: &models.V1GcpClusterConfig{}, | ||
}, | ||
}, | ||
expectedOutput: []interface{}{ | ||
map[string]interface{}{}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
output := flattenClusterConfigsGcp(tt.input) | ||
assert.Equal(t, tt.expectedOutput, output) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.