Skip to content

Commit

Permalink
ec_deployment: Fix where datasource was adding unused topology elemen…
Browse files Browse the repository at this point in the history
…ts (#242)

This PR is a follow up to the bug discovered in #241

The deployment read API returns all possible topology elements
even if the size is set to 0. This means that the ec_deployment datasource
will return more topology elements than what is set in the ec_deployment
resource. This patch makes sure only the topology elements used in the 
deployment are returned.
  • Loading branch information
karencfv authored Feb 18, 2021
1 parent 17cdc41 commit ddf92b7
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 22 deletions.
16 changes: 8 additions & 8 deletions ec/acc/datasource_deployment_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func TestAccDatasourceDeployment_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.http_endpoint_id", resourceName, "elasticsearch.0.http_endpoint_id"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.https_endpoint_id", resourceName, "elasticsearch.0.https_endpoint_id"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.version", resourceName, "elasticsearch.0.version"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.instance_configuration_id", resourceName, "elasticsearch.0.topology.0.instance_configuration_id"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.size", resourceName, "elasticsearch.0.topology.0.size"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.size_resource", resourceName, "elasticsearch.0.topology.0.size_resource"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.zone_count", resourceName, "elasticsearch.0.topology.0.zone_count"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.node_type_data", resourceName, "elasticsearch.0.topology.0.node_type_data"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.node_type_master", resourceName, "elasticsearch.0.topology.0.node_type_master"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.node_type_ingest", resourceName, "elasticsearch.0.topology.0.node_type_ingest"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.1.node_type_ml", resourceName, "elasticsearch.0.topology.0.node_type_ml"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.instance_configuration_id", resourceName, "elasticsearch.0.topology.0.instance_configuration_id"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.size", resourceName, "elasticsearch.0.topology.0.size"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.size_resource", resourceName, "elasticsearch.0.topology.0.size_resource"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.zone_count", resourceName, "elasticsearch.0.topology.0.zone_count"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.node_type_data", resourceName, "elasticsearch.0.topology.0.node_type_data"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.node_type_master", resourceName, "elasticsearch.0.topology.0.node_type_master"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.node_type_ingest", resourceName, "elasticsearch.0.topology.0.node_type_ingest"),
resource.TestCheckResourceAttrPair(datasourceName, "elasticsearch.0.topology.0.node_type_ml", resourceName, "elasticsearch.0.topology.0.node_type_ml"),

