diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go new file mode 100644 index 000000000000..14fe0de5bedb --- /dev/null +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go @@ -0,0 +1,68 @@ +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/services/eventhub/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceEventHubCluster() *schema.Resource { + return &schema.Resource{ + Read: dataSourceEventHubClusterRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "location": azure.SchemaLocationForDataSource(), + + "sku_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceEventHubClusterRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Eventhub.ClusterClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + id := parse.NewClusterID(subscriptionId, resourceGroup, name) + resp, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("%s was not found", id) + } + return fmt.Errorf("making Read request on Azure EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err) + } + d.SetId(id.ID()) + + d.Set("name", resp.Name) + d.Set("resource_group_name", resourceGroup) + d.Set("sku_name", flattenEventHubClusterSkuName(resp.Sku)) + if location := resp.Location; location != nil { + d.Set("location", azure.NormalizeLocation(*location)) + } + + return nil +} diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go new file mode 100644 index 000000000000..eacd5f84ec6d --- /dev/null +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go @@ -0,0 +1,52 @@ +package eventhub_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check" +) + +type EventHubClusterDataSource struct { +} + +func TestAccEventHubClusterDataSource_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_eventhub_cluster", "test") + r := EventHubClusterDataSource{} + + data.DataSourceTest(t, []resource.TestStep{ + { + Config: r.basic(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("sku_name").HasValue("Dedicated_1"), + ), + }, + }) +} + +func (EventHubClusterDataSource) 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_cluster" "test" { + name = "acctesteventhubclusTER-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + sku_name = "Dedicated_1" +} + +data "azurerm_eventhub_cluster" "test" { + name = azurerm_eventhub_cluster.test.name + resource_group_name = azurerm_resource_group.test.name +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) +} diff --git a/azurerm/internal/services/eventhub/registration.go b/azurerm/internal/services/eventhub/registration.go index 2da44f69c885..b66af015efd9 100644 --- a/azurerm/internal/services/eventhub/registration.go +++ b/azurerm/internal/services/eventhub/registration.go @@ -23,6 +23,7 @@ func (r Registration) WebsiteCategories() []string { func (r Registration) SupportedDataSources() map[string]*schema.Resource { return map[string]*schema.Resource{ "azurerm_eventhub": dataSourceEventHub(), + "azurerm_eventhub_cluster": dataSourceEventHubCluster(), "azurerm_eventhub_authorization_rule": EventHubAuthorizationRuleDataSource(), "azurerm_eventhub_consumer_group": EventHubConsumerGroupDataSource(), "azurerm_eventhub_namespace": EventHubNamespaceDataSource(), diff --git a/website/docs/d/eventhub_cluster.html.markdown b/website/docs/d/eventhub_cluster.html.markdown new file mode 100644 index 000000000000..fa886e75b9f9 --- /dev/null +++ b/website/docs/d/eventhub_cluster.html.markdown @@ -0,0 +1,48 @@ +--- +subcategory: "Messaging" +layout: "azurerm" +page_title: "Azure Resource Manager: Data Source: azurerm_eventhub_cluster" +description: |- + Gets information about an existing EventHub Cluster. +--- + +# Data Source: azurerm_eventhub_cluster + +Use this data source to access information about an existing EventHub. + +## Example Usage + +```hcl +data "azurerm_eventhub_cluster" "example" { + name = "search-eventhub" + resource_group_name = "search-service" +} + +output "eventhub_id" { + value = data.azurerm_eventhub_cluster.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name of this EventHub Cluster. + +* `resource_group_name` - (Required) The name of the Resource Group where the EventHub Cluster exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the EventHub Cluster. + +* `sku_name` - SKU name of the EventHub Cluster. + +* `location` - Location of the EventHub Cluster. + +## 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 Cluster.