Skip to content

Commit

Permalink
azurerm_virtual_machine_run_command - fix update (#25186)
Browse files Browse the repository at this point in the history
* fix update

* fix
  • Loading branch information
teowa authored Mar 12, 2024
1 parent 58fba7f commit 0819e5c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
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

0 comments on commit 0819e5c

Please sign in to comment.