stackscripts.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 // Stackscript represents a Linode StackScript
12 type Stackscript struct {
13 ID int `json:"id"`
14 Username string `json:"username"`
15 Label string `json:"label"`
16 Description string `json:"description"`
17 Ordinal int `json:"ordinal"`
18 LogoURL string `json:"logo_url"`
19 Images []string `json:"images"`
20 DeploymentsTotal int `json:"deployments_total"`
21 DeploymentsActive int `json:"deployments_active"`
22 IsPublic bool `json:"is_public"`
23 Mine bool `json:"mine"`
24 Created *time.Time `json:"-"`
25 Updated *time.Time `json:"-"`
26 RevNote string `json:"rev_note"`
27 Script string `json:"script"`
28 UserDefinedFields *[]StackscriptUDF `json:"user_defined_fields"`
29 UserGravatarID string `json:"user_gravatar_id"`
30 }
31
32 // StackscriptUDF define a single variable that is accepted by a Stackscript
33 type StackscriptUDF struct {
34 // A human-readable label for the field that will serve as the input prompt for entering the value during deployment.
35 Label string `json:"label"`
36
37 // The name of the field.
38 Name string `json:"name"`
39
40 // An example value for the field.
41 Example string `json:"example"`
42
43 // A list of acceptable single values for the field.
44 OneOf string `json:"oneOf,omitempty"`
45
46 // A list of acceptable values for the field in any quantity, combination or order.
47 ManyOf string `json:"manyOf,omitempty"`
48
49 // The default value. If not specified, this value will be used.
50 Default string `json:"default,omitempty"`
51 }
52
53 // StackscriptCreateOptions fields are those accepted by CreateStackscript
54 type StackscriptCreateOptions struct {
55 Label string `json:"label"`
56 Description string `json:"description"`
57 Images []string `json:"images"`
58 IsPublic bool `json:"is_public"`
59 RevNote string `json:"rev_note"`
60 Script string `json:"script"`
61 }
62
63 // StackscriptUpdateOptions fields are those accepted by UpdateStackscript
64 type StackscriptUpdateOptions StackscriptCreateOptions
65
66 // UnmarshalJSON implements the json.Unmarshaler interface
67 func (i *Stackscript) UnmarshalJSON(b []byte) error {
68 type Mask Stackscript
69
70 p := struct {
71 *Mask
72
73 Created *parseabletime.ParseableTime `json:"created"`
74 Updated *parseabletime.ParseableTime `json:"updated"`
75 }{
76 Mask: (*Mask)(i),
77 }
78
79 if err := json.Unmarshal(b, &p); err != nil {
80 return err
81 }
82
83 i.Created = (*time.Time)(p.Created)
84 i.Updated = (*time.Time)(p.Updated)
85
86 return nil
87 }
88
89 // GetCreateOptions converts a Stackscript to StackscriptCreateOptions for use in CreateStackscript
90 func (i Stackscript) GetCreateOptions() StackscriptCreateOptions {
91 return StackscriptCreateOptions{
92 Label: i.Label,
93 Description: i.Description,
94 Images: i.Images,
95 IsPublic: i.IsPublic,
96 RevNote: i.RevNote,
97 Script: i.Script,
98 }
99 }
100
101 // GetUpdateOptions converts a Stackscript to StackscriptUpdateOptions for use in UpdateStackscript
102 func (i Stackscript) GetUpdateOptions() StackscriptUpdateOptions {
103 return StackscriptUpdateOptions{
104 Label: i.Label,
105 Description: i.Description,
106 Images: i.Images,
107 IsPublic: i.IsPublic,
108 RevNote: i.RevNote,
109 Script: i.Script,
110 }
111 }
112
113 // ListStackscripts lists Stackscripts
114 func (c *Client) ListStackscripts(ctx context.Context, opts *ListOptions) ([]Stackscript, error) {
115 return getPaginatedResults[Stackscript](ctx, c, "linode/stackscripts", opts)
116 }
117
118 // GetStackscript gets the Stackscript with the provided ID
119 func (c *Client) GetStackscript(ctx context.Context, scriptID int) (*Stackscript, error) {
120 e := formatAPIPath("linode/stackscripts/%d", scriptID)
121 return doGETRequest[Stackscript](ctx, c, e)
122 }
123
124 // CreateStackscript creates a StackScript
125 func (c *Client) CreateStackscript(ctx context.Context, opts StackscriptCreateOptions) (*Stackscript, error) {
126 return doPOSTRequest[Stackscript](ctx, c, "linode/stackscripts", opts)
127 }
128
129 // UpdateStackscript updates the StackScript with the specified id
130 func (c *Client) UpdateStackscript(ctx context.Context, scriptID int, opts StackscriptUpdateOptions) (*Stackscript, error) {
131 e := formatAPIPath("linode/stackscripts/%d", scriptID)
132 return doPUTRequest[Stackscript](ctx, c, e, opts)
133 }
134
135 // DeleteStackscript deletes the StackScript with the specified id
136 func (c *Client) DeleteStackscript(ctx context.Context, scriptID int) error {
137 e := formatAPIPath("linode/stackscripts/%d", scriptID)
138 return doDELETERequest(ctx, c, e)
139 }
140