forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_recovery_network_mapping_resource.go
203 lines (177 loc) · 7.5 KB
/
site_recovery_network_mapping_resource.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
package recoveryservices
import (
"fmt"
"net/http"
"time"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2018-07-10/siterecovery"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
networkParse "github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/network/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/recoveryservices/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/recoveryservices/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/suppress"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceSiteRecoveryNetworkMapping() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSiteRecoveryNetworkMappingCreate,
Read: resourceSiteRecoveryNetworkMappingRead,
Delete: resourceSiteRecoveryNetworkMappingDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ReplicationNetworkMappingID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"recovery_vault_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.RecoveryServicesVaultName,
},
"source_recovery_fabric_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"target_recovery_fabric_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"source_network_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
DiffSuppressFunc: suppress.CaseDifference,
},
"target_network_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
DiffSuppressFunc: suppress.CaseDifference,
},
},
}
}
func resourceSiteRecoveryNetworkMappingCreate(d *pluginsdk.ResourceData, meta interface{}) error {
resGroup := d.Get("resource_group_name").(string)
vaultName := d.Get("recovery_vault_name").(string)
fabricName := d.Get("source_recovery_fabric_name").(string)
targetFabricName := d.Get("target_recovery_fabric_name").(string)
sourceNetworkId := d.Get("source_network_id").(string)
targetNetworkId := d.Get("target_network_id").(string)
name := d.Get("name").(string)
client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(resGroup, vaultName)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
// get network name from id
parsedSourceNetworkId, err := networkParse.VirtualNetworkID(sourceNetworkId)
if err != nil {
return fmt.Errorf("[ERROR] Unable to parse source_network_id '%s' (network mapping %s): %+v", sourceNetworkId, name, err)
}
if d.IsNewResource() {
existing, err := client.Get(ctx, fabricName, parsedSourceNetworkId.Name, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) &&
// todo this workaround can be removed when this bug is fixed
// https://github.com/Azure/azure-sdk-for-go/issues/8705
!utils.ResponseWasStatusCode(existing.Response, http.StatusBadRequest) {
return fmt.Errorf("checking for presence of existing site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_site_recovery_network_mapping", handleAzureSdkForGoBug2824(*existing.ID))
}
}
parameters := siterecovery.CreateNetworkMappingInput{
Properties: &siterecovery.CreateNetworkMappingInputProperties{
RecoveryNetworkID: &targetNetworkId,
RecoveryFabricName: &targetFabricName,
FabricSpecificDetails: siterecovery.AzureToAzureCreateNetworkMappingInput{
PrimaryNetworkID: &sourceNetworkId,
},
},
}
future, err := client.Create(ctx, fabricName, parsedSourceNetworkId.Name, name, parameters)
if err != nil {
return fmt.Errorf("creating site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("creating site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}
resp, err := client.Get(ctx, fabricName, parsedSourceNetworkId.Name, name)
if err != nil {
return fmt.Errorf("retrieving site recovery network mapping %s (vault %s): %+v", name, vaultName, err)
}
d.SetId(handleAzureSdkForGoBug2824(*resp.ID))
return resourceSiteRecoveryNetworkMappingRead(d, meta)
}
func resourceSiteRecoveryNetworkMappingRead(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := parse.ReplicationNetworkMappingID(d.Id())
if err != nil {
return err
}
client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(id.ResourceGroup, id.VaultName)
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
resp, err := client.Get(ctx, id.ReplicationFabricName, id.ReplicationNetworkName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on site recovery network mapping %s (vault %s): %+v", id.Name, id.VaultName, err)
}
d.Set("resource_group_name", id.ResourceGroup)
d.Set("recovery_vault_name", id.VaultName)
d.Set("source_recovery_fabric_name", id.ReplicationFabricName)
d.Set("name", resp.Name)
if props := resp.Properties; props != nil {
d.Set("source_network_id", props.PrimaryNetworkID)
d.Set("target_network_id", props.RecoveryNetworkID)
targetFabricId, err := parse.ReplicationFabricID(handleAzureSdkForGoBug2824(*resp.Properties.RecoveryFabricArmID))
if err != nil {
return err
}
d.Set("target_recovery_fabric_name", targetFabricId.Name)
}
return nil
}
func resourceSiteRecoveryNetworkMappingDelete(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := parse.ReplicationNetworkMappingID(d.Id())
if err != nil {
return err
}
client := meta.(*clients.Client).RecoveryServices.NetworkMappingClient(id.ResourceGroup, id.VaultName)
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
future, err := client.Delete(ctx, id.ReplicationFabricName, id.ReplicationNetworkName, id.Name)
if err != nil {
return fmt.Errorf("deleting site recovery network mapping %s (vault %s): %+v", id.Name, id.VaultName, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of site recovery network mapping %s (vault %s): %+v", id.Name, id.VaultName, err)
}
return nil
}