|
| 1 | +package planetscale |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/pkg/errors" |
| 10 | +) |
| 11 | + |
| 12 | +type ConnectionStrings struct { |
| 13 | + DotNet string `json:"dotnet"` |
| 14 | + General string `json:"general"` |
| 15 | + MySQLCLI string `json:"mysqlcli"` |
| 16 | + PHP string `json:"php"` |
| 17 | + Prisma string `json:"prisma"` |
| 18 | + Rails string `json:"rails"` |
| 19 | + Go string `json:"go"` |
| 20 | + Java string `json:"java"` |
| 21 | + Rust string `json:"rust"` |
| 22 | +} |
| 23 | + |
| 24 | +type DatabaseBranchPassword struct { |
| 25 | + PublicID string `json:"id"` |
| 26 | + Name string `json:"display_name"` |
| 27 | + UserName string `json:"username"` |
| 28 | + Role string `json:"role"` |
| 29 | + Branch DatabaseBranch `json:"database_branch"` |
| 30 | + CreatedAt time.Time `json:"created_at"` |
| 31 | + DeletedAt time.Time `json:"deleted_at"` |
| 32 | + PlainText string `json:"plain_text"` |
| 33 | + ConnectionStrings ConnectionStrings `json:"connection_strings"` |
| 34 | +} |
| 35 | + |
| 36 | +// DatabaseBranchPasswordRequest encapsulates the request for creating/getting/deleting a |
| 37 | +// database branch password. |
| 38 | +type DatabaseBranchPasswordRequest struct { |
| 39 | + Organization string `json:"-"` |
| 40 | + Database string `json:"-"` |
| 41 | + Branch string `json:"-"` |
| 42 | + DisplayName string `json:"display_name"` |
| 43 | +} |
| 44 | + |
| 45 | +// ListDatabaseBranchPasswordRequest encapsulates the request for listing all passwords |
| 46 | +// for a given database branch. |
| 47 | +type ListDatabaseBranchPasswordRequest struct { |
| 48 | + Organization string |
| 49 | + Database string |
| 50 | + Branch string |
| 51 | +} |
| 52 | + |
| 53 | +// GetDatabaseBranchPasswordRequest encapsulates the request for listing all passwords |
| 54 | +// for a given database branch. |
| 55 | +type GetDatabaseBranchPasswordRequest struct { |
| 56 | + Organization string `json:"-"` |
| 57 | + Database string `json:"-"` |
| 58 | + Branch string `json:"-"` |
| 59 | + DisplayName string `json:"display_name"` |
| 60 | + PasswordId string |
| 61 | +} |
| 62 | + |
| 63 | +// DeleteDatabaseBranchPasswordRequest encapsulates the request for deleting a password |
| 64 | +// for a given database branch. |
| 65 | +type DeleteDatabaseBranchPasswordRequest struct { |
| 66 | + Organization string `json:"-"` |
| 67 | + Database string `json:"-"` |
| 68 | + Branch string `json:"-"` |
| 69 | + DisplayName string `json:"display_name"` |
| 70 | + PasswordId string |
| 71 | +} |
| 72 | + |
| 73 | +// DatabaseBranchPasswordsService is an interface for communicating with the PlanetScale |
| 74 | +// Database Branch Passwords API endpoint. |
| 75 | +type PasswordsService interface { |
| 76 | + Create(context.Context, *DatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) |
| 77 | + List(context.Context, *ListDatabaseBranchPasswordRequest) ([]*DatabaseBranchPassword, error) |
| 78 | + Get(context.Context, *GetDatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) |
| 79 | + Delete(context.Context, *DeleteDatabaseBranchPasswordRequest) error |
| 80 | +} |
| 81 | + |
| 82 | +type passwordsService struct { |
| 83 | + client *Client |
| 84 | +} |
| 85 | + |
| 86 | +type passwordsResponse struct { |
| 87 | + Passwords []*DatabaseBranchPassword `json:"data"` |
| 88 | +} |
| 89 | + |
| 90 | +var _ PasswordsService = &passwordsService{} |
| 91 | + |
| 92 | +func NewPasswordsService(client *Client) *passwordsService { |
| 93 | + return &passwordsService{ |
| 94 | + client: client, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// Creates a new password for a branch. |
| 99 | +func (d *passwordsService) Create(ctx context.Context, createReq *DatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) { |
| 100 | + path := passwordsAPIPath(createReq.Organization, createReq.Database, createReq.Branch) |
| 101 | + req, err := d.client.newRequest(http.MethodPost, path, createReq) |
| 102 | + if err != nil { |
| 103 | + return nil, errors.Wrap(err, "error creating http request") |
| 104 | + } |
| 105 | + |
| 106 | + password := &DatabaseBranchPassword{} |
| 107 | + if err := d.client.do(ctx, req, &password); err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + return password, nil |
| 112 | +} |
| 113 | + |
| 114 | +// Delete an existing password for a branch. |
| 115 | +func (d *passwordsService) Delete(ctx context.Context, deleteReq *DeleteDatabaseBranchPasswordRequest) error { |
| 116 | + path := passwordAPIPath(deleteReq.Organization, deleteReq.Database, deleteReq.Branch, deleteReq.PasswordId) |
| 117 | + req, err := d.client.newRequest(http.MethodDelete, path, nil) |
| 118 | + if err != nil { |
| 119 | + return errors.Wrap(err, "error creating http request") |
| 120 | + } |
| 121 | + |
| 122 | + err = d.client.do(ctx, req, nil) |
| 123 | + return err |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +// Get an existing password for a branch. |
| 128 | +func (d *passwordsService) Get(ctx context.Context, getReq *GetDatabaseBranchPasswordRequest) (*DatabaseBranchPassword, error) { |
| 129 | + path := passwordAPIPath(getReq.Organization, getReq.Database, getReq.Branch, getReq.PasswordId) |
| 130 | + req, err := d.client.newRequest(http.MethodGet, path, nil) |
| 131 | + if err != nil { |
| 132 | + return nil, errors.Wrap(err, "error creating http request") |
| 133 | + } |
| 134 | + |
| 135 | + password := &DatabaseBranchPassword{} |
| 136 | + if err := d.client.do(ctx, req, &password); err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + return password, nil |
| 141 | + |
| 142 | +} |
| 143 | + |
| 144 | +// List all existing passwords for a branch. |
| 145 | +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 | + if err != nil { |
| 148 | + return nil, errors.Wrap(err, "error creating http request to list passwords") |
| 149 | + } |
| 150 | + |
| 151 | + passwordsResp := &passwordsResponse{} |
| 152 | + if err := d.client.do(ctx, req, &passwordsResp); err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + |
| 156 | + return passwordsResp.Passwords, nil |
| 157 | +} |
| 158 | + |
| 159 | +func passwordAPIPath(org, db, branch, password string) string { |
| 160 | + return fmt.Sprintf("%s/%s", passwordsAPIPath(org, db, branch), password) |
| 161 | +} |
| 162 | + |
| 163 | +func passwordsAPIPath(org, db, branch string) string { |
| 164 | + return fmt.Sprintf("%s/passwords", databaseBranchAPIPath(org, db, branch)) |
| 165 | +} |
0 commit comments