account.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  // Account associated with the token in use.
  12  type Account struct {
  13  	FirstName         string      `json:"first_name"`
  14  	LastName          string      `json:"last_name"`
  15  	Email             string      `json:"email"`
  16  	Company           string      `json:"company"`
  17  	Address1          string      `json:"address_1"`
  18  	Address2          string      `json:"address_2"`
  19  	Balance           float32     `json:"balance"`
  20  	BalanceUninvoiced float32     `json:"balance_uninvoiced"`
  21  	City              string      `json:"city"`
  22  	State             string      `json:"state"`
  23  	Zip               string      `json:"zip"`
  24  	Country           string      `json:"country"`
  25  	TaxID             string      `json:"tax_id"`
  26  	Phone             string      `json:"phone"`
  27  	CreditCard        *CreditCard `json:"credit_card"`
  28  	EUUID             string      `json:"euuid"`
  29  	BillingSource     string      `json:"billing_source"`
  30  	Capabilities      []string    `json:"capabilities"`
  31  	ActiveSince       *time.Time  `json:"active_since"`
  32  	ActivePromotions  []Promotion `json:"active_promotions"`
  33  }
  34  
  35  // AccountUpdateOptions fields are those accepted by UpdateAccount
  36  type AccountUpdateOptions struct {
  37  	Address1  string `json:"address_1,omitempty"`
  38  	Address2  string `json:"address_2,omitempty"`
  39  	City      string `json:"city,omitempty"`
  40  	Company   string `json:"company,omitempty"`
  41  	Country   string `json:"country,omitempty"`
  42  	Email     string `json:"email,omitempty"`
  43  	FirstName string `json:"first_name,omitempty"`
  44  	LastName  string `json:"last_name,omitempty"`
  45  	Phone     string `json:"phone,omitempty"`
  46  	State     string `json:"state,omitempty"`
  47  	TaxID     string `json:"tax_id,omitempty"`
  48  	Zip       string `json:"zip,omitempty"`
  49  }
  50  
  51  // GetUpdateOptions converts an Account to AccountUpdateOptions for use in UpdateAccount
  52  func (i Account) GetUpdateOptions() (o AccountUpdateOptions) {
  53  	o.Address1 = i.Address1
  54  	o.Address2 = i.Address2
  55  	o.City = i.City
  56  	o.Company = i.Company
  57  	o.Country = i.Country
  58  	o.Email = i.Email
  59  	o.FirstName = i.FirstName
  60  	o.LastName = i.LastName
  61  	o.Phone = i.Phone
  62  	o.State = i.State
  63  	o.TaxID = i.TaxID
  64  	o.Zip = i.Zip
  65  
  66  	return o
  67  }
  68  
  69  // UnmarshalJSON implements the json.Unmarshaler interface
  70  func (i *Account) UnmarshalJSON(b []byte) error {
  71  	type Mask Account
  72  
  73  	p := struct {
  74  		*Mask
  75  
  76  		ActiveSince *parseabletime.ParseableTime `json:"active_since"`
  77  	}{
  78  		Mask: (*Mask)(i),
  79  	}
  80  
  81  	if err := json.Unmarshal(b, &p); err != nil {
  82  		return err
  83  	}
  84  
  85  	i.ActiveSince = (*time.Time)(p.ActiveSince)
  86  
  87  	return nil
  88  }
  89  
  90  // CreditCard information associated with the Account.
  91  type CreditCard struct {
  92  	LastFour string `json:"last_four"`
  93  	Expiry   string `json:"expiry"`
  94  }
  95  
  96  // GetAccount gets the contact and billing information related to the Account.
  97  func (c *Client) GetAccount(ctx context.Context) (*Account, error) {
  98  	return doGETRequest[Account](ctx, c, "account")
  99  }
 100  
 101  // UpdateAccount updates the Account
 102  func (c *Client) UpdateAccount(ctx context.Context, opts AccountUpdateOptions) (*Account, error) {
 103  	return doPUTRequest[Account](ctx, c, "account", opts)
 104  }
 105