Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_search_service - fix error when updating various fields #24903

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions internal/services/search/search_service_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,19 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
// object by removing all of the READ-ONLY fields from the model...
// (e.g., privateEndpointConnections, provisioningState, sharedPrivateLinkResources,
// status and statusDetails)
payload := services.SearchService{
Identity: model.Identity,
Location: model.Location,
Properties: pointer.To(services.SearchServiceProperties{}),
Sku: model.Sku,
Tags: model.Tags,
}
model.Properties.PrivateEndpointConnections = nil
model.Properties.ProvisioningState = nil
model.Properties.SharedPrivateLinkResources = nil
model.Properties.Status = nil
model.Properties.StatusDetails = nil

if d.HasChange("customer_managed_key_enforcement_enabled") {
cmkEnforcement := services.SearchEncryptionWithCmkDisabled
if enabled := d.Get("customer_managed_key_enforcement_enabled").(bool); enabled {
cmkEnforcement = services.SearchEncryptionWithCmkEnabled
}

payload.Properties.EncryptionWithCmk = &services.EncryptionWithCmk{
model.Properties.EncryptionWithCmk = &services.EncryptionWithCmk{
Enforcement: pointer.To(cmkEnforcement),
}
}
Expand All @@ -385,7 +383,7 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
return fmt.Errorf("'hosting_mode' can only be set to %q if the 'sku' is %q, got %q", services.HostingModeHighDensity, services.SkuNameStandardThree, pointer.From(model.Sku.Name))
}

payload.Properties.HostingMode = pointer.To(hostingMode)
model.Properties.HostingMode = pointer.To(hostingMode)
}

if d.HasChange("identity") {
Expand All @@ -394,7 +392,7 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
return fmt.Errorf("expanding `identity`: %+v", err)
}

payload.Identity = expandedIdentity
model.Identity = expandedIdentity
}

if d.HasChange("public_network_access_enabled") {
Expand All @@ -403,7 +401,7 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
publicNetworkAccess = services.PublicNetworkAccessDisabled
}

payload.Properties.PublicNetworkAccess = pointer.To(publicNetworkAccess)
model.Properties.PublicNetworkAccess = pointer.To(publicNetworkAccess)
}

if d.HasChanges("authentication_failure_mode", "local_authentication_enabled") {
Expand Down Expand Up @@ -434,8 +432,8 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
authenticationOptions = nil
}

payload.Properties.DisableLocalAuth = pointer.To(!localAuthenticationEnabled)
payload.Properties.AuthOptions = authenticationOptions
model.Properties.DisableLocalAuth = pointer.To(!localAuthenticationEnabled)
model.Properties.AuthOptions = authenticationOptions
}

if d.HasChange("replica_count") {
Expand All @@ -444,7 +442,7 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
return err
}

payload.Properties.ReplicaCount = pointer.To(replicaCount)
model.Properties.ReplicaCount = pointer.To(replicaCount)
}

if d.HasChange("partition_count") {
Expand All @@ -460,13 +458,13 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
return fmt.Errorf("%q SKUs in %q mode can have a maximum of 3 partitions, got %d", string(services.SkuNameStandardThree), string(services.HostingModeHighDensity), partitionCount)
}

payload.Properties.PartitionCount = pointer.To(partitionCount)
model.Properties.PartitionCount = pointer.To(partitionCount)
}

if d.HasChange("allowed_ips") {
ipRulesRaw := d.Get("allowed_ips").(*pluginsdk.Set).List()

payload.Properties.NetworkRuleSet = &services.NetworkRuleSet{
model.Properties.NetworkRuleSet = &services.NetworkRuleSet{
IPRules: expandSearchServiceIPRules(ipRulesRaw),
}
}
Expand All @@ -482,14 +480,14 @@ func resourceSearchServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) er
return fmt.Errorf("`semantic_search_sku` can only be specified when `sku` is not set to %q", string(services.SkuNameFree))
}

payload.Properties.SemanticSearch = pointer.To(semanticSearchSku)
model.Properties.SemanticSearch = pointer.To(semanticSearchSku)
}

if d.HasChange("tags") {
payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
model.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}

if err = client.CreateOrUpdateThenPoll(ctx, *id, payload, services.CreateOrUpdateOperationOptions{}); err != nil {
if err = client.CreateOrUpdateThenPoll(ctx, *id, model, services.CreateOrUpdateOperationOptions{}); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

Expand Down
45 changes: 45 additions & 0 deletions internal/services/search/search_service_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,28 @@ func TestAccSearchService_apiAccessControlUpdate(t *testing.T) {
})
}

func TestAccSearchService_localAuthEnabled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_search_service", "test")
r := SearchServiceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.localAuthEnabled(data, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.localAuthEnabled(data, false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r SearchServiceResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := services.ParseSearchServiceID(state.ID)
if err != nil {
Expand Down Expand Up @@ -723,3 +745,26 @@ resource "azurerm_search_service" "test" {
}
`, template, data.RandomInteger, localAuthenticationEnabled, authenticationFailureMode)
}

func (r SearchServiceResource) localAuthEnabled(data acceptance.TestData, localAuthEnabled bool) string {
template := r.template(data)
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%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"

replica_count = 1
partition_count = 1
public_network_access_enabled = false
local_authentication_enabled = %t
}
`, template, data.RandomInteger, localAuthEnabled)
}
Loading