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_topic #260

Merged
merged 7 commits into from
Aug 21, 2017
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
8 changes: 8 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/cosmos-db"
"github.com/Azure/azure-sdk-for-go/arm/disk"
"github.com/Azure/azure-sdk-for-go/arm/dns"
"github.com/Azure/azure-sdk-for-go/arm/eventgrid"
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
"github.com/Azure/azure-sdk-for-go/arm/graphrbac"
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
Expand Down Expand Up @@ -83,6 +84,7 @@ type ArmClient struct {
containerRegistryClient containerregistry.RegistriesClient
containerServicesClient containerservice.ContainerServicesClient

eventGridTopicsClient eventgrid.TopicsClient
eventHubClient eventhub.EventHubsClient
eventHubConsumerGroupClient eventhub.ConsumerGroupsClient
eventHubNamespacesClient eventhub.NamespacesClient
Expand Down Expand Up @@ -293,6 +295,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
img.Sender = autorest.CreateSender(withRequestLogging())
client.imageClient = img

egtc := eventgrid.NewTopicsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&egtc.Client)
egtc.Authorizer = auth
egtc.Sender = autorest.CreateSender(withRequestLogging())
client.eventGridTopicsClient = egtc

ehc := eventhub.NewEventHubsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&ehc.Client)
ehc.Authorizer = auth
Expand Down
50 changes: 50 additions & 0 deletions azurerm/import_arm_eventgrid_topic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMEventGridTopic_importBasic(t *testing.T) {
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventGridTopicDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMEventGridTopic_basic(ri),
},

{
ResourceName: "azurerm_eventgrid_topic.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMEventGridTopic_importBasicWithTags(t *testing.T) {
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventGridTopicDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMEventGridTopic_basicWithTags(ri),
},

{
ResourceName: "azurerm_eventgrid_topic.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
7 changes: 5 additions & 2 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ func Provider() terraform.ResourceProvider {
"azurerm_dns_txt_record": resourceArmDnsTxtRecord(),
"azurerm_dns_zone": resourceArmDnsZone(),

"azurerm_eventgrid_topic": resourceArmEventGridTopic(),
"azurerm_eventhub": resourceArmEventHub(),
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
"azurerm_express_route_circuit": resourceArmExpressRouteCircuit(),
"azurerm_image": resourceArmImage(),
"azurerm_key_vault": resourceArmKeyVault(),

"azurerm_image": resourceArmImage(),
"azurerm_key_vault": resourceArmKeyVault(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is there any particular reason why these 2 resources deserve to be separated from others, and not azurerm_express_route_circuit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given a lot of the resources share the same initial character, the resources are vaguely grouped by letter so it was easier to skim-read, but happy to move them up?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, that's ok - I was just curious what's the logic behind it 🙂


"azurerm_lb": resourceArmLoadBalancer(),
"azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
Expand Down Expand Up @@ -260,6 +262,7 @@ func registerAzureResourceProvidersWithSubscription(providerList []resources.Pro
"Microsoft.ContainerRegistry": struct{}{},
"Microsoft.ContainerService": struct{}{},
"Microsoft.DocumentDB": struct{}{},
"Microsoft.EventGrid": struct{}{},
"Microsoft.EventHub": struct{}{},
"Microsoft.KeyVault": struct{}{},
"Microsoft.Insights": struct{}{},
Expand Down
154 changes: 154 additions & 0 deletions azurerm/resource_arm_eventgrid_topic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/arm/eventgrid"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmEventGridTopic() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventGridTopicCreateUpdate,
Read: resourceArmEventGridTopicRead,
Update: resourceArmEventGridTopicCreateUpdate,
Delete: resourceArmEventGridTopicDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"location": locationSchema(),

"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"tags": tagsSchema(),

"endpoint": {
Type: schema.TypeString,
Computed: true,
},

"primary_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}

func resourceArmEventGridTopicCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).eventGridTopicsClient

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

properties := eventgrid.Topic{
Location: &location,
TopicProperties: &eventgrid.TopicProperties{},
Tags: expandTags(tags),
}

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

_, createErr := client.CreateOrUpdate(resourceGroup, name, properties, make(chan struct{}))
err := <-createErr
if err != nil {
return err
}

read, err := client.Get(resourceGroup, name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we cannot use name as ID and avoid making that extra API call? Otherwise this is susceptible to dataloss (and dangling resource) in case the second call fails for any reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency with the Azure API's (and existing resources) - which use ID's in the format:

/subscriptions/foo/resourceGroups/bar/provider/Microsoft.Blah/eventGrid/name

There's an issue open in the SDK to expose a method of obtaining the Resource ID without making this Get Request; once that's done we can switch this out (but that's a future enhancement).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that's sad 😢

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

d.SetId(*read.ID)

return resourceArmEventGridTopicRead(d, meta)
}

func resourceArmEventGridTopicRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).eventGridTopicsClient

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

resp, err := client.Get(resourceGroup, name)
if err != nil {
if responseWasNotFound(resp.Response) {
log.Printf("[WARN] EventGrid Topic '%s' was not found (resource group '%s')", name, resourceGroup)
d.SetId("")
return nil
}

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

keys, err := client.ListSharedAccessKeys(resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Shared Access Keys for EventGrid Topic '%s': %+v", name, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))

if props := resp.TopicProperties; props != nil {
d.Set("endpoint", props.Endpoint)
}

d.Set("primary_access_key", keys.Key1)
d.Set("secondary_access_key", keys.Key2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those access keys sensitive in any way - i.e. would it cause problem if they were exposed in the plan/apply output? If yes, then they should be also marked as Sensitive in the schema.


flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmEventGridTopicDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).eventGridTopicsClient

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

deleteResp, deleteErr := client.Delete(resGroup, name, make(chan struct{}))
resp := <-deleteResp
err = <-deleteErr

if responseWasNotFound(resp) {
return nil
}

return err
}
Loading