Skip to content

Commit

Permalink
PLT-424: values are not applied correctly for cluster group resource. (
Browse files Browse the repository at this point in the history
…#248)

* PLT-424: values are not applied correctly for cluster group resource.

* PLT-424: keep default values.

---------

Co-authored-by: nikolay-spectro <[email protected]>
  • Loading branch information
nikchern and nikchern authored Apr 12, 2023
1 parent e2f5392 commit 3e14575
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
31 changes: 24 additions & 7 deletions spectrocloud/resource_cluster_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,15 @@ func toClusterGroup(d *schema.ResourceData) *models.V1ClusterGroupEntity {
}

var clusterGroupLimitConfig *models.V1ClusterGroupLimitConfig
var values string
resourcesObj, ok := d.GetOk("config")
endpointType := "Ingress" // default endpoint type is ingress
if ok {
resources := resourcesObj.([]interface{})[0].(map[string]interface{})
clusterGroupLimitConfig = toClusterGroupLimitConfig(resources)
if resources["values"] != nil {
values = resources["values"].(string)
}
if resources["host_endpoint_type"] != nil {
endpointType = resources["host_endpoint_type"].(string)
}
Expand All @@ -286,19 +290,32 @@ func toClusterGroup(d *schema.ResourceData) *models.V1ClusterGroupEntity {
Labels: toTags(d),
},
Spec: &models.V1ClusterGroupSpec{
Type: "hostCluster",
ClusterRefs: clusterRefs,
ClustersConfig: &models.V1ClusterGroupClustersConfig{
EndpointType: endpointType,
LimitConfig: clusterGroupLimitConfig,
HostClustersConfig: hostClusterConfig,
},
Type: "hostCluster",
ClusterRefs: clusterRefs,
ClustersConfig: GetClusterGroupConfig(clusterGroupLimitConfig, hostClusterConfig, endpointType, values),
},
}

return ret
}

func GetClusterGroupConfig(clusterGroupLimitConfig *models.V1ClusterGroupLimitConfig, hostClusterConfig []*models.V1ClusterGroupHostClusterConfig, endpointType string, values string) *models.V1ClusterGroupClustersConfig {
if values != "" {
return &models.V1ClusterGroupClustersConfig{
EndpointType: endpointType,
LimitConfig: clusterGroupLimitConfig,
HostClustersConfig: hostClusterConfig,
Values: values,
}
} else {
return &models.V1ClusterGroupClustersConfig{
EndpointType: endpointType,
LimitConfig: clusterGroupLimitConfig,
HostClustersConfig: hostClusterConfig,
}
}
}

func toHostClusterConfigs(clusterConfig []interface{}) []*models.V1ClusterGroupHostClusterConfig {
var hostClusterConfigs []*models.V1ClusterGroupHostClusterConfig
for _, obj := range clusterConfig {
Expand Down
28 changes: 28 additions & 0 deletions spectrocloud/resource_cluster_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func prepareClusterGroupTestData() *schema.ResourceData {
"memory_in_mb": 4096,
"storage_in_gb": 100,
"oversubscription_percent": 200,
"values": "namespace: test-namespace",
},
})
d.Set("clusters", []map[string]interface{}{
Expand Down Expand Up @@ -54,9 +55,36 @@ func TestToClusterGroup(t *testing.T) {
assert.Equal(int32(4096), output.Spec.ClustersConfig.LimitConfig.MemoryMiB)
assert.Equal(int32(100), output.Spec.ClustersConfig.LimitConfig.StorageGiB)
assert.Equal(int32(200), output.Spec.ClustersConfig.LimitConfig.OverSubscription)
assert.Equal("namespace: test-namespace", output.Spec.ClustersConfig.Values)
assert.Equal("LoadBalancer", output.Spec.ClustersConfig.EndpointType)
}

func TestDefaultValuesSet(t *testing.T) {
clusterGroupLimitConfig := &models.V1ClusterGroupLimitConfig{}
hostClusterConfig := []*models.V1ClusterGroupHostClusterConfig{{}}
endpointType := "testEndpointType"
nonEmptyValues := "testValues"
emptyValues := ""

t.Run("Test with non-empty values", func(t *testing.T) {
result := GetClusterGroupConfig(clusterGroupLimitConfig, hostClusterConfig, endpointType, nonEmptyValues)

assert.Equal(t, endpointType, result.EndpointType)
assert.Equal(t, clusterGroupLimitConfig, result.LimitConfig)
assert.Equal(t, hostClusterConfig, result.HostClustersConfig)
assert.Equal(t, nonEmptyValues, result.Values)
})

t.Run("Test with empty values", func(t *testing.T) {
result := GetClusterGroupConfig(clusterGroupLimitConfig, hostClusterConfig, endpointType, emptyValues)

assert.Equal(t, endpointType, result.EndpointType)
assert.Equal(t, clusterGroupLimitConfig, result.LimitConfig)
assert.Equal(t, hostClusterConfig, result.HostClustersConfig)
assert.Equal(t, "", result.Values)
})
}

func TestToClusterGroupLimitConfig(t *testing.T) {
resources := map[string]interface{}{
"cpu_millicore": 4000,
Expand Down

0 comments on commit 3e14575

Please sign in to comment.