Skip to content

Commit 80c7a94

Browse files
committed
Added more parameterization and fixed the create user usage.
1 parent abffabe commit 80c7a94

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

connection_producer.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ type couchbaseCapellaDBConnectionProducer struct {
2121
ClusterType string `json:"cluster_type"`
2222
CloudAPIBaseURL string `json:"cloud_api_base_url"`
2323
CloudAPIClustersPath string `json:"cloud_api_clusters_path"`
24-
Hosts string `json:"hosts"`
25-
Username string `json:"username"`
26-
Password string `json:"password"`
27-
TLS bool `json:"tls"`
28-
InsecureTLS bool `json:"insecure_tls"`
29-
Base64Pem string `json:"base64pem"`
3024
BucketName string `json:"bucket_name"`
25+
ScopeName string `json:"scope_name"`
26+
AccessRole string `json:"access_role"`
27+
28+
Hosts string `json:"hosts"`
29+
Username string `json:"username"`
30+
Password string `json:"password"`
31+
TLS bool `json:"tls"`
32+
InsecureTLS bool `json:"insecure_tls"`
33+
Base64Pem string `json:"base64pem"`
3134

3235
Initialized bool
3336
rawConfig map[string]interface{}
@@ -73,15 +76,25 @@ func (c *couchbaseCapellaDBConnectionProducer) Init(ctx context.Context, initCon
7376
return nil, fmt.Errorf("access_key cannot be empty")
7477
case len(c.SecretKey) == 0:
7578
return nil, fmt.Errorf("secret_key cannot be empty")
76-
case len(c.CloudAPIBaseURL) == 0:
79+
}
80+
81+
if len(c.CloudAPIBaseURL) == 0 {
7782
c.CloudAPIBaseURL = "https://cloudapi.cloud.couchbase.com"
78-
case len(c.ClusterType) == 0:
83+
}
84+
if len(c.ClusterType) == 0 {
7985
c.ClusterType = "provisioned"
80-
case len(c.CloudAPIClustersPath) == 0 && c.ClusterType == "provisioned":
86+
}
87+
if len(c.CloudAPIClustersPath) == 0 && c.ClusterType == "provisioned" {
8188
c.CloudAPIClustersPath = "/v3/clusters"
82-
case len(c.CloudAPIClustersPath) == 0 && c.ClusterType == "invpc":
89+
} else if len(c.CloudAPIClustersPath) == 0 && c.ClusterType == "invpc" {
8390
c.CloudAPIClustersPath = "/v2/clusters"
8491
}
92+
if len(c.AccessRole) == 0 {
93+
c.AccessRole = "data_writer"
94+
}
95+
if len(c.ScopeName) == 0 {
96+
c.ScopeName = "*"
97+
}
8598

8699
if c.TLS {
87100
if len(c.Base64Pem) == 0 {

couchbasecapella.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ func newUser(ctx context.Context, c *couchbaseCapellaDBConnectionProducer, usern
148148
return errwrap.Wrapf("error unmarshalling roles and groups creation statement JSON: {{err}}", err)
149149
}
150150

151-
err = CreateCapellaUser(c.CloudAPIBaseURL, c.ClusterID, c.AccessKey, c.SecretKey, c.CloudAPIClustersPath, "*", username, req.Password, "")
151+
err = CreateCapellaUser(c.CloudAPIBaseURL, c.ClusterID, c.AccessKey, c.SecretKey, c.CloudAPIClustersPath, c.BucketName,
152+
c.ScopeName, username, req.Password, c.AccessRole)
152153
if err != nil {
153154
return err
154155
}

httputils.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,31 +259,30 @@ type UserCreateAccess struct {
259259
}
260260

261261
func CreateCapellaUser(baseUrl string, clusterID string, accessKey string, secretKey,
262-
cloudAPIclustersEndPoint string, bucketName string, username string, password string, roleName string) error {
262+
cloudAPIclustersEndPoint string, bucketName string, scopeName string, username string, password string, roleName string) error {
263263

264264
c := NewCapellaClient(baseUrl, accessKey, secretKey)
265265
if c == nil {
266266
return fmt.Errorf("Failed in creating capella client, %v", c)
267267
}
268268

269-
if roleName == "" || len(roleName) == 0 {
270-
roleName = "data_writer"
271-
}
272269
var userCreatePayload UserCreatePayload
273-
if bucketName != "" || len(bucketName) == 0 {
270+
if len(bucketName) != 0 {
274271
userCreatePayload = UserCreatePayload{
275272
Username: username,
276273
Password: password,
277274
Buckets: []UserCreateAccess{
278275
{
279276
Name: bucketName,
280-
Scope: "*",
277+
Scope: scopeName,
281278
Roles: roleName,
282279
},
283280
},
284281
}
285282
} else {
286283
userCreatePayload = UserCreatePayload{
284+
Username: username,
285+
Password: password,
287286
AllBucketsAccess: roleName,
288287
}
289288
}
@@ -293,11 +292,11 @@ func CreateCapellaUser(baseUrl string, clusterID string, accessKey string, secre
293292
defer resp.Body.Close()
294293
b, err1 := io.ReadAll(resp.Body)
295294
if err1 != nil {
296-
return fmt.Errorf("Failed during capella user creation, reading response error = %v, ep = %s, user = %v",
297-
err1, cloudAPIclustersEndPoint+"/"+clusterID+"/users", userCreatePayload.Username)
295+
return fmt.Errorf("Failed during capella user creation, reading response error = %v, ep = %s, user = %v, payload=%v",
296+
err1, cloudAPIclustersEndPoint+"/"+clusterID+"/users", userCreatePayload.Username, userCreatePayload)
298297
}
299-
return fmt.Errorf("Failed during capella user creation, response = %s, ep = %s, user = %v",
300-
string(b), cloudAPIclustersEndPoint+"/"+clusterID+"/users", userCreatePayload.Username)
298+
return fmt.Errorf("Failed during capella user creation, response = %s, ep = %s, user = %v, payload = %v",
299+
string(b), cloudAPIclustersEndPoint+"/"+clusterID+"/users", userCreatePayload.Username, userCreatePayload)
301300
}
302301
if err != nil {
303302
return err

0 commit comments

Comments
 (0)