forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_provider_registration_resource_test.go
More file actions
208 lines (180 loc) · 6.44 KB
/
resource_provider_registration_resource_test.go
File metadata and controls
208 lines (180 loc) · 6.44 KB
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package resource_test
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-09-01/providers"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/testclient"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/resourceproviders/custompollers"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
// NOTE: this can be moved up a level when all the others are
type ResourceProviderRegistrationResource struct{}
func TestAccResourceProviderRegistration_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_provider_registration", "test")
r := ResourceProviderRegistrationResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic("Microsoft.BlockchainTokens"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}
func TestAccResourceProviderRegistration_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_provider_registration", "test")
r := ResourceProviderRegistrationResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
PreConfig: func() {
// Last error may cause resource provider still in `Registered` status.Need to unregister it before a new test.
if err := r.unRegisterProviders("Microsoft.AgFoodPlatform"); err != nil {
t.Fatalf("Failed to reset resource provider registration with error: %+v", err)
}
},
Config: r.basic("Microsoft.AgFoodPlatform"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(func(data acceptance.TestData) string {
return r.requiresImport("Microsoft.AgFoodPlatform")
}),
})
}
func TestAccResourceProviderRegistration_feature(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_provider_registration", "test")
r := ResourceProviderRegistrationResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
PreConfig: func() {
// Last error may cause resource provider still in `Registered` status.Need to unregister it before a new test.
if err := r.unRegisterProviders("Microsoft.ManufacturingPlatform"); err != nil {
t.Fatalf("Failed to reset feature registration with error: %+v", err)
}
},
Config: r.feature(true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.feature(false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}
func (ResourceProviderRegistrationResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := providers.ParseSubscriptionProviderID(state.ID)
if err != nil {
return nil, err
}
resp, err := client.Resource.ResourceProvidersClient.Get(ctx, *id, providers.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}
isRegistered := false
if model := resp.Model; model != nil && model.RegistrationState != nil {
isRegistered = strings.EqualFold(*model.RegistrationState, "Registered")
}
return pointer.To(isRegistered), nil
}
func (ResourceProviderRegistrationResource) basic(name string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
skip_provider_registration = true
}
resource "azurerm_resource_provider_registration" "test" {
name = %q
lifecycle {
ignore_changes = [feature]
}
}
`, name)
}
func (r ResourceProviderRegistrationResource) requiresImport(name string) string {
template := r.basic(name)
return fmt.Sprintf(`
%s
resource "azurerm_resource_provider_registration" "import" {
name = azurerm_resource_provider_registration.test.name
}
`, template)
}
func (r ResourceProviderRegistrationResource) unRegisterProviders(resourceProviders ...string) error {
client, err := testclient.Build()
if err != nil {
return fmt.Errorf("building client: %+v", err)
}
for _, rp := range resourceProviders {
if err = r.unRegisterProvider(client, rp); err != nil {
return err
}
}
return nil
}
func (r ResourceProviderRegistrationResource) unRegisterProvider(client *clients.Client, resourceProvider string) error {
ctx, cancel := context.WithDeadline(client.StopContext, time.Now().Add(30*time.Minute))
defer cancel()
providersClient := client.Resource.ResourceProvidersClient
id := providers.NewSubscriptionProviderID(client.Account.SubscriptionId, resourceProvider)
provider, err := providersClient.Get(ctx, id, providers.DefaultGetOperationOptions())
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
}
registrationState := ""
if model := provider.Model; model != nil && model.RegistrationState != nil {
registrationState = *model.RegistrationState
}
if registrationState == "" {
return fmt.Errorf("retrieving Resource Provider %q: `registrationState` was nil", resourceProvider)
}
if !strings.EqualFold(registrationState, "Registered") {
return nil
}
if _, err := providersClient.Unregister(ctx, id); err != nil {
return fmt.Errorf("unregistering %s: %+v", id, err)
}
pollerType := custompollers.NewResourceProviderUnregistrationPoller(providersClient, id)
poller := pollers.NewPoller(pollerType, 10*time.Minute, pollers.DefaultNumberOfDroppedConnectionsToAllow)
if err := poller.PollUntilDone(ctx); err != nil {
return fmt.Errorf("waiting for %s to become unregistered: %+v", id, err)
}
return nil
}
func (ResourceProviderRegistrationResource) feature(registered bool) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
skip_provider_registration = true
}
resource "azurerm_resource_provider_registration" "test" {
name = "Microsoft.ManufacturingPlatform"
feature {
name = "DefaultFeature"
registered = %t
}
}
`, registered)
}