account_promo_credits.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 // Promotion represents a Promotion object
12 type Promotion struct {
13 // The amount available to spend per month.
14 CreditMonthlyCap string `json:"credit_monthly_cap"`
15
16 // The total amount of credit left for this promotion.
17 CreditRemaining string `json:"credit_remaining"`
18
19 // A detailed description of this promotion.
20 Description string `json:"description"`
21
22 // When this promotion's credits expire.
23 ExpirationDate *time.Time `json:"-"`
24
25 // The location of an image for this promotion.
26 ImageURL string `json:"image_url"`
27
28 // The service to which this promotion applies.
29 ServiceType string `json:"service_type"`
30
31 // Short details of this promotion.
32 Summary string `json:"summary"`
33
34 // The amount of credit left for this month for this promotion.
35 ThisMonthCreditRemaining string `json:"this_month_credit_remaining"`
36 }
37
38 // PromoCodeCreateOptions fields are those accepted by AddPromoCode
39 type PromoCodeCreateOptions struct {
40 // The Promo Code.
41 PromoCode string `json:"promo_code"`
42 }
43
44 // UnmarshalJSON implements the json.Unmarshaler interface
45 func (i *Promotion) UnmarshalJSON(b []byte) error {
46 type Mask Promotion
47
48 p := struct {
49 *Mask
50
51 ExpirationDate *parseabletime.ParseableTime `json:"date"`
52 }{
53 Mask: (*Mask)(i),
54 }
55
56 if err := json.Unmarshal(b, &p); err != nil {
57 return err
58 }
59
60 i.ExpirationDate = (*time.Time)(p.ExpirationDate)
61
62 return nil
63 }
64
65 // AddPromoCode adds the provided promo code to the account
66 func (c *Client) AddPromoCode(ctx context.Context, opts PromoCodeCreateOptions) (*Promotion, error) {
67 return doPOSTRequest[Promotion](ctx, c, "account/promo-codes", opts)
68 }
69