|
| 1 | +package linodego |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/linode/linodego/internal/parseabletime" |
| 9 | +) |
| 10 | + |
| 11 | +// AccountServiceTransferStatus constants start with AccountServiceTransfer and |
| 12 | +// include Linode API Account Service Transfer Status values. |
| 13 | +type AccountServiceTransferStatus string |
| 14 | + |
| 15 | +// AccountServiceTransferStatus constants reflect the current status of an AccountServiceTransfer |
| 16 | +const ( |
| 17 | + AccountServiceTransferAccepted AccountServiceTransferStatus = "accepted" |
| 18 | + AccountServiceTransferCanceled AccountServiceTransferStatus = "canceled" |
| 19 | + AccountServiceTransferCompleted AccountServiceTransferStatus = "completed" |
| 20 | + AccountServiceTransferFailed AccountServiceTransferStatus = "failed" |
| 21 | + AccountServiceTransferPending AccountServiceTransferStatus = "pending" |
| 22 | + AccountServiceTransferStale AccountServiceTransferStatus = "stale" |
| 23 | +) |
| 24 | + |
| 25 | +// AccountServiceTransfer represents a request to transfer a service on an Account |
| 26 | +type AccountServiceTransfer struct { |
| 27 | + Created *time.Time `json:"-"` |
| 28 | + Entities AccountServiceTransferEntity `json:"entities"` |
| 29 | + Expiry *time.Time `json:"-"` |
| 30 | + IsSender bool `json:"is_sender"` |
| 31 | + Status AccountServiceTransferStatus `json:"status"` |
| 32 | + Token string `json:"token"` |
| 33 | + Updated *time.Time `json:"-"` |
| 34 | +} |
| 35 | + |
| 36 | +// AccountServiceTransferEntity represents a collection of the services to include |
| 37 | +// in a transfer request, separated by type. |
| 38 | +// Note: At this time, only Linodes can be transferred. |
| 39 | +type AccountServiceTransferEntity struct { |
| 40 | + Linodes []int `json:"linodes"` |
| 41 | +} |
| 42 | + |
| 43 | +type AccountServiceTransferRequestOptions struct { |
| 44 | + Entities AccountServiceTransferEntity `json:"entities"` |
| 45 | +} |
| 46 | + |
| 47 | +// UnmarshalJSON implements the json.Unmarshaler interface |
| 48 | +func (ast *AccountServiceTransfer) UnmarshalJSON(b []byte) error { |
| 49 | + type Mask AccountServiceTransfer |
| 50 | + |
| 51 | + p := struct { |
| 52 | + *Mask |
| 53 | + Created *parseabletime.ParseableTime `json:"created"` |
| 54 | + Expiry *parseabletime.ParseableTime `json:"expiry"` |
| 55 | + Updated *parseabletime.ParseableTime `json:"updated"` |
| 56 | + }{ |
| 57 | + Mask: (*Mask)(ast), |
| 58 | + } |
| 59 | + |
| 60 | + if err := json.Unmarshal(b, &p); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + ast.Created = (*time.Time)(p.Created) |
| 65 | + ast.Expiry = (*time.Time)(p.Expiry) |
| 66 | + ast.Updated = (*time.Time)(p.Updated) |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// ListAccountServiceTransfer gets a paginated list of AccountServiceTransfer for the Account. |
| 72 | +func (c *Client) ListAccountServiceTransfer(ctx context.Context, opts *ListOptions) ([]AccountServiceTransfer, error) { |
| 73 | + e := "account/service-transfers" |
| 74 | + return getPaginatedResults[AccountServiceTransfer](ctx, c, e, opts) |
| 75 | +} |
| 76 | + |
| 77 | +// GetAccountServiceTransfer gets the details of the AccountServiceTransfer for the provided token. |
| 78 | +func (c *Client) GetAccountServiceTransfer(ctx context.Context, token string) (*AccountServiceTransfer, error) { |
| 79 | + e := formatAPIPath("account/service-transfers/%s", token) |
| 80 | + return doGETRequest[AccountServiceTransfer](ctx, c, e) |
| 81 | +} |
| 82 | + |
| 83 | +// RequestAccountServiceTransfer creates a transfer request for the specified services. |
| 84 | +func (c *Client) RequestAccountServiceTransfer(ctx context.Context, opts AccountServiceTransferRequestOptions) (*AccountServiceTransfer, error) { |
| 85 | + e := "account/service-transfers" |
| 86 | + return doPOSTRequest[AccountServiceTransfer](ctx, c, e, opts) |
| 87 | +} |
| 88 | + |
| 89 | +// AcceptAccountServiceTransfer accepts an AccountServiceTransfer for the provided token to |
| 90 | +// receive the services included in the transfer to the Account. |
| 91 | +func (c *Client) AcceptAccountServiceTransfer(ctx context.Context, token string) error { |
| 92 | + e := formatAPIPath("account/service-transfers/%s/accept", token) |
| 93 | + _, err := doPOSTRequest[AccountServiceTransfer, any](ctx, c, e) |
| 94 | + return err |
| 95 | +} |
| 96 | + |
| 97 | +// CancelAccountServiceTransfer cancels the AccountServiceTransfer for the provided token. |
| 98 | +func (c *Client) CancelAccountServiceTransfer(ctx context.Context, token string) error { |
| 99 | + e := formatAPIPath("account/service-transfers/%s", token) |
| 100 | + return doDELETERequest(ctx, c, e) |
| 101 | +} |
0 commit comments