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

Azure policy rest version bump to 2019-09-01 #7211

Merged
merged 1 commit into from Jun 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion azurerm/internal/services/policy/client/client.go
Expand Up @@ -2,7 +2,7 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/policyinsights/mgmt/2019-10-01/policyinsights"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
56 changes: 55 additions & 1 deletion azurerm/internal/services/policy/policy.go
@@ -1,10 +1,12 @@
package policy

import (
"bytes"
"context"
"encoding/json"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
)

func getPolicyDefinitionByDisplayName(ctx context.Context, client *policy.DefinitionsClient, displayName, managementGroupName string) (policy.Definition, error) {
Expand Down Expand Up @@ -102,3 +104,55 @@ func getPolicySetDefinitionByDisplayName(ctx context.Context, client *policy.Set

return results[0], nil
}

func expandParameterDefinitionsValueFromString(jsonString string) (map[string]*policy.ParameterDefinitionsValue, error) {
var result map[string]*policy.ParameterDefinitionsValue

err := json.Unmarshal([]byte(jsonString), &result)

return result, err
}

func flattenParameterDefintionsValueToString(input map[string]*policy.ParameterDefinitionsValue) (string, error) {
if len(input) == 0 {
return "", nil
}

result, err := json.Marshal(input)
if err != nil {
return "", err
}

compactJson := bytes.Buffer{}
if err := json.Compact(&compactJson, result); err != nil {
return "", err
}

return compactJson.String(), nil
}

func expandParameterValuesValueFromString(jsonString string) (map[string]*policy.ParameterValuesValue, error) {
var result map[string]*policy.ParameterValuesValue

err := json.Unmarshal([]byte(jsonString), &result)

return result, err
}

func flattenParameterValuesValueToString(input map[string]*policy.ParameterValuesValue) (string, error) {
if len(input) == 0 {
return "", nil
}

result, err := json.Marshal(input)
if err != nil {
return "", err
}

compactJson := bytes.Buffer{}
if err := json.Compact(&compactJson, result); err != nil {
return "", err
}

return compactJson.String(), nil
}
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
Expand Down Expand Up @@ -171,12 +171,12 @@ func resourceArmPolicyAssignmentCreateUpdate(d *schema.ResourceData, meta interf
}

if v := d.Get("parameters").(string); v != "" {
expandedParams, err := structure.ExpandJsonFromString(v)
expandedParams, err := expandParameterValuesValueFromString(v)
if err != nil {
return fmt.Errorf("Error expanding JSON from Parameters %q: %+v", v, err)
}

assignment.AssignmentProperties.Parameters = &expandedParams
assignment.AssignmentProperties.Parameters = expandedParams
}

if _, ok := d.GetOk("not_scopes"); ok {
Expand Down Expand Up @@ -253,8 +253,7 @@ func resourceArmPolicyAssignmentRead(d *schema.ResourceData, meta interface{}) e
d.Set("display_name", props.DisplayName)

if params := props.Parameters; params != nil {
paramsVal := params.(map[string]interface{})
json, err := structure.FlattenJsonToString(paramsVal)
json, err := flattenParameterValuesValueToString(params)
if err != nil {
return fmt.Errorf("Error serializing JSON from Parameters: %+v", err)
}
Expand Down
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
Expand Down Expand Up @@ -120,16 +120,21 @@ func dataSourceArmPolicyDefinitionRead(d *schema.ResourceData, meta interface{})
d.Set("type", policyDefinition.Type)
d.Set("policy_type", policyDefinition.PolicyType)

if policyRuleStr := flattenJSON(policyDefinition.PolicyRule); policyRuleStr != "" {
policyRule := policyDefinition.PolicyRule.(map[string]interface{})
if policyRuleStr := flattenJSON(policyRule); policyRuleStr != "" {
d.Set("policy_rule", policyRuleStr)
} else {
return fmt.Errorf("failed to flatten Policy Definition Rule %q: %+v", name, err)
}

if metadataStr := flattenJSON(policyDefinition.Metadata); metadataStr != "" {
d.Set("metadata", metadataStr)
}

if parametersStr := flattenJSON(policyDefinition.Parameters); parametersStr != "" {
if parametersStr, err := flattenParameterDefintionsValueToString(policyDefinition.Parameters); err == nil {
d.Set("parameters", parametersStr)
} else {
return fmt.Errorf("failed to flatten Policy Parameters %q: %+v", name, err)
}

return nil
Expand Down
24 changes: 11 additions & 13 deletions azurerm/internal/services/policy/policy_definition_resource.go
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -54,20 +54,16 @@ func resourceArmPolicyDefinition() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(policy.TypeBuiltIn),
string(policy.TypeCustom),
string(policy.TypeNotSpecified),
string(policy.BuiltIn),
string(policy.Custom),
string(policy.NotSpecified),
string(policy.Static),
}, true)},

"mode": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(policy.All),
string(policy.Indexed),
string(policy.NotSpecified),
}, true),
},

