-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
resource_group_resource.go
211 lines (173 loc) · 7.11 KB
/
resource_group_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
204
205
206
207
208
209
210
211
package resource
import (
"fmt"
"log"
"sort"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
func resourceResourceGroup() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceResourceGroupCreateUpdate,
Read: resourceResourceGroupRead,
Update: resourceResourceGroupCreateUpdate,
Delete: resourceResourceGroupDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ResourceGroupID(id)
return err
}),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(90 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(90 * time.Minute),
Delete: pluginsdk.DefaultTimeout(90 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": azure.SchemaResourceGroupName(),
"location": azure.SchemaLocation(),
"tags": tags.Schema(),
},
}
}
func resourceResourceGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Resource.GroupsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
name := d.Get("name").(string)
location := location.Normalize(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})
if d.IsNewResource() {
existing, err := client.Get(ctx, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing resource group: %+v", err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_resource_group", *existing.ID)
}
}
parameters := resources.Group{
Location: utils.String(location),
Tags: tags.Expand(t),
}
if _, err := client.CreateOrUpdate(ctx, name, parameters); err != nil {
return fmt.Errorf("creating Resource Group %q: %+v", name, err)
}
resp, err := client.Get(ctx, name)
if err != nil {
return fmt.Errorf("retrieving Resource Group %q: %+v", name, err)
}
// @tombuildsstuff: intentionally leaving this for now, since this'll need
// details in the upgrade notes given how the Resource Group ID is cased incorrectly
// but needs to be fixed (resourcegroups -> resourceGroups)
d.SetId(*resp.ID)
return resourceResourceGroupRead(d, meta)
}
func resourceResourceGroupRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Resource.GroupsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ResourceGroupID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading resource group %q - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("reading resource group: %+v", err)
}
d.Set("name", resp.Name)
d.Set("location", location.NormalizeNilable(resp.Location))
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceResourceGroupDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Resource.GroupsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.ResourceGroupID(d.Id())
if err != nil {
return err
}
// conditionally check for nested resources and error if they exist
if meta.(*clients.Client).Features.ResourceGroup.PreventDeletionIfContainsResources {
resourceClient := meta.(*clients.Client).Resource.ResourcesClient
// Resource groups sometimes hold on to resource information after the resources have been deleted. We'll retry this check to account for that eventual consistency.
err = pluginsdk.Retry(10*time.Minute, func() *pluginsdk.RetryError {
results, err := resourceClient.ListByResourceGroupComplete(ctx, id.ResourceGroup, "", "provisioningState", utils.Int32(500))
if err != nil {
return pluginsdk.NonRetryableError(fmt.Errorf("listing resources in %s: %v", *id, err))
}
nestedResourceIds := make([]string, 0)
for results.NotDone() {
val := results.Value()
if val.ID != nil {
nestedResourceIds = append(nestedResourceIds, *val.ID)
}
if err := results.NextWithContext(ctx); err != nil {
return pluginsdk.NonRetryableError(fmt.Errorf("retrieving next page of nested items for %s: %+v", id, err))
}
}
if len(nestedResourceIds) > 0 {
time.Sleep(30 * time.Second)
return pluginsdk.RetryableError(resourceGroupContainsItemsError(id.ResourceGroup, nestedResourceIds))
}
return nil
})
if err != nil {
return err
}
}
deleteFuture, err := client.Delete(ctx, id.ResourceGroup, "")
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}
err = deleteFuture.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("waiting for the deletion of %s: %+v", *id, err)
}
return nil
}
func resourceGroupContainsItemsError(name string, nestedResourceIds []string) error {
formattedResourceUris := make([]string, 0)
for _, id := range nestedResourceIds {
formattedResourceUris = append(formattedResourceUris, fmt.Sprintf("* `%s`", id))
}
sort.Strings(formattedResourceUris)
message := fmt.Sprintf(`deleting Resource Group %[1]q: the Resource Group still contains Resources.
Terraform is configured to check for Resources within the Resource Group when deleting the Resource Group - and
raise an error if nested Resources still exist to avoid unintentionally deleting these Resources.
Terraform has detected that the following Resources still exist within the Resource Group:
%[2]s
This feature is intended to avoid the unintentional destruction of nested Resources provisioned through some
other means (for example, an ARM Template Deployment) - as such you must either remove these Resources, or
disable this behaviour using the feature flag 'prevent_deletion_if_contains_resources' within the 'features'
block when configuring the Provider, for example:
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
When that feature flag is set, Terraform will skip checking for any Resources within the Resource Group and
delete this using the Azure API directly (which will clear up any nested resources).
More information on the 'features' block can be found in the documentation:
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#features
`, name, strings.Join(formattedResourceUris, "\n"))
return fmt.Errorf(strings.ReplaceAll(message, "'", "`"))
}