Skip to content

Commit

Permalink
provider/azurerm: EventHubs (#9889)
Browse files Browse the repository at this point in the history
* Implemented EventHubs

* Missing the sidebar link

* Fixing the type

* Fixing the docs for Namespace

* Removing premium tests

* Checking the correct status code on delete

* Added a test case for the import

* Documentation for importing

* Fixing a typo
  • Loading branch information
tombuildsstuff authored and stack72 committed Nov 7, 2016
1 parent a0c5d42 commit b15b7e1
Show file tree
Hide file tree
Showing 9 changed files with 578 additions and 6 deletions.
7 changes: 7 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type ArmClient struct {
cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient

eventHubClient eventhub.EventHubsClient
eventHubNamespacesClient eventhub.NamespacesClient

providers resources.ProvidersClient
Expand Down Expand Up @@ -213,6 +214,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
agc.Sender = autorest.CreateSender(withRequestLogging())
client.appGatewayClient = agc

ehc := eventhub.NewEventHubsClient(c.SubscriptionID)
setUserAgent(&ehc.Client)
ehc.Authorizer = spt
ehc.Sender = autorest.CreateSender(withRequestLogging())
client.eventHubClient = ehc

ehnc := eventhub.NewNamespacesClient(c.SubscriptionID)
setUserAgent(&ehnc.Client)
ehnc.Authorizer = spt
Expand Down
34 changes: 34 additions & 0 deletions builtin/providers/azurerm/import_arm_eventhub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package azurerm

import (
"testing"

"fmt"

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

func TestAccAzureRMEventHub_importBasic(t *testing.T) {
resourceName := "azurerm_eventhub.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHub_basic, ri, ri, ri)

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

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),

"azurerm_eventhub": resourceArmEventHub(),
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),

"azurerm_lb": resourceArmLoadBalancer(),
Expand Down
177 changes: 177 additions & 0 deletions builtin/providers/azurerm/resource_arm_eventhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package azurerm

import (
"fmt"
"log"

"net/http"

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

func resourceArmEventHub() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventHubCreate,
Read: resourceArmEventHubRead,
Update: resourceArmEventHubCreate,
Delete: resourceArmEventHubDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

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

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

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

"partition_count": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateEventHubPartitionCount,
},

"message_retention": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateEventHubMessageRetentionCount,
},

"partition_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
},
}
}

func resourceArmEventHubCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
eventhubClient := client.eventHubClient
log.Printf("[INFO] preparing arguments for Azure ARM EventHub creation.")

name := d.Get("name").(string)
namespaceName := d.Get("namespace_name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
partitionCount := int64(d.Get("partition_count").(int))
messageRetention := int64(d.Get("message_retention").(int))

parameters := eventhub.CreateOrUpdateParameters{
Location: &location,
Properties: &eventhub.Properties{
PartitionCount: &partitionCount,
MessageRetentionInDays: &messageRetention,
},
}

_, err := eventhubClient.CreateOrUpdate(resGroup, namespaceName, name, parameters)
if err != nil {
return err
}

read, err := eventhubClient.Get(resGroup, namespaceName, name)
if err != nil {
return err
}

if read.ID == nil {
return fmt.Errorf("Cannot read EventHub %s (resource group %s) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmEventHubRead(d, meta)
}

func resourceArmEventHubRead(d *schema.ResourceData, meta interface{}) error {
eventhubClient := meta.(*ArmClient).eventHubClient

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

resp, err := eventhubClient.Get(resGroup, namespaceName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure EventHub %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("namespace_name", namespaceName)
d.Set("resource_group_name", resGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))

d.Set("partition_count", resp.Properties.PartitionCount)
d.Set("message_retention", resp.Properties.MessageRetentionInDays)
d.Set("partition_ids", resp.Properties.PartitionIds)

return nil
}

func resourceArmEventHubDelete(d *schema.ResourceData, meta interface{}) error {
eventhubClient := meta.(*ArmClient).eventHubClient

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

resp, err := eventhubClient.Delete(resGroup, namespaceName, name)

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub'%s': %s", name, err)
}

return nil
}

func validateEventHubPartitionCount(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

if !(32 >= value && value >= 2) {
errors = append(errors, fmt.Errorf("EventHub Partition Count has to be between 2 and 32"))
}
return
}

func validateEventHubMessageRetentionCount(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

if !(7 >= value && value >= 1) {
errors = append(errors, fmt.Errorf("EventHub Retention Count has to be between 1 and 7"))
}
return
}
40 changes: 37 additions & 3 deletions builtin/providers/azurerm/resource_arm_eventhub_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,27 @@ func TestAccAzureRMEventHubNamespace_basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
),
},
},
})
}

func TestAccAzureRMEventHubNamespace_standard(t *testing.T) {

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMEventHubNamespace_standard, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
Expand All @@ -108,7 +128,7 @@ func TestAccAzureRMEventHubNamespace_readDefaultKeys(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMEventHubNamespaceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMEventHubNamespaceExists("azurerm_eventhub_namespace.test"),
Expand Down Expand Up @@ -187,8 +207,22 @@ resource "azurerm_resource_group" "test" {
}
resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = "West US"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Basic"
}
`

var testAccAzureRMEventHubNamespace_standard = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
capacity = "2"
}
`
Loading

0 comments on commit b15b7e1

Please sign in to comment.