Skip to content

Commit

Permalink
Azure policy rest version bump to 2019-09-01
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpedrosa committed Jun 4, 2020
1 parent 0830fa9 commit 814ead7
Show file tree
Hide file tree
Showing 17 changed files with 548 additions and 109 deletions.
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
45 changes: 44 additions & 1 deletion azurerm/internal/services/policy/policy.go
Expand Up @@ -2,9 +2,10 @@ package policy

import (
"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 +103,45 @@ 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
}

return string(result), 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
}

return string(result), 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]*policy.ParameterDefinitionsValue)
if policyRuleStr, err := flattenParameterDefintionsValueToString(policyRule); err != nil {
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 @@ -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 @@ -183,11 +185,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 @@ -289,8 +291,7 @@ func resourceArmPolicySetDefinitionRead(d *schema.ResourceData, meta interface{}
}

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

"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/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Expand Up @@ -4,6 +4,7 @@ require (
github.com/Azure/azure-sdk-for-go v42.1.0+incompatible
github.com/Azure/go-autorest/autorest v0.10.0
github.com/Azure/go-autorest/autorest/date v0.2.0
github.com/bflad/tfproviderlint v0.14.0 // indirect
github.com/btubbs/datetime v0.1.0
github.com/davecgh/go-spew v1.1.1
github.com/google/uuid v1.1.1
Expand All @@ -13,7 +14,7 @@ require (
github.com/hashicorp/go-uuid v1.0.1
github.com/hashicorp/go-version v1.2.0
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.6.0
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/rickb777/date v1.12.5-0.20200422084442-6300e543c4d9
github.com/satori/go.uuid v1.2.0
github.com/satori/uuid v0.0.0-20160927100844-b061729afc07
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Expand Up @@ -55,6 +55,10 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM=
github.com/aws/aws-sdk-go v1.25.3 h1:uM16hIw9BotjZKMZlX05SN2EFtaWfi/NonPKIARiBLQ=
github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/bflad/gopaniccheck v0.1.0 h1:tJftp+bv42ouERmUMWLoUn/5bi/iQZjHPznM00cP/bU=
github.com/bflad/gopaniccheck v0.1.0/go.mod h1:ZCj2vSr7EqVeDaqVsWN4n2MwdROx1YL+LFo47TSWtsA=
github.com/bflad/tfproviderlint v0.14.0 h1:iki5tDr4l0jp0zEL0chZylptMT5QdE09E60cziFQNZA=
github.com/bflad/tfproviderlint v0.14.0/go.mod h1:1Jtjs6DPKoyqPrbPyMiy33h0ViO2h831uzoOuikCA60=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
Expand Down Expand Up @@ -138,8 +142,14 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8 h1:+RyjwU+Gnd/aTJBPZVDNm903eXVjjqhbaR4Ypx3xYyY=
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8/go.mod h1:p+ivJws3dpqbp1iP84+npOyAmTTOLMgCzrXd3GSdn/A=
github.com/hashicorp/terraform-json v0.4.0 h1:KNh29iNxozP5adfUFBJ4/fWd0Cu3taGgjHB38JYqOF4=
github.com/hashicorp/terraform-json v0.4.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-plugin-sdk v1.6.0 h1:Um5hsAL7kKsfTHtan8lybY/d03F2bHu4fjRB1H6Ag4U=
github.com/hashicorp/terraform-plugin-sdk v1.6.0/go.mod h1:H5QLx/uhwfxBZ59Bc5SqT19M4i+fYt7LZjHTpbLZiAg=
github.com/hashicorp/terraform-plugin-sdk v1.7.0 h1:B//oq0ZORG+EkVrIJy0uPGSonvmXqxSzXe8+GhknoW0=
github.com/hashicorp/terraform-plugin-sdk v1.7.0/go.mod h1:OjgQmey5VxnPej/buEhe+YqKm0KNvV3QqU4hkqHqPCY=
github.com/hashicorp/terraform-plugin-test v1.2.0 h1:AWFdqyfnOj04sxTdaAF57QqvW7XXrT8PseUHkbKsE8I=
github.com/hashicorp/terraform-plugin-test v1.2.0/go.mod h1:QIJHYz8j+xJtdtLrFTlzQVC0ocr3rf/OjIpgZLK56Hs=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
Expand Down Expand Up @@ -233,6 +243,8 @@ github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6Ac
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.1.0 h1:uJwc9HiBOCpoKIObTQaLR+tsEXx1HBHnOsOOpcdhZgw=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.1 h1:vGMsygfmeCl4Xb6OA5U5XVAaQZ69FvoG7X2jUtQujb8=
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty-yaml v1.0.1 h1:up11wlgAaDvlAGENcFDnZgkn0qUJurso7k6EpURKNF8=
github.com/zclconf/go-cty-yaml v1.0.1/go.mod h1:IP3Ylp0wQpYm50IHK8OZWKMu6sPJIUgKa8XhiVHura0=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand All @@ -243,6 +255,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand All @@ -254,6 +267,7 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -308,6 +322,10 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0 h1:Dh6fw+p6FyRl5x/FvNswO1ji0lIGzm3KP8Y9VkS9PTE=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20200214201135-548b770e2dfa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200216192241-b320d3a0f5a2 h1:0sfSpGSa544Fwnbot3Oxq/U6SXqjty6Jy/3wRhVS7ig=
golang.org/x/tools v0.0.0-20200216192241-b320d3a0f5a2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down

0 comments on commit 814ead7

Please sign in to comment.