Skip to content

Commit 93edbcc

Browse files
authored
Add ListClusterSizeSKUs to branches (#234)
* Add ListClusterSizeSKUs to database branches service * Remove unused include rates attribute
1 parent 5c3cdb8 commit 93edbcc

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

planetscale/branches.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ type DatabaseBranchesService interface {
178178
EnableSafeMigrations(context.Context, *EnableSafeMigrationsRequest) (*DatabaseBranch, error)
179179
DisableSafeMigrations(context.Context, *DisableSafeMigrationsRequest) (*DatabaseBranch, error)
180180
LintSchema(context.Context, *LintSchemaRequest) ([]*SchemaLintError, error)
181+
ListClusterSKUs(context.Context, *ListBranchClusterSKUsRequest, ...ListOption) ([]*ClusterSKU, error)
182+
}
183+
184+
// ListBranchClusterSKUsRequest encapsulates the request for getting a list of Cluster SKUs for an organization.
185+
type ListBranchClusterSKUsRequest struct {
186+
Organization string
187+
Database string
188+
Branch string
181189
}
182190

183191
type databaseBranchesService struct {
@@ -439,6 +447,31 @@ func (d *databaseBranchesService) LintSchema(ctx context.Context, lintReq *LintS
439447
return lintResp.Errors, nil
440448
}
441449

450+
func (o *databaseBranchesService) ListClusterSKUs(ctx context.Context, listReq *ListBranchClusterSKUsRequest, opts ...ListOption) ([]*ClusterSKU, error) {
451+
path := fmt.Sprintf("%s/cluster-size-skus", databaseBranchAPIPath(listReq.Organization, listReq.Database, listReq.Branch))
452+
453+
defaultOpts := defaultListOptions()
454+
for _, opt := range opts {
455+
opt(defaultOpts)
456+
}
457+
458+
if vals := defaultOpts.URLValues.Encode(); vals != "" {
459+
path += "?" + vals
460+
}
461+
462+
req, err := o.client.newRequest(http.MethodGet, path, nil)
463+
if err != nil {
464+
return nil, errors.Wrap(err, "error creating http request")
465+
}
466+
467+
clusterSKUs := []*ClusterSKU{}
468+
if err := o.client.do(ctx, req, &clusterSKUs); err != nil {
469+
return nil, err
470+
}
471+
472+
return clusterSKUs, nil
473+
}
474+
442475
func databaseBranchesAPIPath(org, db string) string {
443476
return fmt.Sprintf("%s/%s/branches", databasesAPIPath(org), db)
444477
}

planetscale/branches_test.go

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestDatabaseBranches_Create(t *testing.T) {
3737
})
3838

3939
want := &DatabaseBranch{
40-
ID: "planetscale-go-test-db-branch",
40+
ID: "planetscale-go-test-db-branch",
4141
Name: testBranch,
4242
Region: Region{
4343
Slug: "us-west",
@@ -439,3 +439,120 @@ func TestDatabaseBranches_LintSchema(t *testing.T) {
439439

440440
c.Assert(lintErr, qt.DeepEquals, want)
441441
}
442+
443+
func TestDatabaseBranches_ListClusterSKUs(t *testing.T) {
444+
c := qt.New(t)
445+
446+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
447+
w.WriteHeader(200)
448+
449+
c.Assert(r.URL.String(), qt.Equals, "/v1/organizations/my-cool-org/databases/my-cool-db/branches/main/cluster-size-skus")
450+
out := `[
451+
{
452+
"name": "PS_10",
453+
"type": "ClusterSizeSku",
454+
"display_name": "PS-10",
455+
"cpu": "1/8",
456+
"provider_instance_type": null,
457+
"storage": null,
458+
"ram": "1",
459+
"enabled": true,
460+
"provider": null,
461+
"rate": null,
462+
"replica_rate": null,
463+
"default_vtgate": "VTG_5",
464+
"default_vtgate_rate": null,
465+
"sort_order": 1
466+
}
467+
]`
468+
469+
_, err := w.Write([]byte(out))
470+
c.Assert(err, qt.IsNil)
471+
}))
472+
473+
client, err := NewClient(WithBaseURL(ts.URL))
474+
c.Assert(err, qt.IsNil)
475+
476+
ctx := context.Background()
477+
478+
orgs, err := client.DatabaseBranches.ListClusterSKUs(ctx, &ListBranchClusterSKUsRequest{
479+
Organization: "my-cool-org",
480+
Database: "my-cool-db",
481+
Branch: "main",
482+
})
483+
484+
c.Assert(err, qt.IsNil)
485+
want := []*ClusterSKU{
486+
{
487+
Name: "PS_10",
488+
DisplayName: "PS-10",
489+
CPU: "1/8",
490+
Memory: 1,
491+
Enabled: true,
492+
DefaultVTGate: "VTG_5",
493+
SortOrder: 1,
494+
},
495+
}
496+
497+
c.Assert(orgs, qt.DeepEquals, want)
498+
}
499+
500+
func TestDatabaseBranches_ListClusterSKUsWithRates(t *testing.T) {
501+
c := qt.New(t)
502+
503+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
504+
w.WriteHeader(200)
505+
506+
c.Assert(r.URL.String(), qt.Equals, "/v1/organizations/my-cool-org/databases/my-cool-db/branches/main/cluster-size-skus?rates=true")
507+
out := `[
508+
{
509+
"name": "PS_10",
510+
"type": "ClusterSizeSku",
511+
"display_name": "PS-10",
512+
"cpu": "1/8",
513+
"provider_instance_type": null,
514+
"storage": "100",
515+
"ram": "1",
516+
"sort_order": 1,
517+
"enabled": true,
518+
"provider": null,
519+
"rate": 39,
520+
"replica_rate": 13,
521+
"default_vtgate": "VTG_5",
522+
"default_vtgate_rate": null
523+
}
524+
]`
525+
526+
_, err := w.Write([]byte(out))
527+
c.Assert(err, qt.IsNil)
528+
}))
529+
530+
client, err := NewClient(WithBaseURL(ts.URL))
531+
c.Assert(err, qt.IsNil)
532+
533+
ctx := context.Background()
534+
535+
orgs, err := client.DatabaseBranches.ListClusterSKUs(ctx, &ListBranchClusterSKUsRequest{
536+
Organization: "my-cool-org",
537+
Database: "my-cool-db",
538+
Branch: "main",
539+
}, WithRates())
540+
541+
c.Assert(err, qt.IsNil)
542+
want := []*ClusterSKU{
543+
{
544+
Name: "PS_10",
545+
DisplayName: "PS-10",
546+
CPU: "1/8",
547+
Memory: 1,
548+
Enabled: true,
549+
Storage: Pointer[int64](100),
550+
Rate: Pointer[int64](39),
551+
ReplicaRate: Pointer[int64](13),
552+
DefaultVTGate: "VTG_5",
553+
SortOrder: 1,
554+
},
555+
}
556+
557+
c.Assert(orgs, qt.DeepEquals, want)
558+
}

planetscale/organizations.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ type ListOrganizationRegionsRequest struct {
3535
// ListOrganizationClusterSKUsRequest encapsulates the request for getting a list of Cluster SKUs for an organization.
3636
type ListOrganizationClusterSKUsRequest struct {
3737
Organization string
38-
IncludeRates bool
3938
}
4039

4140
// ClusterSKU represents a SKU for a PlanetScale cluster

0 commit comments

Comments
 (0)