Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switching to use ID Formatters in (some) Requires Import steps #14338

Merged
merged 20 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
aab027d
dns: switching to use the terraform-managed resource id during requir…
tombuildsstuff Nov 24, 2021
55c5d8f
mixedreality: refactoring to use the id parsers for import errors
tombuildsstuff Nov 24, 2021
60788fa
media: refactoring to use id parsers for import errors
tombuildsstuff Nov 24, 2021
229d76a
firewall: refactoring to use resource id formatters
tombuildsstuff Nov 24, 2021
f7cd131
cdn: refactoring to use id formatters through imports
tombuildsstuff Nov 24, 2021
b8312c1
synapse: refactoring to use id formatters for import
tombuildsstuff Nov 24, 2021
2598edd
servicefabric: refactoring to use id formatters
tombuildsstuff Nov 24, 2021
1133289
servicebus: refactoring to use an id formatter for requires import er…
tombuildsstuff Nov 24, 2021
08278f5
search: refactoring to use a resource id formatter for import
tombuildsstuff Nov 24, 2021
d95510f
securitycenter: generating resource id parsers
tombuildsstuff Nov 24, 2021
5421025
securitycenter: generate all the things
tombuildsstuff Nov 25, 2021
a97dab2
securitycenter: switching to use ID Formatters where required
tombuildsstuff Nov 25, 2021
5fe0fac
hsm: switching to use a resource id formatter for import errors
tombuildsstuff Nov 25, 2021
1177675
healthcare: refactoring to use resource id formatters
tombuildsstuff Nov 25, 2021
7314f1a
loadbalancer: refactoring to use ID Formatters
tombuildsstuff Nov 25, 2021
b90f8a1
privatedns: refactoring to use id formatters in import errors
tombuildsstuff Nov 25, 2021
b50d7f3
r/netapp_volume: refactoring to use resource formatters for the impor…
tombuildsstuff Nov 25, 2021
688e34a
netapp: refactoring to use id formatters for importer errors
tombuildsstuff Nov 25, 2021
0a20e4f
fixing pr comments
tombuildsstuff Nov 26, 2021
1ad4c9d
linting
tombuildsstuff Nov 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 28 additions & 45 deletions internal/services/cdn/cdn_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/location"

"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2020-09-01/cdn"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -210,24 +212,22 @@ func resourceCdnEndpoint() *pluginsdk.Resource {
func resourceCdnEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) error {
endpointsClient := meta.(*clients.Client).Cdn.EndpointsClient
profilesClient := meta.(*clients.Client).Cdn.ProfilesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Azure ARM CDN EndPoint creation.")

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
profileName := d.Get("profile_name").(string)

existing, err := endpointsClient.Get(ctx, resourceGroup, profileName, name)
id := parse.NewEndpointID(subscriptionId, d.Get("resource_group_name").(string), d.Get("profile_name").(string), d.Get("name").(string))
existing, err := endpointsClient.Get(ctx, id.ResourceGroup, id.ProfileName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing CDN Endpoint %q (Profile %q / Resource Group %q): %s", name, profileName, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_cdn_endpoint", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_cdn_endpoint", id.ID())
}

location := azure.NormalizeLocation(d.Get("location").(string))
Expand Down Expand Up @@ -282,9 +282,9 @@ func resourceCdnEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) erro
endpoint.EndpointProperties.Origins = &origins
}

profile, err := profilesClient.Get(ctx, resourceGroup, profileName)
profile, err := profilesClient.Get(ctx, id.ResourceGroup, id.ProfileName)
if err != nil {
return fmt.Errorf("creating CDN Endpoint %q while getting CDN Profile (Profile %q / Resource Group %q): %+v", name, profileName, resourceGroup, err)
return fmt.Errorf("retrieving parent CDN Profile for %s: %+v", id, err)
}

if profile.Sku != nil {
Expand All @@ -304,32 +304,22 @@ func resourceCdnEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) erro
}
}

