-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_recovery_services_protected_vm.go
290 lines (239 loc) · 11 KB
/
resource_arm_recovery_services_protected_vm.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
package azurerm
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2017-07-01/backup"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"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/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 resourceArmRecoveryServicesProtectedVm() *schema.Resource {
return &schema.Resource{
Create: resourceArmRecoveryServicesProtectedVmCreateUpdate,
Read: resourceArmRecoveryServicesProtectedVmRead,
Update: resourceArmRecoveryServicesProtectedVmCreateUpdate,
Delete: resourceArmRecoveryServicesProtectedVmDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(80 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(80 * time.Minute),
Delete: schema.DefaultTimeout(80 * time.Minute),
},
Schema: map[string]*schema.Schema{
"resource_group_name": azure.SchemaResourceGroupName(),
"recovery_vault_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateRecoveryServicesVaultName,
},
"source_vm_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"backup_policy_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
"tags": tags.Schema(),
},
}
}
func resourceArmRecoveryServicesProtectedVmCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).RecoveryServices.ProtectedItemsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()
resourceGroup := d.Get("resource_group_name").(string)
t := d.Get("tags").(map[string]interface{})
vaultName := d.Get("recovery_vault_name").(string)
vmId := d.Get("source_vm_id").(string)
policyId := d.Get("backup_policy_id").(string)
//get VM name from id
parsedVmId, err := azure.ParseAzureResourceID(vmId)
if err != nil {
return fmt.Errorf("[ERROR] Unable to parse source_vm_id '%s': %+v", vmId, err)
}
vmName, hasName := parsedVmId.Path["virtualMachines"]
if !hasName {
return fmt.Errorf("[ERROR] parsed source_vm_id '%s' doesn't contain 'virtualMachines'", vmId)
}
protectedItemName := fmt.Sprintf("VM;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)
containerName := fmt.Sprintf("iaasvmcontainer;iaasvmcontainerv2;%s;%s", parsedVmId.ResourceGroup, vmName)
log.Printf("[DEBUG] Creating/updating Recovery Service Protected VM %s (resource group %q)", protectedItemName, resourceGroup)
if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err2 := client.Get(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, "")
if err2 != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Recovery Service Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err2)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_recovery_services_protected_vm", *existing.ID)
}
}
item := backup.ProtectedItemResource{
Tags: tags.Expand(t),
Properties: &backup.AzureIaaSComputeVMProtectedItem{
PolicyID: &policyId,
ProtectedItemType: backup.ProtectedItemTypeMicrosoftClassicComputevirtualMachines,
WorkloadType: backup.DataSourceTypeVM,
SourceResourceID: utils.String(vmId),
FriendlyName: utils.String(vmName),
VirtualMachineID: utils.String(vmId),
},
}
if _, err = client.CreateOrUpdate(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, item); err != nil {
return fmt.Errorf("Error creating/updating Recovery Service Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err)
}
resp, err := resourceArmRecoveryServicesProtectedVmWaitForStateCreateUpdate(ctx, client, vaultName, resourceGroup, containerName, protectedItemName, policyId, d)
if err != nil {
return err
}
id := strings.Replace(*resp.ID, "Subscriptions", "subscriptions", 1) // This code is a workaround for this bug https://github.com/Azure/azure-sdk-for-go/issues/2824
d.SetId(id)
return resourceArmRecoveryServicesProtectedVmRead(d, meta)
}
func resourceArmRecoveryServicesProtectedVmRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).RecoveryServices.ProtectedItemsClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
protectedItemName := id.Path["protectedItems"]
vaultName := id.Path["vaults"]
resourceGroup := id.ResourceGroup
containerName := id.Path["protectionContainers"]
log.Printf("[DEBUG] Reading Recovery Service Protected VM %q (resource group %q)", protectedItemName, resourceGroup)
resp, err := client.Get(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Recovery Service Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err)
}
d.Set("resource_group_name", resourceGroup)
d.Set("recovery_vault_name", vaultName)
if properties := resp.Properties; properties != nil {
if vm, ok := properties.AsAzureIaaSComputeVMProtectedItem(); ok {
d.Set("source_vm_id", vm.SourceResourceID)
if v := vm.PolicyID; v != nil {
d.Set("backup_policy_id", strings.Replace(*v, "Subscriptions", "subscriptions", 1))
}
}
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceArmRecoveryServicesProtectedVmDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).RecoveryServices.ProtectedItemsClient
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
protectedItemName := id.Path["protectedItems"]
resourceGroup := id.ResourceGroup
vaultName := id.Path["vaults"]
containerName := id.Path["protectionContainers"]
log.Printf("[DEBUG] Deleting Recovery Service Protected Item %q (resource group %q)", protectedItemName, resourceGroup)
resp, err := client.Delete(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error issuing delete request for Recovery Service Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err)
}
}
if _, err := resourceArmRecoveryServicesProtectedVmWaitForDeletion(ctx, client, vaultName, resourceGroup, containerName, protectedItemName, "", d); err != nil {
return err
}
return nil
}
func resourceArmRecoveryServicesProtectedVmWaitForStateCreateUpdate(ctx context.Context, client *backup.ProtectedItemsClient, vaultName, resourceGroup, containerName, protectedItemName string, policyId string, d *schema.ResourceData) (backup.ProtectedItemResource, error) {
state := &resource.StateChangeConf{
MinTimeout: 30 * time.Second,
Delay: 10 * time.Second,
Pending: []string{"NotFound"},
Target: []string{"Found"},
Refresh: resourceArmRecoveryServicesProtectedVmRefreshFunc(ctx, client, vaultName, resourceGroup, containerName, protectedItemName, policyId, true),
}
if features.SupportsCustomTimeouts() {
if d.IsNewResource() {
state.Timeout = d.Timeout(schema.TimeoutCreate)
} else {
state.Timeout = d.Timeout(schema.TimeoutUpdate)
}
} else {
state.Timeout = 30 * time.Minute
}
resp, err := state.WaitForState()
if err != nil {
i, _ := resp.(backup.ProtectedItemResource)
return i, fmt.Errorf("Error waiting for the Recovery Service Protected VM %q to be true (Resource Group %q) to provision: %+v", protectedItemName, resourceGroup, err)
}
return resp.(backup.ProtectedItemResource), nil
}
func resourceArmRecoveryServicesProtectedVmWaitForDeletion(ctx context.Context, client *backup.ProtectedItemsClient, vaultName, resourceGroup, containerName, protectedItemName string, policyId string, d *schema.ResourceData) (backup.ProtectedItemResource, error) {
state := &resource.StateChangeConf{
MinTimeout: 30 * time.Second,
Delay: 10 * time.Second,
Pending: []string{"Found"},
Target: []string{"NotFound"},
Refresh: resourceArmRecoveryServicesProtectedVmRefreshFunc(ctx, client, vaultName, resourceGroup, containerName, protectedItemName, policyId, false),
}
if features.SupportsCustomTimeouts() {
state.Timeout = d.Timeout(schema.TimeoutDelete)
} else {
state.Timeout = 30 * time.Minute
}
resp, err := state.WaitForState()
if err != nil {
i, _ := resp.(backup.ProtectedItemResource)
return i, fmt.Errorf("Error waiting for the Recovery Service Protected VM %q to be false (Resource Group %q) to provision: %+v", protectedItemName, resourceGroup, err)
}
return resp.(backup.ProtectedItemResource), nil
}
func resourceArmRecoveryServicesProtectedVmRefreshFunc(ctx context.Context, client *backup.ProtectedItemsClient, vaultName, resourceGroup, containerName, protectedItemName string, policyId string, newResource bool) resource.StateRefreshFunc {
// TODO: split this into two functions
return func() (interface{}, string, error) {
resp, err := client.Get(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return resp, "NotFound", nil
}
return resp, "Error", fmt.Errorf("Error making Read request on Recovery Service Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err)
} else if !newResource && policyId != "" {
if properties := resp.Properties; properties != nil {
if vm, ok := properties.AsAzureIaaSComputeVMProtectedItem(); ok {
if v := vm.PolicyID; v != nil {
if strings.Replace(*v, "Subscriptions", "subscriptions", 1) != policyId {
return resp, "NotFound", nil
}
} else {
return resp, "Error", fmt.Errorf("Error reading policy ID attribute nil on Recovery Service Protected VM %q (Resource Group %q)", protectedItemName, resourceGroup)
}
} else {
return resp, "Error", fmt.Errorf("Error reading properties on Recovery Service Protected VM %q (Resource Group %q)", protectedItemName, resourceGroup)
}
} else {
return resp, "Error", fmt.Errorf("Error reading properties on empty Recovery Service Protected VM %q (Resource Group %q)", protectedItemName, resourceGroup)
}
}
return resp, "Found", nil
}
}