@@ -3,165 +3,78 @@ package policies
33import (
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 ...
10413func 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