Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attributes to databricks_cluster_policy data source #2351

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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