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 +