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

GH-156 Fix bug with Deployment Variable Name validation regex #158

Merged
merged 1 commit into from
May 20, 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
2 changes: 1 addition & 1 deletion bitbucket/data_source_bitbucket_deployment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func dataSourceBitbucketDeploymentVariable() *schema.Resource {
Description: "The name of the variable (must consist of only ASCII letters, numbers, underscores & not begin with a number).",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateRepositoryVariableName,
ValidateDiagFunc: validateDeploymentVariableName,
},
"value": {
Description: "The value of the variable (note: if this variable is marked 'secured', this attribute will be blank).",
Expand Down
53 changes: 53 additions & 0 deletions bitbucket/data_source_bitbucket_deployment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,59 @@ func TestAccBitbucketDeploymentVariableDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "value", deploymentVariableValue),
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "secured", "false"),

resource.TestCheckResourceAttrSet("data.bitbucket_deployment_variable.testacc", "id"),
resource.TestCheckResourceAttrSet("data.bitbucket_deployment_variable.testacc", "deployment"),
),
},
{
Config: fmt.Sprintf(`
data "bitbucket_workspace" "testacc" {
id = "%s"
}

resource "bitbucket_project" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
name = "%s"
key = "%s"
is_private = true
}

resource "bitbucket_repository" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
project_key = bitbucket_project.testacc.key
name = "%s"
enable_pipelines = true
}

resource "bitbucket_deployment" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
name = "TF ACC Test Deployment"
environment = "Test"
}

resource "bitbucket_deployment_variable" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
deployment = bitbucket_deployment.testacc.id
key = "%s"
value = "%s"
secured = false
}

data "bitbucket_deployment_variable" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
deployment = bitbucket_deployment.testacc.id
key = bitbucket_deployment_variable.testacc.key
}`, workspaceSlug, projectName, projectKey, repoName, "__MY_VARIABLE", deploymentVariableValue),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "workspace", workspaceSlug),
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "repository", repoName),
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "key", "__MY_VARIABLE"),
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "value", deploymentVariableValue),
resource.TestCheckResourceAttr("data.bitbucket_deployment_variable.testacc", "secured", "false"),

resource.TestCheckResourceAttrSet("data.bitbucket_deployment_variable.testacc", "id"),
resource.TestCheckResourceAttrSet("data.bitbucket_deployment_variable.testacc", "deployment"),
),
Expand Down
13 changes: 12 additions & 1 deletion bitbucket/resource_bitbucket_deployment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/go-cty/cty"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -47,7 +49,7 @@ func resourceBitbucketDeploymentVariable() *schema.Resource {
Description: "The name of the variable (must consist of only ASCII letters, numbers, underscores & not begin with a number).",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateRepositoryVariableName,
ValidateDiagFunc: validateDeploymentVariableName,
},
"value": {
Description: "The value of the variable.",
Expand Down Expand Up @@ -188,3 +190,12 @@ func resourceBitbucketDeploymentVariableImport(ctx context.Context, resourceData

return ret, nil
}

func validateDeploymentVariableName(val interface{}, path cty.Path) diag.Diagnostics {
match, _ := regexp.MatchString("^([a-zA-Z_])[a-zA-Z0-9_]+$", val.(string))
if !match {
return diag.FromErr(fmt.Errorf("variable name must consist of only ASCII letters, numbers, underscores & not begin with a number (a-z, 0-9, _)"))
}

return diag.Diagnostics{}
}
61 changes: 61 additions & 0 deletions bitbucket/resource_bitbucket_deployment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bitbucket

import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -111,6 +112,52 @@ func TestAccBitbucketDeploymentVariableResource_basic(t *testing.T) {
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "value", deploymentVariableValue),
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "secured", "false"),

resource.TestCheckResourceAttrSet("bitbucket_deployment_variable.testacc", "id"),
resource.TestCheckResourceAttrSet("bitbucket_deployment_variable.testacc", "deployment"),
),
},
{
Config: fmt.Sprintf(`
data "bitbucket_workspace" "testacc" {
id = "%s"
}

resource "bitbucket_project" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
name = "%s"
key = "%s"
is_private = true
}

resource "bitbucket_repository" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
project_key = bitbucket_project.testacc.key
name = "%s"
enable_pipelines = true
}

resource "bitbucket_deployment" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
name = "TF ACC Test Deployment"
environment = "Test"
}

resource "bitbucket_deployment_variable" "testacc" {
workspace = data.bitbucket_workspace.testacc.id
repository = bitbucket_repository.testacc.name
deployment = bitbucket_deployment.testacc.id
key = "%s"
value = "%s"
secured = false
}`, workspaceSlug, projectName, projectKey, repoName, "__MY_VARIABLE123", deploymentVariableValue),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "workspace", workspaceSlug),
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "repository", repoName),
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "key", "__MY_VARIABLE123"),
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "value", deploymentVariableValue),
resource.TestCheckResourceAttr("bitbucket_deployment_variable.testacc", "secured", "false"),

resource.TestCheckResourceAttrSet("bitbucket_deployment_variable.testacc", "id"),
resource.TestCheckResourceAttrSet("bitbucket_deployment_variable.testacc", "deployment"),
),
Expand Down Expand Up @@ -215,3 +262,17 @@ func TestAccBitbucketDeploymentVariableResource_multipleVars(t *testing.T) {
},
})
}

func TestValidateDeploymentVariableName(t *testing.T) {
invalidNames := []string{"4asdasd", "$£$%$", "AS@£$@£$@", "as-adasd"}
for _, name := range invalidNames {
validator := validateDeploymentVariableName(name, nil)
assert.True(t, validator.HasError())
}

validNames := []string{"adasd", "asds2342432", "asdadsa_asdasd23424242", "Asdfdfsdf", "AsfdsdfSDFDFSf", "___MY_VARIABLE"}
for _, name := range validNames {
validator := validateDeploymentVariableName(name, nil)
assert.False(t, validator.HasError())
}
}