/
resource_arm_virtual_machine_extension.go
305 lines (258 loc) · 9.76 KB
/
resource_arm_virtual_machine_extension.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package compute
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmVirtualMachineExtension() *schema.Resource {
return &schema.Resource{
Create: resourceArmVirtualMachineExtensionsCreateUpdate,
Read: resourceArmVirtualMachineExtensionsRead,
Update: resourceArmVirtualMachineExtensionsCreateUpdate,
Delete: resourceArmVirtualMachineExtensionsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
// TODO: Remove in 2.0
"virtual_machine_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Deprecated: "This field has been deprecated in favor of `virtual_machine_id` - will be removed in 2.0 of the Azure Provider",
},
"virtual_machine_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: ValidateVirtualMachineID,
},
"publisher": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"type_handler_version": {
Type: schema.TypeString,
Required: true,
},
"auto_upgrade_minor_version": {
Type: schema.TypeBool,
Optional: true,
},
"settings": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: structure.SuppressJsonDiff,
},
// due to the sensitive nature, these are not returned by the API
"protected_settings": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: structure.SuppressJsonDiff,
},
// TODO: Remove location and resource_group_name in 2.0
"location": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
StateFunc: azure.NormalizeLocation,
DiffSuppressFunc: azure.SuppressLocationDiff,
Deprecated: "location is no longer used",
},
"resource_group_name": azure.SchemaResourceGroupNameDeprecated(),
"tags": tags.Schema(),
},
}
}
func resourceArmVirtualMachineExtensionsCreateUpdate(d *schema.ResourceData, meta interface{}) error {
vmExtensionClient := meta.(*clients.Client).Compute.VMExtensionClient
vmClient := meta.(*clients.Client).Compute.VMClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
var virtualMachineName, resourceGroup, location string
if virtualMachineId, ok := d.GetOk("virtual_machine_id"); ok {
v, err := ParseVirtualMachineID(virtualMachineId.(string))
if err != nil {
return fmt.Errorf("Error parsing Virtual Machine ID %q: %+v", virtualMachineId, err)
}
virtualMachineName = v.Name
resourceGroup = v.ResourceGroup
virtualMachine, err := vmClient.Get(ctx, resourceGroup, virtualMachineName, "")
if err != nil {
return fmt.Errorf("Error getting Virtual Machine %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if location = *virtualMachine.Location; location == "" {
return fmt.Errorf("Error reading location of Virtual Machine %q", virtualMachineName)
}
} else {
if vm, ok := d.GetOk("virtual_machine_name"); !ok {
return fmt.Errorf("Error, one of `virtual_machine_name` (deprecated) or `virtual_machine_id` (preferred) required.")
} else {
virtualMachineName = vm.(string)
resourceGroup = d.Get("resource_group_name").(string)
if resourceGroup == "" {
return fmt.Errorf("`resource_group_name` must be specified if `virtual_machine_id` is not used")
}
location = azure.NormalizeLocation(d.Get("location").(string))
if location == "" {
return fmt.Errorf("`location` must be specified if `virtual_machine_id` is not used")
}
}
}
if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := vmExtensionClient.Get(ctx, resourceGroup, virtualMachineName, name, "")
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Extension %q (Virtual Machine %q / Resource Group %q): %s", name, virtualMachineName, resourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_virtual_machine_extension", *existing.ID)
}
}
publisher := d.Get("publisher").(string)
extensionType := d.Get("type").(string)
typeHandlerVersion := d.Get("type_handler_version").(string)
autoUpgradeMinor := d.Get("auto_upgrade_minor_version").(bool)
t := d.Get("tags").(map[string]interface{})
extension := compute.VirtualMachineExtension{
Location: &location,
VirtualMachineExtensionProperties: &compute.VirtualMachineExtensionProperties{
Publisher: &publisher,
Type: &extensionType,
TypeHandlerVersion: &typeHandlerVersion,
AutoUpgradeMinorVersion: &autoUpgradeMinor,
},
Tags: tags.Expand(t),
}
if settingsString := d.Get("settings").(string); settingsString != "" {
settings, err := structure.ExpandJsonFromString(settingsString)
if err != nil {
return fmt.Errorf("unable to parse settings: %s", err)
}
extension.VirtualMachineExtensionProperties.Settings = &settings
}
if protectedSettingsString := d.Get("protected_settings").(string); protectedSettingsString != "" {
protectedSettings, err := structure.ExpandJsonFromString(protectedSettingsString)
if err != nil {
return fmt.Errorf("unable to parse protected_settings: %s", err)
}
extension.VirtualMachineExtensionProperties.ProtectedSettings = &protectedSettings
}
future, err := vmExtensionClient.CreateOrUpdate(ctx, resourceGroup, virtualMachineName, name, extension)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, vmExtensionClient.Client); err != nil {
return err
}
read, err := vmExtensionClient.Get(ctx, resourceGroup, virtualMachineName, name, "")
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Virtual Machine Extension %s (resource group %s) ID", name, resourceGroup)
}
d.SetId(*read.ID)
return resourceArmVirtualMachineExtensionsRead(d, meta)
}
func resourceArmVirtualMachineExtensionsRead(d *schema.ResourceData, meta interface{}) error {
vmExtensionClient := meta.(*clients.Client).Compute.VMExtensionClient
vmClient := meta.(*clients.Client).Compute.VMClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := ParseVirtualMachineExtensionID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
vmName := id.VirtualMachine
name := id.Name
virtualMachine, err := vmClient.Get(ctx, resourceGroup, vmName, "")
if err != nil {
if utils.ResponseWasNotFound(virtualMachine.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Virtual Machine %s: %s", name, err)
}
d.Set("virtual_machine_id", virtualMachine.ID)
if location := virtualMachine.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
resp, err := vmExtensionClient.Get(ctx, resourceGroup, vmName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Virtual Machine Extension %s: %s", name, err)
}
d.Set("name", resp.Name)
d.Set("virtual_machine_name", vmName)
d.Set("resource_group_name", resourceGroup)
if props := resp.VirtualMachineExtensionProperties; props != nil {
d.Set("publisher", props.Publisher)
d.Set("type", props.Type)
d.Set("type_handler_version", props.TypeHandlerVersion)
d.Set("auto_upgrade_minor_version", props.AutoUpgradeMinorVersion)
if settings := props.Settings; settings != nil {
settingsVal := settings.(map[string]interface{})
settingsJson, err := structure.FlattenJsonToString(settingsVal)
if err != nil {
return fmt.Errorf("unable to parse settings from response: %s", err)
}
d.Set("settings", settingsJson)
}
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceArmVirtualMachineExtensionsDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.VMExtensionClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["extensions"]
vmName := id.Path["virtualMachines"]
future, err := client.Delete(ctx, resGroup, vmName, name)
if err != nil {
return err
}
return future.WaitForCompletionRef(ctx, client.Client)
}