-
Notifications
You must be signed in to change notification settings - Fork 604
/
access_tag.go
85 lines (71 loc) · 2.18 KB
/
access_tag.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
type AccessTag struct {
Name string `json:"name,omitempty"`
AppCount int `json:"app_count,omitempty"`
}
type AccessTagListResponse struct {
Response
Result []AccessTag `json:"result"`
ResultInfo `json:"result_info"`
}
type AccessTagResponse struct {
Response
Result AccessTag `json:"result"`
}
type ListAccessTagsParams struct{}
type CreateAccessTagParams struct {
Name string `json:"name,omitempty"`
}
func (api *API) ListAccessTags(ctx context.Context, rc *ResourceContainer, params ListAccessTagsParams) ([]AccessTag, error) {
uri := buildURI(fmt.Sprintf("/%s/%s/access/tags", rc.Level, rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessTag{}, err
}
var TagsResponse AccessTagListResponse
err = json.Unmarshal(res, &TagsResponse)
if err != nil {
return []AccessTag{}, err
}
return TagsResponse.Result, nil
}
func (api *API) GetAccessTag(ctx context.Context, rc *ResourceContainer, tagName string) (AccessTag, error) {
uri := fmt.Sprintf("/%s/%s/access/tags/%s", rc.Level, rc.Identifier, tagName)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessTag{}, err
}
var TagResponse AccessTagResponse
err = json.Unmarshal(res, &TagResponse)
if err != nil {
return AccessTag{}, err
}
return TagResponse.Result, nil
}
func (api *API) CreateAccessTag(ctx context.Context, rc *ResourceContainer, params CreateAccessTagParams) (AccessTag, error) {
uri := fmt.Sprintf("/%s/%s/access/tags", rc.Level, rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return AccessTag{}, err
}
var TagResponse AccessTagResponse
err = json.Unmarshal(res, &TagResponse)
if err != nil {
return AccessTag{}, err
}
return TagResponse.Result, nil
}
func (api *API) DeleteAccessTag(ctx context.Context, rc *ResourceContainer, tagName string) error {
uri := fmt.Sprintf("/%s/%s/access/tags/%s", rc.Level, rc.Identifier, tagName)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}