From 7c0b9f02bd3a310665d3d28ee8e67ebedb94a787 Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Sun, 10 May 2020 21:35:01 +0100 Subject: [PATCH 1/6] New data source azurerm_eventhub --- .../services/eventhub/eventhub_data_source.go | 80 +++++++++++++++++++ .../services/eventhub/registration.go | 1 + .../tests/eventhub_data_source_test.go | 62 ++++++++++++++ website/docs/d/eventhub.html.markdown | 51 ++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 azurerm/internal/services/eventhub/eventhub_data_source.go create mode 100644 azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go create mode 100644 website/docs/d/eventhub.html.markdown diff --git a/azurerm/internal/services/eventhub/eventhub_data_source.go b/azurerm/internal/services/eventhub/eventhub_data_source.go new file mode 100644 index 000000000000..a04cf05e84b7 --- /dev/null +++ b/azurerm/internal/services/eventhub/eventhub_data_source.go @@ -0,0 +1,80 @@ +package eventhub + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceEventHub() *schema.Resource { + return &schema.Resource{ + Read: dataSourceEventHubRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "namespace_name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_group_name": azure.SchemaResourceGroupNameForDataSource(), + + "partition_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "partition_ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + } +} + +func dataSourceEventHubRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Eventhub.EventHubsClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + namespaceName := d.Get("namespace_name").(string) + + resp, err := client.Get(ctx, resourceGroup, namespaceName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: EventHub %q (Resource Group %q / Namespace Name %q) was not found", name, resourceGroup, namespaceName) + } + + return fmt.Errorf("Error making Read request on EventHub %q (Resource Group %q / Namespace Name %q): %+v", name, resourceGroup, namespaceName, err) + } + + d.SetId(*resp.ID) + + d.Set("name", name) + d.Set("namespace_name", namespaceName) + d.Set("resource_group_name", resourceGroup) + + if props := resp.Properties; props != nil { + d.Set("partition_count", props.PartitionCount) + d.Set("partition_ids", props.PartitionIds) + } + + return nil +} diff --git a/azurerm/internal/services/eventhub/registration.go b/azurerm/internal/services/eventhub/registration.go index 864c7bf8159d..8e1c7c5ec006 100644 --- a/azurerm/internal/services/eventhub/registration.go +++ b/azurerm/internal/services/eventhub/registration.go @@ -21,6 +21,7 @@ func (r Registration) WebsiteCategories() []string { // SupportedDataSources returns the supported Data Sources supported by this Service func (r Registration) SupportedDataSources() map[string]*schema.Resource { return map[string]*schema.Resource{ + "azurerm_eventhub": dataSourceEventHub(), "azurerm_eventhub_authorization_rule": dataSourceEventHubAuthorizationRule(), "azurerm_eventhub_consumer_group": dataSourceEventHubConsumerGroup(), "azurerm_eventhub_namespace": dataSourceEventHubNamespace(), diff --git a/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go b/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go new file mode 100644 index 000000000000..13d4659ddeb6 --- /dev/null +++ b/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go @@ -0,0 +1,62 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" +) + +func TestAccDataSourceAzureRMEventHub_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_eventhub", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceEventHub_basic(data), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(data.ResourceName, "partition_count", "2"), + resource.TestCheckResourceAttr(data.ResourceName, "partition_ids.#", "2"), + ), + }, + }, + }) +} + +func testAccDataSourceEventHub_basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-eventhub-%[1]d" + location = "%[2]s" +} + +resource "azurerm_eventhub_namespace" "test" { + name = "acctest-EHN-%[1]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + + sku = "Basic" +} + +resource "azurerm_eventhub" "test" { + name = "acctest-eh-%[1]d" + resource_group_name = azurerm_resource_group.test.name + namespace_name = azurerm_eventhub_namespace.test.name + partition_count = 2 + message_retention = 1 +} + +data "azurerm_eventhub" "test" { + name = azurerm_eventhub.test.name + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name +} +`, data.RandomInteger, data.Locations.Primary) +} diff --git a/website/docs/d/eventhub.html.markdown b/website/docs/d/eventhub.html.markdown new file mode 100644 index 000000000000..4ca967228c5b --- /dev/null +++ b/website/docs/d/eventhub.html.markdown @@ -0,0 +1,51 @@ +--- +subcategory: "Messaging" +layout: "azurerm" +page_title: "Azure Resource Manager: Data Source: azurerm_eventhub" +description: |- + Gets information about an existing Event Hub. +--- + +# Data Source: azurerm_eventhub + +Use this data source to access information about an existing Event Hub. + +## Example Usage + +```hcl +data "azurerm_eventhub" "example" { + name = "search-eventhub" + resource_group_name = "search-service" + namespace_name = "search-eventhubns" +} + +output "eventhub_id" { + value = data.azurerm_eventhub.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name of this Event Hub. + +* `resource_group_name` - (Required) The name of the Resource Group where the Event Hub exists. + +* `namespace_name` - (Required) The name of the EventHub Namespace where the Event Hub exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Event Hub. + +* `partition_count` - The number of partitions in the Event Hub. + +* `partition_ids` - The identifiers for partitions of this Event Hub. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `read` - (Defaults to 5 minutes) Used when retrieving the Event Hub. \ No newline at end of file From 9ca0dff0b7898bfe03c88894c6a1b9f6ed83a010 Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Sun, 10 May 2020 21:47:19 +0100 Subject: [PATCH 2/6] Removed space from Event Hub in docs --- website/docs/d/eventhub.html.markdown | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/website/docs/d/eventhub.html.markdown b/website/docs/d/eventhub.html.markdown index 4ca967228c5b..b6566d7011bf 100644 --- a/website/docs/d/eventhub.html.markdown +++ b/website/docs/d/eventhub.html.markdown @@ -3,12 +3,12 @@ subcategory: "Messaging" layout: "azurerm" page_title: "Azure Resource Manager: Data Source: azurerm_eventhub" description: |- - Gets information about an existing Event Hub. + Gets information about an existing EventHub. --- # Data Source: azurerm_eventhub -Use this data source to access information about an existing Event Hub. +Use this data source to access information about an existing EventHub. ## Example Usage @@ -28,24 +28,24 @@ output "eventhub_id" { The following arguments are supported: -* `name` - (Required) The name of this Event Hub. +* `name` - (Required) The name of this EventHub. -* `resource_group_name` - (Required) The name of the Resource Group where the Event Hub exists. +* `resource_group_name` - (Required) The name of the Resource Group where the EventHub exists. -* `namespace_name` - (Required) The name of the EventHub Namespace where the Event Hub exists. +* `namespace_name` - (Required) The name of the EventHub Namespace where the EventHub exists. ## Attributes Reference In addition to the Arguments listed above - the following Attributes are exported: -* `id` - The ID of the Event Hub. +* `id` - The ID of the EventHub. -* `partition_count` - The number of partitions in the Event Hub. +* `partition_count` - The number of partitions in the EventHub. -* `partition_ids` - The identifiers for partitions of this Event Hub. +* `partition_ids` - The identifiers for the partitions of this EventHub. ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: -* `read` - (Defaults to 5 minutes) Used when retrieving the Event Hub. \ No newline at end of file +* `read` - (Defaults to 5 minutes) Used when retrieving the EventHub. \ No newline at end of file From 48b534b671e6e60810d01a2b6986b1483fe88aff Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Mon, 11 May 2020 08:23:08 +0100 Subject: [PATCH 3/6] Fixed tflint errors --- .../eventhub/tests/eventhub_data_source_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go b/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go index 13d4659ddeb6..79fcd0a12231 100644 --- a/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go +++ b/azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go @@ -46,17 +46,17 @@ resource "azurerm_eventhub_namespace" "test" { } resource "azurerm_eventhub" "test" { - name = "acctest-eh-%[1]d" - resource_group_name = azurerm_resource_group.test.name - namespace_name = azurerm_eventhub_namespace.test.name - partition_count = 2 - message_retention = 1 + name = "acctest-eh-%[1]d" + resource_group_name = azurerm_resource_group.test.name + namespace_name = azurerm_eventhub_namespace.test.name + partition_count = 2 + message_retention = 1 } data "azurerm_eventhub" "test" { - name = azurerm_eventhub.test.name - namespace_name = azurerm_eventhub_namespace.test.name - resource_group_name = azurerm_resource_group.test.name + name = azurerm_eventhub.test.name + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name } `, data.RandomInteger, data.Locations.Primary) } From b20c35accb61775e6114a43d0038949ff6f5fd37 Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Mon, 11 May 2020 08:27:25 +0100 Subject: [PATCH 4/6] Added azurerm_eventhub data source to website navigation --- website/azurerm.erb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/azurerm.erb b/website/azurerm.erb index 28474f9ca9d1..685e43574374 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -210,6 +210,10 @@ azurerm_disk_encryption_set +
  • + azurerm_eventhub +
  • +
  • azurerm_eventhub_authorization_rule
  • From f58e815070a024dc1f2d0aca845a7be7567b4bbe Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Mon, 11 May 2020 08:29:02 +0100 Subject: [PATCH 5/6] tf format in website docs --- website/docs/d/eventhub.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/docs/d/eventhub.html.markdown b/website/docs/d/eventhub.html.markdown index b6566d7011bf..a905a108e885 100644 --- a/website/docs/d/eventhub.html.markdown +++ b/website/docs/d/eventhub.html.markdown @@ -14,9 +14,9 @@ Use this data source to access information about an existing EventHub. ```hcl data "azurerm_eventhub" "example" { - name = "search-eventhub" + name = "search-eventhub" resource_group_name = "search-service" - namespace_name = "search-eventhubns" + namespace_name = "search-eventhubns" } output "eventhub_id" { @@ -48,4 +48,4 @@ In addition to the Arguments listed above - the following Attributes are exporte The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: -* `read` - (Defaults to 5 minutes) Used when retrieving the EventHub. \ No newline at end of file +* `read` - (Defaults to 5 minutes) Used when retrieving the EventHub. From bed3d85eab012baddb1a8f9cbda7002f863eab43 Mon Sep 17 00:00:00 2001 From: Rob Selway Date: Mon, 11 May 2020 13:16:01 +0100 Subject: [PATCH 6/6] Changed partition_ids from TypeSet to TypeList --- azurerm/internal/services/eventhub/eventhub_data_source.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/azurerm/internal/services/eventhub/eventhub_data_source.go b/azurerm/internal/services/eventhub/eventhub_data_source.go index a04cf05e84b7..69a27e6f8cab 100644 --- a/azurerm/internal/services/eventhub/eventhub_data_source.go +++ b/azurerm/internal/services/eventhub/eventhub_data_source.go @@ -38,10 +38,9 @@ func dataSourceEventHub() *schema.Resource { }, "partition_ids": { - Type: schema.TypeSet, + Type: schema.TypeList, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, }, }