forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_policy_assignment.go
197 lines (158 loc) · 5.22 KB
/
resource_arm_policy_assignment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package azurerm
import (
"fmt"
"log"
"time"
"context"
"strconv"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-12-01/policy"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmPolicyAssignment() *schema.Resource {
return &schema.Resource{
Create: resourceArmPolicyAssignmentCreate,
Read: resourceArmPolicyAssignmentRead,
Delete: resourceArmPolicyAssignmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"scope": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_definition_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"parameters": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.ValidateJsonString,
DiffSuppressFunc: structure.SuppressJsonDiff,
},
},
}
}
func resourceArmPolicyAssignmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).policyAssignmentsClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
scope := d.Get("scope").(string)
policyDefinitionId := d.Get("policy_definition_id").(string)
displayName := d.Get("display_name").(string)
assignment := policy.Assignment{
AssignmentProperties: &policy.AssignmentProperties{
PolicyDefinitionID: utils.String(policyDefinitionId),
DisplayName: utils.String(displayName),
Scope: utils.String(scope),
},
}
if v := d.Get("description").(string); v != "" {
assignment.AssignmentProperties.Description = utils.String(v)
}
if v := d.Get("parameters").(string); v != "" {
expandedParams, err := structure.ExpandJsonFromString(v)
if err != nil {
return fmt.Errorf("Error expanding JSON from Parameters %q: %+v", v, err)
}
assignment.AssignmentProperties.Parameters = &expandedParams
}
_, err := client.Create(ctx, scope, name, assignment)
if err != nil {
return err
}
// Policy Assignments are eventually consistent; wait for them to stabilize
log.Printf("[DEBUG] Waiting for Policy Assignment %q to become available", name)
stateConf := &resource.StateChangeConf{
Pending: []string{"404"},
Target: []string{"200"},
Refresh: policyAssignmentRefreshFunc(ctx, client, scope, name),
Timeout: 5 * time.Minute,
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 10,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Policy Assignment %q to become available: %s", name, err)
}
resp, err := client.Get(ctx, scope, name)
if err != nil {
return err
}
d.SetId(*resp.ID)
return resourceArmPolicyAssignmentRead(d, meta)
}
func resourceArmPolicyAssignmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).policyAssignmentsClient
ctx := meta.(*ArmClient).StopContext
id := d.Id()
resp, err := client.GetByID(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading Policy Assignment %q - removing from state", id)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Policy Assignment %q: %+v", id, err)
}
d.Set("name", resp.Name)
if props := resp.AssignmentProperties; props != nil {
d.Set("scope", props.Scope)
d.Set("policy_definition_id", props.PolicyDefinitionID)
d.Set("description", props.Description)
d.Set("display_name", props.DisplayName)
if params := props.Parameters; params != nil {
paramsVal := params.(map[string]interface{})
json, err := structure.FlattenJsonToString(paramsVal)
if err != nil {
return fmt.Errorf("Error serializing JSON from Parameters: %+v", err)
}
d.Set("parameters", json)
}
}
return nil
}
func resourceArmPolicyAssignmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).policyAssignmentsClient
ctx := meta.(*ArmClient).StopContext
id := d.Id()
resp, err := client.DeleteByID(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}
return fmt.Errorf("Error deleting Policy Assignment %q: %+v", id, err)
}
return nil
}
func policyAssignmentRefreshFunc(ctx context.Context, client policy.AssignmentsClient, scope string, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, scope, name)
if err != nil {
return nil, strconv.Itoa(res.StatusCode), fmt.Errorf("Error issuing read request in policyAssignmentRefreshFunc for Policy Assignment %q (Scope: %q): %s", name, scope, err)
}
return res, strconv.Itoa(res.StatusCode), nil
}
}