// Kibana
resource.TestCheckResourceAttrPair(datasourceName, "kibana.0.elasticsearch_cluster_ref_id", resourceName, "kibana.0.elasticsearch_cluster_ref_id"),
Expand Down
14 changes: 13 additions & 1 deletion ec/ecdatasource/deploymentdatasource/flatteners_apm.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ func flattenApmTopology(plan *models.ApmPlan) []interface{} {
for _, topology := range plan.ClusterTopology {
var m = make(map[string]interface{})

if isApmSizePopulated(topology) && *topology.Size.Value == 0 {
continue
}

m["instance_configuration_id"] = topology.InstanceConfigurationID

if topology.Size != nil && topology.Size.Value != nil {
if isApmSizePopulated(topology) {
m["size"] = util.MemoryToState(*topology.Size.Value)
m["size_resource"] = *topology.Size.Resource
}
Expand All @@ -93,3 +97,11 @@ func flattenApmTopology(plan *models.ApmPlan) []interface{} {

return result
}

func isApmSizePopulated(topology *models.ApmTopologyElement) bool {
if topology.Size != nil && topology.Size.Value != nil {
return true
}

return false
}
8 changes: 8 additions & 0 deletions ec/ecdatasource/deploymentdatasource/flatteners_apm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ func Test_flattenApmResource(t *testing.T) {
Value: ec.Int32(1024),
},
},
{
ZoneCount: 1,
InstanceConfigurationID: "aws.apm.m5d",
Size: &models.TopologySize{
Resource: ec.String("memory"),
Value: ec.Int32(0),
},
},
},
},
}},
Expand Down
14 changes: 13 additions & 1 deletion ec/ecdatasource/deploymentdatasource/flatteners_elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ func flattenElasticsearchTopology(plan *models.ElasticsearchClusterPlan) []inter
for _, topology := range plan.ClusterTopology {
var m = make(map[string]interface{})

if isSizePopulated(topology) && *topology.Size.Value == 0 {
continue
}

m["instance_configuration_id"] = topology.InstanceConfigurationID

if topology.Size != nil && topology.Size.Value != nil {
if isSizePopulated(topology) {
m["size"] = util.MemoryToState(*topology.Size.Value)
m["size_resource"] = *topology.Size.Resource
}
Expand Down Expand Up @@ -108,3 +112,11 @@ func flattenElasticsearchTopology(plan *models.ElasticsearchClusterPlan) []inter

return result
}

func isSizePopulated(topology *models.ElasticsearchClusterTopologyElement) bool {
if topology.Size != nil && topology.Size.Value != nil {
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ func Test_flattenElasticsearchResources(t *testing.T) {
Ml: ec.Bool(false),
},
},
{
NodeCountPerZone: 1,
ZoneCount: 1,
InstanceConfigurationID: "aws.coordinating.m5d",
Size: &models.TopologySize{
Resource: ec.String("memory"),
Value: ec.Int32(0),
},
NodeType: &models.ElasticsearchNodeType{
Data: ec.Bool(true),
Ingest: ec.Bool(true),
Master: ec.Bool(true),
Ml: ec.Bool(false),
},
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ func flattenEnterpriseSearchTopology(plan *models.EnterpriseSearchPlan) []interf
for _, topology := range plan.ClusterTopology {
var m = make(map[string]interface{})

if isEsSizePopulated(topology) && *topology.Size.Value == 0 {
continue
}

m["instance_configuration_id"] = topology.InstanceConfigurationID

m["zone_count"] = topology.ZoneCount

if topology.Size != nil && topology.Size.Value != nil {
if isEsSizePopulated(topology) {
m["size"] = util.MemoryToState(*topology.Size.Value)
m["size_resource"] = *topology.Size.Resource
}
Expand All @@ -106,3 +110,11 @@ func flattenEnterpriseSearchTopology(plan *models.EnterpriseSearchPlan) []interf

return result
}

func isEsSizePopulated(topology *models.EnterpriseSearchTopologyElement) bool {
if topology.Size != nil && topology.Size.Value != nil {
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,32 @@ func Test_flattenEnterpriseSearchResource(t *testing.T) {
EnterpriseSearch: &models.EnterpriseSearchConfiguration{
Version: "7.7.0",
},
ClusterTopology: []*models.EnterpriseSearchTopologyElement{{
ZoneCount: 1,
InstanceConfigurationID: "aws.enterprisesearch.r4",
Size: &models.TopologySize{
Resource: ec.String("memory"),
Value: ec.Int32(1024),
ClusterTopology: []*models.EnterpriseSearchTopologyElement{
{
ZoneCount: 1,
InstanceConfigurationID: "aws.enterprisesearch.r4",
Size: &models.TopologySize{
Resource: ec.String("memory"),
Value: ec.Int32(1024),
},
NodeType: &models.EnterpriseSearchNodeTypes{
Appserver: ec.Bool(true),
Worker: ec.Bool(false),
},
},
NodeType: &models.EnterpriseSearchNodeTypes{
Appserver: ec.Bool(true),
Worker: ec.Bool(false),
{
ZoneCount: 1,
InstanceConfigurationID: "aws.enterprisesearch.m5d",
Size: &models.TopologySize{
Resource: ec.String("memory"),
Value: ec.Int32(0),
},
NodeType: &models.EnterpriseSearchNodeTypes{
Appserver: ec.Bool(true),
Worker: ec.Bool(false),
},
},
}},
},
},
},
},
Expand Down
14 changes: 13 additions & 1 deletion ec/ecdatasource/deploymentdatasource/flatteners_kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ func flattenKibanaTopology(plan *models.KibanaClusterPlan) []interface{} {
for _, topology := range plan.ClusterTopology {
var m = make(map[string]interface{})

if isKibanaSizePopulated(topology) && *topology.Size.Value == 0 {
continue
}

m["instance_configuration_id"] = topology.InstanceConfigurationID

if topology.Size != nil && topology.Size.Value != nil {
if isKibanaSizePopulated(topology) {
m["size"] = util.MemoryToState(*topology.Size.Value)
m["size_resource"] = *topology.Size.Resource
}
Expand All @@ -92,3 +96,11 @@ func flattenKibanaTopology(plan *models.KibanaClusterPlan) []interface{} {

return result
}

func isKibanaSizePopulated(topology *models.KibanaClusterTopologyElement) bool {
if topology.Size != nil && topology.Size.Value != nil {
return true
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func Test_flattenKibanaResources(t *testing.T) {
Value: ec.Int32(1024),
},
},
{
ZoneCount: 1,
InstanceConfigurationID: "aws.kibana.m5d",
Size: &models.TopologySize{
Resource: ec.String("memory"),
Value: ec.Int32(0),
},
},
},
},
},
Expand Down

0 comments on commit ddf92b7

Please sign in to comment.