-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
120 lines (93 loc) · 3.09 KB
/
metadata.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
package edgecloud
import (
"context"
"fmt"
"net/http"
)
const (
metadataPath = "metadata"
metadataItemPath = "metadata_item"
)
type MetadataDetailed struct {
Key string `json:"key"`
Value string `json:"value"`
ReadOnly bool `json:"read_only"`
}
type Metadata map[string]string
// MetadataCreateRequest represent a metadata create struct.
type MetadataCreateRequest struct {
Metadata
}
type MetadataItemOptions struct {
Key string `url:"key,omitempty" validate:"omitempty"`
}
// MetadataRoot represents a Metadata root.
type MetadataRoot struct {
Count int
Metadata []MetadataDetailed `json:"results"`
}
// metadataList helper for same logic methods.
func metadataList(ctx context.Context, client *Client, id, resourcePath string) ([]MetadataDetailed, *Response, error) {
path := fmt.Sprintf("%s/%s/%s", client.addProjectRegionPath(resourcePath), id, metadataPath)
req, err := client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
metadata := new(MetadataRoot)
resp, err := client.Do(ctx, req, metadata)
if err != nil {
return nil, resp, err
}
return metadata.Metadata, resp, err
}
// metadataCreate helper for same logic methods.
func metadataCreate(ctx context.Context, client *Client, id, resourcePath string, metadata *Metadata) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", client.addProjectRegionPath(resourcePath), id, metadataPath)
req, err := client.NewRequest(ctx, http.MethodPost, path, metadata)
if err != nil {
return nil, err
}
return client.Do(ctx, req, nil)
}
// metadataUpdate helper for same logic methods.
func metadataUpdate(ctx context.Context, client *Client, id, resourcePath string, metadata *Metadata) (*Response, error) {
path := fmt.Sprintf("%s/%s/%s", client.addProjectRegionPath(resourcePath), id, metadataPath)
req, err := client.NewRequest(ctx, http.MethodPut, path, metadata)
if err != nil {
return nil, err
}
return client.Do(ctx, req, nil)
}
// metadataDeleteItem helper for same logic methods.
func metadataDeleteItem(ctx context.Context, client *Client, id, resourcePath string, opts *MetadataItemOptions) (*Response, error) {
path := client.addProjectRegionPath(resourcePath)
path = fmt.Sprintf("%s/%s/%s", path, id, metadataItemPath)
path, err := addOptions(path, opts)
if err != nil {
return nil, err
}
req, err := client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return client.Do(ctx, req, nil)
}
// metadataGetItem helper for same logic methods.
func metadataGetItem(ctx context.Context, client *Client, id, resourcePath string, opts *MetadataItemOptions) (*MetadataDetailed, *Response, error) {
path := client.addProjectRegionPath(resourcePath)
path = fmt.Sprintf("%s/%s/%s", path, id, metadataItemPath)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
metadata := new(MetadataDetailed)
resp, err := client.Do(ctx, req, metadata)
if err != nil {
return nil, resp, err
}
return metadata, resp, err
}