Skip to content

Commit 3be95a2

Browse files
Add Vtctld.SetShardTabletControl client
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 736716f commit 3be95a2

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

planetscale/vtctld_general.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type VtctldService interface {
1717
ListKeyspaces(context.Context, *VtctldListKeyspacesRequest) (json.RawMessage, error)
1818
GetRoutingRules(context.Context, *VtctldGetRoutingRulesRequest) (json.RawMessage, error)
1919
GetShard(context.Context, *VtctldGetShardRequest) (json.RawMessage, error)
20+
SetShardTabletControl(context.Context, *VtctldSetShardTabletControlRequest) (json.RawMessage, error)
2021
ListTablets(context.Context, *ListBranchTabletsRequest) ([]*TabletGroup, error)
2122
StartWorkflow(context.Context, *VtctldStartWorkflowRequest) (json.RawMessage, error)
2223
StopWorkflow(context.Context, *VtctldStopWorkflowRequest) (json.RawMessage, error)
@@ -60,6 +61,22 @@ type VtctldGetShardRequest struct {
6061
Shard string `json:"-"`
6162
}
6263

64+
// VtctldSetShardTabletControlRequest is a request for updating shard tablet
65+
// controls via vtctld.
66+
type VtctldSetShardTabletControlRequest struct {
67+
Organization string `json:"-"`
68+
Database string `json:"-"`
69+
Branch string `json:"-"`
70+
71+
Keyspace string `json:"keyspace"`
72+
Shard string `json:"shard"`
73+
TabletType string `json:"tablet_type"`
74+
Cells []string `json:"cells,omitempty"`
75+
DeniedTables []string `json:"denied_tables,omitempty"`
76+
Remove *bool `json:"remove,omitempty"`
77+
DisableQueryService *bool `json:"disable_query_service,omitempty"`
78+
}
79+
6380
// VtctldStartWorkflowRequest is a request for starting a workflow.
6481
type VtctldStartWorkflowRequest struct {
6582
Organization string `json:"-"`
@@ -166,6 +183,10 @@ func vtctldShardAPIPath(org, db, branch string) string {
166183
return path.Join(databaseBranchAPIPath(org, db, branch), "vtctld", "shard")
167184
}
168185

186+
func vtctldShardTabletControlAPIPath(org, db, branch string) string {
187+
return path.Join(databaseBranchAPIPath(org, db, branch), "vtctld", "shard", "tablet-control")
188+
}
189+
169190
func (s *vtctldService) ListWorkflows(ctx context.Context, req *VtctldListWorkflowsRequest) (json.RawMessage, error) {
170191
p := vtctldWorkflowsAPIPath(req.Organization, req.Database, req.Branch)
171192
v := url.Values{}
@@ -283,6 +304,20 @@ func (s *vtctldService) GetThrottlerStatus(ctx context.Context, req *VtctldGetTh
283304
return resp.Data, nil
284305
}
285306

307+
// SetShardTabletControl updates tablet controls on a shard via vtctld.
308+
func (s *vtctldService) SetShardTabletControl(ctx context.Context, req *VtctldSetShardTabletControlRequest) (json.RawMessage, error) {
309+
p := vtctldShardTabletControlAPIPath(req.Organization, req.Database, req.Branch)
310+
httpReq, err := s.client.newRequest(http.MethodPut, p, req)
311+
if err != nil {
312+
return nil, fmt.Errorf("error creating http request: %w", err)
313+
}
314+
resp := &vtctldDataResponse{}
315+
if err := s.client.do(ctx, httpReq, resp); err != nil {
316+
return nil, err
317+
}
318+
return resp.Data, nil
319+
}
320+
286321
// CheckThrottler issues a throttler check against a single tablet.
287322
func (s *vtctldService) CheckThrottler(ctx context.Context, req *VtctldCheckThrottlerRequest) (json.RawMessage, error) {
288323
p := path.Join(vtctldThrottlerAPIPath(req.Organization, req.Database, req.Branch), "check")

planetscale/vtctld_general_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,49 @@ func TestVtctld_GetShard(t *testing.T) {
176176
c.Assert(string(data), qt.Equals, `{"keyspace":"commerce","name":"-"}`)
177177
}
178178

179+
func TestVtctld_SetShardTabletControl(t *testing.T) {
180+
c := qt.New(t)
181+
182+
remove := true
183+
184+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
185+
c.Assert(r.Method, qt.Equals, http.MethodPut)
186+
c.Assert(r.URL.Path, qt.Equals, "/v1/organizations/my-org/databases/my-db/branches/my-branch/vtctld/shard/tablet-control")
187+
188+
var body VtctldSetShardTabletControlRequest
189+
err := json.NewDecoder(r.Body).Decode(&body)
190+
c.Assert(err, qt.IsNil)
191+
c.Assert(body.Keyspace, qt.Equals, "commerce")
192+
c.Assert(body.Shard, qt.Equals, "-")
193+
c.Assert(body.TabletType, qt.Equals, "rdonly")
194+
c.Assert(body.DeniedTables, qt.DeepEquals, []string{"customers"})
195+
c.Assert(body.Remove, qt.Not(qt.IsNil))
196+
c.Assert(*body.Remove, qt.Equals, true)
197+
198+
w.WriteHeader(200)
199+
_, err = w.Write([]byte(`{"data":{}}`))
200+
c.Assert(err, qt.IsNil)
201+
}))
202+
defer ts.Close()
203+
204+
client, err := NewClient(WithBaseURL(ts.URL))
205+
c.Assert(err, qt.IsNil)
206+
207+
ctx := context.Background()
208+
data, err := client.Vtctld.SetShardTabletControl(ctx, &VtctldSetShardTabletControlRequest{
209+
Organization: "my-org",
210+
Database: "my-db",
211+
Branch: "my-branch",
212+
Keyspace: "commerce",
213+
Shard: "-",
214+
TabletType: "rdonly",
215+
DeniedTables: []string{"customers"},
216+
Remove: &remove,
217+
})
218+
c.Assert(err, qt.IsNil)
219+
c.Assert(string(data), qt.Equals, `{}`)
220+
}
221+
179222
func TestVtctld_ListKeyspaces(t *testing.T) {
180223
c := qt.New(t)
181224

0 commit comments

Comments
 (0)