Skip to content

Commit

Permalink
r/search_service: refactoring comments from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Apr 26, 2023
1 parent f4fa384 commit 6e9e741
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 246 deletions.
39 changes: 24 additions & 15 deletions internal/services/search/search_service_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package search

import (
"fmt"
"net/http"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
Expand All @@ -13,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceSearchService() *pluginsdk.Resource {
Expand All @@ -29,7 +33,7 @@ func dataSourceSearchService() *pluginsdk.Resource {
Required: true,
},

"resource_group_name": commonschema.ResourceGroupName(),
"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"replica_count": {
Type: pluginsdk.TypeInt,
Expand Down Expand Up @@ -102,9 +106,9 @@ func dataSourceSearchServiceRead(d *pluginsdk.ResourceData, meta interface{}) er

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
partitionCount := 0
replicaCount := 0
publicNetworkAccess := false
partitionCount := 1
replicaCount := 1
publicNetworkAccess := true

if count := props.PartitionCount; count != nil {
partitionCount = int(*count)
Expand All @@ -115,7 +119,7 @@ func dataSourceSearchServiceRead(d *pluginsdk.ResourceData, meta interface{}) er
}

if props.PublicNetworkAccess != nil {
publicNetworkAccess = *props.PublicNetworkAccess != "Disabled"
publicNetworkAccess = strings.EqualFold(string(pointer.From(props.PublicNetworkAccess)), string(services.PublicNetworkAccessEnabled))
}

d.Set("partition_count", partitionCount)
Expand All @@ -128,30 +132,35 @@ func dataSourceSearchServiceRead(d *pluginsdk.ResourceData, meta interface{}) er
}
}

primaryKey := ""
secondaryKey := ""
adminKeysClient := meta.(*clients.Client).Search.AdminKeysClient
adminKeysId, err := adminkeys.ParseSearchServiceID(d.Id())
if err != nil {
return err
}

adminKeysResp, err := adminKeysClient.Get(ctx, *adminKeysId, adminkeys.GetOperationOptions{})
if err == nil {
if model := adminKeysResp.Model; model != nil {
d.Set("primary_key", model.PrimaryKey)
d.Set("secondary_key", model.SecondaryKey)
}
if err != nil && !response.WasStatusCode(adminKeysResp.HttpResponse, http.StatusForbidden) {
return fmt.Errorf("retrieving Admin Keys for %s: %+v", id, err)
}
if model := adminKeysResp.Model; model != nil {
primaryKey = utils.NormalizeNilableString(model.PrimaryKey)
secondaryKey = utils.NormalizeNilableString(model.SecondaryKey)
}
d.Set("primary_key", primaryKey)
d.Set("secondary_key", secondaryKey)

queryKeysClient := meta.(*clients.Client).Search.QueryKeysClient
queryKeysId, err := querykeys.ParseSearchServiceID(d.Id())
if err != nil {
return err
}
queryKeysResp, err := queryKeysClient.ListBySearchService(ctx, *queryKeysId, querykeys.ListBySearchServiceOperationOptions{})
if err == nil {
if model := queryKeysResp.Model; model != nil {
d.Set("query_keys", flattenSearchQueryKeys(*model))
}
if err != nil && !response.WasStatusCode(queryKeysResp.HttpResponse, http.StatusForbidden) {
return fmt.Errorf("retrieving Query Keys for %s: %+v", id, err)
}
if err := d.Set("query_keys", flattenSearchQueryKeys(queryKeysResp.Model)); err != nil {
return fmt.Errorf("setting `query_keys`: %+v", err)
}

return nil
Expand Down
Loading

0 comments on commit 6e9e741

Please sign in to comment.