|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package resource |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/hashicorp/go-azure-helpers/lang/response" |
| 13 | + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" |
| 14 | + "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2021-07-01/features" |
| 15 | + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" |
| 16 | + "github.com/hashicorp/terraform-provider-azurerm/internal/resourceproviders" |
| 17 | + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" |
| 18 | + "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/custompollers" |
| 19 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" |
| 20 | + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" |
| 21 | +) |
| 22 | + |
| 23 | +//go:generate go run ../../tools/generator-tests resourceidentity -resource-name resource_provider_feature_registration -properties "name,provider_name" -known-values "subscription_id:data.Subscriptions.Secondary" -test-name "basicForResourceIdentity" |
| 24 | + |
| 25 | +var _ sdk.ResourceWithIdentity = ResourceProviderFeatureRegistrationResource{} |
| 26 | + |
| 27 | +type ResourceProviderFeatureRegistrationResource struct{} |
| 28 | + |
| 29 | +func (r ResourceProviderFeatureRegistrationResource) Identity() resourceids.ResourceId { |
| 30 | + return new(features.FeatureId) |
| 31 | +} |
| 32 | + |
| 33 | +type ResourceProviderFeatureRegistrationModel struct { |
| 34 | + Name string `tfschema:"name"` |
| 35 | + ProviderName string `tfschema:"provider_name"` |
| 36 | +} |
| 37 | + |
| 38 | +func (r ResourceProviderFeatureRegistrationResource) Arguments() map[string]*pluginsdk.Schema { |
| 39 | + return map[string]*pluginsdk.Schema{ |
| 40 | + "name": { |
| 41 | + Type: pluginsdk.TypeString, |
| 42 | + Required: true, |
| 43 | + ForceNew: true, |
| 44 | + ValidateFunc: validation.StringIsNotEmpty, |
| 45 | + }, |
| 46 | + |
| 47 | + "provider_name": { |
| 48 | + Type: pluginsdk.TypeString, |
| 49 | + Required: true, |
| 50 | + ForceNew: true, |
| 51 | + ValidateFunc: resourceproviders.EnhancedValidate, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (r ResourceProviderFeatureRegistrationResource) Attributes() map[string]*pluginsdk.Schema { |
| 57 | + return map[string]*pluginsdk.Schema{} |
| 58 | +} |
| 59 | + |
| 60 | +func (r ResourceProviderFeatureRegistrationResource) ModelObject() interface{} { |
| 61 | + return &ResourceProviderFeatureRegistrationModel{} |
| 62 | +} |
| 63 | + |
| 64 | +func (r ResourceProviderFeatureRegistrationResource) ResourceType() string { |
| 65 | + return "azurerm_resource_provider_feature_registration" |
| 66 | +} |
| 67 | + |
| 68 | +func (r ResourceProviderFeatureRegistrationResource) Create() sdk.ResourceFunc { |
| 69 | + return sdk.ResourceFunc{ |
| 70 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 71 | + client := metadata.Client.Resource.FeaturesClient |
| 72 | + |
| 73 | + var obj ResourceProviderFeatureRegistrationModel |
| 74 | + if err := metadata.Decode(&obj); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + featureId := features.NewFeatureID(metadata.Client.Account.SubscriptionId, obj.ProviderName, obj.Name) |
| 79 | + |
| 80 | + provider, err := client.Get(ctx, featureId) |
| 81 | + if err != nil { |
| 82 | + if response.WasNotFound(provider.HttpResponse) { |
| 83 | + return fmt.Errorf("%s was not found", featureId) |
| 84 | + } |
| 85 | + |
| 86 | + return fmt.Errorf("retrieving %s: %+v", featureId, err) |
| 87 | + } |
| 88 | + |
| 89 | + registrationState := "" |
| 90 | + if model := provider.Model; model != nil && model.Properties != nil && model.Properties.State != nil { |
| 91 | + registrationState = *model.Properties.State |
| 92 | + } |
| 93 | + |
| 94 | + if registrationState == "" { |
| 95 | + return fmt.Errorf("retrieving %s: `registrationState` was nil", featureId) |
| 96 | + } |
| 97 | + |
| 98 | + if strings.EqualFold(registrationState, Registered) { |
| 99 | + return metadata.ResourceRequiresImport(r.ResourceType(), featureId) |
| 100 | + } |
| 101 | + |
| 102 | + if strings.EqualFold(registrationState, Pending) { |
| 103 | + return fmt.Errorf("%s which requires manual approval can not be managed by Terraform", featureId) |
| 104 | + } |
| 105 | + |
| 106 | + resp, err := client.Register(ctx, featureId) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("registering %s: %+v", featureId, err) |
| 109 | + } |
| 110 | + |
| 111 | + if resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.State != nil { |
| 112 | + if strings.EqualFold(*resp.Model.Properties.State, Pending) { |
| 113 | + return fmt.Errorf("%s which requires manual approval can not be managed by terraform", featureId) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + pollerType := custompollers.NewResourceProviderFeatureRegistrationPoller(client, featureId, Registered) |
| 118 | + poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) |
| 119 | + if err := poller.PollUntilDone(ctx); err != nil { |
| 120 | + return fmt.Errorf("waiting for registration of %s: %+v", featureId, err) |
| 121 | + } |
| 122 | + |
| 123 | + metadata.SetID(featureId) |
| 124 | + return pluginsdk.SetResourceIdentityData(metadata.ResourceData, &featureId) |
| 125 | + }, |
| 126 | + |
| 127 | + Timeout: 30 * time.Minute, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func (r ResourceProviderFeatureRegistrationResource) Read() sdk.ResourceFunc { |
| 132 | + return sdk.ResourceFunc{ |
| 133 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 134 | + client := metadata.Client.Resource.FeaturesClient |
| 135 | + |
| 136 | + featureId, err := features.ParseFeatureID(metadata.ResourceData.Id()) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + resp, err := client.Get(ctx, *featureId) |
| 142 | + if err != nil { |
| 143 | + if response.WasNotFound(resp.HttpResponse) { |
| 144 | + return metadata.MarkAsGone(featureId) |
| 145 | + } |
| 146 | + |
| 147 | + return fmt.Errorf("retrieving %s: %+v", featureId, err) |
| 148 | + } |
| 149 | + |
| 150 | + registrationState := "" |
| 151 | + if model := resp.Model; model != nil && model.Properties != nil && model.Properties.State != nil { |
| 152 | + registrationState = *model.Properties.State |
| 153 | + } |
| 154 | + |
| 155 | + if !strings.EqualFold(registrationState, Registered) { |
| 156 | + return metadata.MarkAsGone(featureId) |
| 157 | + } |
| 158 | + |
| 159 | + if err := pluginsdk.SetResourceIdentityData(metadata.ResourceData, featureId); err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + return metadata.Encode(&ResourceProviderFeatureRegistrationModel{ |
| 164 | + Name: featureId.FeatureName, |
| 165 | + ProviderName: featureId.ProviderName, |
| 166 | + }) |
| 167 | + }, |
| 168 | + Timeout: 5 * time.Minute, |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func (r ResourceProviderFeatureRegistrationResource) Delete() sdk.ResourceFunc { |
| 173 | + return sdk.ResourceFunc{ |
| 174 | + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { |
| 175 | + client := metadata.Client.Resource.FeaturesClient |
| 176 | + |
| 177 | + featureId, err := features.ParseFeatureID(metadata.ResourceData.Id()) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + |
| 182 | + if _, err := client.Unregister(ctx, *featureId); err != nil { |
| 183 | + return fmt.Errorf("unregistering %s: %+v", featureId, err) |
| 184 | + } |
| 185 | + |
| 186 | + pollerType := custompollers.NewResourceProviderFeatureRegistrationPoller(client, *featureId, Unregistered) |
| 187 | + poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) |
| 188 | + if err := poller.PollUntilDone(ctx); err != nil { |
| 189 | + return fmt.Errorf("waiting for registration of %s: %+v", featureId, err) |
| 190 | + } |
| 191 | + |
| 192 | + return nil |
| 193 | + }, |
| 194 | + Timeout: 30 * time.Minute, |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func (r ResourceProviderFeatureRegistrationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { |
| 199 | + return features.ValidateFeatureID |
| 200 | +} |
0 commit comments