(ctx context.Context, app, addonUUID, username string, readonly bool)
| 12 | ) |
| 13 | |
| 14 | func CreateUser(ctx context.Context, app, addonUUID, username string, readonly bool) error { |
| 15 | isSupported, err := doesDatabaseHandleUserManagement(ctx, app, addonUUID) |
| 16 | if err != nil { |
| 17 | return errors.Wrap(ctx, err, "get user management information") |
| 18 | } |
| 19 | |
| 20 | if !isSupported { |
| 21 | io.Error(ErrDatabaseNotSupportUserManagement) |
| 22 | return nil |
| 23 | } |
| 24 | |
| 25 | if usernameValidation, ok := isUsernameValid(username); !ok { |
| 26 | io.Error(usernameValidation) |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | c, err := config.ScalingoClient(ctx) |
| 31 | if err != nil { |
| 32 | return errors.Wrap(ctx, err, "get Scalingo client") |
| 33 | } |
| 34 | l, err := c.DatabaseListUsers(ctx, app, addonUUID) |
| 35 | if err != nil { |
| 36 | return errors.Wrap(ctx, err, "list the database's users") |
| 37 | } |
| 38 | |
| 39 | // Check if the user exists |
| 40 | var userExists bool |
| 41 | for _, user := range l { |
| 42 | if user.Name == username { |
| 43 | userExists = true |
| 44 | break |
| 45 | } |
| 46 | } |
| 47 | if userExists { |
| 48 | return errors.New(ctx, fmt.Sprintf("User \"%s\" already exists", username)) |
| 49 | } |
| 50 | |
| 51 | password, confirmedPassword, err := askForPasswordWithRetry(ctx, 3) |
| 52 | if err != nil { |
| 53 | io.Error(err) |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | isPasswordGenerated := false |
| 58 | if password == "" { |
| 59 | isPasswordGenerated = true |
| 60 | password = gopassword.Generate() |
| 61 | confirmedPassword = password |
| 62 | } |
| 63 | |
| 64 | user := scalingo.DatabaseCreateUserParam{ |
| 65 | DatabaseID: addonUUID, |
| 66 | Name: username, |
| 67 | Password: password, |
| 68 | PasswordConfirmation: confirmedPassword, |
| 69 | ReadOnly: readonly, |
| 70 | } |
| 71 | databaseUsers, err := c.DatabaseCreateUser(ctx, app, addonUUID, user) |
nothing calls this directly
no test coverage detected