Debian Codegraph @

fastly/service.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "sort"
6 "time"
7 )
8
9 // Service represents a single service for the Fastly account.
10 type Service struct {
11 ID string `mapstructure:"id"`
12 Name string `mapstructure:"name"`
13 Comment string `mapstructure:"comment"`
14 CustomerID string `mapstructure:"customer_id"`
15 CreatedAt *time.Time `mapstructure:"created_at"`
16 UpdatedAt *time.Time `mapstructure:"updated_at"`
17 DeletedAt *time.Time `mapstructure:"deleted_at"`
18 ActiveVersion uint `mapstructure:"version"`
19 Versions []*Version `mapstructure:"versions"`
20 }
21
22 type ServiceDetail struct {
23 ID string `mapstructure:"id"`
24 Name string `mapstructure:"name"`
25 Comment string `mapstructure:"comment"`
26 CustomerID string `mapstructure:"customer_id"`
27 ActiveVersion Version `mapstructure:"active_version"`
28 Version Version `mapstructure:"version"`
29 Versions []*Version `mapstructure:"versions"`
30 CreatedAt *time.Time `mapstructure:"created_at"`
31 UpdatedAt *time.Time `mapstructure:"updated_at"`
32 DeletedAt *time.Time `mapstructure:"deleted_at"`
33 }
34
35 type ServiceDomain struct {
36 Locked bool `mapstructure:"locked"`
37 Version int64 `mapstructure:"version"`
38 Name string `mapstructure:"name"`
39 DeletedAt *time.Time `mapstructure:"deleted_at"`
40 ServiceID string `mapstructure:"service_id"`
41 CreatedAt *time.Time `mapstructure:"created_at"`
42 Comment string `mapstructure:"comment"`
43 UpdatedAt *time.Time `mapstructure:"updated_at"`
44 }
45 type ServiceDomainsList []*ServiceDomain
46
47 // servicesByName is a sortable list of services.
48 type servicesByName []*Service
49
50 // Len, Swap, and Less implement the sortable interface.
51 func (s servicesByName) Len() int { return len(s) }
52 func (s servicesByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
53 func (s servicesByName) Less(i, j int) bool {
54 return s[i].Name < s[j].Name
55 }
56
57 // ListServicesInput is used as input to the ListServices function.
58 type ListServicesInput struct{}
59
60 // ListServices returns the full list of services for the current account.
61 func (c *Client) ListServices(i *ListServicesInput) ([]*Service, error) {
62 resp, err := c.Get("/service", nil)
63 if err != nil {
64 return nil, err
65 }
66
67 var s []*Service
68 if err := decodeJSON(&s, resp.Body); err != nil {
69 return nil, err
70 }
71 sort.Stable(servicesByName(s))
72 return s, nil
73 }
74
75 // CreateServiceInput is used as input to the CreateService function.
76 type CreateServiceInput struct {
77 Name string `form:"name,omitempty"`
78 Comment string `form:"comment,omitempty"`
79 }
80
81 // CreateService creates a new service with the given information.
82 func (c *Client) CreateService(i *CreateServiceInput) (*Service, error) {
83 resp, err := c.PostForm("/service", i, nil)
84 if err != nil {
85 return nil, err
86 }
87
88 var s *Service
89 if err := decodeJSON(&s, resp.Body); err != nil {
90 return nil, err
91 }
92 return s, nil
93 }
94
95 // GetServiceInput is used as input to the GetService function.
96 type GetServiceInput struct {
97 ID string
98 }
99
100 // GetService retrieves the service information for the service with the given
101 // id. If no service exists for the given id, the API returns a 400 response
102 // (not a 404).
103 func (c *Client) GetService(i *GetServiceInput) (*Service, error) {
104 if i.ID == "" {
105 return nil, ErrMissingID
106 }
107
108 path := fmt.Sprintf("/service/%s", i.ID)
109 resp, err := c.Get(path, nil)
110 if err != nil {
111 return nil, err
112 }
113
114 var s *Service
115 if err := decodeJSON(&s, resp.Body); err != nil {
116 return nil, err
117 }
118
119 return s, nil
120 }
121
122 // GetService retrieves the details for the service with the given id. If no
123 // service exists for the given id, the API returns a 400 response (not a 404).
124 func (c *Client) GetServiceDetails(i *GetServiceInput) (*ServiceDetail, error) {
125 if i.ID == "" {
126 return nil, ErrMissingID
127 }
128
129 path := fmt.Sprintf("/service/%s/details", i.ID)
130 resp, err := c.Get(path, nil)
131 if err != nil {
132 return nil, err
133 }
134
135 var s *ServiceDetail
136 if err := decodeJSON(&s, resp.Body); err != nil {
137 return nil, err
138 }
139
140 return s, nil
141 }
142
143 // UpdateServiceInput is used as input to the UpdateService function.
144 type UpdateServiceInput struct {
145 ID string
146
147 Name string `form:"name,omitempty"`
148 Comment string `form:"comment,omitempty"`
149 }
150
151 // UpdateService updates the service with the given input.
152 func (c *Client) UpdateService(i *UpdateServiceInput) (*Service, error) {
153 if i.ID == "" {
154 return nil, ErrMissingID
155 }
156
157 path := fmt.Sprintf("/service/%s", i.ID)
158 resp, err := c.PutForm(path, i, nil)
159 if err != nil {
160 return nil, err
161 }
162
163 var s *Service
164 if err := decodeJSON(&s, resp.Body); err != nil {
165 return nil, err
166 }
167 return s, nil
168 }
169
170 // DeleteServiceInput is used as input to the DeleteService function.
171 type DeleteServiceInput struct {
172 ID string
173 }
174
175 // DeleteService updates the service with the given input.
176 func (c *Client) DeleteService(i *DeleteServiceInput) error {
177 if i.ID == "" {
178 return ErrMissingID
179 }
180
181 path := fmt.Sprintf("/service/%s", i.ID)
182 resp, err := c.Delete(path, nil)
183 if err != nil {
184 return err
185 }
186
187 var r *statusResp
188 if err := decodeJSON(&r, resp.Body); err != nil {
189 return err
190 }
191 if !r.Ok() {
192 return fmt.Errorf("Not Ok")
193 }
194 return nil
195 }
196
197 // SearchServiceInput is used as input to the SearchService function.
198 type SearchServiceInput struct {
199 Name string
200 }
201
202 // SearchService gets a specific service by name. If no service exists by that
203 // name, the API returns a 400 response (not a 404).
204 func (c *Client) SearchService(i *SearchServiceInput) (*Service, error) {
205 if i.Name == "" {
206 return nil, ErrMissingName
207 }
208
209 resp, err := c.Get("/service/search", &RequestOptions{
210 Params: map[string]string{
211 "name": i.Name,
212 },
213 })
214 if err != nil {
215 return nil, err
216 }
217
218 var s *Service
219 if err := decodeJSON(&s, resp.Body); err != nil {
220 return nil, err
221 }
222
223 return s, nil
224 }
225
226 type ListServiceDomainInput struct {
227 ID string
228 }
229
230 // ListServiceDomains lists all domains associated with a given service
231 func (c *Client) ListServiceDomains(i *ListServiceDomainInput) (ServiceDomainsList, error) {
232 if i.ID == "" {
233 return nil, ErrMissingID
234 }
235 path := fmt.Sprintf("/service/%s/domain", i.ID)
236 resp, err := c.Get(path, nil)
237 if err != nil {
238 return nil, err
239 }
240
241 var ds ServiceDomainsList
242
243 if err := decodeJSON(&ds, resp.Body); err != nil {
244 return nil, err
245 }
246
247 return ds, nil
248 }