Skip to content

Commit

Permalink
Migrated account_users to firewall_devices to request helpers (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezilber-akamai authored Jun 25, 2024
1 parent 0b5959c commit bd6c262
Show file tree
Hide file tree
Showing 31 changed files with 23,794 additions and 11,438 deletions.
65 changes: 12 additions & 53 deletions account_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -82,91 +79,53 @@ func (i User) GetUpdateOptions() (o UserUpdateOptions) {
return
}

// UsersPagedResponse represents a paginated User API response
type UsersPagedResponse struct {
*PageOptions
Data []User `json:"data"`
}

// endpoint gets the endpoint URL for User
func (UsersPagedResponse) endpoint(_ ...any) string {
return "account/users"
}

func (resp *UsersPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(UsersPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*UsersPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListUsers lists Users on the account
func (c *Client) ListUsers(ctx context.Context, opts *ListOptions) ([]User, error) {
response := UsersPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[User](ctx, c, "account/users", opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}

// GetUser gets the user with the provided ID
func (c *Client) GetUser(ctx context.Context, userID string) (*User, error) {
userID = url.PathEscape(userID)
e := fmt.Sprintf("account/users/%s", userID)
req := c.R(ctx).SetResult(&User{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("account/users/%s", userID)
response, err := doGETRequest[User](ctx, c, e)
if err != nil {
return nil, err
}

return r.Result().(*User), nil
return response, nil
}

// CreateUser creates a User. The email address must be confirmed before the
// User account can be accessed.
func (c *Client) CreateUser(ctx context.Context, opts UserCreateOptions) (*User, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

e := "account/users"
req := c.R(ctx).SetResult(&User{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
response, err := doPOSTRequest[User](ctx, c, e, opts)
if err != nil {
return nil, err
}

return r.Result().(*User), nil
return response, nil
}

// UpdateUser updates the User with the specified id
func (c *Client) UpdateUser(ctx context.Context, userID string, opts UserUpdateOptions) (*User, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}

userID = url.PathEscape(userID)
e := fmt.Sprintf("account/users/%s", userID)
req := c.R(ctx).SetResult(&User{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
e := formatAPIPath("account/users/%s", userID)
response, err := doPUTRequest[User](ctx, c, e, opts)
if err != nil {
return nil, err
}

return r.Result().(*User), nil
return response, nil
}

// DeleteUser deletes the User with the specified id
func (c *Client) DeleteUser(ctx context.Context, userID string) error {
userID = url.PathEscape(userID)
e := fmt.Sprintf("account/users/%s", userID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
e := formatAPIPath("account/users/%s", userID)
err := doDELETERequest(ctx, c, e)
return err
}
38 changes: 6 additions & 32 deletions betas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand All @@ -32,17 +29,6 @@ type BetaProgram struct {
MoreInfo string `json:"more_info"`
}

// BetasPagedResponse represents a paginated Beta Programs API response
type BetasPagedResponse struct {
*PageOptions
Data []BetaProgram `json:"data"`
}

// endpoint gets the endpoint URL for BetaProgram
func (BetasPagedResponse) endpoint(_ ...any) string {
return "/betas"
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (beta *BetaProgram) UnmarshalJSON(b []byte) error {
type Mask BetaProgram
Expand All @@ -65,35 +51,23 @@ func (beta *BetaProgram) UnmarshalJSON(b []byte) error {
return nil
}

func (resp *BetasPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(BetasPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*BetasPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// ListBetaPrograms lists active beta programs
func (c *Client) ListBetaPrograms(ctx context.Context, opts *ListOptions) ([]BetaProgram, error) {
response := BetasPagedResponse{}
err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[BetaProgram](ctx, c, "/betas", opts)
if err != nil {
return nil, err
}
return response.Data, nil

return response, nil
}

// GetBetaProgram gets the beta program's detail with the ID
func (c *Client) GetBetaProgram(ctx context.Context, betaID string) (*BetaProgram, error) {
req := c.R(ctx).SetResult(&BetaProgram{})
betaID = url.PathEscape(betaID)
b := fmt.Sprintf("betas/%s", betaID)
r, err := coupleAPIErrors(req.Get(b))
e := formatAPIPath("betas/%s", betaID)
response, err := doGETRequest[BetaProgram](ctx, c, e)
if err != nil {
return nil, err
}

return r.Result().(*BetaProgram), nil
return response, nil
}
132 changes: 12 additions & 120 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)

Expand Down Expand Up @@ -53,63 +50,6 @@ const (
DatabaseStatusBackingUp DatabaseStatus = "backing_up"
)

type DatabasesPagedResponse struct {
*PageOptions
Data []Database `json:"data"`
}

func (DatabasesPagedResponse) endpoint(_ ...any) string {
return "databases/instances"
}

func (resp *DatabasesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DatabasesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DatabasesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

type DatabaseEnginesPagedResponse struct {
*PageOptions
Data []DatabaseEngine `json:"data"`
}

func (DatabaseEnginesPagedResponse) endpoint(_ ...any) string {
return "databases/engines"
}

func (resp *DatabaseEnginesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DatabaseEnginesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DatabaseEnginesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

type DatabaseTypesPagedResponse struct {
*PageOptions
Data []DatabaseType `json:"data"`
}

func (DatabaseTypesPagedResponse) endpoint(_ ...any) string {
return "databases/types"
}

func (resp *DatabaseTypesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(DatabaseTypesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*DatabaseTypesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}

// A Database is a instance of Linode Managed Databases
type Database struct {
ID int `json:"id"`
Expand Down Expand Up @@ -202,100 +142,52 @@ func (d *Database) UnmarshalJSON(b []byte) error {

// ListDatabases lists all Database instances in Linode Managed Databases for the account
func (c *Client) ListDatabases(ctx context.Context, opts *ListOptions) ([]Database, error) {
response := DatabasesPagedResponse{}

err := c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[Database](ctx, c, "databases/instances", opts)
if err != nil {
return nil, err
}

return response.Data, nil
return response, nil
}

// ListDatabaseEngines lists all Database Engines. This endpoint is cached by default.
func (c *Client) ListDatabaseEngines(ctx context.Context, opts *ListOptions) ([]DatabaseEngine, error) {
response := DatabaseEnginesPagedResponse{}

endpoint, err := generateListCacheURL(response.endpoint(), opts)
if err != nil {
return nil, err
}

if result := c.getCachedResponse(endpoint); result != nil {
return result.([]DatabaseEngine), nil
}

err = c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[DatabaseEngine](ctx, c, "databases/engines", opts)
if err != nil {
return nil, err
}

c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)

return response.Data, nil
return response, nil
}

// GetDatabaseEngine returns a specific Database Engine. This endpoint is cached by default.
func (c *Client) GetDatabaseEngine(ctx context.Context, _ *ListOptions, engineID string) (*DatabaseEngine, error) {
engineID = url.PathEscape(engineID)
e := fmt.Sprintf("databases/engines/%s", engineID)

if result := c.getCachedResponse(e); result != nil {
result := result.(DatabaseEngine)
return &result, nil
}

req := c.R(ctx).SetResult(&DatabaseEngine{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("databases/engines/%s", engineID)
response, err := doGETRequest[DatabaseEngine](ctx, c, e)
if err != nil {
return nil, err
}

c.addCachedResponse(e, r.Result(), &cacheExpiryTime)

return r.Result().(*DatabaseEngine), nil
return response, nil
}

// ListDatabaseTypes lists all Types of Database provided in Linode Managed Databases. This endpoint is cached by default.
func (c *Client) ListDatabaseTypes(ctx context.Context, opts *ListOptions) ([]DatabaseType, error) {
response := DatabaseTypesPagedResponse{}

endpoint, err := generateListCacheURL(response.endpoint(), opts)
if err != nil {
return nil, err
}

if result := c.getCachedResponse(endpoint); result != nil {
return result.([]DatabaseType), nil
}

err = c.listHelper(ctx, &response, opts)
response, err := getPaginatedResults[DatabaseType](ctx, c, "databases/types", opts)
if err != nil {
return nil, err
}

c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)

return response.Data, nil
return response, nil
}

// GetDatabaseType returns a specific Database Type. This endpoint is cached by default.
func (c *Client) GetDatabaseType(ctx context.Context, _ *ListOptions, typeID string) (*DatabaseType, error) {
typeID = url.PathEscape(typeID)
e := fmt.Sprintf("databases/types/%s", typeID)

if result := c.getCachedResponse(e); result != nil {
result := result.(DatabaseType)
return &result, nil
}

req := c.R(ctx).SetResult(&DatabaseType{})
r, err := coupleAPIErrors(req.Get(e))
e := formatAPIPath("databases/types/%s", typeID)
response, err := doGETRequest[DatabaseType](ctx, c, e)
if err != nil {
return nil, err
}

c.addCachedResponse(e, r.Result(), &cacheExpiryTime)

return r.Result().(*DatabaseType), nil
return response, nil
}
Loading

0 comments on commit bd6c262

Please sign in to comment.