generated from davidji99/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathattributes.go
132 lines (108 loc) · 5.17 KB
/
attributes.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
package api
import (
"fmt"
"github.com/davidji99/simpleresty"
"net/url"
)
// AttributesService handles communication with the attributes related
// methods of the Split.io APIv2.
//
// Reference: https://docs.split.io/reference/attributes-overview
type AttributesService service
// Attribute represents an attribute in Split.
type Attribute struct {
ID *string `json:"id"` // this is different from the usually computed ID.
OrganizationId *string `json:"organizationId"`
TrafficTypeID *string `json:"trafficTypeId"`
DisplayName *string `json:"displayName"`
Description *string `json:"description"`
DataType *string `json:"dataType"` // (Optional) The data type of the attribute used for display formatting, defaults to displaying the raw string. Must be one of: null, "string", "datetime", "number", "set"
IsSearchable *bool `json:"isSearchable"`
SuggestedValues []string `json:"suggestedValues"`
}
// AttributeRequest represents a request to create an attribute.
type AttributeRequest struct {
Identifier *string `json:"id,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
Description *string `json:"description,omitempty"`
TrafficTypeID *string `json:"trafficTypeId,omitempty"`
DataType *string `json:"dataType,omitempty"`
IsSearchable *bool `json:"isSearchable,omitempty"`
SuggestedValues []string `json:"suggestedValues,omitempty"`
}
// AttributeListQueryParams represents all query parameters available when listing attributes
type AttributeListQueryParams struct {
// Whether to paginate the response.
Paginate bool `url:"Paginate,omitempty"`
// Search prefix under which to look for attributes (ex. att returns attribute1, but not myAttribute).
// Search is case insensitive, and only available with pagination.
SearchPrefix string `url:"searchPrefix,omitempty"`
// Get results 'After' the marker passed into this parameter. Only available with pagination. Markers are obfuscated
// strings by design.
AfterMarker string `url:"afterMarker,omitempty"`
// Get results 'Before' the marker passed into this parameter. Only available with pagination. Markers are obfuscated
// strings by design.
BeforeMarker string `url:"beforeMarker,omitempty"`
// Limit the number of return objects. If nothing or something invalid is given, then this will be the default markerLimit
// for the query. If something greater than the maximum limit is passed in, this will be the maximum allowed markerLimit for this query.
MarkerLimit int `url:"markerLimit,omitempty"`
}
// List all attributes for a traffic type.
//
// Reference: https://docs.split.io/reference/get-attributes
func (a *AttributesService) List(workspaceID, trafficTypeID string, opts *AttributeListQueryParams) ([]*Attribute, *simpleresty.Response, error) {
var result []*Attribute
urlStr, urlStrErr := a.client.http.RequestURLWithQueryParams(fmt.Sprintf("/schema/ws/%s/trafficTypes/%s", workspaceID,
trafficTypeID), opts)
if urlStrErr != nil {
return nil, nil, urlStrErr
}
response, listErr := a.client.get(urlStr, &result, nil)
return result, response, listErr
}
// FindByID retrieves an attribute by its ID.
//
// This is a helper method as it is not possible to retrieve a single attribute.
func (a *AttributesService) FindByID(workspaceID, trafficTypeID, attributeID string, opts *AttributeListQueryParams) (*Attribute, *simpleresty.Response, error) {
attributes, listResponse, listErr := a.List(workspaceID, trafficTypeID, opts)
if listErr != nil {
return nil, listResponse, listErr
}
for _, a := range attributes {
if a.GetID() == attributeID {
return a, nil, nil
}
}
return nil, nil, fmt.Errorf("could not find attribute [%s]", attributeID)
}
// Create an attribute.
//
// Reference: https://docs.split.io/reference/save-attribute
func (a *AttributesService) Create(workspaceID, trafficTypeID string, opts *AttributeRequest) (*Attribute, *simpleresty.Response, error) {
var result Attribute
urlStr := a.client.http.RequestURL("/schema/ws/%s/trafficTypes/%s", workspaceID, trafficTypeID)
// Execute the request
response, createErr := a.client.post(urlStr, &result, opts)
return &result, response, createErr
}
// Update an attribute.
//
// Reference: https://docs.split.io/reference/update-attribute
func (a *AttributesService) Update(workspaceID, trafficTypeID, attributeID string, opts *AttributeRequest) (*Attribute, *simpleresty.Response, error) {
var result Attribute
attributeIdEncoded := url.QueryEscape(attributeID)
urlStr := a.client.http.RequestURL("/schema/ws/%s/trafficTypes/%s/%s", workspaceID, trafficTypeID, attributeIdEncoded)
// Execute the request
response, createErr := a.client.patch(urlStr, &result, opts)
return &result, response, createErr
}
// Delete an attribute.
//
// Reference: https://docs.split.io/reference/delete-attribute
func (a *AttributesService) Delete(workspaceID, trafficTypeID, attributeID string) (*simpleresty.Response, error) {
attributeIdEncoded := url.QueryEscape(attributeID)
urlStr := a.client.http.RequestURL("/schema/ws/%s/trafficTypes/%s/%s", workspaceID, trafficTypeID, attributeIdEncoded)
// Execute the request
response, deleteErr := a.client.delete(urlStr, nil, nil)
return response, deleteErr
}