From f6b7d1ced8121c7ddcfc0b0e3867972c038a07b6 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 13 Jan 2021 13:13:27 -0600 Subject: [PATCH 1/9] New data source `azurerm_search_service` --- .../internal/services/search/registration.go | 4 +- .../search/search_service_data_source.go | 159 ++++++++++++++++++ .../search/search_service_data_source_test.go | 47 ++++++ website/azurerm.erb | 4 + website/docs/d/search_service.html.markdown | 85 ++++++++++ 5 files changed, 298 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..d3b55fba1373 --- /dev/null +++ b/azurerm/internal/services/search/search_service_data_source.go @@ -0,0 +1,159 @@ +package search + +import ( + "fmt" + "log" + "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, + ForceNew: 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, + MaxItems: 1, + 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 + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := parse.SearchServiceID(d.Id()) + if err != nil { + return err + } + + resp, err := client.Get(ctx, id.ResourceGroup, id.Name, nil) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id()) + d.SetId("") + return nil + } + + return fmt.Errorf("Error reading Search Service: %+v", err) + } + + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + + 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..3607a9b82d6d --- /dev/null +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -0,0 +1,47 @@ +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, "azurerm_search_service", "test") + r := SearchServiceResource{} + + data.ResourceTest(t, r, []resource.TestStep{ + { + Config: r.basic(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("id").Exists(), + 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.type").Exists(), + check.That(data.ResourceName).Key("identity.0.principal_id").Exists(), + check.That(data.ResourceName).Key("identity.0.tenant_id").Exists(), + ), + }, + data.ImportStep(), + }) +} + +func (SearchServiceResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_search_service" "test" { + name = azurerm_search_service.test.name + resource_group_name = azurerm_resource_group.test.name +} +`, SearchServiceResource{}.basic(data)) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index ab4a0d2fe7f3..67657a405dbd 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -586,6 +586,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..ecd433f51fdd --- /dev/null +++ b/website/docs/d/search_service.html.markdown @@ -0,0 +1,85 @@ +--- +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 which should be used for this Search Service. Changing this forces a new Search Service to be created. + +* `resource_group_name` - (Required) The name of the Resource Group where the Search Service should exist. Changing this forces a new Search Service to be created. + +## 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 allowed for this resource. Defaults to `true`. + +* `partition_count` - The number of partitions which have been created. + +* `replica_count` - The number of replica's which have been created. + +-> **Note:** `partition_count` and `replica_count` can only be configured when using a `standard` sku. + +* `identity` - An `identity` block as defined below. + +* `tags` - (Optional) A mapping of tags which should be assigned to the Search Service. + +--- + +A `identity` block supports the following: + +* `type` - The Type of Identity which should be used for the Search Service. At this time the only possible value is `SystemAssigned`. + +--- + +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. From 6ba255015b02d7eaac3210328b60f48a72e6f6d9 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 13 Jan 2021 13:44:38 -0600 Subject: [PATCH 2/9] Fix copy/pasta error --- .../internal/services/search/search_service_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index 3607a9b82d6d..c1bbea8b820b 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -35,7 +35,7 @@ func TestAccDataSourceSearchService_basic(t *testing.T) { }) } -func (SearchServiceResource) basic(data acceptance.TestData) string { +func (SearchServiceDataSource) basic(data acceptance.TestData) string { return fmt.Sprintf(` %s From 2d14c6535bd6b484bf8f0fe904f5e67760400504 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 13 Jan 2021 14:08:27 -0600 Subject: [PATCH 3/9] Fix --- .../internal/services/search/search_service_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index c1bbea8b820b..d8a96eaf008e 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -14,7 +14,7 @@ type SearchServiceDataSource struct { func TestAccDataSourceSearchService_basic(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_search_service", "test") - r := SearchServiceResource{} + r := SearchServiceDataSource{} data.ResourceTest(t, r, []resource.TestStep{ { From b4ba62505e4fadfadee381c88a3e6aabd6e683e9 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 13 Jan 2021 14:32:49 -0600 Subject: [PATCH 4/9] Use right test helper --- .../internal/services/search/search_service_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index d8a96eaf008e..6408fadf6f51 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -16,7 +16,7 @@ func TestAccDataSourceSearchService_basic(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_search_service", "test") r := SearchServiceDataSource{} - data.ResourceTest(t, r, []resource.TestStep{ + data.DataSourceTest(t, []resource.TestStep{ { Config: r.basic(data), Check: resource.ComposeTestCheckFunc( From 672c97d6511325a6a5604b1b0c98185733cb1d68 Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 21 Jan 2021 11:46:46 -0600 Subject: [PATCH 5/9] Review feedback --- .../services/search/search_service_data_source.go | 2 +- .../services/search/search_service_data_source_test.go | 1 - website/docs/d/search_service.html.markdown | 8 ++------ 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/azurerm/internal/services/search/search_service_data_source.go b/azurerm/internal/services/search/search_service_data_source.go index d3b55fba1373..bd6085c9d1b9 100644 --- a/azurerm/internal/services/search/search_service_data_source.go +++ b/azurerm/internal/services/search/search_service_data_source.go @@ -117,7 +117,7 @@ func dataSourceSearchServiceRead(d *schema.ResourceData, meta interface{}) error if utils.ResponseWasNotFound(resp.Response) { log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id()) d.SetId("") - return nil + 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) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index 6408fadf6f51..f1a0175faa5b 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -20,7 +20,6 @@ func TestAccDataSourceSearchService_basic(t *testing.T) { { Config: r.basic(data), Check: resource.ComposeTestCheckFunc( - check.That(data.ResourceName).Key("id").Exists(), check.That(data.ResourceName).Key("replica_count").Exists(), check.That(data.ResourceName).Key("partition_count").Exists(), check.That(data.ResourceName).Key("primary_key").Exists(), diff --git a/website/docs/d/search_service.html.markdown b/website/docs/d/search_service.html.markdown index ecd433f51fdd..b2c7eafbb9c9 100644 --- a/website/docs/d/search_service.html.markdown +++ b/website/docs/d/search_service.html.markdown @@ -44,23 +44,19 @@ In addition to the Arguments listed above - the following Attributes are exporte * `query_keys` - A `query_keys` block as defined below. -* `public_network_access_enabled` - Whether or not public network access is allowed for this resource. Defaults to `true`. +* `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. --> **Note:** `partition_count` and `replica_count` can only be configured when using a `standard` sku. - * `identity` - An `identity` block as defined below. -* `tags` - (Optional) A mapping of tags which should be assigned to the Search Service. - --- A `identity` block supports the following: -* `type` - The Type of Identity which should be used for the Search Service. At this time the only possible value is `SystemAssigned`. +* `type` - The Type of Managed Identity which is used for the Search Service. --- From fdaa63ba07ec8a5adc6bba60698078f40d6a2de9 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 26 Jan 2021 13:59:25 -0600 Subject: [PATCH 6/9] More PR feedback --- .../services/search/search_service_data_source.go | 14 +++----------- .../search/search_service_data_source_test.go | 2 +- website/docs/d/search_service.html.markdown | 4 ++-- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/azurerm/internal/services/search/search_service_data_source.go b/azurerm/internal/services/search/search_service_data_source.go index bd6085c9d1b9..51ba8d27f555 100644 --- a/azurerm/internal/services/search/search_service_data_source.go +++ b/azurerm/internal/services/search/search_service_data_source.go @@ -2,7 +2,6 @@ package search import ( "fmt" - "log" "time" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -25,7 +24,6 @@ func dataSourceSearchService() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - ForceNew: true, }, "resource_group_name": azure.SchemaResourceGroupName(), @@ -78,7 +76,6 @@ func dataSourceSearchService() *schema.Resource { "identity": { Type: schema.TypeList, Computed: true, - MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "type": { @@ -104,27 +101,22 @@ func dataSourceSearchService() *schema.Resource { 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, err := parse.SearchServiceID(d.Id()) - if err != nil { - return err - } + 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) { - log.Printf("[INFO] Error reading Search Service %q - removing from state", d.Id()) - d.SetId("") 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.Set("name", id.Name) - d.Set("resource_group_name", id.ResourceGroup) + d.SetId(id.ID()) if props := resp.ServiceProperties; props != nil { if count := props.PartitionCount; count != 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 index f1a0175faa5b..c80ca0942f9a 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -13,7 +13,7 @@ type SearchServiceDataSource struct { } func TestAccDataSourceSearchService_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_search_service", "test") + data := acceptance.BuildTestData(t, "data.azurerm_search_service", "test") r := SearchServiceDataSource{} data.DataSourceTest(t, []resource.TestStep{ diff --git a/website/docs/d/search_service.html.markdown b/website/docs/d/search_service.html.markdown index b2c7eafbb9c9..2907fe24c7a7 100644 --- a/website/docs/d/search_service.html.markdown +++ b/website/docs/d/search_service.html.markdown @@ -28,9 +28,9 @@ data "azurerm_search_service" "example" { The following arguments are supported: -* `name` - (Required) The Name which should be used for this Search Service. Changing this forces a new Search Service to be created. +* `name` - (Required) The Name of the Search Service. -* `resource_group_name` - (Required) The name of the Resource Group where the Search Service should exist. Changing this forces a new Search Service to be created. +* `resource_group_name` - (Required) The name of the Resource Group where the Search Service exists. ## Attributes Reference From cb29eea8bd0093c52b8c1598ef7881db9cd1b2fc Mon Sep 17 00:00:00 2001 From: kt Date: Wed, 27 Jan 2021 15:59:43 -0800 Subject: [PATCH 7/9] Update azurerm/internal/services/search/search_service_data_source_test.go Co-authored-by: Tom Harvey --- .../internal/services/search/search_service_data_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index c80ca0942f9a..00f47c7671b6 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -25,7 +25,7 @@ func TestAccDataSourceSearchService_basic(t *testing.T) { 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.type").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(), ), From 3d27642c8ef96ab8092973c275879d179dfe1184 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 27 Jan 2021 20:46:17 -0600 Subject: [PATCH 8/9] Manually define TF for test --- .../search/search_service_data_source_test.go | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index 00f47c7671b6..cf2855571d4f 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -36,11 +36,29 @@ func TestAccDataSourceSearchService_basic(t *testing.T) { func (SearchServiceDataSource) basic(data acceptance.TestData) string { return fmt.Sprintf(` -%s +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 } -`, SearchServiceResource{}.basic(data)) +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) } From de4b23dcd487182ed9e4a793bc9f31f3b55eea04 Mon Sep 17 00:00:00 2001 From: kt Date: Wed, 27 Jan 2021 22:57:01 -0800 Subject: [PATCH 9/9] Update azurerm/internal/services/search/search_service_data_source_test.go --- .../internal/services/search/search_service_data_source_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/internal/services/search/search_service_data_source_test.go b/azurerm/internal/services/search/search_service_data_source_test.go index cf2855571d4f..0d3f54ebe9db 100644 --- a/azurerm/internal/services/search/search_service_data_source_test.go +++ b/azurerm/internal/services/search/search_service_data_source_test.go @@ -30,7 +30,6 @@ func TestAccDataSourceSearchService_basic(t *testing.T) { check.That(data.ResourceName).Key("identity.0.tenant_id").Exists(), ), }, - data.ImportStep(), }) }