Skip to content

Commit

Permalink
feat(product_enablement): implement API
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Feb 17, 2023
1 parent 1805966 commit d4ddc73
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ var ErrManagedLoggingEnabled = errors.New("managed logging already enabled")
// requires a "Token" key, but one was not set.
var ErrMissingToken = NewFieldError("Token")

// ErrMissingProductID is an error that is returned when an input struct
// requires a "ProductID" key, but one was not set.
var ErrMissingProductID = NewFieldError("ProductID")

// Ensure HTTPError is, in fact, an error.
var _ error = (*HTTPError)(nil)

Expand Down
43 changes: 43 additions & 0 deletions fastly/fixtures/product_enablement/disable.yaml
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: ""
52 changes: 52 additions & 0 deletions fastly/fixtures/product_enablement/enable.yaml
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: ""
42 changes: 42 additions & 0 deletions fastly/fixtures/product_enablement/get-disabled.yaml
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: ""
42 changes: 42 additions & 0 deletions fastly/fixtures/product_enablement/get.yaml
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: ""
116 changes: 116 additions & 0 deletions fastly/product_enablement.go
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
}
Loading

0 comments on commit d4ddc73

Please sign in to comment.