File: account_service_transfer.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (99 lines) | stat: -rw-r--r-- 3,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package linodego

import (
	"context"
	"encoding/json"
	"time"

	"github.com/linode/linodego/internal/parseabletime"
)

// AccountServiceTransferStatus constants start with AccountServiceTransfer and
// include Linode API Account Service Transfer Status values.
type AccountServiceTransferStatus string

// AccountServiceTransferStatus constants reflect the current status of an AccountServiceTransfer
const (
	AccountServiceTransferAccepted  AccountServiceTransferStatus = "accepted"
	AccountServiceTransferCanceled  AccountServiceTransferStatus = "canceled"
	AccountServiceTransferCompleted AccountServiceTransferStatus = "completed"
	AccountServiceTransferFailed    AccountServiceTransferStatus = "failed"
	AccountServiceTransferPending   AccountServiceTransferStatus = "pending"
	AccountServiceTransferStale     AccountServiceTransferStatus = "stale"
)

// AccountServiceTransfer represents a request to transfer a service on an Account
type AccountServiceTransfer struct {
	Created  *time.Time                   `json:"-"`
	Entities AccountServiceTransferEntity `json:"entities"`
	Expiry   *time.Time                   `json:"-"`
	IsSender bool                         `json:"is_sender"`
	Status   AccountServiceTransferStatus `json:"status"`
	Token    string                       `json:"token"`
	Updated  *time.Time                   `json:"-"`
}

// AccountServiceTransferEntity represents a collection of the services to include
// in a transfer request, separated by type.
// Note: At this time, only Linodes can be transferred.
type AccountServiceTransferEntity struct {
	Linodes []int `json:"linodes"`
}

type AccountServiceTransferRequestOptions struct {
	Entities AccountServiceTransferEntity `json:"entities"`
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (ast *AccountServiceTransfer) UnmarshalJSON(b []byte) error {
	type Mask AccountServiceTransfer

	p := struct {
		*Mask

		Created *parseabletime.ParseableTime `json:"created"`
		Expiry  *parseabletime.ParseableTime `json:"expiry"`
		Updated *parseabletime.ParseableTime `json:"updated"`
	}{
		Mask: (*Mask)(ast),
	}

	if err := json.Unmarshal(b, &p); err != nil {
		return err
	}

	ast.Created = (*time.Time)(p.Created)
	ast.Expiry = (*time.Time)(p.Expiry)
	ast.Updated = (*time.Time)(p.Updated)

	return nil
}

// ListAccountServiceTransfer gets a paginated list of AccountServiceTransfer for the Account.
func (c *Client) ListAccountServiceTransfer(ctx context.Context, opts *ListOptions) ([]AccountServiceTransfer, error) {
	return getPaginatedResults[AccountServiceTransfer](ctx, c, "account/service-transfers", opts)
}

// GetAccountServiceTransfer gets the details of the AccountServiceTransfer for the provided token.
func (c *Client) GetAccountServiceTransfer(ctx context.Context, token string) (*AccountServiceTransfer, error) {
	e := formatAPIPath("account/service-transfers/%s", token)
	return doGETRequest[AccountServiceTransfer](ctx, c, e)
}

// RequestAccountServiceTransfer creates a transfer request for the specified services.
func (c *Client) RequestAccountServiceTransfer(ctx context.Context, opts AccountServiceTransferRequestOptions) (*AccountServiceTransfer, error) {
	return doPOSTRequest[AccountServiceTransfer](ctx, c, "account/service-transfers", opts)
}

// AcceptAccountServiceTransfer accepts an AccountServiceTransfer for the provided token to
// receive the services included in the transfer to the Account.
func (c *Client) AcceptAccountServiceTransfer(ctx context.Context, token string) error {
	e := formatAPIPath("account/service-transfers/%s/accept", token)
	return doPOSTRequestNoRequestResponseBody(ctx, c, e)
}

// CancelAccountServiceTransfer cancels the AccountServiceTransfer for the provided token.
func (c *Client) CancelAccountServiceTransfer(ctx context.Context, token string) error {
	e := formatAPIPath("account/service-transfers/%s", token)
	return doDELETERequest(ctx, c, e)
}