Skip to content

Commit

Permalink
Merge pull request #1 from terraform-providers/policy-definition
Browse files Browse the repository at this point in the history
Fixes for `azurerm_policy_definition`
  • Loading branch information
jaymitre authored Mar 27, 2018
2 parents 163edd5 + 74156c0 commit 06a5842
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 180 deletions.
29 changes: 29 additions & 0 deletions azurerm/import_arm_policy_definition_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
37 changes: 31 additions & 6 deletions azurerm/resource_arm_policy_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func resourceArmPolicyDefinition() *schema.Resource {
DiffSuppressFunc: structure.SuppressJsonDiff,
},

"meta_data": {
"metadata": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateJsonString,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions azurerm/resource_arm_policy_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPolicyDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: testAzureRMPolicyDefinition_basic(ri),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPolicyDefinitionExists(resourceName)),
testCheckAzureRMPolicyDefinitionExists(resourceName),
),
},
},
})
Expand Down Expand Up @@ -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 =<<POLICY_RULE
name = "acctestpol-%d"
policy_type = "Custom"
mode = "All"
display_name = "acctestpol-%d"
policy_rule = <<POLICY_RULE
{
"if": {
"not": {
Expand All @@ -99,9 +99,9 @@ resource "azurerm_policy_definition" "test" {
"effect": "audit"
}
}
POLICY_RULE
POLICY_RULE
parameters =<<PARAMETERS
parameters = <<PARAMETERS
{
"allowedLocations": {
"type": "Array",
Expand All @@ -112,6 +112,7 @@ resource "azurerm_policy_definition" "test" {
}
}
}
PARAMETERS
}`, ri, ri)
PARAMETERS
}
`, ri, ri)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 06a5842

Please sign in to comment.