mongodb / terraform-provider-mongodbatlas

Terraform MongoDB Atlas Provider: Deploy, update, and manage MongoDB Atlas infrastructure as code through HashiCorp Terraform
https://registry.terraform.io/providers/mongodb/mongodbatlas
Mozilla Public License 2.0
241 stars 167 forks source link

mongodbatlas_database_user can not be imported when they contain dashes "-" in the name #179

Closed AntjeRadkiewicz1337 closed 4 years ago

AntjeRadkiewicz1337 commented 4 years ago

Hi,

we are trying to switch to this provider. While trying out the import for the mongodbatlas_database_user we found out that we can not import database_users which contain "-" in the username. This symbol is allowed in mongoDB and all our users contain dashes. :octopus:

In https://github.com/terraform-providers/terraform-provider-mongodbatlas/blob/master/mongodbatlas/resource_mongodbatlas_database_user.go line number 249 we saw that you are splitting the last information (project_id-username-auth_database_name) block from the import into 3 parts like this:

parts := strings.SplitN(d.Id(), "-", 3)
if len(parts) != 3 {
    r eturn nil, errors.New("import format error: to import a database user, use the format {project_id}-{username}-{auth_database_name}")
}

projectID := parts[0]
username := parts[1]
authDatabaseName := parts[2]

it would be awesome if you could implement it so that we can put for example quotes around the username.

example: "super-cool-database-user"

func SplitAtDash(s string) []string {
    res := []string{}
    var beg int
    var inString bool

    for i := 0; i < len(s); i++ {
    if s[i] == '-' && !inString {
        res = append(res, s[beg:i])
        beg = i+1
    } else if s[i] == '"' || s[i] == '\'' {
        if !inString {
            inString = true
        } else if i > 0 && s[i-1] != '\\' {
            inString = false
        }
    }
    }
    return append(res, s[beg:])
}

func main(){
    strs := []string{
        `12345678901234567890abc-"super-cool-database-user"-admin`,
        `12345678901234567890abc-'super-cool-database-user'-admin`,
        `12345678901234567890abc-super-cool-database-user-admin`,
    }
    for _, str := range strs {
    fmt.Println("input string:", str)
        res := SplitAtDash(str)
        for i := range res {
    fmt.Println(res[i])
        }
    }
}

Thanks in advance

themantissa commented 4 years ago

@AntjeRadkiewicz1337 thank you for the suggestion, if memory served we improved this for project already but I'll put DB Users on the list. Is this currently blocking you or were you able to work around for now?

AntjeRadkiewicz1337 commented 4 years ago

Hi @themantissa ,

right now it is blocking us from the migration. In order to migrate we would either have to check out the code, adapt it and import the database users, or we wait until the enhancement is done.

themantissa commented 4 years ago

@AntjeRadkiewicz1337 thank you for confirming that. I've put it in our backlog and hope we can find the time to address it soon and remove the blocker. We will keep you updated on the ETA

YuryYaroshevich commented 4 years ago

@themantissa any updates on this feature? it is blocking us also

themantissa commented 4 years ago

@YuryYaroshevich we have it on the near-term backlog so, baring any emergency needs, we hope to addressthis w/ the next release and we have been releasing each month.