Skip to content

Commit 9ce93bb

Browse files
authored
feat(mongodb): add list databases route (scaleway#2662)
1 parent de749e3 commit 9ce93bb

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

api/mongodb/v1/mongodb_sdk.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ func (enum *InstanceStatus) UnmarshalJSON(data []byte) error {
9090
return nil
9191
}
9292

93+
type ListDatabasesRequestOrderBy string
94+
95+
const (
96+
ListDatabasesRequestOrderByNameAsc = ListDatabasesRequestOrderBy("name_asc")
97+
ListDatabasesRequestOrderByNameDesc = ListDatabasesRequestOrderBy("name_desc")
98+
)
99+
100+
func (enum ListDatabasesRequestOrderBy) String() string {
101+
if enum == "" {
102+
// return default value if empty
103+
return string(ListDatabasesRequestOrderByNameAsc)
104+
}
105+
return string(enum)
106+
}
107+
108+
func (enum ListDatabasesRequestOrderBy) Values() []ListDatabasesRequestOrderBy {
109+
return []ListDatabasesRequestOrderBy{
110+
"name_asc",
111+
"name_desc",
112+
}
113+
}
114+
115+
func (enum ListDatabasesRequestOrderBy) MarshalJSON() ([]byte, error) {
116+
return []byte(fmt.Sprintf(`"%s"`, enum)), nil
117+
}
118+
119+
func (enum *ListDatabasesRequestOrderBy) UnmarshalJSON(data []byte) error {
120+
tmp := ""
121+
122+
if err := json.Unmarshal(data, &tmp); err != nil {
123+
return err
124+
}
125+
126+
*enum = ListDatabasesRequestOrderBy(ListDatabasesRequestOrderBy(tmp).String())
127+
return nil
128+
}
129+
93130
type ListInstancesRequestOrderBy string
94131

95132
const (
@@ -491,6 +528,11 @@ type EndpointSpec struct {
491528
PrivateNetwork *EndpointSpecPrivateNetworkDetails `json:"private_network,omitempty"`
492529
}
493530

531+
// Database: database.
532+
type Database struct {
533+
Name string `json:"name"`
534+
}
535+
494536
// Instance: instance.
495537
type Instance struct {
496538
// ID: UUID of the Database Instance.
@@ -771,6 +813,51 @@ type GetSnapshotRequest struct {
771813
SnapshotID string `json:"-"`
772814
}
773815

816+
// ListDatabasesRequest: list databases request.
817+
type ListDatabasesRequest struct {
818+
// Region: region to target. If none is passed will use default region from the config.
819+
Region scw.Region `json:"-"`
820+
821+
// InstanceID: UUID of the Database Instance.
822+
InstanceID string `json:"-"`
823+
824+
// OrderBy: criteria to use when requesting user listing.
825+
// Default value: name_asc
826+
OrderBy ListDatabasesRequestOrderBy `json:"-"`
827+
828+
Page *int32 `json:"-"`
829+
830+
PageSize *uint32 `json:"-"`
831+
}
832+
833+
// ListDatabasesResponse: list databases response.
834+
type ListDatabasesResponse struct {
835+
// Databases: list of the databases.
836+
Databases []*Database `json:"databases"`
837+
838+
// TotalCount: total count of databases present on a Database Instance.
839+
TotalCount uint64 `json:"total_count"`
840+
}
841+
842+
// UnsafeGetTotalCount should not be used
843+
// Internal usage only
844+
func (r *ListDatabasesResponse) UnsafeGetTotalCount() uint64 {
845+
return r.TotalCount
846+
}
847+
848+
// UnsafeAppend should not be used
849+
// Internal usage only
850+
func (r *ListDatabasesResponse) UnsafeAppend(res any) (uint64, error) {
851+
results, ok := res.(*ListDatabasesResponse)
852+
if !ok {
853+
return 0, errors.New("%T type cannot be appended to type %T", res, r)
854+
}
855+
856+
r.Databases = append(r.Databases, results.Databases...)
857+
r.TotalCount += uint64(len(results.Databases))
858+
return uint64(len(results.Databases)), nil
859+
}
860+
774861
// ListInstancesRequest: list instances request.
775862
type ListInstancesRequest struct {
776863
// Region: region to target. If none is passed will use default region from the config.
@@ -1848,6 +1935,48 @@ func (s *API) SetUserRole(req *SetUserRoleRequest, opts ...scw.RequestOption) (*
18481935
return &resp, nil
18491936
}
18501937

1938+
// ListDatabases: List all databases of a given Database Instance.
1939+
func (s *API) ListDatabases(req *ListDatabasesRequest, opts ...scw.RequestOption) (*ListDatabasesResponse, error) {
1940+
var err error
1941+
1942+
if req.Region == "" {
1943+
defaultRegion, _ := s.client.GetDefaultRegion()
1944+
req.Region = defaultRegion
1945+
}
1946+
1947+
defaultPageSize, exist := s.client.GetDefaultPageSize()
1948+
if (req.PageSize == nil || *req.PageSize == 0) && exist {
1949+
req.PageSize = &defaultPageSize
1950+
}
1951+
1952+
query := url.Values{}
1953+
parameter.AddToQuery(query, "order_by", req.OrderBy)
1954+
parameter.AddToQuery(query, "page", req.Page)
1955+
parameter.AddToQuery(query, "page_size", req.PageSize)
1956+
1957+
if fmt.Sprint(req.Region) == "" {
1958+
return nil, errors.New("field Region cannot be empty in request")
1959+
}
1960+
1961+
if fmt.Sprint(req.InstanceID) == "" {
1962+
return nil, errors.New("field InstanceID cannot be empty in request")
1963+
}
1964+
1965+
scwReq := &scw.ScalewayRequest{
1966+
Method: "GET",
1967+
Path: "/mongodb/v1/regions/" + fmt.Sprint(req.Region) + "/instances/" + fmt.Sprint(req.InstanceID) + "/databases",
1968+
Query: query,
1969+
}
1970+
1971+
var resp ListDatabasesResponse
1972+
1973+
err = s.client.Do(scwReq, &resp, opts...)
1974+
if err != nil {
1975+
return nil, err
1976+
}
1977+
return &resp, nil
1978+
}
1979+
18511980
// DeleteEndpoint: Delete the endpoint of a Database Instance. You must specify the `endpoint_id` parameter of the endpoint you want to delete. Note that you might need to update any environment configurations that point to the deleted endpoint.
18521981
func (s *API) DeleteEndpoint(req *DeleteEndpointRequest, opts ...scw.RequestOption) error {
18531982
var err error

0 commit comments

Comments
 (0)