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_virtual_machine_run_command - fix update #25186

Merged
merged 2 commits into from
Mar 12, 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
32 changes: 20 additions & 12 deletions internal/services/compute/virtual_machine_run_command_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ sdk.Resource = VirtualMachineRunCommandResource{}
var _ sdk.ResourceWithUpdate = VirtualMachineRunCommandResource{}
var (
_ sdk.Resource = VirtualMachineRunCommandResource{}
_ sdk.ResourceWithUpdate = VirtualMachineRunCommandResource{}
)

type VirtualMachineRunCommandResource struct{}

Expand Down Expand Up @@ -304,7 +306,6 @@ func (r VirtualMachineRunCommandResource) Arguments() map[string]*pluginsdk.Sche

"tags": commonschema.Tags(),
}

}

func (r VirtualMachineRunCommandResource) Attributes() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -515,19 +516,26 @@ func (r VirtualMachineRunCommandResource) Update() sdk.ResourceFunc {
return err
}

resp, err := client.GetByVirtualMachine(ctx, *id, virtualmachineruncommands.GetByVirtualMachineOperationOptions{
// otherwise, the response will not contain instanceView
Expand: pointer.To("instanceView"),
})
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
if resp.Model == nil {
return fmt.Errorf("unexpected null model of %s", *id)
}
payload := resp.Model
if payload.Properties == nil {
return fmt.Errorf("unexpected null properties of %s", *id)
}

var config VirtualMachineRunCommandResourceSchema
if err := metadata.Decode(&config); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

payload := virtualmachineruncommands.VirtualMachineRunCommandUpdate{
Properties: &virtualmachineruncommands.VirtualMachineRunCommandProperties{
TreatFailureAsDeploymentFailure: pointer.To(true),
AsyncExecution: pointer.To(false),
TimeoutInSeconds: pointer.To(int64(metadata.ResourceData.Timeout(pluginsdk.TimeoutUpdate).Seconds())),
},
}

if metadata.ResourceData.HasChange("error_blob_managed_identity") {
payload.Properties.ErrorBlobManagedIdentity = expandVirtualMachineRunCommandBlobManagedIdentity(config.ErrorBlobManagedIdentity)
}
Expand Down Expand Up @@ -568,7 +576,7 @@ func (r VirtualMachineRunCommandResource) Update() sdk.ResourceFunc {
payload.Tags = tags.Expand(config.Tags)
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package compute_test

// NOTE: this file is generated - manual changes will be overwritten.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See NOTICE.txt in the project root for license information.
import (
"context"
"fmt"
Expand Down Expand Up @@ -128,6 +125,28 @@ func TestAccVirtualMachineRunCommand_complete(t *testing.T) {
})
}

func TestAccVirtualMachineRunCommand_updateParameters(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_virtual_machine_run_command", "test")
r := VirtualMachineRunCommandTestResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basicWithParameters(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("protected_parameter"),
})
}

func TestAccVirtualMachineRunCommand_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_virtual_machine_run_command", "test")
r := VirtualMachineRunCommandTestResource{}
Expand Down Expand Up @@ -205,6 +224,35 @@ resource "azurerm_virtual_machine_run_command" "import" {
`, r.basic(data))
}

func (r VirtualMachineRunCommandTestResource) basicWithParameters(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%s

resource "azurerm_virtual_machine_run_command" "test" {
name = "acctestvmrc-${var.random_string}"
location = azurerm_resource_group.test.location
virtual_machine_id = azurerm_linux_virtual_machine.test.id
source {
script = "echo 'hello world'"
}

parameter {
name = "acctestvmrc-${var.random_string}"
value = "val-${var.random_string}"
}

protected_parameter {
name = "acctestvmrc2-${var.random_string}"
value = "val-${var.random_string}"
}
}
`, r.template(data))
}

func (r VirtualMachineRunCommandTestResource) basicWithScriptError(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Loading