-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
143 lines (123 loc) · 4.08 KB
/
context.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
package circleci
import "time"
const (
contextBasePath = "/context"
defaultContextType = "organization"
)
// ContextService is an interface for Context in Project API.
type ContextService interface {
List(slug string) (*ContextList, error)
Create(projectSlug, name string) (*Context, error)
Delete(id string) error
Get(id string) (*Context, error)
ListEnvVar(id string) (*ContextEnvVarList, error)
UpsertEnvVar(id, envVarName, envVarValue string) (*ContextEnvVar, error)
RemoveEnvVar(id, envVarName string) error
}
// ContextOp handles communication with the project related methods in the CircleCI API v2.
type ContextOp struct {
client *Client
}
var _ ContextService = (*ContextOp)(nil)
// Context represents information about a context in CircleCI.
type Context struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
// ContextList represents a list of Context variables.
type ContextList struct {
NextPageToken string `json:"next_page_token,omitempty"`
Items []*Context `json:"items,omitempty"`
}
// Owner represents Owner used in ContextCreate.
type Owner struct {
Slug string `json:"slug,omitempty"`
Type string `json:"type,omitempty"`
}
// ContextCreate represents payload to create Context.
type ContextCreate struct {
Name string `json:"name,omitempty"`
*Owner `json:"owner,omitempty"`
}
// ContextEnvVar represents information about an environment variable of Context.
type ContextEnvVar struct {
Variable string `json:"variable,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
ContextID string `json:"context_id,omitempty"`
}
// ContextEnvVarList represents a list of ContextEnvVar.
type ContextEnvVarList struct {
NextPageToken string `json:"next_page_token,omitempty"`
Items []*ContextEnvVar `json:"items,omitempty"`
}
// List list contexts for an owner.
// owner-slug is expected but not owner-id.
func (ps *ContextOp) List(slug string) (*ContextList, error) {
cl := &ContextList{}
path := contextBasePath + "?owner-slug=" + slug
err := ps.client.Get(path, cl, nil)
if err != nil {
return nil, err
}
return cl, nil
}
// Create adds a new environment variable or update existing variable on the specified project.
// Returns the added env var (the value will be masked).
func (ps *ContextOp) Create(projectSlug, name string) (*Context, error) {
c := &Context{}
err := ps.client.Post(contextBasePath, &ContextCreate{Name: name, Owner: &Owner{Slug: projectSlug, Type: defaultContextType}}, c)
if err != nil {
return nil, err
}
return c, nil
}
// Delete deletes the specified environment variable from the project.
func (ps *ContextOp) Delete(id string) error {
return ps.client.Delete(contextBasePath + "/" + id)
}
// Get gets environment variable.
// Returns the env vars (the value will be masked).
func (ps *ContextOp) Get(id string) (*Context, error) {
c := &Context{}
err := ps.client.Get(contextBasePath+"/"+id, c, nil)
if err != nil {
return nil, err
}
return c, nil
}
// ListEnvVar list contexts for an owner.
// Returns the env vars (the value will be masked).
func (ps *ContextOp) ListEnvVar(id string) (*ContextEnvVarList, error) {
cel := &ContextEnvVarList{}
err := ps.client.Get(contextEnvVarPath(id), cel, nil)
if err != nil {
return nil, err
}
return cel, nil
}
// UpsertEnvVar list contexts for an owner.
// Returns the env vars (the value will be masked).
func (ps *ContextOp) UpsertEnvVar(id, envVarName, envVarValue string) (*ContextEnvVar, error) {
ce := &ContextEnvVar{}
path := contextEnvVarPath(id) + "/" + envVarName
err := ps.client.Put(path,
struct {
Value string `json:"value"`
}{
Value: envVarValue,
},
ce)
if err != nil {
return nil, err
}
return ce, nil
}
// RemoveEnvVar list contexts for an owner.
// Returns the env vars (the value will be masked).
func (ps *ContextOp) RemoveEnvVar(id, envVarName string) error {
return ps.client.Delete(contextEnvVarPath(id) + "/" + envVarName)
}
func contextEnvVarPath(id string) string {
return contextBasePath + "/" + id + "/environment-variable"
}