-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathproducts.go
145 lines (128 loc) · 4.53 KB
/
products.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package apigee
import (
"path"
"errors"
)
const productsPath = "apiproducts"
// ProductsService is an interface for interfacing with the Apigee Edge Admin API
// dealing with apiproducts.
type ProductsService interface {
List() ([]string, *Response, error)
Get(string) (*ApiProduct, *Response, error)
Create(ApiProduct) (*ApiProduct, *Response, error)
Update(ApiProduct) (*ApiProduct, *Response, error)
Delete(string) (*ApiProduct, *Response, error)
}
type ProductsServiceOp struct {
client *ApigeeClient
}
var _ ProductsService = &ProductsServiceOp{}
// ApiProduct contains information about an API Product within an Edge organization.
type ApiProduct struct {
Name string `json:"name,omitempty"`
ApiResources []string `json:"apiResources,omitempty"`
ApprovalType string `json:"approvalType,omitempty"`
Attributes Attributes `json:"attributes,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
CreatedAt Timestamp `json:"createdAt,omitempty"`
Description string `json:"description,omitempty"`
DisplayName string `json:"displayName,omitempty"`
LastModifiedBy string `json:"lastModifiedBy,omitempty"`
LastModifiedAt Timestamp `json:"lastModifiedAt,omitempty"`
Environments []string `json:"environments,omitempty"`
Proxies []string `json:"proxies,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
func reallyUpdateProduct(s ProductsServiceOp, product ApiProduct) (*ApiProduct, *Response, error) {
path := path.Join(productsPath, product.Name)
req, e := s.client.NewRequest("POST", path, product)
if e != nil {
return nil, nil, e
}
returnedProduct := ApiProduct{}
resp, e := s.client.Do(req, &returnedProduct)
if e != nil {
return nil, resp, e
}
return &returnedProduct, resp, e
}
func (s *ProductsServiceOp) Update(product ApiProduct) (*ApiProduct, *Response, error) {
if product.Name == "" {
return nil, nil, errors.New("must specify Name of ApiProduct to update")
}
if product.ApprovalType == "" || product.DisplayName == "" || product.Environments == nil {
// The request is lacking some required information.
// Must get the apiproduct first, to fill in these "required" parameters.
retrievedProduct, resp, e := s.Get(product.Name)
if e != nil {
return nil, resp, e
}
if product.ApprovalType == "" {
product.ApprovalType = retrievedProduct.ApprovalType
}
if product.DisplayName == "" {
product.DisplayName = retrievedProduct.DisplayName
}
if product.Environments == nil {
product.Environments = retrievedProduct.Environments
}
}
// We have all required information...
// If the caller has omitted the list of api proxies from the product,
// this call will update the product to have no proxies! Likewise
// attributes.
return reallyUpdateProduct(*s, product);
}
func (s *ProductsServiceOp) Create(product ApiProduct) (*ApiProduct, *Response, error) {
req, e := s.client.NewRequest("POST", productsPath, product)
if e != nil {
return nil, nil, e
}
returnedProduct := ApiProduct{}
resp, e := s.client.Do(req, &returnedProduct)
if e != nil {
return nil, resp, e
}
return &returnedProduct, resp, e
}
func (s *ProductsServiceOp) Delete(productName string) (*ApiProduct, *Response, error) {
path := path.Join(productsPath, productName)
req, e := s.client.NewRequest("DELETE", path, nil)
if e != nil {
return nil, nil, e
}
deletedProduct := ApiProduct{}
resp, e := s.client.Do(req, &deletedProduct)
if e != nil {
return nil, resp, e
}
return &deletedProduct, resp, e
}
// List retrieves the list of apiproduct names for the organization referred by the ApigeeClient.
func (s *ProductsServiceOp) List() ([]string, *Response, error) {
req, e := s.client.NewRequest("GET", productsPath, nil)
if e != nil {
return nil, nil, e
}
namelist := make([]string,0)
resp, e := s.client.Do(req, &namelist)
if e != nil {
return nil, resp, e
}
return namelist, resp, e
}
// Get retrieves the information about an API Product in an organization, information including
// the list of API Proxies, the scopes, the quota, and other attributes.
func (s *ProductsServiceOp) Get(productName string) (*ApiProduct, *Response, error) {
path := path.Join(productsPath, productName)
req, e := s.client.NewRequest("GET", path, nil)
if e != nil {
return nil, nil, e
}
returnedProduct := ApiProduct{}
resp, e := s.client.Do(req, &returnedProduct)
if e != nil {
return nil, resp, e
}
return &returnedProduct, resp, e
}