Skip to content

Commit

Permalink
Add more attributes to databricks_cluster_policy data source (#2351)
Browse files Browse the repository at this point in the history
* Add more attributes to cluster policy

* Prefix family_id and family_definition_overrides with policy_ to match the API response from Databricks

* Modify TestDataSourceClusterPolicy to test new attributes

---------

Co-authored-by: Cedric <23346008+Cedric98@users.noreply.github.com>
  • Loading branch information
840 and 840 authored Jun 7, 2023
1 parent 036603b commit 039bdb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
6 changes: 5 additions & 1 deletion docs/data-sources/cluster_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ Data source exposes the following attributes:

- `id` - The id of the cluster policy.
- `definition` - Policy definition: JSON document expressed in [Databricks Policy Definition Language](https://docs.databricks.com/administration-guide/clusters/policies.html#cluster-policy-definition).
- `max_clusters_per_user` - Max number of clusters per user that can be active using this policy
- `description` - Additional human-readable description of the cluster policy.
- `policy_family_id` - ID of the policy family.
- `policy_family_definition_overrides` - Policy definition JSON document expressed in Databricks [Policy Definition Language](https://docs.databricks.com/administration-guide/clusters/policies.html#cluster-policy-definitions).
- `is_default` - If true, policy is a default policy created and managed by Databricks.
- `max_clusters_per_user` - Max number of clusters per user that can be active using this policy.
16 changes: 12 additions & 4 deletions policies/data_cluster_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ import (
// DataSourceClusterPolicy returns information about cluster policy specified by name
func DataSourceClusterPolicy() *schema.Resource {
return common.WorkspaceData(func(ctx context.Context, data *struct {
Id string `json:"id,omitempty" tf:"computed"`
Name string `json:"name,omitempty" tf:"computed"`
Definition string `json:"definition,omitempty" tf:"computed"`
MaxClustersPerUser int `json:"max_clusters_per_user,omitempty" tf:"computed"`
Id string `json:"id,omitempty" tf:"computed"`
Name string `json:"name,omitempty" tf:"computed"`
Definition string `json:"definition,omitempty" tf:"computed"`
Description string `json:"description,omitempty" tf:"computed"`
PolicyFamilyId string `json:"policy_family_id,omitempty" tf:"computed"`
PolicyFamilyDefinitionOverrides string `json:"policy_family_definition_overrides,omitempty" tf:"computed"`
IsDefault bool `json:"is_default,omitempty" tf:"computed"`
MaxClustersPerUser int `json:"max_clusters_per_user,omitempty" tf:"computed"`
}, w *databricks.WorkspaceClient) error {
policy, err := w.ClusterPolicies.GetByName(ctx, data.Name)
if err != nil {
return err
}
data.Id = policy.PolicyId
data.Definition = policy.Definition
data.Description = policy.Description
data.PolicyFamilyId = policy.PolicyFamilyId
data.PolicyFamilyDefinitionOverrides = policy.PolicyFamilyDefinitionOverrides
data.IsDefault = policy.IsDefault
data.MaxClustersPerUser = int(policy.MaxClustersPerUser)
return nil
})
Expand Down
20 changes: 15 additions & 5 deletions policies/data_cluster_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ func TestDataSourceClusterPolicy(t *testing.T) {
Response: compute.ListPoliciesResponse{
Policies: []compute.Policy{
{
PolicyId: "abc",
Name: "policy",
Definition: `{"abc":"123"}`,
PolicyId: "abc",
Name: "policy",
Definition: `{"abc":"123"}`,
Description: "A description",
PolicyFamilyId: "def",
PolicyFamilyDefinitionOverrides: `{"def":"456"}`,
IsDefault: true,
MaxClustersPerUser: 42,
},
},
},
Expand All @@ -31,8 +36,13 @@ func TestDataSourceClusterPolicy(t *testing.T) {
ID: ".",
HCL: `name = "policy"`,
}.ApplyAndExpectData(t, map[string]any{
"id": "abc",
"definition": `{"abc":"123"}`,
"id": "abc",
"definition": `{"abc":"123"}`,
"description": "A description",
"policy_family_id": "def",
"policy_family_definition_overrides": `{"def":"456"}`,
"is_default": true,
"max_clusters_per_user": 42,
})
}

Expand Down

0 comments on commit 039bdb2

Please sign in to comment.