Debian Codegraph @

fastly/condition.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // Condition represents a condition response from the Fastly API.
11 type Condition struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Comment string `mapstructure:"comment"`
17 Statement string `mapstructure:"statement"`
18 Type string `mapstructure:"type"`
19 Priority int `mapstructure:"priority"`
20 CreatedAt *time.Time `mapstructure:"created_at"`
21 UpdatedAt *time.Time `mapstructure:"updated_at"`
22 DeletedAt *time.Time `mapstructure:"deleted_at"`
23 }
24
25 // conditionsByName is a sortable list of conditions.
26 type conditionsByName []*Condition
27
28 // Len, Swap, and Less implement the sortable interface.
29 func (s conditionsByName) Len() int { return len(s) }
30 func (s conditionsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
31 func (s conditionsByName) Less(i, j int) bool {
32 return s[i].Name < s[j].Name
33 }
34
35 // ListConditionsInput is used as input to the ListConditions function.
36 type ListConditionsInput struct {
37 // Service is the ID of the service (required).
38 Service string
39
40 // Version is the specific configuration version (required).
41 Version int
42 }
43
44 // ListConditions returns the list of conditions for the configuration version.
45 func (c *Client) ListConditions(i *ListConditionsInput) ([]*Condition, error) {
46 if i.Service == "" {
47 return nil, ErrMissingService
48 }
49
50 if i.Version == 0 {
51 return nil, ErrMissingVersion
52 }
53
54 path := fmt.Sprintf("/service/%s/version/%d/condition", i.Service, i.Version)
55 resp, err := c.Get(path, nil)
56 if err != nil {
57 return nil, err
58 }
59
60 var cs []*Condition
61 if err := decodeJSON(&cs, resp.Body); err != nil {
62 return nil, err
63 }
64 sort.Stable(conditionsByName(cs))
65 return cs, nil
66 }
67
68 // CreateConditionInput is used as input to the CreateCondition function.
69 type CreateConditionInput struct {
70 // Service is the ID of the service. Version is the specific configuration
71 // version. Both fields are required.
72 Service string
73 Version int
74
75 Name string `form:"name,omitempty"`
76 Statement string `form:"statement,omitempty"`
77 Type string `form:"type,omitempty"`
78 Priority int `form:"priority,omitempty"`
79 }
80
81 // CreateCondition creates a new Fastly condition.
82 func (c *Client) CreateCondition(i *CreateConditionInput) (*Condition, error) {
83 if i.Service == "" {
84 return nil, ErrMissingService
85 }
86
87 if i.Version == 0 {
88 return nil, ErrMissingVersion
89 }
90
91 path := fmt.Sprintf("/service/%s/version/%d/condition", i.Service, i.Version)
92 resp, err := c.PostForm(path, i, nil)
93 if err != nil {
94 return nil, err
95 }
96
97 var co *Condition
98 if err := decodeJSON(&co, resp.Body); err != nil {
99 return nil, err
100 }
101 return co, nil
102 }
103
104 // GetConditionInput is used as input to the GetCondition function.
105 type GetConditionInput struct {
106 // Service is the ID of the service. Version is the specific configuration
107 // version. Both fields are required.
108 Service string
109 Version int
110
111 // Name is the name of the condition to fetch.
112 Name string
113 }
114
115 // GetCondition gets the condition configuration with the given parameters.
116 func (c *Client) GetCondition(i *GetConditionInput) (*Condition, error) {
117 if i.Service == "" {
118 return nil, ErrMissingService
119 }
120
121 if i.Version == 0 {
122 return nil, ErrMissingVersion
123 }
124
125 if i.Name == "" {
126 return nil, ErrMissingName
127 }
128
129 path := fmt.Sprintf("/service/%s/version/%d/condition/%s", i.Service, i.Version, url.PathEscape(i.Name))
130 resp, err := c.Get(path, nil)
131 if err != nil {
132 return nil, err
133 }
134
135 var co *Condition
136 if err := decodeJSON(&co, resp.Body); err != nil {
137 return nil, err
138 }
139 return co, nil
140 }
141
142 // UpdateConditionInput is used as input to the UpdateCondition function.
143 type UpdateConditionInput struct {
144 // Service is the ID of the service. Version is the specific configuration
145 // version. Both fields are required.
146 Service string
147 Version int
148
149 // Name is the name of the condition to update.
150 Name string
151
152 Comment string `form:"comment,omitempty"`
153 Statement string `form:"statement,omitempty"`
154 Type string `form:"type,omitempty"`
155 Priority int `form:"priority,omitempty"`
156 }
157
158 // UpdateCondition updates a specific condition.
159 func (c *Client) UpdateCondition(i *UpdateConditionInput) (*Condition, error) {
160 if i.Service == "" {
161 return nil, ErrMissingService
162 }
163
164 if i.Version == 0 {
165 return nil, ErrMissingVersion
166 }
167
168 if i.Name == "" {
169 return nil, ErrMissingName
170 }
171
172 path := fmt.Sprintf("/service/%s/version/%d/condition/%s", i.Service, i.Version, url.PathEscape(i.Name))
173 resp, err := c.PutForm(path, i, nil)
174 if err != nil {
175 return nil, err
176 }
177
178 var co *Condition
179 if err := decodeJSON(&co, resp.Body); err != nil {
180 return nil, err
181 }
182 return co, nil
183 }
184
185 // DeleteConditionInput is the input parameter to DeleteCondition.
186 type DeleteConditionInput struct {
187 // Service is the ID of the service. Version is the specific configuration
188 // version. Both fields are required.
189 Service string
190 Version int
191
192 // Name is the name of the condition to delete (required).
193 Name string
194 }
195
196 // DeleteCondition deletes the given condition version.
197 func (c *Client) DeleteCondition(i *DeleteConditionInput) error {
198 if i.Service == "" {
199 return ErrMissingService
200 }
201
202 if i.Version == 0 {
203 return ErrMissingVersion
204 }
205
206 if i.Name == "" {
207 return ErrMissingName
208 }
209
210 path := fmt.Sprintf("/service/%s/version/%d/condition/%s", i.Service, i.Version, url.PathEscape(i.Name))
211 resp, err := c.Delete(path, nil)
212 if err != nil {
213 return err
214 }
215
216 var r *statusResp
217 if err := decodeJSON(&r, resp.Body); err != nil {
218 return err
219 }
220 if !r.Ok() {
221 return fmt.Errorf("Not Ok")
222 }
223 return nil
224 }