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_resource_deployment_script_azure_power_shell azurerm_resource_deployment_script_azure_cli - loosen version validation #23370

Merged
merged 3 commits into from
Oct 16, 2023
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
133 changes: 9 additions & 124 deletions internal/services/resource/resource_deployment_script_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2020-10-01/deploymentscripts"
"github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
resourceValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
Expand Down Expand Up @@ -222,133 +223,17 @@ func getDeploymentScriptArguments(kind DeploymentScriptKind) map[string]*plugins

if kind == AzurePowerShellKind {
result["version"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"2.7",
"2.8",
"3.0",
"3.1",
"3.2",
"3.3",
"3.4",
"3.5",
"3.6",
"3.7",
"3.8",
"4.1",
"4.2",
"4.3",
"4.4",
"4.5",
"4.6",
"4.7",
"4.8",
"5.0",
"5.1",
"5.2",
"5.3",
"5.4",
"5.5",
"5.6",
"5.7",
"5.8",
"5.9",
"6.0",
"6.1",
"6.2",
"6.3",
"6.4",
"6.5",
"6.6",
"7.0",
"7.1",
"7.2",
"7.3",
"7.4",
"7.5",
"8.0",
"8.1",
"8.2",
"8.3",
"9.0",
}, false),
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: resourceValidate.ResourceDeploymentScriptAzurePowerShellVersion,
}
} else {
result["version"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"2.0.77",
"2.0.78",
"2.0.79",
"2.0.80",
"2.0.81",
"2.1.0",
"2.10.0",
"2.10.1",
"2.11.0",
"2.11.1",
"2.12.0",
"2.12.1",
"2.13.0",
"2.14.0",
"2.14.1",
"2.14.2",
"2.15.0",
"2.15.1",
"2.16.0",
"2.17.0",
"2.17.1",
"2.18.0",
"2.19.0",
"2.19.1",
"2.2.0",
"2.20.0",
"2.21.0",
"2.22.0",
"2.22.1",
"2.23.0",
"2.24.0",
"2.24.1",
"2.24.2",
"2.25.0",
"2.26.0",
"2.26.1",
"2.27.0",
"2.27.1",
"2.27.2",
"2.28.0",
"2.29.0",
"2.29.1",
"2.29.2",
"2.3.0",
"2.3.1",
"2.30.0",
"2.31.0",
"2.32.0",
"2.33.0",
"2.33.1",
"2.34.0",
"2.34.1",
"2.35.0",
"2.36.0",
"2.37.0",
"2.38.0",
"2.39.0",
"2.4.0",
"2.40.0",
"2.41.0",
"2.5.0",
"2.5.1",
"2.6.0",
"2.7.0",
"2.8.0",
"2.9.0",
"2.9.1",
}, false),
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: resourceValidate.ResourceDeploymentScriptAzureCliVersion,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validate

import (
"fmt"
"regexp"
)

func ResourceDeploymentScriptAzureCliVersion(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
}

var errors []error
if matched := regexp.MustCompile(`^\d+\.\d+\.\d+$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q should be in the format `X.Y.Z` (e.g. `2.30.0`)", k))
}

return nil, errors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package validate

import "testing"

func TestResourceDeploymentScriptAzureCliVersion(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
Input: "",
Valid: false,
},

{
Input: "2",
Valid: false,
},

{
Input: "1.1",
Valid: false,
},

{
Input: "3..",
Valid: false,
},

{
Input: "2.9.0.1",
Valid: false,
},

{
Input: "2.9.0",
Valid: true,
},

{
Input: "2.0.77",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := ResourceDeploymentScriptAzureCliVersion(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package validate

import (
"fmt"
"regexp"
)

func ResourceDeploymentScriptAzurePowerShellVersion(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
}

var errors []error
if matched := regexp.MustCompile(`^\d+\.\d+$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q should be in the format `X.Y` (e.g. `9.7`)", k))
}

return nil, errors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package validate

import "testing"

func TestResourceDeploymentScriptAzurePowerShellVersion(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
Input: "",
Valid: false,
},

{
Input: "2",
Valid: false,
},

