Skip to content

Commit 9671881

Browse files
authored
Migrate databricks_cluster_policy data source to Go SDK (#2155)
1 parent f8972ab commit 9671881

File tree

3 files changed

+57
-78
lines changed

3 files changed

+57
-78
lines changed

docs/data-sources/cluster_policy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ Data source allows you to pick a cluster policy by the following attribute
3434
Data source exposes the following attributes:
3535

3636
- `id` - The id of the cluster policy.
37-
* `definition` - Policy definition: JSON document expressed in [Databricks Policy Definition Language](https://docs.databricks.com/administration-guide/clusters/policies.html#cluster-policy-definition).
38-
37+
- `definition` - Policy definition: JSON document expressed in [Databricks Policy Definition Language](https://docs.databricks.com/administration-guide/clusters/policies.html#cluster-policy-definition).
38+
- `max_clusters_per_user` - Max number of clusters per user that can be active using this policy

policies/data_cluster_policy.go

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,27 @@ package policies
22

33
import (
44
"context"
5-
"fmt"
65

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
6+
"github.com/databricks/databricks-sdk-go"
7+
"github.com/databricks/terraform-provider-databricks/common"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

11-
func getPolicy(ClusterPoliciesAPI ClusterPoliciesAPI, name string) (*ClusterPolicy, error) {
12-
policyList, err := ClusterPoliciesAPI.List()
13-
if err != nil {
14-
return nil, err
15-
}
16-
for _, policy := range policyList {
17-
if policy.Name == name {
18-
return &policy, nil
19-
}
20-
}
21-
22-
return nil, fmt.Errorf("cluster policy '%s' wasn't found", name)
23-
}
24-
2511
// DataSourceClusterPolicy returns information about cluster policy specified by name
2612
func DataSourceClusterPolicy() *schema.Resource {
27-
return &schema.Resource{
28-
Schema: map[string]*schema.Schema{
29-
"name": {
30-
Type: schema.TypeString,
31-
Required: true,
32-
},
33-
"definition": {
34-
Type: schema.TypeString,
35-
Computed: true,
36-
},
37-
"max_clusters_per_user": {
38-
Type: schema.TypeInt,
39-
Computed: true,
40-
},
41-
},
42-
ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
43-
ClusterPoliciesAPI := NewClusterPoliciesAPI(ctx, m)
44-
policy, err := getPolicy(ClusterPoliciesAPI, d.Get("name").(string))
45-
if err != nil {
46-
return diag.FromErr(err)
47-
}
48-
d.SetId(policy.PolicyID)
49-
d.Set("definition", policy.Definition)
50-
d.Set("max_clusters_per_user", policy.MaxClustersPerUser)
51-
return nil
52-
},
53-
}
13+
return common.WorkspaceData(func(ctx context.Context, data *struct {
14+
Id string `json:"id,omitempty" tf:"computed"`
15+
Name string `json:"name,omitempty" tf:"computed"`
16+
Definition string `json:"definition,omitempty" tf:"computed"`
17+
MaxClustersPerUser int `json:"max_clusters_per_user,omitempty" tf:"computed"`
18+
}, w *databricks.WorkspaceClient) error {
19+
policy, err := w.ClusterPolicies.GetByName(ctx, data.Name)
20+
if err != nil {
21+
return err
22+
}
23+
data.Id = policy.PolicyId
24+
data.Definition = policy.Definition
25+
data.MaxClustersPerUser = int(policy.MaxClustersPerUser)
26+
return nil
27+
})
5428
}
Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package policies
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/databricks/databricks-sdk-go/apierr"
8-
"github.com/databricks/terraform-provider-databricks/common"
97
"github.com/databricks/terraform-provider-databricks/qa"
10-
"github.com/stretchr/testify/assert"
11-
"github.com/stretchr/testify/require"
128
)
139

1410
func TestDataSourceClusterPolicy(t *testing.T) {
15-
d, err := qa.ResourceFixture{
11+
qa.ResourceFixture{
1612
Fixtures: []qa.HTTPFixture{
1713
{
1814
Method: "GET",
19-
Resource: "/api/2.0/policies/clusters/list",
15+
Resource: "/api/2.0/policies/clusters/list?",
2016
Response: ClusterPolicyList{
2117
Policies: []ClusterPolicy{
2218
{
@@ -32,37 +28,46 @@ func TestDataSourceClusterPolicy(t *testing.T) {
3228
NonWritable: true,
3329
Resource: DataSourceClusterPolicy(),
3430
ID: ".",
35-
State: map[string]any{
36-
"name": "policy",
31+
HCL: `name = "policy"`,
32+
}.ApplyAndExpectData(t, map[string]any{
33+
"id": "abc",
34+
"definition": `{"abc":"123"}`,
35+
})
36+
}
37+
38+
func TestDataSourceClusterPolicyError(t *testing.T) {
39+
qa.ResourceFixture{
40+
Fixtures: []qa.HTTPFixture{
41+
{
42+
Method: "GET",
43+
Resource: "/api/2.0/policies/clusters/list?",
44+
Status: 404,
45+
Response: apierr.APIError{
46+
Message: "searching_error",
47+
},
48+
},
3749
},
38-
}.Apply(t)
39-
require.NoError(t, err)
40-
assert.Equal(t, "abc", d.Id())
41-
assert.Equal(t, `{"abc":"123"}`, d.Get("definition").(string))
50+
Read: true,
51+
NonWritable: true,
52+
Resource: DataSourceClusterPolicy(),
53+
ID: ".",
54+
HCL: `name = "policy"`,
55+
}.ExpectError(t, "searching_error")
4256
}
4357

4458
func TestDataSourceClusterPolicyNotFound(t *testing.T) {
45-
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
46-
{
47-
Method: "GET",
48-
Resource: "/api/2.0/policies/clusters/list",
49-
Status: 404,
50-
Response: apierr.APIError{
51-
Message: "searching_error",
59+
qa.ResourceFixture{
60+
Fixtures: []qa.HTTPFixture{
61+
{
62+
Method: "GET",
63+
Resource: "/api/2.0/policies/clusters/list?",
64+
Response: ClusterPolicyList{},
5265
},
5366
},
54-
{
55-
Method: "GET",
56-
Resource: "/api/2.0/policies/clusters/list",
57-
Response: ClusterPolicyList{},
58-
},
59-
}, func(ctx context.Context, client *common.DatabricksClient) {
60-
poolsAPI := NewClusterPoliciesAPI(ctx, client)
61-
62-
_, err := getPolicy(poolsAPI, "searching_error")
63-
assert.EqualError(t, err, "searching_error")
64-
65-
_, err = getPolicy(poolsAPI, "unknown")
66-
assert.EqualError(t, err, "cluster policy 'unknown' wasn't found")
67-
})
67+
Read: true,
68+
NonWritable: true,
69+
Resource: DataSourceClusterPolicy(),
70+
ID: ".",
71+
HCL: `name = "policy"`,
72+
}.ExpectError(t, "Policy named 'policy' does not exist")
6873
}

0 commit comments

Comments
 (0)