Skip to content

Commit ab800b9

Browse files
authored
Migrate databricks_cluster_policy to Go SDK (#2250)
1 parent 54432e7 commit ab800b9

File tree

5 files changed

+91
-160
lines changed

5 files changed

+91
-160
lines changed

docs/resources/cluster_policy.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ module "engineering_compute_policy" {
9898
The following arguments are required:
9999

100100
* `name` - (Required) Cluster policy name. This must be unique. Length must be between 1 and 100 characters.
101-
* `definition` - (Required) Policy definition: JSON document expressed in [Databricks Policy Definition Language](https://docs.databricks.com/administration-guide/clusters/policies.html#cluster-policy-definition).
101+
* `description` - (Optional) Additional human-readable description of the cluster policy.
102+
* `definition` - Policy definition: JSON document expressed in [Databricks Policy Definition Language](https://docs.databricks.com/administration-guide/clusters/policies.html#cluster-policy-definition). Cannot be used with `policy_family_id`
102103
* `max_clusters_per_user` - (Optional, integer) Maximum number of clusters allowed per user. When omitted, there is no limit. If specified, value must be greater than zero.
104+
* `policy_family_definition_overrides`(Optional) Policy definition JSON document expressed in Databricks Policy Definition Language. The JSON document must be passed as a string and cannot be embedded in the requests. You can use this to customize the policy definition inherited from the policy family. Policy rules specified here are merged into the inherited policy definition.
105+
* `policy_family_id` (Optional) ID of the policy family. The cluster policy's policy definition inherits the policy family's policy definition. Cannot be used with `definition`. Use `policy_family_definition_overrides` instead to customize the policy definition.
103106

104107
## Attribute Reference
105108

@@ -113,7 +116,7 @@ In addition to all arguments above, the following attributes are exported:
113116
The resource cluster policy can be imported using the policy id:
114117

115118
```bash
116-
$ terraform import databricks_cluster_policy.this <cluster-policy-id>
119+
terraform import databricks_cluster_policy.this <cluster-policy-id>
117120
```
118121

119122
## Related Resources

exporter/exporter_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/databricks/databricks-sdk-go/service/clusterpolicies"
1516
clustersApi "github.com/databricks/databricks-sdk-go/service/clusters"
1617
"github.com/databricks/databricks-sdk-go/service/gitcredentials"
1718
"github.com/databricks/terraform-provider-databricks/access"
@@ -22,7 +23,6 @@ import (
2223
"github.com/databricks/terraform-provider-databricks/jobs"
2324
"github.com/databricks/terraform-provider-databricks/libraries"
2425
"github.com/databricks/terraform-provider-databricks/pipelines"
25-
"github.com/databricks/terraform-provider-databricks/policies"
2626
"github.com/databricks/terraform-provider-databricks/qa"
2727
"github.com/databricks/terraform-provider-databricks/repos"
2828
"github.com/databricks/terraform-provider-databricks/scim"
@@ -831,8 +831,8 @@ func TestImportingJobs_JobList(t *testing.T) {
831831
{
832832
Method: "GET",
833833
Resource: "/api/2.0/policies/clusters/get?policy_id=123",
834-
Response: policies.ClusterPolicy{
835-
PolicyID: "123",
834+
Response: clusterpolicies.Policy{
835+
PolicyId: "123",
836836
Name: "dummy",
837837
Definition: `{
838838
"aws_attributes.instance_profile_arn": {
@@ -1077,8 +1077,8 @@ func TestImportingJobs_JobListMultiTask(t *testing.T) {
10771077
{
10781078
Method: "GET",
10791079
Resource: "/api/2.0/policies/clusters/get?policy_id=123",
1080-
Response: policies.ClusterPolicy{
1081-
PolicyID: "123",
1080+
Response: clusterpolicies.Policy{
1081+
PolicyId: "123",
10821082
Name: "dummy",
10831083
Definition: `{
10841084
"aws_attributes.instance_profile_arn": {

policies/data_cluster_policy_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/databricks/databricks-sdk-go/apierr"
7+
clusterpolicies "github.com/databricks/databricks-sdk-go/service/clusterpolicies"
78
"github.com/databricks/terraform-provider-databricks/qa"
89
)
910

@@ -13,10 +14,10 @@ func TestDataSourceClusterPolicy(t *testing.T) {
1314
{
1415
Method: "GET",
1516
Resource: "/api/2.0/policies/clusters/list?",
16-
Response: ClusterPolicyList{
17-
Policies: []ClusterPolicy{
17+
Response: clusterpolicies.ListPoliciesResponse{
18+
Policies: []clusterpolicies.Policy{
1819
{
19-
PolicyID: "abc",
20+
PolicyId: "abc",
2021
Name: "policy",
2122
Definition: `{"abc":"123"}`,
2223
},
@@ -61,7 +62,7 @@ func TestDataSourceClusterPolicyNotFound(t *testing.T) {
6162
{
6263
Method: "GET",
6364
Resource: "/api/2.0/policies/clusters/list?",
64-
Response: ClusterPolicyList{},
65+
Response: clusterpolicies.ListPoliciesResponse{},
6566
},
6667
},
6768
Read: true,

policies/resource_cluster_policy.go

Lines changed: 44 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -3,165 +3,78 @@ package policies
33
import (
44
"context"
55

6+
"github.com/databricks/databricks-sdk-go/service/clusterpolicies"
67
"github.com/databricks/terraform-provider-databricks/common"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1010
)
1111

12-
// ClusterPolicy defines cluster policy
13-
type ClusterPolicy struct {
14-
PolicyID string `json:"policy_id,omitempty"`
15-
Name string `json:"name"`
16-
Definition string `json:"definition"`
17-
CreatedAtTimeStamp int64 `json:"created_at_timestamp"`
18-
MaxClustersPerUser int64 `json:"max_clusters_per_user,omitempty"`
19-
}
20-
21-
// ClusterPolicyCreate is the entity used for request
22-
type ClusterPolicyCreate struct {
23-
Name string `json:"name"`
24-
Definition string `json:"definition"`
25-
MaxClustersPerUser int64 `json:"max_clusters_per_user,omitempty"`
26-
}
27-
28-
type ClusterPolicyList struct {
29-
Policies []ClusterPolicy `json:"policies"`
30-
}
31-
32-
// NewClusterPoliciesAPI creates ClusterPoliciesAPI instance from provider meta
33-
// Creation and editing is available to admins only.
34-
func NewClusterPoliciesAPI(ctx context.Context, m any) ClusterPoliciesAPI {
35-
return ClusterPoliciesAPI{m.(*common.DatabricksClient), ctx}
36-
}
37-
38-
// ClusterPoliciesAPI struct for cluster policies API
39-
type ClusterPoliciesAPI struct {
40-
client *common.DatabricksClient
41-
context context.Context
42-
}
43-
44-
type policyIDWrapper struct {
45-
PolicyID string `json:"policy_id,omitempty" url:"policy_id,omitempty"`
46-
}
47-
48-
// Create creates new cluster policy and sets PolicyID
49-
func (a ClusterPoliciesAPI) Create(clusterPolicy *ClusterPolicy) error {
50-
var policyIDResponse = policyIDWrapper{}
51-
err := a.client.Post(a.context, "/policies/clusters/create", clusterPolicy, &policyIDResponse)
52-
if err != nil {
53-
return err
54-
}
55-
clusterPolicy.PolicyID = policyIDResponse.PolicyID
56-
return nil
57-
}
58-
59-
// Edit will update an existing policy.
60-
// This may make some clusters governed by this policy invalid.
61-
// For such clusters the next cluster edit must provide a confirming configuration,
62-
// but otherwise they can continue to run.
63-
func (a ClusterPoliciesAPI) Edit(clusterPolicy *ClusterPolicy) error {
64-
return a.client.Post(a.context, "/policies/clusters/edit", clusterPolicy, nil)
65-
}
66-
67-
// Get returns cluster policy
68-
func (a ClusterPoliciesAPI) Get(policyID string) (policy ClusterPolicy, err error) {
69-
err = a.client.Get(a.context, "/policies/clusters/get", policyIDWrapper{policyID}, &policy)
70-
return
71-
}
72-
73-
// Delete removes cluster policy
74-
func (a ClusterPoliciesAPI) Delete(policyID string) error {
75-
return a.client.Post(a.context, "/policies/clusters/delete", policyIDWrapper{policyID}, nil)
76-
}
77-
78-
// Get returns cluster policy
79-
func (a ClusterPoliciesAPI) List() ([]ClusterPolicy, error) {
80-
var lst ClusterPolicyList
81-
err := a.client.Get(a.context, "/policies/clusters/list", nil, &lst)
82-
if err != nil {
83-
return []ClusterPolicy{}, err
84-
}
85-
return lst.Policies, nil
86-
}
87-
88-
func parsePolicyFromData(d *schema.ResourceData) (*ClusterPolicy, error) {
89-
clusterPolicy := new(ClusterPolicy)
90-
clusterPolicy.PolicyID = d.Id()
91-
if name, ok := d.GetOk("name"); ok {
92-
clusterPolicy.Name = name.(string)
93-
}
94-
if data, ok := d.GetOk("definition"); ok {
95-
clusterPolicy.Definition = data.(string)
96-
}
97-
if max_clusters, ok := d.GetOk("max_clusters_per_user"); ok {
98-
clusterPolicy.MaxClustersPerUser = int64(max_clusters.(int))
99-
}
100-
return clusterPolicy, nil
101-
}
102-
10312
// ResourceClusterPolicy ...
10413
func ResourceClusterPolicy() *schema.Resource {
105-
return common.Resource{
106-
Schema: map[string]*schema.Schema{
107-
"policy_id": {
14+
s := common.StructToSchema(
15+
clusterpolicies.CreatePolicy{},
16+
func(m map[string]*schema.Schema) map[string]*schema.Schema {
17+
m["policy_id"] = &schema.Schema{
10818
Type: schema.TypeString,
10919
Computed: true,
110-
},
111-
"name": {
112-
Type: schema.TypeString,
113-
Required: true,
114-
Description: "Cluster policy name. This must be unique.\n" +
115-
"Length must be between 1 and 100 characters.",
116-
ValidateFunc: validation.StringLenBetween(1, 100),
117-
},
118-
"definition": {
119-
Type: schema.TypeString,
120-
Required: true,
121-
Description: "Policy definition JSON document expressed in\n" +
122-
"Databricks Policy Definition Language.",
123-
ValidateFunc: validation.StringIsJSON,
124-
},
125-
"max_clusters_per_user": {
126-
Type: schema.TypeInt,
127-
Optional: true,
128-
Description: "Max number of clusters per user that can be active\n" +
129-
"using this policy. If not present, there is no max limit.",
130-
ValidateFunc: validation.IntAtLeast(1),
131-
},
132-
},
20+
}
21+
m["definition"].ConflictsWith = []string{"policy_family_definition_overrides", "policy_family_id"}
22+
m["policy_family_definition_overrides"].ConflictsWith = []string{"definition"}
23+
m["policy_family_id"].ConflictsWith = []string{"definition"}
24+
m["policy_family_definition_overrides"].RequiredWith = []string{"policy_family_id"}
25+
26+
return m
27+
})
28+
29+
return common.Resource{
30+
Schema: s,
13331
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
134-
clusterPolicy, err := parsePolicyFromData(d)
32+
w, err := c.WorkspaceClient()
13533
if err != nil {
13634
return err
13735
}
138-
if err = NewClusterPoliciesAPI(ctx, c).Create(clusterPolicy); err != nil {
36+
37+
var request clusterpolicies.CreatePolicy
38+
common.DataToStructPointer(d, s, &request)
39+
40+
clusterPolicy, err := w.ClusterPolicies.Create(ctx, request)
41+
if err != nil {
13942
return err
14043
}
141-
d.SetId(clusterPolicy.PolicyID)
44+
45+
d.SetId(clusterPolicy.PolicyId)
14246
return nil
14347
},
14448
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
145-
clusterPolicy, err := NewClusterPoliciesAPI(ctx, c).Get(d.Id())
49+
w, err := c.WorkspaceClient()
14650
if err != nil {
14751
return err
14852
}
149-
d.Set("name", clusterPolicy.Name)
150-
d.Set("definition", clusterPolicy.Definition)
151-
d.Set("policy_id", clusterPolicy.PolicyID)
152-
d.SetId(clusterPolicy.PolicyID)
153-
d.Set("max_clusters_per_user", clusterPolicy.MaxClustersPerUser)
154-
return nil
53+
54+
resp, err := w.ClusterPolicies.GetByPolicyId(ctx, d.Id())
55+
if err != nil {
56+
return err
57+
}
58+
return common.StructToData(resp, s, d)
15559
},
15660
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
157-
clusterPolicy, err := parsePolicyFromData(d)
61+
w, err := c.WorkspaceClient()
15862
if err != nil {
15963
return err
16064
}
161-
return NewClusterPoliciesAPI(ctx, c).Edit(clusterPolicy)
65+
66+
var request clusterpolicies.EditPolicy
67+
common.DataToStructPointer(d, s, &request)
68+
request.PolicyId = d.Id()
69+
70+
return w.ClusterPolicies.Edit(ctx, request)
16271
},
16372
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
164-
return NewClusterPoliciesAPI(ctx, c).Delete(d.Id())
73+
w, err := c.WorkspaceClient()
74+
if err != nil {
75+
return err
76+
}
77+
return w.ClusterPolicies.DeleteByPolicyId(ctx, d.Id())
16578
},
16679
}.ToResource()
16780
}

0 commit comments

Comments
 (0)