-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6841 from robselway/feature/eventhub_data_source
New data source azurerm_eventhub
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
azurerm/internal/services/eventhub/eventhub_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
azurerm/internal/services/eventhub/tests/eventhub_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |