-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
a8321cb
283d288
20548fe
deeb083
a64e2e7
cb5b911
41a22cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}, | ||
}, | ||
}) | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason we cannot use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 | ||
} |
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🙂