Skip to content

Commit 30f417a

Browse files
authored
Fix max_clusters_per_user to databricks_cluster_policy resource (#1956)
🤦 we need to try to get rid from the non-struct schemas
1 parent 2086e9b commit 30f417a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

policies/resource_cluster_policy.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ type ClusterPolicy struct {
2020

2121
// ClusterPolicyCreate is the endity used for request
2222
type ClusterPolicyCreate struct {
23-
Name string `json:"name"`
24-
Definition string `json:"definition"`
23+
Name string `json:"name"`
24+
Definition string `json:"definition"`
25+
MaxClustersPerUser int64 `json:"max_clusters_per_user,omitempty"`
2526
}
2627

2728
type ClusterPolicyList struct {
@@ -93,6 +94,9 @@ func parsePolicyFromData(d *schema.ResourceData) (*ClusterPolicy, error) {
9394
if data, ok := d.GetOk("definition"); ok {
9495
clusterPolicy.Definition = data.(string)
9596
}
97+
if max_clusters, ok := d.GetOk("max_clusters_per_user"); ok {
98+
clusterPolicy.MaxClustersPerUser = int64(max_clusters.(int))
99+
}
96100
return clusterPolicy, nil
97101
}
98102

@@ -113,11 +117,15 @@ func ResourceClusterPolicy() *schema.Resource {
113117
},
114118
"definition": {
115119
Type: schema.TypeString,
116-
Optional: true,
120+
Required: true,
117121
Description: "Policy definition JSON document expressed in\n" +
118122
"Databricks Policy Definition Language.",
119123
ValidateFunc: validation.StringIsJSON,
120124
},
125+
"max_clusters_per_user": {
126+
Type: schema.TypeInt,
127+
Optional: true,
128+
},
121129
},
122130
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
123131
clusterPolicy, err := parsePolicyFromData(d)
@@ -135,15 +143,11 @@ func ResourceClusterPolicy() *schema.Resource {
135143
if err != nil {
136144
return err
137145
}
138-
if err = d.Set("name", clusterPolicy.Name); err != nil {
139-
return err
140-
}
141-
if err = d.Set("definition", clusterPolicy.Definition); err != nil {
142-
return err
143-
}
144-
if err = d.Set("policy_id", clusterPolicy.PolicyID); err != nil {
145-
return err
146-
}
146+
d.Set("name", clusterPolicy.Name)
147+
d.Set("definition", clusterPolicy.Definition)
148+
d.Set("policy_id", clusterPolicy.PolicyID)
149+
d.SetId(clusterPolicy.PolicyID)
150+
d.Set("max_clusters_per_user", clusterPolicy.MaxClustersPerUser)
147151
return nil
148152
},
149153
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {

policies/resource_cluster_policy_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestResourceClusterPolicyRead(t *testing.T) {
2020
Name: "Dummy",
2121
Definition: "{\"spark_conf.foo\": {\"type\": \"fixed\", \"value\": \"bar\"}}",
2222
CreatedAtTimeStamp: 0,
23+
MaxClustersPerUser: 5,
2324
},
2425
},
2526
},
@@ -32,6 +33,7 @@ func TestResourceClusterPolicyRead(t *testing.T) {
3233
assert.Equal(t, "Dummy", d.Get("name"))
3334
assert.Equal(t, "{\"spark_conf.foo\": {\"type\": \"fixed\", \"value\": \"bar\"}}", d.Get("definition"))
3435
assert.Equal(t, "abc", d.Get("policy_id"))
36+
assert.Equal(t, 5, d.Get("max_clusters_per_user"))
3537
}
3638

3739
func TestResourceClusterPolicyRead_NotFound(t *testing.T) {
@@ -85,6 +87,7 @@ func TestResourceClusterPolicyCreate(t *testing.T) {
8587
Name: "Dummy",
8688
Definition: "{\"spark_conf.foo\": {\"type\": \"fixed\", \"value\": \"bar\"}}",
8789
CreatedAtTimeStamp: 0,
90+
MaxClustersPerUser: 3,
8891
},
8992
Response: ClusterPolicy{
9093
PolicyID: "abc",
@@ -98,18 +101,21 @@ func TestResourceClusterPolicyCreate(t *testing.T) {
98101
Name: "Dummy",
99102
Definition: "{\"spark_conf.foo\": {\"type\": \"fixed\", \"value\": \"bar\"}}",
100103
CreatedAtTimeStamp: 0,
104+
MaxClustersPerUser: 3,
101105
},
102106
},
103107
},
104108
Resource: ResourceClusterPolicy(),
105109
State: map[string]any{
106-
"definition": `{"spark_conf.foo": {"type": "fixed", "value": "bar"}}`,
107-
"name": "Dummy",
110+
"definition": `{"spark_conf.foo": {"type": "fixed", "value": "bar"}}`,
111+
"max_clusters_per_user": 3,
112+
"name": "Dummy",
108113
},
109114
Create: true,
110115
}.Apply(t)
111116
assert.NoError(t, err, err)
112117
assert.Equal(t, "abc", d.Id())
118+
assert.Equal(t, 3, d.Get("max_clusters_per_user"))
113119
}
114120

115121
func TestResourceClusterPolicyCreate_Error(t *testing.T) {

0 commit comments

Comments
 (0)