"management_group_id": {
Expand Down Expand Up @@ -178,7 +174,7 @@ func resourceArmPolicyDefinitionCreateUpdate(d *schema.ResourceData, meta interf

properties := policy.DefinitionProperties{
PolicyType: policy.Type(policyType),
Mode: policy.Mode(mode),
Mode: utils.String(mode),
DisplayName: utils.String(displayName),
Description: utils.String(description),
}
Expand All @@ -200,11 +196,11 @@ func resourceArmPolicyDefinitionCreateUpdate(d *schema.ResourceData, meta interf
}

if parametersString := d.Get("parameters").(string); parametersString != "" {
parameters, err := structure.ExpandJsonFromString(parametersString)
parameters, err := expandParameterDefinitionsValueFromString(parametersString)
if err != nil {
return fmt.Errorf("unable to parse parameters: %s", err)
}
properties.Parameters = &parameters
properties.Parameters = parameters
}

definition := policy.Definition{
Expand Down Expand Up @@ -300,8 +296,10 @@ func resourceArmPolicyDefinitionRead(d *schema.ResourceData, meta interface{}) e
d.Set("metadata", metadataStr)
}

if parametersStr := flattenJSON(props.Parameters); parametersStr != "" {
if parametersStr, err := flattenParameterDefintionsValueToString(props.Parameters); err == nil {
d.Set("parameters", parametersStr)
} else {
return fmt.Errorf("Error flattening policy definition parameters %+v", err)
}
}

Expand Down
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
Expand Down Expand Up @@ -102,7 +102,12 @@ func dataSourceArmPolicySetDefinitionRead(d *schema.ResourceData, meta interface
d.Set("description", setDefinition.Description)
d.Set("policy_type", setDefinition.PolicyType)
d.Set("metadata", flattenJSON(setDefinition.Metadata))
d.Set("parameters", flattenJSON(setDefinition.Parameters))

if paramsStr, err := flattenParameterDefintionsValueToString(setDefinition.Parameters); err != nil {
return fmt.Errorf("unable to flatten JSON for `parameters`: %+v", err)
} else {
d.Set("parameters", paramsStr)
}

definitionBytes, err := json.Marshal(setDefinition.PolicyDefinitions)
if err != nil {
Expand Down
25 changes: 17 additions & 8 deletions azurerm/internal/services/policy/policy_set_definition_resource.go
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/policy"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -58,8 +58,10 @@ func resourceArmPolicySetDefinition() *schema.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(policy.TypeBuiltIn),
string(policy.TypeCustom),
string(policy.BuiltIn),
string(policy.Custom),
string(policy.NotSpecified),
string(policy.Static),
}, false),
},

Expand Down Expand Up @@ -141,6 +143,14 @@ func policyDefinitionsDiffSuppressFunc(_, old, new string, _ *schema.ResourceDat
return false
}

for i := range newPolicyDefinitions {
newPolicyDefinitions[i].PolicyDefinitionReferenceID = nil
}

for i := range oldPolicyDefinitions {
oldPolicyDefinitions[i].PolicyDefinitionReferenceID = nil
}

return reflect.DeepEqual(oldPolicyDefinitions, newPolicyDefinitions)
}

Expand Down Expand Up @@ -183,11 +193,11 @@ func resourceArmPolicySetDefinitionCreateUpdate(d *schema.ResourceData, meta int
}

if parametersString := d.Get("parameters").(string); parametersString != "" {
parameters, err := structure.ExpandJsonFromString(parametersString)
parameters, err := expandParameterDefinitionsValueFromString(parametersString)
if err != nil {
return fmt.Errorf("unable to expand parameters json: %s", err)
}
properties.Parameters = &parameters
properties.Parameters = parameters
}

if policyDefinitionsString := d.Get("policy_definitions").(string); policyDefinitionsString != "" {
Expand Down Expand Up @@ -288,9 +298,8 @@ func resourceArmPolicySetDefinitionRead(d *schema.ResourceData, meta interface{}
d.Set("metadata", metadataStr)
}

if parameters := props.Parameters; parameters != nil {
paramsVal := parameters.(map[string]interface{})
parametersStr, err := structure.FlattenJsonToString(paramsVal)
if props.Parameters != nil {
parametersStr, err := flattenParameterDefintionsValueToString(props.Parameters)
if err != nil {
return fmt.Errorf("unable to flatten JSON for `parameters`: %s", err)
}
Expand Down