From fb02e3b32801dfaf8105404314a03bf94b4e243e Mon Sep 17 00:00:00 2001 From: Cedric <23346008+Cedric98@users.noreply.github.com> Date: Tue, 30 May 2023 16:02:28 +0200 Subject: [PATCH 1/3] Add more attributes to cluster policy --- docs/data-sources/cluster_policy.md | 6 +++++- policies/data_cluster_policy.go | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docs/data-sources/cluster_policy.md b/docs/data-sources/cluster_policy.md index c3f044c7a6..7e522831af 100644 --- a/docs/data-sources/cluster_policy.md +++ b/docs/data-sources/cluster_policy.md @@ -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. +- `family_id` - ID of the policy family. +- `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. diff --git a/policies/data_cluster_policy.go b/policies/data_cluster_policy.go index 6915a22555..454b1cd784 100644 --- a/policies/data_cluster_policy.go +++ b/policies/data_cluster_policy.go @@ -11,10 +11,14 @@ 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"` + FamilyId string `json:"family_id,omitempty" tf:"computed"` + FamilyDefinitionOverrides string `json:"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 { @@ -22,6 +26,10 @@ func DataSourceClusterPolicy() *schema.Resource { } data.Id = policy.PolicyId data.Definition = policy.Definition + data.Description = policy.Description + data.FamilyId = policy.PolicyFamilyId + data.FamilyDefinitionOverrides = policy.PolicyFamilyDefinitionOverrides + data.IsDefault = policy.IsDefault data.MaxClustersPerUser = int(policy.MaxClustersPerUser) return nil }) From e09d4d25f2b02256aae33e940782560dbcf0df3c Mon Sep 17 00:00:00 2001 From: Cedric <23346008+Cedric98@users.noreply.github.com> Date: Tue, 30 May 2023 17:17:38 +0200 Subject: [PATCH 2/3] Prefix family_id and family_definition_overrides with policy_ to match the API response from Databricks --- docs/data-sources/cluster_policy.md | 4 ++-- policies/data_cluster_policy.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/data-sources/cluster_policy.md b/docs/data-sources/cluster_policy.md index 7e522831af..ecd5ddb495 100644 --- a/docs/data-sources/cluster_policy.md +++ b/docs/data-sources/cluster_policy.md @@ -36,7 +36,7 @@ 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). - `description` - Additional human-readable description of the cluster policy. -- `family_id` - ID of the policy family. -- `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). +- `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. diff --git a/policies/data_cluster_policy.go b/policies/data_cluster_policy.go index 454b1cd784..7924c672cb 100644 --- a/policies/data_cluster_policy.go +++ b/policies/data_cluster_policy.go @@ -11,14 +11,14 @@ 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"` - Description string `json:"description,omitempty" tf:"computed"` - FamilyId string `json:"family_id,omitempty" tf:"computed"` - FamilyDefinitionOverrides string `json:"family_definition_overrides,omitempty" tf:"computed"` - IsDefault bool `json:"is_default,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 { @@ -27,8 +27,8 @@ func DataSourceClusterPolicy() *schema.Resource { data.Id = policy.PolicyId data.Definition = policy.Definition data.Description = policy.Description - data.FamilyId = policy.PolicyFamilyId - data.FamilyDefinitionOverrides = policy.PolicyFamilyDefinitionOverrides + data.PolicyFamilyId = policy.PolicyFamilyId + data.PolicyFamilyDefinitionOverrides = policy.PolicyFamilyDefinitionOverrides data.IsDefault = policy.IsDefault data.MaxClustersPerUser = int(policy.MaxClustersPerUser) return nil From f8a116db16908d43df78f9014358b2716ebea76f Mon Sep 17 00:00:00 2001 From: Cedric <23346008+Cedric98@users.noreply.github.com> Date: Tue, 30 May 2023 17:18:35 +0200 Subject: [PATCH 3/3] Modify TestDataSourceClusterPolicy to test new attributes --- policies/data_cluster_policy_test.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/policies/data_cluster_policy_test.go b/policies/data_cluster_policy_test.go index beff0a07ae..1016b6c190 100644 --- a/policies/data_cluster_policy_test.go +++ b/policies/data_cluster_policy_test.go @@ -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, }, }, }, @@ -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, }) }