Skip to content

Commit

Permalink
Cleaned up schema definition
Browse files Browse the repository at this point in the history
  • Loading branch information
nfx committed Jun 22, 2020
1 parent b1831ac commit 25da4b1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 141 deletions.
69 changes: 0 additions & 69 deletions client/model/cluster_policies.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,15 @@
package model

import (
"encoding/json"
"fmt"

"github.com/pkg/errors"
)

// AttributePolicy defines JSON mapping for attribute policy
type AttributePolicy struct {
Path *string `json:"-"`
Type string `json:"type"`
Value interface{} `json:"value,omitempty"`
DefaultValue interface{} `json:"defaultValue,omitempty"`
Values []interface{} `json:"values,omitempty"`
Hidden bool `json:"hidden,omitempty"`
IsOptional bool `json:"isOptional,omitempty"`
Pattern string `json:"pattern,omitempty"`
MinValue int `json:"minValue,omitempty"`
MaxValue int `json:"maxValue,omitempty"`
}

// ClusterPolicy defines cluster policy
type ClusterPolicy struct {
PolicyID string `json:"policy_id,omitempty"`
Name string `json:"name"`
Definition string `json:"definition"`
CreatedAtTimeStamp int64 `json:"created_at_timestamp"`

Attributes map[string]*AttributePolicy
}

// ParseDefinition parses policy definition
func (clusterPolicy *ClusterPolicy) ParseDefinition(definition string) error {
clusterPolicy.Definition = definition

err := json.Unmarshal([]byte(definition), &clusterPolicy.Attributes)
if err != nil {
return err
}

return nil
}

// ClusterPolicyCreate is the endity used for request
type ClusterPolicyCreate struct {
Name string `json:"name"`
Definition string `json:"definition"`
}

// // Prepare sets definition from attributes
// func (clusterPolicy *ClusterPolicy) Prepare() ([]byte, error) {
// policyJSONBytes, err := json.Marshal(clusterPolicy.attributes)
// if err != nil {
// return nil, errors.Wrapf(err, "Problem serializing %s policy", clusterPolicy.Name)
// }
// clusterPolicy.Definition = string(policyJSONBytes)
// }

// MarshalJSON is called when json.Marshal is invoked
func (clusterPolicy *ClusterPolicy) MarshalJSON() ([]byte, error) {
policyJSONBytes, err := json.Marshal(clusterPolicy.Attributes)
if err != nil {
return nil, errors.Wrapf(err, "Problem serializing %s policy", clusterPolicy.Name)
}
clusterPolicy.Definition = string(policyJSONBytes)
return json.Marshal(&struct {
PolicyID string `json:"policy_id,omitempty"`
Name string `json:"name"`
Definition string `json:"definition"`
}{
clusterPolicy.PolicyID, clusterPolicy.Name, clusterPolicy.Definition,
})
}

// ToString returns debug JSON, ignoring errors
func (clusterPolicy *ClusterPolicy) ToString() string {
j, err := clusterPolicy.MarshalJSON()
if err != nil {
return ""
}
return fmt.Sprintf("%s", j)
}
76 changes: 4 additions & 72 deletions databricks/resource_databricks_cluster_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package databricks

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/databrickslabs/databricks-terraform/client/model"
"github.com/databrickslabs/databricks-terraform/client/service"
Expand All @@ -14,10 +15,7 @@ func parsePolicyFromData(d *schema.ResourceData) (*model.ClusterPolicy, error) {
clusterPolicy.Name = name.(string)
}
if data, ok := d.GetOk("definition"); ok {
err := clusterPolicy.ParseDefinition(data.(string))
if err != nil {
return nil, err
}
clusterPolicy.Definition = data.(string)
}
return clusterPolicy, nil
}
Expand Down Expand Up @@ -46,23 +44,10 @@ func resourceClusterPolicyRead(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}

err = d.Set("definition", clusterPolicy.Definition)
if err != nil {
return err
}

// err = clusterPolicy.ParseDefinition(clusterPolicy.Definition)
// if err != nil {
// return err
// }
// policies := clusterPolicy.AttributePoliciesState()
// err = d.Set("attribute_policy", policies)
// if err != nil {
// return err
// }
// TODO: add definitions!!!

return nil
}

Expand Down Expand Up @@ -102,67 +87,14 @@ func resourceClusterPolicy() *schema.Resource {
Required: true,
Description: "Cluster policy name. This must be unique.\n" +
"Length must be between 1 and 100 characters.",
ValidateFunc: validation.StringLenBetween(1, 100),
},
"definition": {
Type: schema.TypeString,
Optional: true,
Description: "Policy definition JSON document expressed in\n" +
"Databricks Policy Definition Language.",
ConflictsWith: []string{"attribute_policy"},
},
"attribute_policy": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
ConflictsWith: []string{"definition"},
ConfigMode: schema.SchemaConfigModeAttr,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"path": {
Type: schema.TypeString,
Required: true,
},
"value": { // type: fixed
Type: schema.TypeString,
Optional: true,
},
"default_value": { // type: limiting
Type: schema.TypeString,
Optional: true,
},
"values": { // type: limiting
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"hidden": { // type: fixed
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"is_optional": { // type: limiting
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"pattern": { // type: regex
Type: schema.TypeString,
Optional: true,
},
"min_value": { // type: range
Type: schema.TypeInt,
Optional: true,
},
"max_value": { // type: range
Type: schema.TypeInt,
Optional: true,
},
},
},
ValidateFunc: validation.StringIsJSON,
},
},
}
Expand Down

0 comments on commit 25da4b1

Please sign in to comment.