forked from OctopusDeploy/go-octopusdeploy
/
library_variable_set.go
164 lines (126 loc) · 4.57 KB
/
library_variable_set.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
package octopusdeploy
import (
"fmt"
"gopkg.in/go-playground/validator.v9"
"net/url"
"github.com/dghubble/sling"
)
type LibraryVariableSetService struct {
sling *sling.Sling
}
func NewLibraryVariableSetService(sling *sling.Sling) *LibraryVariableSetService {
return &LibraryVariableSetService{
sling: sling,
}
}
type LibraryVariableSets struct {
Items []LibraryVariableSet `json:"Items"`
PagedResults
}
type LibraryVariableSet struct {
ID string `json:"Id,omitempty"`
Name string `json:"Name" validate:"required"`
Description string `json:"Description,omitempty"`
VariableSetId string `json:"VariableSetId,omitempty"`
ContentType VariableSetContentType `json:"ContentType" validate:"required"`
Templates []ActionTemplateParameter `json:"Templates,omitempty"`
}
type VariableSetContentType string
const (
VariableSetContentType_Variables = VariableSetContentType("Variables")
VariableSetContentType_ScriptModule = VariableSetContentType("ScriptModule")
)
func NewLibraryVariableSet(name string) *LibraryVariableSet {
return &LibraryVariableSet{
Name: name,
ContentType: VariableSetContentType_Variables,
}
}
// ValidateLibraryVariableSetValues checks the values of a LibraryVariableSet object to see if they are suitable for
// sending to Octopus Deploy. Used when adding or updating libraryVariableSets.
func ValidateLibraryVariableSetValues(LibraryVariableSet *LibraryVariableSet) error {
validate := validator.New()
err := validate.Struct(LibraryVariableSet)
return err
}
// Get returns a single LibraryVariableSet by its Id in Octopus Deploy
func (s *LibraryVariableSetService) Get(libraryVariableSetID string) (*LibraryVariableSet, error) {
path := fmt.Sprintf("libraryVariableSets/%s", libraryVariableSetID)
resp, err := apiGet(s.sling, new(LibraryVariableSet), path)
if err != nil {
return nil, err
}
return resp.(*LibraryVariableSet), nil
}
// GetAll returns all libraryVariableSets in Octopus Deploy
func (s *LibraryVariableSetService) GetAll() (*[]LibraryVariableSet, error) {
return s.get("")
}
func (s *LibraryVariableSetService) get(query string) (*[]LibraryVariableSet, error) {
var p []LibraryVariableSet
path := "libraryvariablesets?take=2147483647"
if query != "" {
path = fmt.Sprintf("%s&%s", path, query)
}
loadNextPage := true
for loadNextPage { // Older Octopus Servers do not accept the take parameter, so the only choice is to page through them
resp, err := apiGet(s.sling, new(LibraryVariableSets), path)
if err != nil {
return nil, err
}
r := resp.(*LibraryVariableSets)
for _, item := range r.Items {
p = append(p, item)
}
path, loadNextPage = LoadNextPage(r.PagedResults)
}
return &p, nil
}
// GetByName gets an existing Library Variable Set by its name in Octopus Deploy
func (s *LibraryVariableSetService) GetByName(name string) (*LibraryVariableSet, error) {
var foundLibraryVariableSet LibraryVariableSet
libraryVariableSets, err := s.get(fmt.Sprintf("partialName=%s", url.PathEscape(name)))
if err != nil {
return nil, err
}
for _, libraryVariableSet := range *libraryVariableSets {
if libraryVariableSet.Name == name {
return &libraryVariableSet, nil
}
}
return &foundLibraryVariableSet, fmt.Errorf("no Library Variable Set found with name %s", name)
}
// Add adds an new libraryVariableSet in Octopus Deploy
func (s *LibraryVariableSetService) Add(libraryVariableSet *LibraryVariableSet) (*LibraryVariableSet, error) {
err := ValidateLibraryVariableSetValues(libraryVariableSet)
if err != nil {
return nil, err
}
resp, err := apiAdd(s.sling, libraryVariableSet, new(LibraryVariableSet), "libraryVariableSets")
if err != nil {
return nil, err
}
return resp.(*LibraryVariableSet), nil
}
// Delete deletes an existing libraryVariableSet in Octopus Deploy
func (s *LibraryVariableSetService) Delete(libraryVariableSetid string) error {
path := fmt.Sprintf("libraryVariableSets/%s", libraryVariableSetid)
err := apiDelete(s.sling, path)
if err != nil {
return err
}
return nil
}
// Update updates an existing libraryVariableSet in Octopus Deploy
func (s *LibraryVariableSetService) Update(libraryVariableSet *LibraryVariableSet) (*LibraryVariableSet, error) {
err := ValidateLibraryVariableSetValues(libraryVariableSet)
if err != nil {
return nil, err
}
path := fmt.Sprintf("libraryVariableSets/%s", libraryVariableSet.ID)
resp, err := apiUpdate(s.sling, libraryVariableSet, new(LibraryVariableSet), path)
if err != nil {
return nil, err
}
return resp.(*LibraryVariableSet), nil
}