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 Data Source: azurerm_eventhub_namespace #673

Merged
merged 4 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
112 changes: 112 additions & 0 deletions azurerm/data_source_eventhub_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceEventHubNamespace() *schema.Resource {
return &schema.Resource{
Read: dataSourceEventHubNamespaceRead,

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

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"location": locationForDataSourceSchema(),

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

"capacity": {
Type: schema.TypeInt,
Computed: true,
},

"auto_inflate_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"maximum_throughput_units": {
Type: schema.TypeInt,
Computed: true,
},

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

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

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

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

"tags": tagsSchema(),
},
}
}

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

resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)

resp, err := client.Get(resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on EventHub Namespace %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("resource_group_name", resourceGroup)
d.Set("sku", string(resp.Sku.Name))
d.Set("capacity", resp.Sku.Capacity)

keys, err := client.ListKeys(resourceGroup, name, eventHubNamespaceDefaultAuthorizationRule)
if err != nil {
log.Printf("[ERROR] Unable to List default keys for EventHub Namespace %q (Resource Group %q): %+v", name, resourceGroup, err)
Copy link
Member

Choose a reason for hiding this comment

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

Is this worth error-ing out completely instead of just logging? The ERROR makes it seem like it's an issue instead of keys just not being there. Might be worth swapping to an INFO or WARN tag.

Copy link
Contributor Author

@tombuildsstuff tombuildsstuff Jan 9, 2018

Choose a reason for hiding this comment

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

good shout - I'll update this to a WARN 👍

erroring out completely doesn't make sense since it's valid that a user may have permissions to the EventHub but not the keys (AFAIK)

} else {
d.Set("default_primary_connection_string", keys.PrimaryConnectionString)
d.Set("default_secondary_connection_string", keys.SecondaryConnectionString)
d.Set("default_primary_key", keys.PrimaryKey)
d.Set("default_secondary_key", keys.SecondaryKey)
}

if props := resp.EHNamespaceProperties; props != nil {
d.Set("auto_inflate_enabled", props.IsAutoInflateEnabled)
d.Set("maximum_throughput_units", int(*props.MaximumThroughputUnits))
}

flattenAndSetTags(d, resp.Tags)

return nil
}
96 changes: 96 additions & 0 deletions azurerm/data_source_eventhub_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package azurerm

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMEventHubNamespace_basic(t *testing.T) {
dataSourceName := "data.azurerm_eventhub_namespace.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceEventHubNamespace_basic(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "sku", "Basic"),
),
},
},
})
}

func TestAccDataSourceAzureRMEventHubNamespace_complete(t *testing.T) {
dataSourceName := "data.azurerm_eventhub_namespace.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceEventHubNamespace_complete(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "sku", "Standard"),
resource.TestCheckResourceAttr(dataSourceName, "capacity", "2"),
resource.TestCheckResourceAttr(dataSourceName, "auto_inflate_enabled", "true"),
resource.TestCheckResourceAttr(dataSourceName, "maximum_throughput_units", "20"),
),
},
},
})
}

func testAccDataSourceEventHubNamespace_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Basic"
}

data "azurerm_eventhub_namespace" "test" {
name = "${azurerm_eventhub_namespace.test.name}"
resource_group_name = "${azurerm_eventhub_namespace.test.resource_group_name}"
}
`, rInt, location, rInt)
}

func testAccDataSourceEventHubNamespace_complete(rInt int, location string) string {
return fmt.Sprintf(`

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

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"
auto_inflate_enabled = true
maximum_throughput_units = 20
}

data "azurerm_eventhub_namespace" "test" {
name = "${azurerm_eventhub_namespace.test.name}"
resource_group_name = "${azurerm_eventhub_namespace.test.resource_group_name}"
}
`, rInt, location, rInt)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
Expand Down
59 changes: 18 additions & 41 deletions azurerm/resource_arm_eventhub_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package azurerm
import (
"fmt"
"log"
"strings"

"net/http"

"github.com/Azure/azure-sdk-for-go/arm/eventhub"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -39,17 +36,20 @@ func resourceArmEventHubNamespace() *schema.Resource {
"resource_group_name": resourceGroupNameSchema(),

"sku": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateEventHubNamespaceSku,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(eventhub.Basic),
string(eventhub.Standard),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"capacity": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validateEventHubNamespaceCapacity,
ValidateFunc: validation.IntBetween(1, 20),
},

"auto_inflate_enabled": {
Expand Down Expand Up @@ -93,7 +93,7 @@ func resourceArmEventHubNamespace() *schema.Resource {
func resourceArmEventHubNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
namespaceClient := client.eventHubNamespacesClient
log.Printf("[INFO] preparing arguments for Azure ARM EventHub Namespace creation.")
log.Printf("[INFO] preparing arguments for AzureRM EventHub Namespace creation.")

name := d.Get("name").(string)
location := d.Get("location").(string)
Expand Down Expand Up @@ -122,8 +122,8 @@ func resourceArmEventHubNamespaceCreate(d *schema.ResourceData, meta interface{}
parameters.EHNamespaceProperties.MaximumThroughputUnits = utils.Int32(int32(maximumThroughputUnits))
}

_, error := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{}))
err := <-error
_, createErr := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{}))
err := <-createErr
if err != nil {
return err
}
Expand All @@ -134,7 +134,7 @@ func resourceArmEventHubNamespaceCreate(d *schema.ResourceData, meta interface{}
}

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

d.SetId(*read.ID)
Expand All @@ -158,7 +158,7 @@ func resourceArmEventHubNamespaceRead(d *schema.ResourceData, meta interface{})
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure EventHub Namespace %s: %+v", name, err)
return fmt.Errorf("Error making Read request on EventHub Namespace %q: %+v", name, err)
}

d.Set("name", resp.Name)
Expand All @@ -169,7 +169,7 @@ func resourceArmEventHubNamespaceRead(d *schema.ResourceData, meta interface{})

keys, err := namespaceClient.ListKeys(resGroup, name, eventHubNamespaceDefaultAuthorizationRule)
if err != nil {
log.Printf("[ERROR] Unable to List default keys for Namespace %s: %+v", name, err)
log.Printf("[ERROR] Unable to List default keys for EventHub Namespace %q: %+v", name, err)
} else {
d.Set("default_primary_connection_string", keys.PrimaryConnectionString)
d.Set("default_secondary_connection_string", keys.SecondaryConnectionString)
Expand Down Expand Up @@ -201,36 +201,13 @@ func resourceArmEventHubNamespaceDelete(d *schema.ResourceData, meta interface{}
resp := <-deleteResp
err = <-error

if resp.StatusCode == http.StatusNotFound {
return nil
}

if err != nil {
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Namespace '%s': %+v", name, err)
}

return nil
}
if utils.ResponseWasNotFound(resp) {
return nil
}

func validateEventHubNamespaceSku(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))
skus := map[string]bool{
"basic": true,
"standard": true,
return fmt.Errorf("Error issuing delete request of EventHub Namespace %q (Resource Group %q): %+v", name, resGroup, err)
}

if !skus[value] {
errors = append(errors, fmt.Errorf("EventHub Namespace SKU can only be Basic or Standard"))
}
return
}

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

if value > maxCapacity || value < 1 {
errors = append(errors, fmt.Errorf("EventHub Namespace Capacity must be 20 or fewer Throughput Units for Basic or Standard SKU"))
}
return
return nil
}
Loading