Skip to content

Commit

Permalink
new resource azurerm_virtual_machine_run_command (#23377)
Browse files Browse the repository at this point in the history
* run cmd

* fix

* switch to use commonids

* fix doc

* deploy fail when cmd fail

* test storage SAS

* fix lint

* remove timeout_in_seconds and async_execution; setID before poll

* fix lint

* add recreate test

* go mod vendor

* add comment
  • Loading branch information
teowa committed Jan 18, 2024
1 parent 7d465c9 commit 6d5439d
Show file tree
Hide file tree
Showing 35 changed files with 3,423 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryapplications"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryapplicationversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/gallerysharingupdate"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-03-01/virtualmachineruncommands"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-04-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2015-06-01/agreements"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand Down Expand Up @@ -58,6 +59,7 @@ type Client struct {
SSHPublicKeysClient *sshpublickeys.SshPublicKeysClient
SnapshotsClient *snapshots.SnapshotsClient
VirtualMachinesClient *virtualmachines.VirtualMachinesClient
VirtualMachineRunCommandsClient *virtualmachineruncommands.VirtualMachineRunCommandsClient
VMExtensionImageClient *compute.VirtualMachineExtensionImagesClient
VMExtensionClient *compute.VirtualMachineExtensionsClient
VMScaleSetClient *compute.VirtualMachineScaleSetsClient
Expand Down Expand Up @@ -192,6 +194,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(virtualMachinesClient.Client, o.Authorizers.ResourceManager)

virtualMachineRunCommandsClient, err := virtualmachineruncommands.NewVirtualMachineRunCommandsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building VirtualMachineRunCommands client: %+v", err)
}
o.Configure(virtualMachineRunCommandsClient.Client, o.Authorizers.ResourceManager)

vmExtensionImageClient := compute.NewVirtualMachineExtensionImagesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&vmExtensionImageClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -238,6 +246,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
SSHPublicKeysClient: sshPublicKeysClient,
SnapshotsClient: snapshotsClient,
VirtualMachinesClient: virtualMachinesClient,
VirtualMachineRunCommandsClient: virtualMachineRunCommandsClient,
VMExtensionImageClient: &vmExtensionImageClient,
VMExtensionClient: &vmExtensionClient,
VMScaleSetClient: &vmScaleSetClient,
Expand Down
1 change: 1 addition & 0 deletions internal/services/compute/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (r Registration) DataSources() []sdk.DataSource {

func (r Registration) Resources() []sdk.Resource {
return []sdk.Resource{
VirtualMachineRunCommandResource{},
GalleryApplicationResource{},
GalleryApplicationVersionResource{},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import (
"fmt"
"regexp"
"strings"
)

func VirtualMachineRunCommandName(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string but it wasn't", k))
return
}

// The value must not be empty.
if strings.TrimSpace(v) == "" {
errors = append(errors, fmt.Errorf("%q must not be empty", k))
return
}

const maxLength = 80
// VM name can be 1-80 characters in length
if len(v) > maxLength {
errors = append(errors, fmt.Errorf("%q can be at most %d characters, got %d", k, maxLength, len(v)))
}

if matched := regexp.MustCompile(`^[a-zA-Z0-9._-]+$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters, dots, dashes and underscores", k))
}

if matched := regexp.MustCompile(`^[a-zA-Z0-9]`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q must begin with an alphanumeric character", k))
}

if matched := regexp.MustCompile(`\w$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q must end with an alphanumeric character or underscore", k))
}

return warnings, errors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import "testing"

func TestVirtualMachineRunCommandName(t *testing.T) {
testData := []struct {
input string
expected bool
}{
{
// empty
input: "",
expected: false,
},
{
// basic example
input: "hello",
expected: true,
},
{
// 79 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza",
expected: true,
},
{
// 80 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab",
expected: true,
},
{
// 81 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc",
expected: false,
},
{
// may contain alphanumerics, dots, dashes and underscores
input: "hello_world7.goodbye-world4",
expected: true,
},
{
// must begin with an alphanumeric
input: "_hello",
expected: false,
},
{
// can't end with a period
input: "hello.",
expected: false,
},
{
// can't end with a dash
input: "hello-",
expected: false,
},
{
// can end with an underscore
input: "hello_",
expected: true,
},
{
// can't contain an exclamation mark
input: "hello!",
expected: false,
},
{
// start with a number
input: "0abc",
expected: true,
},
{
// can contain only numbers
input: "12345",
expected: true,
},
{
// can start with upper case letter
input: "Test",
expected: true,
},
{
// can end with upper case letter
input: "TEST",
expected: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q..", v.input)

_, errors := VirtualMachineRunCommandName(v.input, "name")
actual := len(errors) == 0
if v.expected != actual {
t.Fatalf("Expected %t but got %t", v.expected, actual)
}
}
}
Loading

0 comments on commit 6d5439d

Please sign in to comment.