diff --git a/azurerm/import_arm_policy_definition_test.go b/azurerm/import_arm_policy_definition_test.go new file mode 100644 index 000000000000..347b20f3d649 --- /dev/null +++ b/azurerm/import_arm_policy_definition_test.go @@ -0,0 +1,29 @@ +package azurerm + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAzureRMPolicyDefinition_importBasic(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "azurerm_policy_definition.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPolicyDefinitionDestroy, + Steps: []resource.TestStep{ + { + Config: testAzureRMPolicyDefinition_basic(rInt), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/azurerm/resource_arm_policy_definition.go b/azurerm/resource_arm_policy_definition.go index d7c315926e7b..bb8dccd1397a 100644 --- a/azurerm/resource_arm_policy_definition.go +++ b/azurerm/resource_arm_policy_definition.go @@ -67,7 +67,7 @@ func resourceArmPolicyDefinition() *schema.Resource { DiffSuppressFunc: structure.SuppressJsonDiff, }, - "meta_data": { + "metadata": { Type: schema.TypeString, Optional: true, ValidateFunc: validation.ValidateJsonString, @@ -109,10 +109,10 @@ func resourceArmPolicyDefinitionCreateUpdate(d *schema.ResourceData, meta interf properties.PolicyRule = &policyRule } - if metaDataString := d.Get("meta_data").(string); metaDataString != "" { + if metaDataString := d.Get("metadata").(string); metaDataString != "" { metaData, err := structure.ExpandJsonFromString(metaDataString) if err != nil { - return fmt.Errorf("unable to parse meta_data: %s", err) + return fmt.Errorf("unable to parse metadata: %s", err) } properties.Metadata = &metaData } @@ -166,14 +166,39 @@ func resourceArmPolicyDefinitionRead(d *schema.ResourceData, meta interface{}) e } d.Set("name", resp.Name) + if props := resp.DefinitionProperties; props != nil { d.Set("policy_type", props.PolicyType) d.Set("mode", props.Mode) d.Set("display_name", props.DisplayName) d.Set("description", props.Description) - d.Set("policy_rule", props.PolicyRule) - d.Set("meta_data", props.Metadata) - d.Set("parameters", props.Parameters) + + if policyRule := props.PolicyRule; policyRule != nil { + policyRuleStr, err := structure.FlattenJsonToString(*policyRule) + if err != nil { + return fmt.Errorf("unable to flatten JSON for `policy_rule`: %s", err) + } + + d.Set("policy_rule", policyRuleStr) + } + + if metadata := props.Metadata; metadata != nil { + metadataStr, err := structure.FlattenJsonToString(*metadata) + if err != nil { + return fmt.Errorf("unable to flatten JSON for `metadata`: %s", err) + } + + d.Set("metadata", metadataStr) + } + + if parameters := props.Parameters; parameters != nil { + parametersStr, err := structure.FlattenJsonToString(*props.Parameters) + if err != nil { + return fmt.Errorf("unable to flatten JSON for `parameters`: %s", err) + } + + d.Set("parameters", parametersStr) + } } return nil diff --git a/azurerm/resource_arm_policy_definition_test.go b/azurerm/resource_arm_policy_definition_test.go index 44b873279dd7..3e4f970b3977 100644 --- a/azurerm/resource_arm_policy_definition_test.go +++ b/azurerm/resource_arm_policy_definition_test.go @@ -14,7 +14,6 @@ func TestAccAzureRMPolicyDefinition_basic(t *testing.T) { resourceName := "azurerm_policy_definition.test" ri := acctest.RandInt() - config := testAzureRMPolicyDefinition(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -22,9 +21,10 @@ func TestAccAzureRMPolicyDefinition_basic(t *testing.T) { CheckDestroy: testCheckAzureRMPolicyDefinitionDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: testAzureRMPolicyDefinition_basic(ri), Check: resource.ComposeTestCheckFunc( - testCheckAzureRMPolicyDefinitionExists(resourceName)), + testCheckAzureRMPolicyDefinitionExists(resourceName), + ), }, }, }) @@ -80,14 +80,14 @@ func testCheckAzureRMPolicyDefinitionDestroy(s *terraform.State) error { return nil } -func testAzureRMPolicyDefinition(ri int) string { +func testAzureRMPolicyDefinition_basic(ri int) string { return fmt.Sprintf(` resource "azurerm_policy_definition" "test" { - name = "acctestpol-%d" - policy_type = "Custom" - mode = "All" - display_name = "acctestpol-%d" - policy_rule =<