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 more resources over to using ID Parsers #15839

Merged
merged 6 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,17 @@ func resourceEventGridEventSubscriptionCreateUpdate(d *pluginsdk.ResourceData, m
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
scope := d.Get("scope").(string)

id := parse.NewEventSubscriptionID(d.Get("scope").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, scope, name)
existing, err := client.Get(ctx, id.Scope, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing EventGrid Event Subscription %q (Scope %q): %s", name, scope, err)
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_eventgrid_event_subscription", *existing.ID)
return tf.ImportAsExistsError("azurerm_eventgrid_event_subscription", id.ID())
}
}

Expand All @@ -185,12 +183,12 @@ func resourceEventGridEventSubscriptionCreateUpdate(d *pluginsdk.ResourceData, m

filter, err := expandEventGridEventSubscriptionFilter(d)
if err != nil {
return fmt.Errorf("expanding filters for EventGrid Event Subscription %q (Scope %q): %+v", name, scope, err)
return fmt.Errorf("expanding filters for %s: %+v", id, err)
}

expirationTime, err := expandEventGridExpirationTime(d)
if err != nil {
return fmt.Errorf("creating/updating EventGrid Event Subscription %q (Scope %q): %s", name, scope, err)
return fmt.Errorf("expanding `expiration_time` for %s: %+v", id, err)
}

deadLetterDestination := expandEventGridEventSubscriptionStorageBlobDeadLetterDestination(d)
Expand Down Expand Up @@ -240,27 +238,15 @@ func resourceEventGridEventSubscriptionCreateUpdate(d *pluginsdk.ResourceData, m
EventSubscriptionProperties: &eventSubscriptionProperties,
}

log.Printf("[INFO] preparing arguments for AzureRM EventGrid Event Subscription creation with Properties: %+v.", eventSubscription)

future, err := client.CreateOrUpdate(ctx, scope, name, eventSubscription)
future, err := client.CreateOrUpdate(ctx, id.Scope, id.Name, eventSubscription)
if err != nil {
return fmt.Errorf("creating/updating EventGrid Event Subscription %q (Scope %q): %s", name, scope, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for EventGrid Event Subscription %q (Scope %q) to become available: %s", name, scope, err)
}

read, err := client.Get(ctx, scope, name)
if err != nil {
return fmt.Errorf("retrieving EventGrid Event Subscription %q (Scope %q): %s", name, scope, err)
return fmt.Errorf("waiting for %s: %+v", id, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read EventGrid Event Subscription %s (Scope %s) ID", name, scope)
}

d.SetId(*read.ID)

d.SetId(id.ID())
return resourceEventGridEventSubscriptionRead(d, meta)
}

Expand All @@ -277,15 +263,15 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
resp, err := client.Get(ctx, id.Scope, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] EventGrid Event Subscription '%s' was not found (resource group '%s')", id.Name, id.Scope)
log.Printf("[WARN] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("making Read request on EventGrid Event Subscription '%s': %+v", id.Name, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", resp.Name)
d.Set("name", id.Name)
d.Set("scope", id.Scope)

if props := resp.EventSubscriptionProperties; props != nil {
Expand All @@ -302,82 +288,82 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
deliveryIdentityFlattened = flattenEventGridEventSubscriptionIdentity(deliveryIdentity.Identity)
}
if err := d.Set("delivery_identity", deliveryIdentityFlattened); err != nil {
return fmt.Errorf("setting `delivery_identity` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `delivery_identity` for %s: %+v", *id, err)
}

if azureFunctionEndpoint, ok := destination.AsAzureFunctionEventSubscriptionDestination(); ok {
if err := d.Set("azure_function_endpoint", flattenEventGridEventSubscriptionAzureFunctionEndpoint(azureFunctionEndpoint)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "azure_function_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `azure_function_endpoint` for %s: %+v", *id, err)
}

if azureFunctionEndpoint.DeliveryAttributeMappings != nil {
if err := d.Set("delivery_property", flattenDeliveryProperties(d, azureFunctionEndpoint.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "azure_function_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `delivery_property` for %s: %+v", *id, err)
}
}
}
if v, ok := destination.AsEventHubEventSubscriptionDestination(); ok {
if err := d.Set("eventhub_endpoint_id", v.ResourceID); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "eventhub_endpoint_id", id.Name, id.Scope, err)
return fmt.Errorf("setting `eventhub_endpoint_id` for %s: %+v", *id, err)
}

if err := d.Set("eventhub_endpoint", flattenEventGridEventSubscriptionEventhubEndpoint(v)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "eventhub_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `eventhub_endpoint` for %s: %+v", *id, err)
}