future, err := endpointsClient.Create(ctx, resourceGroup, profileName, name, endpoint)
future, err := endpointsClient.Create(ctx, id.ResourceGroup, id.ProfileName, id.Name, endpoint)
if err != nil {
return fmt.Errorf("creating CDN Endpoint %q (Profile %q / Resource Group %q): %+v", name, profileName, resourceGroup, err)
return fmt.Errorf("creating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, endpointsClient.Client); err != nil {
return fmt.Errorf("waiting for CDN Endpoint %q (Profile %q / Resource Group %q) to finish creating: %+v", name, profileName, resourceGroup, err)
}

read, err := endpointsClient.Get(ctx, resourceGroup, profileName, name)
if err != nil {
return fmt.Errorf("retrieving CDN Endpoint %q (Profile %q / Resource Group %q): %+v", name, profileName, resourceGroup, err)
}

id, err := parse.EndpointID(*read.ID)
if err != nil {
return err
return fmt.Errorf("waiting for the creation of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceCdnEndpointRead(d, meta)
}

func resourceCdnEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
endpointsClient := meta.(*clients.Client).Cdn.EndpointsClient
profilesClient := meta.(*clients.Client).Cdn.ProfilesClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -382,13 +372,9 @@ func resourceCdnEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
endpoint.EndpointPropertiesUpdateParameters.ProbePath = utils.String(probePath)
}

profilesClient := meta.(*clients.Client).Cdn.ProfilesClient
profileGetCtx, profileGetCancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer profileGetCancel()

profile, err := profilesClient.Get(profileGetCtx, id.ResourceGroup, id.ProfileName)
profile, err := profilesClient.Get(ctx, id.ResourceGroup, id.ProfileName)
if err != nil {
return fmt.Errorf("creating CDN Endpoint %q while getting CDN Profile (Profile %q / Resource Group %q): %+v", id.Name, id.ProfileName, id.ResourceGroup, err)
return fmt.Errorf("retrieving parent CDN Profile for %s: %+v", *id, err)
}

if profile.Sku != nil {
Expand All @@ -410,11 +396,11 @@ func resourceCdnEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) erro

future, err := endpointsClient.Update(ctx, id.ResourceGroup, id.ProfileName, id.Name, endpoint)
if err != nil {
return fmt.Errorf("updating CDN Endpoint %q (Profile %q / Resource Group %q): %s", id.Name, id.ProfileName, id.ResourceGroup, err)
return fmt.Errorf("updating %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, endpointsClient.Client); err != nil {
return fmt.Errorf("waiting for the CDN Endpoint %q (Profile %q / Resource Group %q) to finish updating: %+v", id.Name, id.ProfileName, id.ResourceGroup, err)
return fmt.Errorf("waiting for update of %s: %+v", *id, err)
}

return resourceCdnEndpointRead(d, meta)
Expand All @@ -430,25 +416,20 @@ func resourceCdnEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error
return err
}

log.Printf("[INFO] Retrieving CDN Endpoint %q (Profile %q / Resource Group %q)", id.Name, id.ProfileName, id.ResourceGroup)

resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("making Read request on Azure CDN Endpoint %q (Profile %q / Resource Group %q): %+v", id.Name, id.ProfileName, id.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("profile_name", id.ProfileName)

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("location", location.NormalizeNilable(resp.Location))

if props := resp.EndpointProperties; props != nil {
d.Set("host_name", props.HostName)
Expand All @@ -460,11 +441,13 @@ func resourceCdnEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error
d.Set("probe_path", props.ProbePath)
d.Set("optimization_type", string(props.OptimizationType))

compressionEnabled := false
if _, ok := d.GetOk("is_compression_enabled"); ok {
if compressionEnabled := props.IsCompressionEnabled; compressionEnabled != nil {
d.Set("is_compression_enabled", compressionEnabled)
if v := props.IsCompressionEnabled; v != nil {
compressionEnabled = *v
}
}
d.Set("is_compression_enabled", compressionEnabled)

contentTypes := flattenAzureRMCdnEndpointContentTypes(props.ContentTypesToCompress)
if err := d.Set("content_types_to_compress", contentTypes); err != nil {
Expand All @@ -481,7 +464,7 @@ func resourceCdnEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error
return fmt.Errorf("setting `origin`: %+v", err)
}

flattenedDeliveryPolicies, err := flattenArmCdnEndpointDeliveryPolicy(props.DeliveryPolicy)
flattenedDeliveryPolicies, err := flattenEndpointDeliveryPolicy(props.DeliveryPolicy)
if err != nil {
return err
}
Expand All @@ -508,11 +491,11 @@ func resourceCdnEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) erro

future, err := client.Delete(ctx, id.ResourceGroup, id.ProfileName, id.Name)
if err != nil {
return fmt.Errorf("deleting CDN Endpoint %q (Profile %q / Resource Group %q): %+v", id.Name, id.ProfileName, id.ResourceGroup, err)
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for CDN Endpoint %q (Profile %q / Resource Group %q) to be deleted: %+v", id.Name, id.ProfileName, id.ResourceGroup, err)
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return nil
Expand Down Expand Up @@ -701,7 +684,7 @@ type flattenedEndpointDeliveryPolicies struct {
deliveryRules []interface{}
}

func flattenArmCdnEndpointDeliveryPolicy(input *cdn.EndpointPropertiesUpdateParametersDeliveryPolicy) (*flattenedEndpointDeliveryPolicies, error) {
func flattenEndpointDeliveryPolicy(input *cdn.EndpointPropertiesUpdateParametersDeliveryPolicy) (*flattenedEndpointDeliveryPolicies, error) {
output := flattenedEndpointDeliveryPolicies{
globalDeliveryRules: make([]interface{}, 0),
deliveryRules: make([]interface{}, 0),
Expand Down
49 changes: 16 additions & 33 deletions internal/services/cdn/cdn_profile_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/location"

"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2020-09-01/cdn"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -75,24 +77,23 @@ func resourceCdnProfile() *pluginsdk.Resource {

func resourceCdnProfileCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cdn.ProfilesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)

id := parse.NewProfileID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing CDN Profile %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_cdn_profile", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_cdn_profile", id.ID())
}
}

Expand All @@ -108,26 +109,13 @@ func resourceCdnProfileCreate(d *pluginsdk.ResourceData, meta interface{}) error
},
}

future, err := client.Create(ctx, resGroup, name, cdnProfile)
future, err := client.Create(ctx, id.ResourceGroup, id.Name, cdnProfile)
if err != nil {
return err
return fmt.Errorf("creating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return err
}

read, err := client.Get(ctx, resGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read CDN Profile %s (resource group %s) ID", name, resGroup)
}

id, err := parse.ProfileID(*read.ID)
if err != nil {
return err
return fmt.Errorf("waiting for the creation of %s: %+v", id, err)
}

d.SetId(id.ID())
Expand All @@ -149,8 +137,6 @@ func resourceCdnProfileUpdate(d *pluginsdk.ResourceData, meta interface{}) error
return err
}

// name := d.Get("name").(string)
// resourceGroup := d.Get("resource_group_name").(string)
newTags := d.Get("tags").(map[string]interface{})

props := cdn.ProfileUpdateParameters{
Expand All @@ -159,11 +145,10 @@ func resourceCdnProfileUpdate(d *pluginsdk.ResourceData, meta interface{}) error

future, err := client.Update(ctx, id.ResourceGroup, id.Name, props)
if err != nil {
return fmt.Errorf("issuing update request for CDN Profile %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("updating %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the update of CDN Profile %q (Resource Group %q) to commplete: %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("waiting for the update of %s: %+v", *id, err)
}

return resourceCdnProfileRead(d, meta)
Expand All @@ -190,9 +175,7 @@ func resourceCdnProfileRead(d *pluginsdk.ResourceData, meta interface{}) error {

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("location", location.NormalizeNilable(resp.Location))

if sku := resp.Sku; sku != nil {
d.Set("sku", string(sku.Name))
Expand All @@ -213,11 +196,11 @@ func resourceCdnProfileDelete(d *pluginsdk.ResourceData, meta interface{}) error

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting CDN Profile %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of CDN Profile %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("waiting for the deletion of %s: %+v", *id, err)
}

return err
Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_a_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func resourceDnsARecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{})
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_a_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_a_record", resourceId.ID())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_aaaa_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func resourceDnsAaaaRecordCreateUpdate(d *pluginsdk.ResourceData, meta interface
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_aaaa_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_aaaa_record", resourceId.ID())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_caa_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func resourceDnsCaaRecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_caa_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_caa_record", resourceId.ID())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_cname_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func resourceDnsCNameRecordCreateUpdate(d *pluginsdk.ResourceData, meta interfac
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_cname_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_cname_record", resourceId.ID())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_mx_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func resourceDnsMxRecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_mx_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_mx_record", resourceId.ID())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_ns_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func resourceDnsNsRecordCreate(d *pluginsdk.ResourceData, meta interface{}) erro
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_ns_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_ns_record", resourceId.ID())
}

ttl := int64(d.Get("ttl").(int))
Expand Down
4 changes: 2 additions & 2 deletions internal/services/dns/dns_ptr_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func resourceDnsPtrRecordCreateUpdate(d *pluginsdk.ResourceData, meta interface{
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_dns_ptr_record", *existing.ID)
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_dns_ptr_record", resourceId.ID())
}
}

Expand Down
Loading