Skip to content
Open
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
29 changes: 29 additions & 0 deletions stackit/internal/services/mongodbflex/mongodbflex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,32 @@ resource "stackit_mongodbflex_user" "user" {
},
})
}

func TestMongoDBUserInvalidUsernameValidationError(t *testing.T) {
projectId := uuid.NewString()
instanceId := uuid.NewString()
tfConfig := fmt.Sprintf(`
provider "stackit" {
mongodbflex_custom_endpoint = "http://localhost:12345"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that custom endpoint needed?

service_account_token = "mock-server-needs-no-auth"
}

resource "stackit_mongodbflex_user" "user" {
project_id = "%s"
instance_id = "%s"
username = "-invalid-username"
roles = ["read"]
database = "db-name"
}
`, projectId, instanceId)

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: tfConfig,
ExpectError: regexp.MustCompile("Attribute username must start with a letter.*"),
},
},
})
}
8 changes: 8 additions & 0 deletions stackit/internal/services/mongodbflex/user/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"errors"
"fmt"
"net/http"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
Expand Down Expand Up @@ -183,6 +185,12 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile("^[A-Za-z][A-Za-z0-9-]{1,61}[A-Za-z0-9]$"),
"must start with a letter, must have between 3 and 63 letters, numbers or hyphens, and no hyphen at the end",
),
},
},
"roles": schema.SetAttribute{
Description: descriptions["roles"],
Expand Down
Loading