{"record":{"id":"aed7b41abd5d716c","repo":"googleapis/mcp-toolbox","slug":"error-creating-alloydb-user-w","errorCode":null,"errorMessage":"error creating AlloyDB user: %w","messagePattern":"error creating AlloyDB user: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/alloydbadmin/alloydbadmin.go","lineNumber":230,"sourceCode":"\tif userType == \"ALLOYDB_BUILT_IN\" {\n\t\tuser.Password = password\n\t}\n\n\tif len(roles) > 0 {\n\t\tuser.DatabaseRoles = roles\n\t}\n\n\tservice, err := s.getService(ctx, accessToken)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\turlString := fmt.Sprintf(\"projects/%s/locations/%s/clusters/%s\", project, location, cluster)\n\n\t// The Create API returns a long-running operation.\n\tresp, err := service.Projects.Locations.Clusters.Users.Create(urlString, user).UserId(userID).Do()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error creating AlloyDB user: %w\", err)\n\t}\n\n\treturn resp, nil\n}\n\nfunc (s *Source) GetCluster(ctx context.Context, project, location, cluster, accessToken string) (any, error) {\n\tservice, err := s.getService(ctx, accessToken)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\turlString := fmt.Sprintf(\"projects/%s/locations/%s/clusters/%s\", project, location, cluster)\n\n\tresp, err := service.Projects.Locations.Clusters.Get(urlString).Do()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting AlloyDB cluster: %w\", err)\n\t}\n","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/alloydbadmin/alloydbadmin.go#L212-L248","documentation":"This error wraps a failure from the AlloyDB Admin API Users.Create call, which creates a database user (IAM-based or password-based) on a cluster. It fires when the REST request to projects/*/locations/*/clusters/*/users returns an error before any long-running operation completes. The wrapped error retains the Google API details for downstream inspection.","triggerScenarios":"Calling Source.CreateUser with a malformed cluster URL, a duplicate or invalid userId, a missing required user type field, or an unauthorized access token when calling Users.Create(urlString, user).UserId(userID).Do().","commonSituations":"Creating a user that already exists (409), granting roles that require IAM permissions the caller lacks, using a token without the alloydb.admin scope, typos in project/location/cluster, or invalid password policy violations.","solutions":["Verify the cluster exists and project/location/cluster names are spelled correctly","Ensure the userId is unique in the cluster and meets validation rules","Confirm the access token has alloydb.admin or cloud-platform scope and is fresh","Unwrap with errors.As(*googleapi.Error) to see the exact status (400/403/409) and fix inputs accordingly","Check the User struct fields (userType, password, databaseRoles) against the AlloyDB API schema"],"exampleFix":"// before\nresp, err := service.Projects.Locations.Clusters.Users.Create(urlString, user).UserId(userID).Do()\nif err != nil { return nil, fmt.Errorf(\"error creating AlloyDB user: %w\", err) }\n// after\nresp, err := service.Projects.Locations.Clusters.Users.Create(urlString, user).UserId(userID).Do()\nif err != nil {\n    var gerr *googleapi.Error\n    if errors.As(err, &gerr) && gerr.Code == 409 {\n        return nil, fmt.Errorf(\"user %q already exists in cluster %q: %w\", userID, cluster, err)\n    }\n    return nil, fmt.Errorf(\"error creating AlloyDB user: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"// Go: pre-check user uniqueness and inputs\nif userID == \"\" || userType == \"\" {\n    return fmt.Errorf(\"userID and userType are required\")\n}\nif _, err := s.GetUsers(ctx, project, location, cluster, userID, accessToken); err == nil {\n    return fmt.Errorf(\"user %s already exists in cluster %s\", userID, cluster)\n}","typeGuard":"func IsAlreadyExists(err error) bool {\n    var gerr *googleapi.Error\n    return errors.As(err, &gerr) && gerr.Code == 409\n}","tryCatchPattern":"_, err := s.CreateUser(ctx, userType, password, roles, accessToken, project, location, cluster, userID)\nif err != nil {\n    var gerr *googleapi.Error\n    if errors.As(err, &gerr) && gerr.Code == 409 {\n        return nil // treat as idempotent success\n    }\n    return err\n}","preventionTips":["Check user existence with GetUsers before create for idempotency","Validate userId format and role names against the AlloyDB API schema","Refresh OAuth tokens before long administrative workflows","Use a dedicated service account with roles/alloydb.admin"],"tags":["gcp","alloydb","rest-api","authorization","iam-user"],"backgroundTag":"gcp-api-request-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}