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

New Resource: azurerm_eventgrid_system_topic #8735

Merged
merged 14 commits into from
Oct 15, 2020
5 changes: 5 additions & 0 deletions azurerm/internal/services/eventgrid/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Client struct {
DomainTopicsClient *eventgrid.DomainTopicsClient
EventSubscriptionsClient *eventgrid.EventSubscriptionsClient
TopicsClient *eventgrid.TopicsClient
SystemTopicsClient *eventgrid.SystemTopicsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -25,10 +26,14 @@ func NewClient(o *common.ClientOptions) *Client {
TopicsClient := eventgrid.NewTopicsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&TopicsClient.Client, o.ResourceManagerAuthorizer)

SystemTopicsClient := eventgrid.NewSystemTopicsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&SystemTopicsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
DomainsClient: &DomainsClient,
EventSubscriptionsClient: &EventSubscriptionsClient,
DomainTopicsClient: &DomainTopicsClient,
TopicsClient: &TopicsClient,
SystemTopicsClient: &SystemTopicsClient,
}
}
217 changes: 217 additions & 0 deletions azurerm/internal/services/eventgrid/eventgrid_system_topic_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package eventgrid

import (
"fmt"
"log"
"time"

"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/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"
)

func resourceArmEventGridSystemTopic() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventGridSystemTopicCreateUpdate,
Read: resourceArmEventGridSystemTopicRead,
Update: resourceArmEventGridSystemTopicCreateUpdate,
Delete: resourceArmEventGridSystemTopicDelete,

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),
},

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

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

"location": azure.SchemaLocation(),

"resource_group_name": azure.SchemaResourceGroupName(),

"source_resource_id": {
jrauschenbusch marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"topic_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Microsoft.Eventhub.Namespaces",
"Microsoft.Storage.StorageAccounts",
"Microsoft.Resources.Subscriptions",
"Microsoft.Resources.ResourceGroups",
"Microsoft.Devices.IoTHubs",
"Microsoft.EventGrid.Topics",
"Microsoft.ServiceBus.Namespaces",
"Microsoft.ContainerRegistry.Registries",
"Microsoft.Media.MediaServices",
"Microsoft.Maps.Accounts",
"Microsoft.EventGrid.Domains",
"Microsoft.AppConfiguration.ConfigurationStores",
"Microsoft.KeyVault.vaults",
"Microsoft.Web.Sites",
"Microsoft.Web.ServerFarms",
"Microsoft.SignalRService.SignalR",
"Microsoft.MachineLearningServices.Workspaces",
"Microsoft.Communication.CommunicationServices",
jrauschenbusch marked this conversation as resolved.
Show resolved Hide resolved
}, false),
},

"metric_resource_id": {
jrauschenbusch marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
},

"tags": tags.Schema(),
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
source := d.Get("source_resource_id").(string)
topicType := d.Get("topic_type").(string)

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

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

location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

systemTopic := eventgrid.SystemTopic{
Location: &location,
SystemTopicProperties: &eventgrid.SystemTopicProperties{
Source: &source,
TopicType: &topicType,
},
Tags: tags.Expand(t),
}

log.Printf("[INFO] preparing arguments for AzureRM EventGrid System Topic creation with Properties: %+v.", systemTopic)

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, systemTopic)
if err != nil {
return err
}

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

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read EventGrid System Topic %s (resource group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmEventGridSystemTopicRead(d, meta)
}

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

id, err := parse.EventGridSystemTopicID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] EventGrid System Topic '%s' was not found (resource group '%s')", id.Name, id.ResourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on EventGrid System Topic '%s': %+v", id.Name, err)
}

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

if props := resp.SystemTopicProperties; props != nil {
d.Set("source_resource_id", props.Source)
d.Set("topic_type", props.TopicType)
d.Set("metric_resource_id", props.MetricResourceID)
}

return tags.FlattenAndSet(d, resp.Tags)
}

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

id, err := parse.EventGridSystemTopicID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error deleting EventGrid System Topic %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 EventGrid System Topic %q: %+v", id.Name, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parse

import (
"fmt"

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

type EventGridSystemTopicId struct {
ResourceGroup string
Name string
}

func EventGridSystemTopicID(input string) (*EventGridSystemTopicId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse EventGrid System Topic ID %q: %+v", input, err)
}

topic := EventGridSystemTopicId{
ResourceGroup: id.ResourceGroup,
}

if topic.Name, err = id.PopSegment("systemTopics"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &topic, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package parse

import (
"testing"
)

func TestEventGridSystemTopicId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *EventGridSystemTopicId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No System Topic",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/providers/Microsoft.EventGrid",
Expected: nil,
},
{
Name: "EventGrid System Topic ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.EventGrid/systemTopics/systemTopic1",
Expected: &EventGridSystemTopicId{
Name: "systemTopic1",
ResourceGroup: "resGroup1",
},
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := EventGridSystemTopicID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/eventgrid/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_eventgrid_domain_topic": resourceArmEventGridDomainTopic(),
"azurerm_eventgrid_event_subscription": resourceArmEventGridEventSubscription(),
"azurerm_eventgrid_topic": resourceArmEventGridTopic(),
"azurerm_eventgrid_system_topic": resourceArmEventGridSystemTopic(),
}
}
Loading