{
Input: "1.1.1",
Valid: false,
},

{
Input: "3.",
Valid: false,
},

{
Input: "9.7",
Valid: true,
},

{
Input: "10.3",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := ResourceDeploymentScriptAzurePowerShellVersion(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The following arguments are supported:

* `location` - (Required) Specifies the Azure Region where the Resource Deployment Script should exist. Changing this forces a new Resource Deployment Script to be created.

* `version` - (Required) Azure CLI module version to be used. The supported versions are `2.0.77`, `2.0.78`, `2.0.79`, `2.0.80`, `2.0.81`, `2.1.0`, `2.10.0`, `2.10.1`, `2.11.0`, `2.11.1`, `2.12.0`, `2.12.1`, `2.13.0`, `2.14.0`, `2.14.1`, `2.14.2`, `2.15.0`, `2.15.1`, `2.16.0`, `2.17.0`, `2.17.1`, `2.18.0`, `2.19.0`, `2.19.1`, `2.2.0`, `2.20.0`, `2.21.0`, `2.22.0`, `2.22.1`, `2.23.0`, `2.24.0`, `2.24.1`, `2.24.2`, `2.25.0`, `2.26.0`, `2.26.1`, `2.27.0`, `2.27.1`, `2.27.2`, `2.28.0`, `2.29.0`, `2.29.1`, `2.29.2`, `2.3.0`, `2.3.1`, `2.30.0`, `2.31.0`, `2.32.0`, `2.33.0`, `2.33.1`, `2.34.0`, `2.34.1`, `2.35.0`, `2.36.0`, `2.37.0`, `2.38.0`, `2.39.0`, `2.4.0`, `2.40.0`, `2.41.0`, `2.5.0`, `2.5.1`, `2.6.0`, `2.7.0`, `2.8.0`, `2.9.0`, `2.9.1`. Changing this forces a new Resource Deployment Script to be created.
* `version` - (Required) Specifies the version of the Azure CLI that should be used in the format `X.Y.Z` (e.g. `2.30.0`). A canonical list of versions [is available from the Microsoft Container Registry API](https://mcr.microsoft.com/v2/azure-cli/tags/list). Changing this forces a new Resource Deployment Script to be created.

* `retention_interval` - (Required) Interval for which the service retains the script resource after it reaches a terminal state. Resource will be deleted when this duration expires. The time duration should be between `1` hour and `26` hours (inclusive) and should be specified in ISO 8601 format. Changing this forces a new Resource Deployment Script to be created.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The following arguments are supported:

* `location` - (Required) Specifies the Azure Region where the Resource Deployment Script should exist. Changing this forces a new Resource Deployment Script to be created.

* `version` - (Required) Azure PowerShell module version to be used. The supported versions are `2.7`, `2.8`, `3.0`, `3.1`, `3.2`, `3.3`, `3.4`, `3.5`, `3.6`, `3.7`, `3.8`, `4.1`, `4.2`, `4.3`, `4.4`, `4.5`, `4.6`, `4.7`, `4.8`, `5.0`, `5.1`, `5.2`, `5.3`, `5.4`, `5.5`, `5.6`, `5.7`, `5.8`, `5.9`, `6.0`, `6.1`, `6.2`, `6.3`, `6.4`, `6.5`, `6.6`, `7.0`, `7.1`, `7.2`, `7.3`, `7.4`, `7.5`, `8.0`, `8.1`, `8.2`, `8.3`, `9.0`. Changing this forces a new Resource Deployment Script to be created.
* `version` - (Required) Specifies the version of the Azure PowerShell that should be used in the format `X.Y` (e.g. `9.7`). A canonical list of versions [is available from the Microsoft Container Registry API](https://mcr.microsoft.com/v2/azure-powershell/tags/list). Changing this forces a new Resource Deployment Script to be created.

* `retention_interval` - (Required) Interval for which the service retains the script resource after it reaches a terminal state. Resource will be deleted when this duration expires. The time duration should be between `1` hour and `26` hours (inclusive) and should be specified in ISO 8601 format. Changing this forces a new Resource Deployment Script to be created.

Expand Down
Loading