Skip to content

Commit accc969

Browse files
authored
Merge pull request #215 from planetscale/iheanyi/add-keyspace-create
Add keyspace creation to API
2 parents 66d4d3b + 42dbffd commit accc969

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
version: '2'
1+
version: "2"
22

33
services:
44
app:
5-
image: golang:1.19
5+
image: golang:1.23
66
volumes:
77
- .:/work
88
working_dir: /work

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/planetscale/planetscale-go
22

3-
go 1.19
3+
go 1.23
44

55
require (
66
github.com/frankban/quicktest v1.14.6

planetscale/keyspaces.go

+28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ type Keyspace struct {
1616
Sharded bool `json:"sharded"`
1717
Replicas uint64 `json:"replicas"`
1818
ExtraReplicas uint64 `json:"extra_replicas"`
19+
ResizePending bool `json:"resize_pending"`
1920
Resizing bool `json:"resizing"`
21+
Ready bool `json:"ready"`
2022
ClusterSize ClusterSize `json:"cluster_rate_name"`
2123
CreatedAt time.Time `json:"created_at"`
2224
UpdatedAt time.Time `json:"updated_at"`
@@ -34,6 +36,16 @@ type ListBranchKeyspacesRequest struct {
3436
Branch string `json:"-"`
3537
}
3638

39+
type CreateBranchKeyspaceRequest struct {
40+
Organization string `json:"-"`
41+
Database string `json:"-"`
42+
Branch string `json:"-"`
43+
Name string `json:"name"`
44+
ClusterSize ClusterSize `json:"cluster_size"`
45+
ExtraReplicas int `json:"replicas"`
46+
Shards int `json:"shards"`
47+
}
48+
3749
type GetBranchKeyspaceRequest struct {
3850
Organization string `json:"-"`
3951
Database string `json:"-"`
@@ -62,6 +74,7 @@ type branchKeyspacesResponse struct {
6274

6375
// BranchKeyspaceService is an interface for interacting with the keyspace endpoints of the PlanetScale API
6476
type BranchKeyspacesService interface {
77+
Create(context.Context, *CreateBranchKeyspaceRequest) (*Keyspace, error)
6578
List(context.Context, *ListBranchKeyspacesRequest) ([]*Keyspace, error)
6679
Get(context.Context, *GetBranchKeyspaceRequest) (*Keyspace, error)
6780
VSchema(context.Context, *GetKeyspaceVSchemaRequest) (*VSchema, error)
@@ -108,6 +121,21 @@ func (s *branchKeyspacesService) Get(ctx context.Context, getReq *GetBranchKeysp
108121
return keyspace, nil
109122
}
110123

124+
// Create creates a keyspace for a branch
125+
func (s *branchKeyspacesService) Create(ctx context.Context, createReq *CreateBranchKeyspaceRequest) (*Keyspace, error) {
126+
req, err := s.client.newRequest(http.MethodPost, databaseBranchKeyspacesAPIPath(createReq.Organization, createReq.Database, createReq.Branch), createReq)
127+
if err != nil {
128+
return nil, errors.Wrap(err, "error creating http request")
129+
}
130+
131+
keyspace := &Keyspace{}
132+
if err := s.client.do(ctx, req, keyspace); err != nil {
133+
return nil, err
134+
}
135+
136+
return keyspace, nil
137+
}
138+
111139
// VSchema returns the VSchema for a keyspace in a branch
112140
func (s *branchKeyspacesService) VSchema(ctx context.Context, getReq *GetKeyspaceVSchemaRequest) (*VSchema, error) {
113141
path := fmt.Sprintf("%s/vschema", databaseBranchKeyspaceAPIPath(getReq.Organization, getReq.Database, getReq.Branch, getReq.Keyspace))

planetscale/keyspaces_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,40 @@ func TestKeyspaces_Get(t *testing.T) {
6969
c.Assert(keyspace.Shards, qt.Equals, 2)
7070
}
7171

72+
func TestKeyspaces_Create(t *testing.T) {
73+
c := qt.New(t)
74+
75+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
76+
w.WriteHeader(201)
77+
out := `{"type":"Keyspace","id":"thisisanid","name":"planetscale","shards":2,"sharded":true,"created_at":"2022-01-14T15:39:28.394Z","updated_at":"2021-12-20T21:11:07.697Z"}`
78+
_, err := w.Write([]byte(out))
79+
c.Assert(err, qt.IsNil)
80+
c.Assert(r.Method, qt.Equals, http.MethodPost)
81+
}))
82+
83+
client, err := NewClient(WithBaseURL(ts.URL))
84+
c.Assert(err, qt.IsNil)
85+
86+
ctx := context.Background()
87+
88+
keyspace, err := client.Keyspaces.Create(ctx, &CreateBranchKeyspaceRequest{
89+
Organization: "foo",
90+
Database: "bar",
91+
Branch: "baz",
92+
Name: "qux",
93+
ClusterSize: "small",
94+
ExtraReplicas: 3,
95+
Shards: 2,
96+
})
97+
98+
wantID := "thisisanid"
99+
100+
c.Assert(err, qt.IsNil)
101+
c.Assert(keyspace.ID, qt.Equals, wantID)
102+
c.Assert(keyspace.Sharded, qt.Equals, true)
103+
c.Assert(keyspace.Shards, qt.Equals, 2)
104+
}
105+
72106
func TestKeyspaces_VSchema(t *testing.T) {
73107
c := qt.New(t)
74108

0 commit comments

Comments
 (0)