From 5dc1eafe189acea143134d8ba1f098c86e7b2730 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Tue, 18 May 2021 07:40:23 +0200 Subject: [PATCH 1/9] New data source: `azurerm_eventhub_cluster` New data source: `azurerm_eventhub_cluster` --- .../eventhub/eventhub_cluster_data_source.go | 65 +++++++++++++++++++ .../eventhub_cluster_data_source_test.go | 52 +++++++++++++++ .../services/eventhub/registration.go | 1 + website/docs/d/eventhub_cluster.html.markdown | 46 +++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 azurerm/internal/services/eventhub/eventhub_cluster_data_source.go create mode 100644 azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go create mode 100644 website/docs/d/eventhub_cluster.html.markdown 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..9f16206b6248 --- /dev/null +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go @@ -0,0 +1,65 @@ +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 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(), + + "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) + + resp, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("making Read request on Azure EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.SetId(*resp.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..58af1455568e --- /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", "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.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..b4a835c42cb5 --- /dev/null +++ b/website/docs/d/eventhub_cluster.html.markdown @@ -0,0 +1,46 @@ +--- +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. + +## 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. From 0b2c0127af7eb0e25048acd65860d72b88c7128e Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Tue, 18 May 2021 14:18:08 +0200 Subject: [PATCH 2/9] Website lint --- website/docs/d/eventhub_cluster.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/eventhub_cluster.html.markdown b/website/docs/d/eventhub_cluster.html.markdown index b4a835c42cb5..0bfcd8d265b2 100644 --- a/website/docs/d/eventhub_cluster.html.markdown +++ b/website/docs/d/eventhub_cluster.html.markdown @@ -3,7 +3,7 @@ subcategory: "Messaging" layout: "azurerm" page_title: "Azure Resource Manager: Data Source: azurerm_eventhub_cluster" description: |- -Gets information about an existing EventHub Cluster. + Gets information about an existing EventHub Cluster. --- # Data Source: azurerm_eventhub_cluster From b8b8cb28cb06044e23cd009ef223aff1439fb968 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Tue, 18 May 2021 18:37:01 +0200 Subject: [PATCH 3/9] Update azurerm/internal/services/eventhub/eventhub_cluster_data_source.go Co-authored-by: Tom Harvey --- .../internal/services/eventhub/eventhub_cluster_data_source.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go index 9f16206b6248..5feacbeec719 100644 --- a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go @@ -46,8 +46,7 @@ func dataSourceEventHubClusterRead(d *schema.ResourceData, meta interface{}) err resp, err := client.Get(ctx, resourceGroup, name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - d.SetId("") - return nil + 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) } From 03405536a0ef100ee4e5dbb31a4814be9f333d8c Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Tue, 18 May 2021 18:39:47 +0200 Subject: [PATCH 4/9] Use parser for ID --- .../services/eventhub/eventhub_cluster_data_source.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go index 5feacbeec719..a7eba364460f 100644 --- a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go @@ -7,6 +7,7 @@ import ( "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" ) @@ -43,6 +44,8 @@ func dataSourceEventHubClusterRead(d *schema.ResourceData, meta interface{}) err 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) { @@ -50,8 +53,7 @@ func dataSourceEventHubClusterRead(d *schema.ResourceData, meta interface{}) err } return fmt.Errorf("making Read request on Azure EventHub Cluster %q (Resource Group %q): %+v", name, resourceGroup, err) } - - d.SetId(*resp.ID) + d.SetId(id.ID()) d.Set("name", resp.Name) d.Set("resource_group_name", resourceGroup) From e40415ee1e337134d421e7897cfd5e41dd174d9f Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 19 May 2021 09:23:01 +0200 Subject: [PATCH 5/9] Test typofix --- .../services/eventhub/eventhub_cluster_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go index 58af1455568e..3e273197bd8f 100644 --- a/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go @@ -45,7 +45,7 @@ resource "azurerm_eventhub_cluster" "test" { } data "azurerm_eventhub_cluster" "test" { - name = azurerm_eventhub.test.name + name = azurerm_eventhub_cluster.test.name resource_group_name = azurerm_resource_group.test.name } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger) From 6aee83498a447f185ceb88fb303afe7f41f79f47 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 19 May 2021 09:30:33 +0200 Subject: [PATCH 6/9] Missing attribute added --- .../internal/services/eventhub/eventhub_cluster_data_source.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go index a7eba364460f..d4219e6ffc33 100644 --- a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go @@ -28,6 +28,8 @@ func dataSourceEventHubCluster() *schema.Resource { "resource_group_name": azure.SchemaResourceGroupName(), + "location": azure.SchemaLocation(), + "sku_name": { Type: schema.TypeString, Computed: true, From 7618214850a444ae3ae0baad8c51981ba0877006 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 19 May 2021 09:32:27 +0200 Subject: [PATCH 7/9] Fix type --- .../internal/services/eventhub/eventhub_cluster_data_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go index d4219e6ffc33..14fe0de5bedb 100644 --- a/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source.go @@ -28,7 +28,7 @@ func dataSourceEventHubCluster() *schema.Resource { "resource_group_name": azure.SchemaResourceGroupName(), - "location": azure.SchemaLocation(), + "location": azure.SchemaLocationForDataSource(), "sku_name": { Type: schema.TypeString, From b74acebb676098cf785158cc63339f045b204628 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 19 May 2021 09:49:35 +0200 Subject: [PATCH 8/9] Docs update --- website/docs/d/eventhub_cluster.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/d/eventhub_cluster.html.markdown b/website/docs/d/eventhub_cluster.html.markdown index 0bfcd8d265b2..fa886e75b9f9 100644 --- a/website/docs/d/eventhub_cluster.html.markdown +++ b/website/docs/d/eventhub_cluster.html.markdown @@ -39,6 +39,8 @@ In addition to the Arguments listed above - the following Attributes are exporte * `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: From e05058a2d5e429679800ee3cef16d02dee4119d0 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 19 May 2021 14:08:47 +0200 Subject: [PATCH 9/9] Vladimir, get some coffee or something --- .../services/eventhub/eventhub_cluster_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go b/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go index 3e273197bd8f..eacd5f84ec6d 100644 --- a/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go +++ b/azurerm/internal/services/eventhub/eventhub_cluster_data_source_test.go @@ -13,7 +13,7 @@ type EventHubClusterDataSource struct { } func TestAccEventHubClusterDataSource_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "data.azurerm_eventhub", "test") + data := acceptance.BuildTestData(t, "data.azurerm_eventhub_cluster", "test") r := EventHubClusterDataSource{} data.DataSourceTest(t, []resource.TestStep{