Skip to content

Commit 249cc8f

Browse files
stikkireddynfx
andauthored
Cluster instance pool fix (#174)
* converted appropriate aws_attributes to computed fields; node_type_id is also a computed field when instance_pool_id is configured; instance_pool_id now conflicts with the appropriate attributes; zone_id is not required; added integration tests; * fixed test to include aws_attributes for InstancePools * fmt fix * updated docs for clusters & instance_pools Co-authored-by: Serge Smertin <[email protected]>
1 parent cab737f commit 249cc8f

File tree

4 files changed

+286
-27
lines changed

4 files changed

+286
-27
lines changed

databricks/resource_databricks_cluster.go

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"log"
7+
"reflect"
78
"strings"
89
"time"
910

@@ -66,35 +67,40 @@ func resourceCluster() *schema.Resource {
6667
"availability": {
6768
Type: schema.TypeString,
6869
Optional: true,
69-
Default: "SPOT_WITH_FALLBACK",
70+
Computed: true,
7071
},
7172
"zone_id": {
7273
Type: schema.TypeString,
73-
Required: true,
74+
Optional: true,
75+
Computed: true,
7476
},
7577
"spot_bid_price_percent": {
7678
Type: schema.TypeInt,
7779
Optional: true,
78-
Default: "100",
80+
Computed: true,
7981
},
8082
"instance_profile_arn": {
8183
Type: schema.TypeString,
8284
Optional: true,
8385
},
8486
"first_on_demand": {
8587
Type: schema.TypeInt,
88+
Computed: true,
8689
Optional: true,
8790
},
8891
"ebs_volume_type": {
8992
Type: schema.TypeString,
93+
Computed: true,
9094
Optional: true,
9195
},
9296
"ebs_volume_count": {
9397
Type: schema.TypeInt,
98+
Computed: true,
9499
Optional: true,
95100
},
96101
"ebs_volume_size": {
97102
Type: schema.TypeInt,
103+
Computed: true,
98104
Optional: true,
99105
},
100106
},
@@ -109,6 +115,7 @@ func resourceCluster() *schema.Resource {
109115
"node_type_id": {
110116
Type: schema.TypeString,
111117
Optional: true,
118+
Computed: true,
112119
ConflictsWith: []string{"instance_pool_id"},
113120
AtLeastOneOf: []string{"instance_pool_id"},
114121
},
@@ -126,7 +133,6 @@ func resourceCluster() *schema.Resource {
126133
Type: schema.TypeList,
127134
Optional: true,
128135
MaxItems: 1,
129-
//ConfigMode: schema.SchemaConfigModeAttr,
130136
Elem: &schema.Resource{
131137
Schema: map[string]*schema.Schema{
132138
"dbfs": {
@@ -291,18 +297,27 @@ func resourceCluster() *schema.Resource {
291297
Type: schema.TypeInt,
292298
Optional: true,
293299
Default: 60,
294-
//Computed: true,
295300
},
296301
"enable_elastic_disk": {
297302
Type: schema.TypeBool,
298303
Optional: true,
299304
Computed: true,
300305
},
301306
"instance_pool_id": {
302-
Type: schema.TypeString,
303-
Optional: true,
304-
ConflictsWith: []string{"node_type_id", "driver_node_type_id", "aws_attributes"},
305-
AtLeastOneOf: []string{"node_type_id"},
307+
Type: schema.TypeString,
308+
Optional: true,
309+
ConflictsWith: []string{
310+
"node_type_id",
311+
"driver_node_type_id",
312+
"aws_attributes.0.availability",
313+
"aws_attributes.0.zone_id",
314+
"aws_attributes.0.spot_bid_price_percent",
315+
"aws_attributes.0.first_on_demand",
316+
"aws_attributes.0.ebs_volume_type",
317+
"aws_attributes.0.ebs_volume_count",
318+
"aws_attributes.0.ebs_volume_size",
319+
},
320+
AtLeastOneOf: []string{"node_type_id"},
306321
},
307322
"idempotency_token": {
308323
Type: schema.TypeInt,
@@ -506,6 +521,7 @@ func resourceClusterCreate(d *schema.ResourceData, m interface{}) error {
506521
client := m.(*service.DBApiClient)
507522

508523
cluster := parseSchemaToCluster(d, "")
524+
handleInstancePoolClusterRequest(&cluster)
509525
libraries := parseSchemaToClusterLibraries(d)
510526

511527
clusterInfo, err := client.Clusters().Create(cluster)
@@ -690,8 +706,11 @@ func resourceClusterRead(d *schema.ResourceData, m interface{}) error {
690706
}
691707
}
692708

693-
_, ok := d.GetOk("aws_attributes")
694-
if ok && clusterInfo.AwsAttributes != nil {
709+
// attempt to identify diff if instance_profile_arn is set
710+
isInstanceProfileArnFound := !reflect.ValueOf(clusterInfo.AwsAttributes.InstanceProfileArn).IsZero()
711+
//Separating computed fields and instance_profile_arn which is not computed (thus remote state should be tracked)
712+
_, awsAttributesDefined := d.GetOk("aws_attributes")
713+
if (awsAttributesDefined || isInstanceProfileArnFound) && clusterInfo.AwsAttributes != nil {
695714
awsAtts := map[string]interface{}{}
696715
awsAtts["availability"] = string(clusterInfo.AwsAttributes.Availability)
697716
awsAtts["zone_id"] = clusterInfo.AwsAttributes.ZoneID
@@ -706,11 +725,6 @@ func resourceClusterRead(d *schema.ResourceData, m interface{}) error {
706725
if err != nil {
707726
return err
708727
}
709-
} else {
710-
err = d.Set("aws_attributes", nil)
711-
if err != nil {
712-
return err
713-
}
714728
}
715729

716730
err = d.Set("driver_node_type_id", clusterInfo.DriverNodeTypeID)
@@ -1060,9 +1074,12 @@ func resourceClusterUpdate(d *schema.ResourceData, m interface{}) error {
10601074

10611075
clusterState := clusterInfo.State
10621076

1077+
cluster := parseSchemaToCluster(d, "")
1078+
// Remove all fields that conflict with instancePools
1079+
handleInstancePoolClusterRequest(&cluster)
1080+
10631081
switch {
10641082
case model.ContainsClusterState([]model.ClusterState{model.ClusterState(model.ClusterStateTerminated)}, clusterState):
1065-
cluster := parseSchemaToCluster(d, "")
10661083
cluster.ClusterID = id
10671084
err := client.Clusters().Edit(cluster)
10681085
if err != nil {
@@ -1099,7 +1116,6 @@ func resourceClusterUpdate(d *schema.ResourceData, m interface{}) error {
10991116
}
11001117
return resourceClusterRead(d, m)
11011118
case model.ContainsClusterState([]model.ClusterState{model.ClusterState(model.ClusterStateRunning)}, clusterState):
1102-
cluster := parseSchemaToCluster(d, "")
11031119
cluster.ClusterID = id
11041120

11051121
if len(installs) > 0 {
@@ -1132,7 +1148,6 @@ func resourceClusterUpdate(d *schema.ResourceData, m interface{}) error {
11321148
if err != nil {
11331149
return err
11341150
}
1135-
cluster := parseSchemaToCluster(d, "")
11361151
cluster.ClusterID = id
11371152

11381153
if len(installs) > 0 {
@@ -1174,6 +1189,23 @@ func resourceClusterDelete(d *schema.ResourceData, m interface{}) error {
11741189
return err
11751190
}
11761191

1192+
// This function helps remove all requests that should not be submitted when instance pool is selected.
1193+
func handleInstancePoolClusterRequest(clusterModel *model.Cluster) {
1194+
if reflect.ValueOf(clusterModel.InstancePoolID).IsZero() {
1195+
return
1196+
}
1197+
if clusterModel.AwsAttributes != nil {
1198+
// Reset AwsAttributes
1199+
awsAttributes := model.AwsAttributes{
1200+
InstanceProfileArn: clusterModel.AwsAttributes.InstanceProfileArn,
1201+
}
1202+
clusterModel.AwsAttributes = &awsAttributes
1203+
}
1204+
clusterModel.EnableElasticDisk = false
1205+
clusterModel.NodeTypeID = ""
1206+
clusterModel.DriverNodeTypeID = ""
1207+
}
1208+
11771209
func parseSchemaToCluster(d *schema.ResourceData, schemaAttPrefix string) model.Cluster {
11781210
cluster := model.Cluster{}
11791211

@@ -1441,7 +1473,11 @@ func parseSchemaToCluster(d *schema.ResourceData, schemaAttPrefix string) model.
14411473
func getMapFromOneItemList(input interface{}) map[string]interface{} {
14421474
inputList := input.([]interface{})
14431475
if len(inputList) >= 1 {
1444-
return inputList[0].(map[string]interface{})
1476+
resp, ok := inputList[0].(map[string]interface{})
1477+
if !ok {
1478+
return make(map[string]interface{})
1479+
}
1480+
return resp
14451481
}
14461482
return nil
14471483
}

0 commit comments

Comments
 (0)