account_service_transfer.go raw
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
54 Created *parseabletime.ParseableTime `json:"created"`
55 Expiry *parseabletime.ParseableTime `json:"expiry"`
56 Updated *parseabletime.ParseableTime `json:"updated"`
57 }{
58 Mask: (*Mask)(ast),
59 }
60
61 if err := json.Unmarshal(b, &p); err != nil {
62 return err
63 }
64
65 ast.Created = (*time.Time)(p.Created)
66 ast.Expiry = (*time.Time)(p.Expiry)
67 ast.Updated = (*time.Time)(p.Updated)
68
69 return nil
70 }
71
72 // ListAccountServiceTransfer gets a paginated list of AccountServiceTransfer for the Account.
73 func (c *Client) ListAccountServiceTransfer(ctx context.Context, opts *ListOptions) ([]AccountServiceTransfer, error) {
74 return getPaginatedResults[AccountServiceTransfer](ctx, c, "account/service-transfers", 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 return doPOSTRequest[AccountServiceTransfer](ctx, c, "account/service-transfers", opts)
86 }
87
88 // AcceptAccountServiceTransfer accepts an AccountServiceTransfer for the provided token to
89 // receive the services included in the transfer to the Account.
90 func (c *Client) AcceptAccountServiceTransfer(ctx context.Context, token string) error {
91 e := formatAPIPath("account/service-transfers/%s/accept", token)
92 return doPOSTRequestNoRequestResponseBody(ctx, c, e)
93 }
94
95 // CancelAccountServiceTransfer cancels the AccountServiceTransfer for the provided token.
96 func (c *Client) CancelAccountServiceTransfer(ctx context.Context, token string) error {
97 e := formatAPIPath("account/service-transfers/%s", token)
98 return doDELETERequest(ctx, c, e)
99 }
100