forked from fastly/go-fastly
/
product_enablement.go
118 lines (102 loc) · 2.89 KB
/
product_enablement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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"
case ProductUndefined:
return "unknown"
}
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
}