-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from terraform-providers/eventgrid-topic
New Resource: `azurerm_eventgrid_topic`
- Loading branch information
Showing
17 changed files
with
2,814 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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) | ||
|
||
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 | ||
} |
Oops, something went wrong.