Skip to content

Commit 844944d

Browse files
committed
passwords: support listing all passwords
The API wasn't able to list all passwords. However both in the UI and in the CLI we want to be able to show all existing passwords. This PR changes the `passwords.List` command to show all existing passwords for a given database. It also supports filtering according to a branch, if given.
1 parent 446c71c commit 844944d

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

planetscale/passwords.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewPasswordsService(client *Client) *passwordsService {
9797

9898
// Creates a new password for a branch.
9999
func (d *passwordsService) Create(ctx context.Context, createReq *DatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) {
100-
path := passwordsAPIPath(createReq.Organization, createReq.Database, createReq.Branch)
100+
path := passwordsBranchAPIPath(createReq.Organization, createReq.Database, createReq.Branch)
101101
req, err := d.client.newRequest(http.MethodPost, path, createReq)
102102
if err != nil {
103103
return nil, errors.Wrap(err, "error creating http request")
@@ -113,7 +113,7 @@ func (d *passwordsService) Create(ctx context.Context, createReq *DatabaseBranch
113113

114114
// Delete an existing password for a branch.
115115
func (d *passwordsService) Delete(ctx context.Context, deleteReq *DeleteDatabaseBranchPasswordRequest) error {
116-
path := passwordAPIPath(deleteReq.Organization, deleteReq.Database, deleteReq.Branch, deleteReq.PasswordId)
116+
path := passwordBranchAPIPath(deleteReq.Organization, deleteReq.Database, deleteReq.Branch, deleteReq.PasswordId)
117117
req, err := d.client.newRequest(http.MethodDelete, path, nil)
118118
if err != nil {
119119
return errors.Wrap(err, "error creating http request")
@@ -126,7 +126,7 @@ func (d *passwordsService) Delete(ctx context.Context, deleteReq *DeleteDatabase
126126

127127
// Get an existing password for a branch.
128128
func (d *passwordsService) Get(ctx context.Context, getReq *GetDatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) {
129-
path := passwordAPIPath(getReq.Organization, getReq.Database, getReq.Branch, getReq.PasswordId)
129+
path := passwordBranchAPIPath(getReq.Organization, getReq.Database, getReq.Branch, getReq.PasswordId)
130130
req, err := d.client.newRequest(http.MethodGet, path, nil)
131131
if err != nil {
132132
return nil, errors.Wrap(err, "error creating http request")
@@ -141,9 +141,15 @@ func (d *passwordsService) Get(ctx context.Context, getReq *GetDatabaseBranchPas
141141

142142
}
143143

144-
// List all existing passwords for a branch.
144+
// List all existing passwords. If req.Branch is set, all passwords for that
145+
// branch will be listed.
145146
func (d *passwordsService) List(ctx context.Context, listReq *ListDatabaseBranchPasswordRequest) ([]*DatabaseBranchPassword, error) {
146-
req, err := d.client.newRequest(http.MethodGet, passwordsAPIPath(listReq.Organization, listReq.Database, listReq.Branch), nil)
147+
path := passwordsAPIPath(listReq.Organization, listReq.Database)
148+
if listReq.Branch != "" {
149+
path = passwordBranchAPIPath(listReq.Organization, listReq.Database, listReq.Branch, "")
150+
}
151+
152+
req, err := d.client.newRequest(http.MethodGet, path, nil)
147153
if err != nil {
148154
return nil, errors.Wrap(err, "error creating http request to list passwords")
149155
}
@@ -156,10 +162,14 @@ func (d *passwordsService) List(ctx context.Context, listReq *ListDatabaseBranch
156162
return passwordsResp.Passwords, nil
157163
}
158164

159-
func passwordAPIPath(org, db, branch, password string) string {
160-
return fmt.Sprintf("%s/%s", passwordsAPIPath(org, db, branch), password)
165+
func passwordBranchAPIPath(org, db, branch, password string) string {
166+
return fmt.Sprintf("%s/%s", passwordsBranchAPIPath(org, db, branch), password)
161167
}
162168

163-
func passwordsAPIPath(org, db, branch string) string {
169+
func passwordsBranchAPIPath(org, db, branch string) string {
164170
return fmt.Sprintf("%s/passwords", databaseBranchAPIPath(org, db, branch))
165171
}
172+
173+
func passwordsAPIPath(org, db string) string {
174+
return fmt.Sprintf("%s/%s/passwords", databasesAPIPath(org), db)
175+
}

planetscale/passwords_test.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,52 @@ func TestPasswords_List(t *testing.T) {
8080
client, err := NewClient(WithBaseURL(ts.URL))
8181
c.Assert(err, qt.IsNil)
8282

83+
ctx := context.Background()
84+
org := "my-org"
85+
db := "planetscale-go-test-db"
86+
87+
passwords, err := client.Passwords.List(ctx, &ListDatabaseBranchPasswordRequest{
88+
Organization: org,
89+
Database: db,
90+
})
91+
92+
want := []*DatabaseBranchPassword{
93+
{
94+
Name: testPasswordID,
95+
PublicID: testPasswordID,
96+
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
97+
},
98+
}
99+
100+
c.Assert(err, qt.IsNil)
101+
c.Assert(passwords, qt.DeepEquals, want)
102+
}
103+
104+
func TestPasswords_ListBranch(t *testing.T) {
105+
c := qt.New(t)
106+
107+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
108+
w.WriteHeader(200)
109+
out := `{
110+
"data":
111+
[
112+
{
113+
"id": "planetscale-go-test-password",
114+
"display_name": "planetscale-go-test-password",
115+
"database_branch": {
116+
"name": "my-branch"
117+
},
118+
"created_at": "2021-01-14T10:19:23.000Z"
119+
}
120+
]
121+
}`
122+
_, err := w.Write([]byte(out))
123+
c.Assert(err, qt.IsNil)
124+
}))
125+
126+
client, err := NewClient(WithBaseURL(ts.URL))
127+
c.Assert(err, qt.IsNil)
128+
83129
ctx := context.Background()
84130
org := "my-org"
85131
db := "planetscale-go-test-db"
@@ -91,11 +137,16 @@ func TestPasswords_List(t *testing.T) {
91137
Branch: branch,
92138
})
93139

94-
want := []*DatabaseBranchPassword{{
95-
Name: testPasswordID,
96-
PublicID: testPasswordID,
97-
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
98-
}}
140+
want := []*DatabaseBranchPassword{
141+
{
142+
Name: testPasswordID,
143+
Branch: DatabaseBranch{
144+
Name: branch,
145+
},
146+
PublicID: testPasswordID,
147+
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
148+
},
149+
}
99150

100151
c.Assert(err, qt.IsNil)
101152
c.Assert(passwords, qt.DeepEquals, want)
@@ -117,12 +168,10 @@ func TestPasswords_ListEmpty(t *testing.T) {
117168
ctx := context.Background()
118169
org := "my-org"
119170
db := "planetscale-go-test-db"
120-
branch := "my-branch"
121171

122172
passwords, err := client.Passwords.List(ctx, &ListDatabaseBranchPasswordRequest{
123173
Organization: org,
124174
Database: db,
125-
Branch: branch,
126175
})
127176

128177
c.Assert(err, qt.IsNil)

0 commit comments

Comments
 (0)