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..69a27e6f8cab --- /dev/null +++ b/azurerm/internal/services/eventhub/eventhub_data_source.go @@ -0,0 +1,79 @@ +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.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +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..79fcd0a12231 --- /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/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
  • diff --git a/website/docs/d/eventhub.html.markdown b/website/docs/d/eventhub.html.markdown new file mode 100644 index 000000000000..a905a108e885 --- /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 EventHub. +--- + +# Data Source: azurerm_eventhub + +Use this data source to access information about an existing EventHub. + +## 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 EventHub. + +* `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 EventHub exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the EventHub. + +* `partition_count` - The number of partitions in the EventHub. + +* `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 EventHub.