-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(product_enablement): implement API
- Loading branch information
1 parent
1805966
commit dd93a95
Showing
7 changed files
with
424 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
version: 1 | ||
interactions: | ||
- request: | ||
body: "" | ||
form: {} | ||
headers: | ||
User-Agent: | ||
- FastlyGo/7.2.0 (+github.com/fastly/go-fastly; go1.18.5) | ||
url: https://api.fastly.com/enabled-products/brotli_compression/services/7i6HN3TK9wS159v2gPAZ8A | ||
method: DELETE | ||
response: | ||
body: "" | ||
headers: | ||
Accept-Ranges: | ||
- bytes | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 17 Feb 2023 15:08:02 GMT | ||
Fastly-Ratelimit-Remaining: | ||
- "998" | ||
Fastly-Ratelimit-Reset: | ||
- "1676649600" | ||
Status: | ||
- 204 No Content | ||
Strict-Transport-Security: | ||
- max-age=31536000 | ||
Vary: | ||
- Accept-Encoding | ||
Via: | ||
- 1.1 varnish, 1.1 varnish | ||
X-Cache: | ||
- MISS, MISS | ||
X-Cache-Hits: | ||
- 0, 0 | ||
X-Served-By: | ||
- cache-control-cp-aws-us-east-2-prod-7-CONTROL-AWS-UE2, cache-lhr7368-LHR | ||
X-Timer: | ||
- S1676646482.821386,VS0,VE327 | ||
status: 204 No Content | ||
code: 204 | ||
duration: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
version: 1 | ||
interactions: | ||
- request: | ||
body: ProductID=brotli_compression&ServiceID=7i6HN3TK9wS159v2gPAZ8A | ||
form: | ||
ProductID: | ||
- brotli_compression | ||
ServiceID: | ||
- 7i6HN3TK9wS159v2gPAZ8A | ||
headers: | ||
Content-Type: | ||
- application/x-www-form-urlencoded | ||
User-Agent: | ||
- FastlyGo/7.2.0 (+github.com/fastly/go-fastly; go1.18.5) | ||
url: https://api.fastly.com/enabled-products/brotli_compression/services/7i6HN3TK9wS159v2gPAZ8A | ||
method: PUT | ||
response: | ||
body: | | ||
{"product":{"id":"brotli_compression","object":"product"},"service":{"id":"7i6HN3TK9wS159v2gPAZ8A","object":"service"},"_links":{"self":"/enabled-products/brotli_compression/services/7i6HN3TK9wS159v2gPAZ8A"}} | ||
headers: | ||
Accept-Ranges: | ||
- bytes | ||
Content-Length: | ||
- "209" | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 17 Feb 2023 15:08:01 GMT | ||
Fastly-Ratelimit-Remaining: | ||
- "999" | ||
Fastly-Ratelimit-Reset: | ||
- "1676649600" | ||
Status: | ||
- 200 OK | ||
Strict-Transport-Security: | ||
- max-age=31536000 | ||
Vary: | ||
- Accept-Encoding | ||
Via: | ||
- 1.1 varnish, 1.1 varnish | ||
X-Cache: | ||
- MISS, MISS | ||
X-Cache-Hits: | ||
- 0, 0 | ||
X-Served-By: | ||
- cache-control-cp-aws-us-east-2-prod-1-CONTROL-AWS-UE2, cache-lhr7368-LHR | ||
X-Timer: | ||
- S1676646481.093534,VS0,VE491 | ||
status: 200 OK | ||
code: 200 | ||
duration: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
version: 1 | ||
interactions: | ||
- request: | ||
body: "" | ||
form: {} | ||
headers: | ||
User-Agent: | ||
- FastlyGo/7.2.0 (+github.com/fastly/go-fastly; go1.18.5) | ||
url: https://api.fastly.com/enabled-products/brotli_compression/services/7i6HN3TK9wS159v2gPAZ8A | ||
method: GET | ||
response: | ||
body: | | ||
{"type":"","title":"no product on service","status":400,"errors":null,"detail":""} | ||
headers: | ||
Accept-Ranges: | ||
- bytes | ||
Content-Length: | ||
- "83" | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 17 Feb 2023 15:08:02 GMT | ||
Status: | ||
- 400 Bad Request | ||
Strict-Transport-Security: | ||
- max-age=31536000 | ||
Vary: | ||
- Accept-Encoding | ||
Via: | ||
- 1.1 varnish, 1.1 varnish | ||
X-Cache: | ||
- MISS, MISS | ||
X-Cache-Hits: | ||
- 0, 0 | ||
X-Served-By: | ||
- cache-control-cp-aws-us-east-2-prod-1-CONTROL-AWS-UE2, cache-lhr7368-LHR | ||
X-Timer: | ||
- S1676646482.180149,VS0,VE194 | ||
status: 400 Bad Request | ||
code: 400 | ||
duration: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
version: 1 | ||
interactions: | ||
- request: | ||
body: "" | ||
form: {} | ||
headers: | ||
User-Agent: | ||
- FastlyGo/7.2.0 (+github.com/fastly/go-fastly; go1.18.5) | ||
url: https://api.fastly.com/enabled-products/brotli_compression/services/7i6HN3TK9wS159v2gPAZ8A | ||
method: GET | ||
response: | ||
body: | | ||
{"product":{"id":"brotli_compression","object":"product"},"service":{"id":"7i6HN3TK9wS159v2gPAZ8A","object":"service"},"_links":{"self":"/enabled-products/brotli_compression/services/7i6HN3TK9wS159v2gPAZ8A"}} | ||
headers: | ||
Accept-Ranges: | ||
- bytes | ||
Content-Length: | ||
- "209" | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 17 Feb 2023 15:08:01 GMT | ||
Status: | ||
- 200 OK | ||
Strict-Transport-Security: | ||
- max-age=31536000 | ||
Vary: | ||
- Accept-Encoding | ||
Via: | ||
- 1.1 varnish, 1.1 varnish | ||
X-Cache: | ||
- MISS, MISS | ||
X-Cache-Hits: | ||
- 0, 0 | ||
X-Served-By: | ||
- cache-control-cp-aws-us-east-2-prod-2-CONTROL-AWS-UE2, cache-lhr7368-LHR | ||
X-Timer: | ||
- S1676646482.611546,VS0,VE190 | ||
status: 200 OK | ||
code: 200 | ||
duration: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package fastly | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ProductEnablement represents a response from the Fastly API. | ||
type ProductEnablement struct { | ||
Product ProductEnablementNested `mapstructure:"product"` | ||
Service ProductEnablementNested `mapstructure:"service"` | ||
} | ||
|
||
type ProductEnablementNested struct { | ||
ID string `mapstructure:"id,omitempty"` | ||
Object string `mapstructure:"object,omitempty"` | ||
} | ||
|
||
// Product is a base for the different product variants. | ||
type Product int64 | ||
|
||
func (p Product) String() string { | ||
switch p { | ||
case ProductBrotliCompression: | ||
return "brotli_compression" | ||
case ProductDomainInspector: | ||
return "domain_inspector" | ||
case ProductFanout: | ||
return "fanout" | ||
case ProductImageOptimizer: | ||
return "image_optimizer" | ||
case ProductOriginInspector: | ||
return "origin_inspector" | ||
case ProductWebSockets: | ||
return "websockets" | ||
} | ||
return "unknown" | ||
} | ||
|
||
const ( | ||
ProductUndefined Product = iota | ||
ProductBrotliCompression | ||
ProductDomainInspector | ||
ProductFanout | ||
ProductImageOptimizer | ||
ProductOriginInspector | ||
ProductWebSockets | ||
) | ||
|
||
// ProductEnablementInput is used as input to the various product API functions. | ||
type ProductEnablementInput struct { | ||
// ProductID is the ID of the product and is constrained by the Product type (required). | ||
ProductID Product | ||
// ServiceID is the ID of the service (required). | ||
ServiceID string | ||
} | ||
|
||
// GetProduct retrieves the details of the product enabled on the service. | ||
func (c *Client) GetProduct(i *ProductEnablementInput) (*ProductEnablement, error) { | ||
if i.ProductID == ProductUndefined { | ||
return nil, ErrMissingProductID | ||
} | ||
if i.ServiceID == "" { | ||
return nil, ErrMissingServiceID | ||
} | ||
|
||
path := fmt.Sprintf("/enabled-products/%s/services/%s", i.ProductID, i.ServiceID) | ||
resp, err := c.Get(path, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var h *ProductEnablement | ||
if err := decodeBodyMap(resp.Body, &h); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h, nil | ||
} | ||
|
||
// EnableProduct enables the specified product on the service. | ||
func (c *Client) EnableProduct(i *ProductEnablementInput) (*ProductEnablement, error) { | ||
if i.ProductID == ProductUndefined { | ||
return nil, ErrMissingProductID | ||
} | ||
if i.ServiceID == "" { | ||
return nil, ErrMissingServiceID | ||
} | ||
|
||
path := fmt.Sprintf("/enabled-products/%s/services/%s", i.ProductID, i.ServiceID) | ||
resp, err := c.PutForm(path, i, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var http3 *ProductEnablement | ||
if err := decodeBodyMap(resp.Body, &http3); err != nil { | ||
return nil, err | ||
} | ||
return http3, nil | ||
} | ||
|
||
// DisableProduct disables the specified product on the service. | ||
func (c *Client) DisableProduct(i *ProductEnablementInput) error { | ||
if i.ProductID == ProductUndefined { | ||
return ErrMissingProductID | ||
} | ||
if i.ServiceID == "" { | ||
return ErrMissingServiceID | ||
} | ||
|
||
path := fmt.Sprintf("/enabled-products/%s/services/%s", i.ProductID, i.ServiceID) | ||
_, err := c.Delete(path, nil) | ||
return err | ||
} |
Oops, something went wrong.