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 +