if v.DeliveryAttributeMappings != nil {
if err := d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "eventhub_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `delivery_property` for %s: %+v", *id, err)
}
}
}
if v, ok := destination.AsHybridConnectionEventSubscriptionDestination(); ok {
if err := d.Set("hybrid_connection_endpoint_id", v.ResourceID); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "hybrid_connection_endpoint_id", id.Name, id.Scope, err)
return fmt.Errorf("setting `hybrid_connection_endpoint_id` for %s: %+v", *id, err)
}

if err := d.Set("hybrid_connection_endpoint", flattenEventGridEventSubscriptionHybridConnectionEndpoint(v)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "hybrid_connection_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `hybrid_connection_endpoint` for %s: %+v", *id, err)
}

if v.DeliveryAttributeMappings != nil {
if err := d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "hybrid_connection_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `delivery_property` for %s: %+v", *id, err)
}
}
}
if serviceBusQueueEndpoint, ok := destination.AsServiceBusQueueEventSubscriptionDestination(); ok {
if err := d.Set("service_bus_queue_endpoint_id", serviceBusQueueEndpoint.ResourceID); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "service_bus_queue_endpoint_id", id.Name, id.Scope, err)
return fmt.Errorf("setting `service_bus_queue_endpoint_id` for %s: %+v", *id, err)
}
}
if serviceBusTopicEndpoint, ok := destination.AsServiceBusTopicEventSubscriptionDestination(); ok {
if err := d.Set("service_bus_topic_endpoint_id", serviceBusTopicEndpoint.ResourceID); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "service_bus_topic_endpoint_id", id.Name, id.Scope, err)
return fmt.Errorf("setting `service_bus_topic_endpoint_id` for %s: %+v", *id, err)
}
if serviceBusTopicEndpoint.DeliveryAttributeMappings != nil {
if err := d.Set("delivery_property", flattenDeliveryProperties(d, serviceBusTopicEndpoint.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "service_bus_topic_endpoint_id", id.Name, id.Scope, err)
return fmt.Errorf("setting `delivery_property` for %s: %+v", *id, err)
}
}
}
if v, ok := destination.AsStorageQueueEventSubscriptionDestination(); ok {
if err := d.Set("storage_queue_endpoint", flattenEventGridEventSubscriptionStorageQueueEndpoint(v)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "storage_queue_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `storage_queue_endpoint` for %s: %+v", *id, err)
}
}
if v, ok := destination.AsWebHookEventSubscriptionDestination(); ok {
fullURL, err := client.GetFullURL(ctx, id.Scope, id.Name)
if err != nil {
return fmt.Errorf("making Read request on EventGrid Event Subscription full URL '%s': %+v", id.Name, err)
return fmt.Errorf("retrieving Full WebHook URL for %s: %+v", *id, err)
}
if err := d.Set("webhook_endpoint", flattenEventGridEventSubscriptionWebhookEndpoint(v, &fullURL)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid Event Subscription %q (Scope %q): %s", "webhook_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `webhook_endpoint` for %s: %+v", *id, err)
}

if v.DeliveryAttributeMappings != nil {
if err := d.Set("delivery_property", flattenDeliveryProperties(d, v.DeliveryAttributeMappings)); err != nil {
return fmt.Errorf("setting `%q` for EventGrid delivery properties %q (Scope %q): %s", "webhook_endpoint", id.Name, id.Scope, err)
return fmt.Errorf("setting `delivery_property` for %s: %+v", *id, err)
}
}
}
Expand All @@ -389,13 +375,13 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
deadLetterIdentityFlattened = flattenEventGridEventSubscriptionIdentity(deadLetterIdentity.Identity)
}
if err := d.Set("dead_letter_identity", deadLetterIdentityFlattened); err != nil {
return fmt.Errorf("setting `dead_letter_identity` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `dead_letter_identity` for %s: %+v", *id, err)
}

