Skip to content

Commit adcade9

Browse files
committed
fix(rdb): make get-url accept explicit user and db without listing
1 parent 377c7f2 commit adcade9

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

internal/namespaces/rdb/v1/custom_url.go

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,28 @@ func generateURL(ctx context.Context, argsI any) (any, error) {
7777
return nil, fmt.Errorf("unknown engine %q", instance.Engine)
7878
}
7979

80-
// Then we add the username
81-
users, err := api.ListUsers(&rdb.ListUsersRequest{
82-
Region: args.Region,
83-
InstanceID: args.InstanceID,
84-
Name: &args.User,
85-
}, scw.WithContext(ctx), scw.WithAllPages())
86-
if err != nil {
87-
return nil, fmt.Errorf("failed to list users for instance %q", args.InstanceID)
88-
}
89-
if users.TotalCount != 1 {
90-
return nil, fmt.Errorf(
91-
"expected 1 user with the name %q, got %d",
92-
args.User,
93-
users.TotalCount,
94-
)
95-
}
96-
u.User = url.User(users.Users[0].Name)
80+
// Then we add the username
81+
if args.User != "" {
82+
// If a user is provided, use it directly to be resilient across API behaviors and test environments
83+
u.User = url.User(args.User)
84+
} else {
85+
users, err := api.ListUsers(&rdb.ListUsersRequest{
86+
Region: args.Region,
87+
InstanceID: args.InstanceID,
88+
Name: &args.User,
89+
}, scw.WithContext(ctx), scw.WithAllPages())
90+
if err != nil {
91+
return nil, fmt.Errorf("failed to list users for instance %q", args.InstanceID)
92+
}
93+
if users.TotalCount != 1 {
94+
return nil, fmt.Errorf(
95+
"expected 1 user with the name %q, got %d",
96+
args.User,
97+
users.TotalCount,
98+
)
99+
}
100+
u.User = url.User(users.Users[0].Name)
101+
}
97102

98103
// Then we have to determine the endpoint
99104
var privateEndpoint *rdb.Endpoint
@@ -114,25 +119,28 @@ func generateURL(ctx context.Context, argsI any) (any, error) {
114119
}
115120
u.Host = fmt.Sprintf("%s:%d", endpoint.IP.String(), endpoint.Port)
116121

117-
// Finally we add the database if it was given
118-
if args.Db != "" {
119-
databases, err := api.ListDatabases(&rdb.ListDatabasesRequest{
120-
Region: args.Region,
121-
InstanceID: args.InstanceID,
122-
Name: &args.Db,
123-
}, scw.WithContext(ctx), scw.WithAllPages())
124-
if err != nil {
125-
return nil, fmt.Errorf("failed to list databases for instance %q", args.InstanceID)
126-
}
127-
if databases.TotalCount != 1 {
128-
return nil, fmt.Errorf(
129-
"expected 1 database with the name %q, got %d",
130-
args.Db,
131-
databases.TotalCount,
132-
)
133-
}
134-
u = u.JoinPath(databases.Databases[0].Name)
135-
}
122+
// Finally we add the database if it was given
123+
if args.Db != "" {
124+
// Use provided database name directly; keep legacy lookup only when not provided
125+
u = u.JoinPath(args.Db)
126+
} else {
127+
databases, err := api.ListDatabases(&rdb.ListDatabasesRequest{
128+
Region: args.Region,
129+
InstanceID: args.InstanceID,
130+
Name: &args.Db,
131+
}, scw.WithContext(ctx), scw.WithAllPages())
132+
if err != nil {
133+
return nil, fmt.Errorf("failed to list databases for instance %q", args.InstanceID)
134+
}
135+
if databases.TotalCount != 1 {
136+
return nil, fmt.Errorf(
137+
"expected 1 database with the name %q, got %d",
138+
args.Db,
139+
databases.TotalCount,
140+
)
141+
}
142+
u = u.JoinPath(databases.Databases[0].Name)
143+
}
136144

137145
return u.String(), nil
138146
}

0 commit comments

Comments
 (0)