This repository has been archived by the owner on May 25, 2023. It is now read-only.
/
api-func-variables.go
218 lines (199 loc) · 6.71 KB
/
api-func-variables.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Package cmd Copyright 2021 VMware, Inc.
SPDX-License-Identifier: BSD-2-Clause
*/
package cmd
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/go-resty/resty/v2"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
func getVariable(id, name, project, exportPath string) ([]*CodeStreamVariableResponse, error) {
var arrVariables []*CodeStreamVariableResponse
//var qParams = make(map[string]string)
client := resty.New()
// Get by ID
if id != "" {
v, e := getVariableByID(id)
arrVariables = append(arrVariables, v)
return arrVariables, e
}
if name != "" && project != "" {
qParams["$filter"] = "((name eq '" + name + "') and (project eq '" + project + "'))"
} else {
// Get by name
if name != "" {
qParams["$filter"] = "(name eq '" + name + "')"
}
// Get by project
if project != "" {
qParams["$filter"] = "(project eq '" + project + "')"
}
}
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Accept", "application/json").
SetResult(&documentsList{}).
SetAuthToken(targetConfig.accesstoken).
Get("https://" + targetConfig.server + "/pipeline/api/variables")
if queryResponse.IsError() {
return nil, queryResponse.Error().(error)
}
log.Debugln(queryResponse.Request.URL)
for _, value := range queryResponse.Result().(*documentsList).Documents {
c := CodeStreamVariableResponse{}
mapstructure.Decode(value, &c)
arrVariables = append(arrVariables, &c)
if exportPath != "" {
exportVariable(c, exportPath)
}
}
return arrVariables, err
}
// getVariableByID - get Code Stream Variable by ID
func getVariableByID(id string) (*CodeStreamVariableResponse, error) {
client := resty.New()
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Accept", "application/json").
SetResult(&CodeStreamVariableResponse{}).
SetAuthToken(targetConfig.accesstoken).
Get("https://" + targetConfig.server + "/pipeline/api/variables/" + id)
if queryResponse.IsError() {
log.Errorln("GET Variable failed", err)
}
return queryResponse.Result().(*CodeStreamVariableResponse), err
}
// createVariable - Create a new Code Stream Variable
func createVariable(name string, description string, variableType string, project string, value string) (*CodeStreamVariableResponse, error) {
client := resty.New()
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetBody(
CodeStreamVariableRequest{
Project: project,
Kind: "VARIABLE",
Name: name,
Description: description,
Type: variableType,
Value: value,
}).
SetHeader("Accept", "application/json").
SetResult(&CodeStreamVariableResponse{}).
SetError(&CodeStreamException{}).
SetAuthToken(targetConfig.accesstoken).
Post("https://" + targetConfig.server + "/pipeline/api/variables")
if queryResponse.IsError() {
return nil, errors.New(queryResponse.Error().(*CodeStreamException).Message)
}
return queryResponse.Result().(*CodeStreamVariableResponse), err
}
// updateVariable - Create a new Code Stream Variable
func updateVariable(id string, name string, description string, typename string, value string) (*CodeStreamVariableResponse, error) {
variable, _ := getVariableByID(id)
if name != "" {
variable.Name = name
}
if description != "" {
variable.Description = description
}
if typename != "" {
variable.Type = typename
}
if value != "" {
variable.Value = value
}
client := resty.New()
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetBody(variable).
SetHeader("Accept", "application/json").
SetResult(&CodeStreamVariableResponse{}).
SetError(&CodeStreamException{}).
SetAuthToken(targetConfig.accesstoken).
Put("https://" + targetConfig.server + "/pipeline/api/variables/" + id)
if queryResponse.IsError() {
return nil, errors.New(queryResponse.Error().(*CodeStreamException).Message)
}
return queryResponse.Result().(*CodeStreamVariableResponse), err
}
// deleteVariable - Delete a Code Stream Variable
func deleteVariable(id string) (*CodeStreamVariableResponse, error) {
client := resty.New()
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Accept", "application/json").
SetResult(&CodeStreamVariableResponse{}).
SetAuthToken(targetConfig.accesstoken).
Delete("https://" + targetConfig.server + "/pipeline/api/variables/" + id)
if queryResponse.IsError() {
return nil, queryResponse.Error().(error)
}
return queryResponse.Result().(*CodeStreamVariableResponse), err
}
func deleteVariableByProject(project string) ([]*CodeStreamVariableResponse, error) {
var deletedVariables []*CodeStreamVariableResponse
Variables, err := getVariable("", "", project, "")
if err != nil {
return nil, err
}
confirm := askForConfirmation("This will attempt to delete " + fmt.Sprint(len(Variables)) + " variables in " + project + ", are you sure?")
if confirm {
for _, Variable := range Variables {
deletedVariable, err := deleteVariable(Variable.ID)
if err != nil {
log.Warnln("Unable to delete "+Variable.Name, err)
}
deletedVariables = append(deletedVariables, deletedVariable)
}
return deletedVariables, nil
} else {
return nil, errors.New("user declined")
}
}
// exportVariable - Export a variable to YAML
func exportVariable(variable interface{}, exportPath string) {
var exportFile string
// variable will be a CodeStreamVariableResponse, so lets remap to CodeStreamVariableRequest
c := CodeStreamVariableRequest{}
mapstructure.Decode(variable, &c)
yaml, err := yaml.Marshal(c)
if err != nil {
log.Errorln("Unable to export variable ", c.Name)
}
if filepath.Ext(exportPath) != ".yaml" {
exportFile = filepath.Join(exportPath, "variables.yaml")
} else {
exportFile = exportPath
}
file, err := os.OpenFile(exportFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatal(err)
}
defer file.Close()
file.WriteString("---\n" + string(yaml))
}
// importVariables - Import variables from the filePath
func importVariables(filePath string) []CodeStreamVariableRequest {
var returnVariables []CodeStreamVariableRequest
filename, _ := filepath.Abs(filePath)
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
reader := bytes.NewReader(yamlFile)
decoder := yaml.NewDecoder(reader)
var request CodeStreamVariableRequest
for decoder.Decode(&request) == nil {
returnVariables = append(returnVariables, request)
}
return returnVariables
}