Skip to content

Commit e149f69

Browse files
authored
adjust v3 api request/response (#1052)
Adjust request/response models for v3 api so that they are almost exactly matching v1/v2 models
2 parents 59ea5b7 + aa7f146 commit e149f69

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

internal/cmd/db_create.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,9 @@ func createDatabaseV2(client *turso.Client, name, location, groupName string, se
190190

191191
func CreateDatabaseV3BodyFromFlags(name string, seed *turso.DBSeed) turso.CreateDatabaseV3Body {
192192
body := turso.CreateDatabaseV3Body{
193-
Name: name,
194-
CreationMode: "empty",
195-
}
196-
if tursoDBFlag {
197-
body.DatabaseType = "tursodb"
198-
} else {
199-
body.DatabaseType = "libsql"
200-
}
201-
if seed != nil && seed.Type == "database" {
202-
body.CreationMode = "fork"
203-
body.ParentDBName = seed.Name
204-
if seed.Timestamp != nil {
205-
body.Timestamp = seed.Timestamp
206-
}
207-
}
208-
if seed != nil && seed.Type == "upload" {
209-
body.CreationMode = "upload"
193+
Name: name,
194+
Seed: seed,
195+
UseTursoDB: tursoDBFlag,
210196
}
211197
if remoteEncryptionKeyFlag() != "" {
212198
body.RemoteEncryption = &turso.RemoteEncryption{

internal/turso/databases_v3.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import (
55
"net/http"
66
"net/url"
77
"strconv"
8-
"time"
98

109
"github.com/tursodatabase/turso-cli/internal/flags"
1110
)
1211

1312
type DatabasesV3Client client
1413

14+
// CreateDatabaseV3Body mirrors the legacy create request (turso.CreateDBParams):
15+
// forks are expressed through Seed just like v1/v2. The single v3-specific
16+
// addition is GroupID, which carries the group UUID instead of the group name.
1517
type CreateDatabaseV3Body struct {
1618
Name string `json:"name"`
1719
GroupID string `json:"group_id,omitempty"`
18-
DatabaseType string `json:"database_type,omitempty"`
19-
CreationMode string `json:"creation_mode,omitempty"`
20-
ParentDBName string `json:"parent_db_name,omitempty"`
21-
Timestamp *time.Time `json:"parent_db_timestamp,omitempty"`
20+
Seed *DBSeed `json:"seed,omitempty"`
2221
RemoteEncryption *RemoteEncryption `json:"remote_encryption,omitempty"`
22+
UseTursoDB bool `json:"use_tursodb,omitempty"`
2323
}
2424

2525
func (d *DatabasesV3Client) url(orgID, suffix string) string {
@@ -63,14 +63,18 @@ func (d *DatabasesV3Client) List(orgID string, options DatabaseV3ListOptions) ([
6363
}
6464

6565
type response struct {
66-
Databases []Database `json:"databases"`
67-
NextCursor string `json:"next_cursor"`
66+
Databases []Database `json:"databases"`
67+
Pagination *Pagination `json:"pagination,omitempty"`
6868
}
6969
resp, err := unmarshal[response](r)
7070
if err != nil {
7171
return nil, "", err
7272
}
73-
return resp.Databases, resp.NextCursor, nil
73+
next := ""
74+
if resp.Pagination != nil && resp.Pagination.Next != nil {
75+
next = *resp.Pagination.Next
76+
}
77+
return resp.Databases, next, nil
7478
}
7579

7680
func (d *DatabasesV3Client) Get(orgID, dbID string) (Database, error) {
@@ -105,14 +109,31 @@ func (d *DatabasesV3Client) GetConfig(orgID, dbID string) (DatabaseConfig, error
105109
return DatabaseConfig{}, fmt.Errorf("failed to get database: %w", parseResponseError(r))
106110
}
107111

112+
// v3 has no dedicated /configuration endpoint: the database response
113+
// carries delete protection directly and the allow rules nested under
114+
// allow_rules_config, so we project them onto DatabaseConfig here.
108115
type response struct {
109-
Database DatabaseConfig `json:"database"`
116+
Database struct {
117+
DeleteProtection bool `json:"delete_protection"`
118+
AllowRulesConfig struct {
119+
AllowedIPs []string `json:"allowed_ips"`
120+
AllowedAwsVpcIDs []string `json:"allowed_aws_vpc_ids"`
121+
} `json:"allow_rules_config"`
122+
} `json:"database"`
110123
}
111124
resp, err := unmarshal[response](r)
112125
if err != nil {
113126
return DatabaseConfig{}, err
114127
}
115-
return resp.Database, nil
128+
deleteProtection := resp.Database.DeleteProtection
129+
config := DatabaseConfig{DeleteProtection: &deleteProtection}
130+
if ips := resp.Database.AllowRulesConfig.AllowedIPs; ips != nil {
131+
config.AllowedIPs = &ips
132+
}
133+
if vpcs := resp.Database.AllowRulesConfig.AllowedAwsVpcIDs; vpcs != nil {
134+
config.AllowedAwsVpcIDs = &vpcs
135+
}
136+
return config, nil
116137
}
117138

118139
func (d *DatabasesV3Client) Delete(orgID, dbID string) error {

0 commit comments

Comments
 (0)