if deadLetterDestination != nil {
if storageBlobDeadLetterDestination, ok := deadLetterDestination.AsStorageBlobDeadLetterDestination(); ok {
if err := d.Set("storage_blob_dead_letter_destination", flattenEventGridEventSubscriptionStorageBlobDeadLetterDestination(storageBlobDeadLetterDestination)); err != nil {
return fmt.Errorf("Error setting `storage_blob_dead_letter_destination` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("Error setting `storage_blob_dead_letter_destination` for %s: %+v", *id, err)
}
}
}
Expand All @@ -404,29 +390,29 @@ func resourceEventGridEventSubscriptionRead(d *pluginsdk.ResourceData, meta inte
d.Set("included_event_types", filter.IncludedEventTypes)
d.Set("advanced_filtering_on_arrays_enabled", filter.EnableAdvancedFilteringOnArrays)
if err := d.Set("subject_filter", flattenEventGridEventSubscriptionSubjectFilter(filter)); err != nil {
return fmt.Errorf("setting `subject_filter` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `subject_filter` for %s: %+v", *id, err)
}
if err := d.Set("advanced_filter", flattenEventGridEventSubscriptionAdvancedFilter(filter)); err != nil {
return fmt.Errorf("setting `advanced_filter` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `advanced_filter` for %s: %+v", *id, err)
}
}

if props.DeadLetterDestination != nil {
if storageBlobDeadLetterDestination, ok := props.DeadLetterDestination.AsStorageBlobDeadLetterDestination(); ok {
if err := d.Set("storage_blob_dead_letter_destination", flattenEventGridEventSubscriptionStorageBlobDeadLetterDestination(storageBlobDeadLetterDestination)); err != nil {
return fmt.Errorf("setting `storage_blob_dead_letter_destination` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `storage_blob_dead_letter_destination` for %s: %+v", *id, err)
}
}
}

if retryPolicy := props.RetryPolicy; retryPolicy != nil {
if err := d.Set("retry_policy", flattenEventGridEventSubscriptionRetryPolicy(retryPolicy)); err != nil {
return fmt.Errorf("setting `retry_policy` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `retry_policy` for %s: %+v", *id, err)
}
}

if err := d.Set("labels", props.Labels); err != nil {
return fmt.Errorf("setting `labels` for EventGrid Event Subscription %q (Scope %q): %s", id.Name, id.Scope, err)
return fmt.Errorf("setting `labels` for %s: %+v", *id, err)
}
}

Expand Down
25 changes: 25 additions & 0 deletions internal/services/eventgrid/parse/event_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,39 @@ import (
"fmt"
"strings"

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

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
)

var _ resourceids.Id = EventSubscriptionId{}

type EventSubscriptionId struct {
Scope string
Name string
}

func NewEventSubscriptionID(scope string, name string) EventSubscriptionId {
return EventSubscriptionId{
Scope: scope,
Name: name,
}
}

func (id EventSubscriptionId) ID() string {
fmtStr := "%s/providers/Microsoft.EventGrid/eventSubscriptions/%s"
return fmt.Sprintf(fmtStr, id.Scope, id.Name)
}

func (id EventSubscriptionId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Scope %q", id.Scope),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Event Subscription", segmentsStr)
}

func EventSubscriptionID(input string) (*EventSubscriptionId, error) {
_, err := azure.ParseAzureResourceID(input)
if err != nil {
Expand Down
34 changes: 9 additions & 25 deletions internal/services/lighthouse/lighthouse_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Azure/azure-sdk-for-go/services/managedservices/mgmt/2019-06-01/managedservices"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse/validate"
Expand Down Expand Up @@ -77,40 +76,25 @@ func resourceLighthouseAssignmentCreate(d *pluginsdk.ResourceData, meta interfac
lighthouseAssignmentName = uuid
}

scope := d.Get("scope").(string)

existing, err := client.Get(ctx, scope, lighthouseAssignmentName, utils.Bool(false))
id := parse.NewLighthouseAssignmentID(d.Get("scope").(string), lighthouseAssignmentName)
existing, err := client.Get(ctx, id.Scope, id.Name, utils.Bool(false))
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Lighthouse Assignment %q (Scope %q): %+v", lighthouseAssignmentName, scope, err)
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_lighthouse_assignment", *existing.ID)
}

parameters := managedservices.RegistrationAssignment{
Properties: &managedservices.RegistrationAssignmentProperties{
RegistrationDefinitionID: utils.String(d.Get("lighthouse_definition_id").(string)),
},
}

if _, err := client.CreateOrUpdate(ctx, scope, lighthouseAssignmentName, parameters); err != nil {
return fmt.Errorf("creating Lighthouse Assignment %q (Scope %q): %+v", lighthouseAssignmentName, scope, err)
if _, err := client.CreateOrUpdate(ctx, id.Scope, id.Name, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

read, err := client.Get(ctx, scope, lighthouseAssignmentName, utils.Bool(false))
if err != nil {
return fmt.Errorf("retrieving Lighthouse Assessment %q (Scope %q): %+v", lighthouseAssignmentName, scope, err)
}

if read.ID == nil || *read.ID == "" {
return fmt.Errorf("ID was nil or empty for Lighthouse Assignment %q ID (scope %q) ID", lighthouseAssignmentName, scope)
}

d.SetId(*read.ID)

d.SetId(id.ID())
return resourceLighthouseAssignmentRead(d, meta)
}

Expand All @@ -127,15 +111,15 @@ func resourceLighthouseAssignmentRead(d *pluginsdk.ResourceData, meta interface{
resp, err := client.Get(ctx, id.Scope, id.Name, utils.Bool(false))
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] Lighthouse Assignment %q was not found (Scope %q)", id.Name, id.Scope)
log.Printf("[WARN] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("making Read request on Lighthouse Assignment %q (Scope %q): %+v", id.Name, id.Scope, err)
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", resp.Name)
d.Set("name", id.Name)
d.Set("scope", id.Scope)

if props := resp.Properties; props != nil {
Expand Down
Loading