Skip to content

Commit

Permalink
Update: r/eventgrid_*
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauschenbusch committed May 6, 2020
1 parent 2c60f40 commit f305c0b
Show file tree
Hide file tree
Showing 26 changed files with 3,891 additions and 446 deletions.
7 changes: 6 additions & 1 deletion azurerm/internal/services/eventgrid/client/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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-01-01-preview/eventgrid"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
DomainsClient *eventgrid.DomainsClient
DomainTopicsClient *eventgrid.DomainTopicsClient
EventSubscriptionsClient *eventgrid.EventSubscriptionsClient
TopicsClient *eventgrid.TopicsClient
}
Expand All @@ -15,6 +16,9 @@ func NewClient(o *common.ClientOptions) *Client {
DomainsClient := eventgrid.NewDomainsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&DomainsClient.Client, o.ResourceManagerAuthorizer)

DomainTopicsClient := eventgrid.NewDomainTopicsClient(o.SubscriptionId)
o.ConfigureClient(&DomainTopicsClient.Client, o.ResourceManagerAuthorizer)

EventSubscriptionsClient := eventgrid.NewEventSubscriptionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&EventSubscriptionsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -23,6 +27,7 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
DomainsClient: &DomainsClient,
DomainTopicsClient: &DomainTopicsClient,
EventSubscriptionsClient: &EventSubscriptionsClient,
TopicsClient: &TopicsClient,
}
Expand Down
1 change: 1 addition & 0 deletions azurerm/internal/services/eventgrid/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_eventgrid_domain": resourceArmEventGridDomain(),
"azurerm_eventgrid_domain_topic": resourceArmEventGridDomainTopic(),
"azurerm_eventgrid_event_subscription": resourceArmEventGridEventSubscription(),
"azurerm_eventgrid_topic": resourceArmEventGridTopic(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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-01-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"
Expand Down Expand Up @@ -54,7 +54,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package eventgrid

import (
"fmt"
"log"
"time"

"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"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/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmEventGridDomainTopic() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventGridDomainTopicCreateUpdate,
Read: resourceArmEventGridDomainTopicRead,
Update: resourceArmEventGridDomainTopicCreateUpdate,
Delete: resourceArmEventGridDomainTopicDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"domain_name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": azure.SchemaResourceGroupName(),
},
}
}

func resourceArmEventGridDomainTopicCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).EventGrid.DomainTopicsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
domainName := d.Get("domain_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, domainName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing EventGrid Domain Topic %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

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

future, err := client.CreateOrUpdate(ctx, resourceGroup, domainName, name)
if err != nil {
return fmt.Errorf("Error creating/updating EventGrid Domain Topic %q (Resource Group %q): %s", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for EventGrid Domain Topic %q (Resource Group %q) to become available: %s", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, domainName, name)
if err != nil {
return fmt.Errorf("Error retrieving EventGrid Domain Topic %q (Resource Group %q): %s", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read EventGrid Domain Topic %q (resource group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmEventGridDomainTopicRead(d, meta)
}

func resourceArmEventGridDomainTopicRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).EventGrid.DomainTopicsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

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

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

d.Set("name", resp.Name)
d.Set("domain_name", domainName)
d.Set("resource_group_name", resourceGroup)

return nil
}

func resourceArmEventGridDomainTopicDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).EventGrid.DomainTopicsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

future, err := client.Delete(ctx, resGroup, domainName, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error deleting Event Grid Domain Topic %q: %+v", 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 Topic %q: %+v", name, err)
}

return nil
}
Loading

0 comments on commit f305c0b

Please sign in to comment.