Debian Codegraph @

fastly/domain.go

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