-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new resource
azurerm_virtual_machine_run_command
(#23377)
* 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
Showing
35 changed files
with
3,423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
internal/services/compute/validate/virtual_machine_run_command_name.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
99 changes: 99 additions & 0 deletions
99
internal/services/compute/validate/virtual_machine_run_command_name_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.