From eb49bad6ea8a58d6799b9822b755e3c7b42033db Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 28 Jan 2021 02:59:51 -0600 Subject: [PATCH] New data source `azurerm_search_service` (#10181) Co-authored-by: Tom Harvey Co-authored-by: Jack Co-authored-by: kt Co-authored-by: Jack Fixes #10153 --- .../internal/services/search/registration.go | 4 +- .../search/search_service_data_source.go | 151 ++++++++++++++++++ .../search/search_service_data_source_test.go | 63 ++++++++ website/azurerm.erb | 4 + website/docs/d/search_service.html.markdown | 81 ++++++++++ 5 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 azurerm/internal/services/search/search_service_data_source.go create mode 100644 azurerm/internal/services/search/search_service_data_source_test.go create mode 100644 website/docs/d/search_service.html.markdown diff --git a/azurerm/internal/services/search/registration.go b/azurerm/internal/services/search/registration.go index 4f496a3c034f..10fdd9a7224d 100644 --- a/azurerm/internal/services/search/registration.go +++ b/azurerm/internal/services/search/registration.go @@ -20,7 +20,9 @@ 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{} + return map[string]*schema.Resource{ + "azurerm_search_service": dataSourceSearchService(), + } } // SupportedResources returns the supported Resources supported by this Service diff --git a/azurerm/internal/services/search/search_service_data_source.go b/azurerm/internal/services/search/search_service_data_source.go new file mode 100644 index 000000000000..51ba8d27f555 --- /dev/null +++ b/azurerm/internal/services/search/search_service_data_source.go @@ -0,0 +1,151 @@ +package search + +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/search/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceSearchService() *schema.Resource { + return &schema.Resource{ + Read: dataSourceSearchServiceRead, + + 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(), + + "replica_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "partition_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "primary_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "secondary_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "query_keys": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + + "key": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "public_network_access_enabled": { + Type: schema.TypeBool, + Computed: true, + }, + + "identity": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "type": { + Type: schema.TypeString, + Computed: true, + }, + + "principal_id": { + Type: schema.TypeString, + Computed: true, + }, + + "tenant_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceSearchServiceRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Search.ServicesClient + subscriptionID := meta.(*clients.Client).Account.SubscriptionId + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id := parse.NewSearchServiceID(subscriptionID, d.Get("resource_group_name").(string), d.Get("name").(string)) + + resp, err := client.Get(ctx, id.ResourceGroup, id.Name, nil) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Search Service %q (Resource Group %q) was not found", id.Name, id.ResourceGroup) + } + + return fmt.Errorf("Error reading Search Service: %+v", err) + } + + d.SetId(id.ID()) + + if props := resp.ServiceProperties; props != nil { + if count := props.PartitionCount; count != nil { + d.Set("partition_count", int(*count)) + } + + if count := props.ReplicaCount; count != nil { + d.Set("replica_count", int(*count)) + } + + d.Set("public_network_access_enabled", props.PublicNetworkAccess != "Disabled") + } + + adminKeysClient := meta.(*clients.Client).Search.AdminKeysClient + adminKeysResp, err := adminKeysClient.Get(ctx, id.ResourceGroup, id.Name, nil) + if err == nil { + d.Set("primary_key", adminKeysResp.PrimaryKey) + d.Set("secondary_key", adminKeysResp.SecondaryKey) + } + + queryKeysClient := meta.(*clients.Client).Search.QueryKeysClient + queryKeysResp, err := queryKeysClient.ListBySearchService(ctx, id.ResourceGroup, id.Name, nil) + if err == nil { + d.Set("query_keys", flattenSearchQueryKeys(queryKeysResp.Values())) + } + + if err := d.Set("identity", flattenSearchServiceIdentity(resp.Identity)); err != nil { + return fmt.Errorf("setting `identity`: %s", err) + } + + return nil +} diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go new file mode 100644 index 000000000000..0d3f54ebe9db --- /dev/null +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -0,0 +1,63 @@ +package search_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 SearchServiceDataSource struct { +} + +func TestAccDataSourceSearchService_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_search_service", "test") + r := SearchServiceDataSource{} + + data.DataSourceTest(t, []resource.TestStep{ + { + Config: r.basic(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("replica_count").Exists(), + check.That(data.ResourceName).Key("partition_count").Exists(), + check.That(data.ResourceName).Key("primary_key").Exists(), + check.That(data.ResourceName).Key("secondary_key").Exists(), + check.That(data.ResourceName).Key("public_network_access_enabled").Exists(), + check.That(data.ResourceName).Key("identity.0.type").Exists(), + check.That(data.ResourceName).Key("identity.0.principal_id").Exists(), + check.That(data.ResourceName).Key("identity.0.tenant_id").Exists(), + ), + }, + }) +} + +func (SearchServiceDataSource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_search_service" "test" { + name = "acctestsearchservice%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + sku = "standard" + + identity { + type = "SystemAssigned" + } +} + +data "azurerm_search_service" "test" { + name = azurerm_search_service.test.name + resource_group_name = azurerm_resource_group.test.name +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 7d624f89732b..8d7af8fc03e5 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -590,6 +590,10 @@ azurerm_servicebus_topic_authorization_rule +
  • + azurerm_search_service +
  • +
  • azurerm_shared_image
  • diff --git a/website/docs/d/search_service.html.markdown b/website/docs/d/search_service.html.markdown new file mode 100644 index 000000000000..2907fe24c7a7 --- /dev/null +++ b/website/docs/d/search_service.html.markdown @@ -0,0 +1,81 @@ +--- +subcategory: "Search" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_search_service" +description: |- + Manages a Search Service. +--- + +# azurerm_search_service + +Manages a Search Service. + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West Europe" +} + +data "azurerm_search_service" "example" { + name = "example-search-service" + resource_group_name = azurerm_resource_group.example.name +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The Name of the Search Service. + +* `resource_group_name` - (Required) The name of the Resource Group where the Search Service exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Search Service. + +* `primary_key` - The Primary Key used for Search Service Administration. + +* `secondary_key` - The Secondary Key used for Search Service Administration. + +* `query_keys` - A `query_keys` block as defined below. + +* `public_network_access_enabled` - Whether or not public network access is enabled for this resource. + +* `partition_count` - The number of partitions which have been created. + +* `replica_count` - The number of replica's which have been created. + +* `identity` - An `identity` block as defined below. + +--- + +A `identity` block supports the following: + +* `type` - The Type of Managed Identity which is used for the Search Service. + +--- + +A `query_keys` block exports the following: + +* `key` - The value of this Query Key. + +* `name` - The name of this Query Key. + +--- + +A `identity` block exports the following: + +* `principal_id` - The (Client) ID of the Service Principal. + +* `tenant_id` - The ID of the Tenant the Service Principal is assigned in. + +## 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 Search Service.