Skip to content

Commit

Permalink
Upgrade EventGrid SDK to 2020-04-01-preview (#6837)
Browse files Browse the repository at this point in the history
Upgrade EventGrid SDK to 2020-04-01-preview from 2018-09-15-preview to address @katbyte comments in PR #5948.
  • Loading branch information
jrauschenbusch committed May 11, 2020
1 parent d92443c commit 0a635d7
Show file tree
Hide file tree
Showing 35 changed files with 14,805 additions and 4,207 deletions.
2 changes: 1 addition & 1 deletion azurerm/internal/services/eventgrid/client/client.go
@@ -1,7 +1,7 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2018-09-15-preview/eventgrid"
"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2020-04-01-preview/eventgrid"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
48 changes: 24 additions & 24 deletions azurerm/internal/services/eventgrid/eventgrid_domain_resource.go
Expand Up @@ -5,15 +5,17 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2018-09-15-preview/eventgrid"
"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2020-04-01-preview/eventgrid"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/eventgrid/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -24,9 +26,6 @@ func resourceArmEventGridDomain() *schema.Resource {
Read: resourceArmEventGridDomainRead,
Update: resourceArmEventGridDomainCreateUpdate,
Delete: resourceArmEventGridDomainDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Expand All @@ -35,6 +34,11 @@ func resourceArmEventGridDomain() *schema.Resource {
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.EventGridDomainID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -54,7 +58,7 @@ func resourceArmEventGridDomain() *schema.Resource {
Default: string(eventgrid.InputSchemaEventGridSchema),
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(eventgrid.InputSchemaCloudEventV01Schema),
string(eventgrid.InputSchemaCloudEventSchemaV10),
string(eventgrid.InputSchemaCustomEventSchema),
string(eventgrid.InputSchemaEventGridSchema),
}, false),
Expand Down Expand Up @@ -211,26 +215,24 @@ func resourceArmEventGridDomainRead(d *schema.ResourceData, meta interface{}) er
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.EventGridDomainID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["domains"]

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] EventGrid Domain %q was not found (Resource Group %q)", name, resourceGroup)
log.Printf("[WARN] EventGrid Domain %q was not found (Resource Group %q)", id.Name, id.ResourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on EventGrid Domain %q: %+v", name, err)
return fmt.Errorf("Error making Read request on EventGrid Domain %q: %+v", id.Name, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand All @@ -242,24 +244,24 @@ func resourceArmEventGridDomainRead(d *schema.ResourceData, meta interface{}) er

inputMappingFields, err := flattenAzureRmEventgridDomainInputMapping(props.InputSchemaMapping)
if err != nil {
return fmt.Errorf("Unable to flatten `input_schema_mapping_fields` for EventGrid Domain %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("Unable to flatten `input_schema_mapping_fields` for EventGrid Domain %q (Resource Group %q): %s", id.Name, id.ResourceGroup, err)
}
if err := d.Set("input_mapping_fields", inputMappingFields); err != nil {
return fmt.Errorf("Error setting `input_schema_mapping_fields` for EventGrid Domain %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("Error setting `input_schema_mapping_fields` for EventGrid Domain %q (Resource Group %q): %s", id.Name, id.ResourceGroup, err)
}

inputMappingDefaultValues, err := flattenAzureRmEventgridDomainInputMappingDefaultValues(props.InputSchemaMapping)
if err != nil {
return fmt.Errorf("Unable to flatten `input_schema_mapping_default_values` for EventGrid Domain %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("Unable to flatten `input_schema_mapping_default_values` for EventGrid Domain %q (Resource Group %q): %s", id.Name, id.ResourceGroup, err)
}
if err := d.Set("input_mapping_default_values", inputMappingDefaultValues); err != nil {
return fmt.Errorf("Error setting `input_schema_mapping_fields` for EventGrid Domain %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("Error setting `input_schema_mapping_fields` for EventGrid Domain %q (Resource Group %q): %s", id.Name, id.ResourceGroup, err)
}
}

keys, err := client.ListSharedAccessKeys(ctx, resourceGroup, name)
keys, err := client.ListSharedAccessKeys(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("Error retrieving Shared Access Keys for EventGrid Domain %q: %+v", name, err)
return fmt.Errorf("Error retrieving Shared Access Keys for EventGrid Domain %q: %+v", id.Name, err)
}
d.Set("primary_access_key", keys.Key1)
d.Set("secondary_access_key", keys.Key2)
Expand All @@ -272,26 +274,24 @@ func resourceArmEventGridDomainDelete(d *schema.ResourceData, meta interface{})
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.EventGridDomainID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["domains"]

future, err := client.Delete(ctx, resGroup, name)
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error deleting Event Grid Domain %q: %+v", name, err)
return fmt.Errorf("Error deleting Event Grid Domain %q: %+v", id.Name, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error deleting Event Grid Domain %q: %+v", name, err)
return fmt.Errorf("Error deleting Event Grid Domain %q: %+v", id.Name, err)
}

return nil
Expand Down
Expand Up @@ -3,30 +3,32 @@ package eventgrid
import (
"fmt"
"log"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2018-09-15-preview/eventgrid"
"github.com/Azure/azure-sdk-for-go/services/preview/eventgrid/mgmt/2020-04-01-preview/eventgrid"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/eventgrid/parse"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func getEnpointTypes() []string {
return []string{"webhook_endpoint", "storage_queue_endpoint", "eventhub_endpoint", "hybrid_connection_endpoint"}
}

func resourceArmEventGridEventSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventGridEventSubscriptionCreateUpdate,
Read: resourceArmEventGridEventSubscriptionRead,
Update: resourceArmEventGridEventSubscriptionCreateUpdate,
Delete: resourceArmEventGridEventSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Expand All @@ -35,6 +37,11 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.EventGridEventSubscriptionID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -56,9 +63,9 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
ForceNew: true,
Default: string(eventgrid.EventGridSchema),
ValidateFunc: validation.StringInSlice([]string{
string(eventgrid.CloudEventV01Schema),
string(eventgrid.CustomInputSchema),
string(eventgrid.EventGridSchema),
string(eventgrid.CloudEventSchemaV10),
string(eventgrid.CustomInputSchema),
}, false),
},

Expand All @@ -72,7 +79,7 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"eventhub_endpoint", "hybrid_connection_endpoint", "webhook_endpoint"},
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "storage_queue_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"storage_account_id": {
Expand All @@ -93,7 +100,7 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"storage_queue_endpoint", "hybrid_connection_endpoint", "webhook_endpoint"},
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "eventhub_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"eventhub_id": {
Expand All @@ -109,7 +116,7 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"storage_queue_endpoint", "eventhub_endpoint", "webhook_endpoint"},
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "hybrid_connection_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hybrid_connection_id": {
Expand All @@ -125,7 +132,7 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"storage_queue_endpoint", "eventhub_endpoint", "hybrid_connection_endpoint"},
ConflictsWith: utils.RemoveFromStringArray(getEnpointTypes(), "webhook_endpoint"),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Expand All @@ -142,7 +149,8 @@ func resourceArmEventGridEventSubscription() *schema.Resource {
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},

Expand Down Expand Up @@ -243,22 +251,20 @@ func resourceArmEventGridEventSubscriptionCreateUpdate(d *schema.ResourceData, m

destination := expandEventGridEventSubscriptionDestination(d)
if destination == nil {
return fmt.Errorf("One of `webhook_endpoint`, eventhub_endpoint` `hybrid_connection_endpoint` or `storage_queue_endpoint` must be specificed to create an EventGrid Event Subscription")
return fmt.Errorf("One of the following endpoint types must be specificed to create an EventGrid Event Subscription: %q", getEnpointTypes())
}

filter := expandEventGridEventSubscriptionFilter(d)

eventSubscriptionProperties := eventgrid.EventSubscriptionProperties{
Destination: destination,
Filter: expandEventGridEventSubscriptionFilter(d),
Filter: filter,
DeadLetterDestination: expandEventGridEventSubscriptionStorageBlobDeadLetterDestination(d),
RetryPolicy: expandEventGridEventSubscriptionRetryPolicy(d),
Labels: utils.ExpandStringSlice(d.Get("labels").([]interface{})),
EventDeliverySchema: eventgrid.EventDeliverySchema(d.Get("event_delivery_schema").(string)),
}

if v, ok := d.GetOk("topic_name"); ok {
eventSubscriptionProperties.Topic = utils.String(v.(string))
}

eventSubscription := eventgrid.EventSubscription{
EventSubscriptionProperties: &eventSubscriptionProperties,
}
Expand Down Expand Up @@ -292,7 +298,7 @@ func resourceArmEventGridEventSubscriptionRead(d *schema.ResourceData, meta inte
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parseAzureEventGridEventSubscriptionID(d.Id())
id, err := parse.EventGridEventSubscriptionID(d.Id())
if err != nil {
return err
}
Expand Down Expand Up @@ -377,7 +383,7 @@ func resourceArmEventGridEventSubscriptionDelete(d *schema.ResourceData, meta in
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parseAzureEventGridEventSubscriptionID(d.Id())
id, err := parse.EventGridEventSubscriptionID(d.Id())
if err != nil {
return err
}
Expand All @@ -400,26 +406,6 @@ func resourceArmEventGridEventSubscriptionDelete(d *schema.ResourceData, meta in
return nil
}

type AzureEventGridEventSubscriptionID struct {
Scope string
Name string
}

func parseAzureEventGridEventSubscriptionID(id string) (*AzureEventGridEventSubscriptionID, error) {
segments := strings.Split(id, "/providers/Microsoft.EventGrid/eventSubscriptions/")
if len(segments) != 2 {
return nil, fmt.Errorf("Expected ID to be in the format `{scope}/providers/Microsoft.EventGrid/eventSubscriptions/{name} - got %d segments", len(segments))
}

scope := segments[0]
name := segments[1]
eventSubscriptionID := AzureEventGridEventSubscriptionID{
Scope: scope,
Name: name,
}
return &eventSubscriptionID, nil
}

func expandEventGridEventSubscriptionDestination(d *schema.ResourceData) eventgrid.BasicEventSubscriptionDestination {
if _, ok := d.GetOk("storage_queue_endpoint"); ok {
return expandEventGridEventSubscriptionStorageQueueEndpoint(d)
Expand Down

0 comments on commit 0a635d7

Please sign in to comment.