-
Notifications
You must be signed in to change notification settings - Fork 25
/
environment_service.go
208 lines (170 loc) · 6.66 KB
/
environment_service.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
package environments
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/dghubble/sling"
)
type EnvironmentService struct {
sortOrderPath string
summaryPath string
services.CanDeleteService
}
// NewEnvironmentService returns an EnvironmentService with a preconfigured
// client.
func NewEnvironmentService(sling *sling.Sling, uriTemplate string, sortOrderPath string, summaryPath string) *EnvironmentService {
return &EnvironmentService{
sortOrderPath: sortOrderPath,
summaryPath: summaryPath,
CanDeleteService: services.CanDeleteService{
Service: services.NewService(constants.ServiceEnvironmentService, sling, uriTemplate),
},
}
}
// Add creates a new environment.
//
// Deprecated: use environments.Add
func (s *EnvironmentService) Add(environment *Environment) (*Environment, error) {
if IsNil(environment) {
return nil, internal.CreateInvalidParameterError(constants.OperationAdd, constants.ParameterEnvironment)
}
path, err := services.GetAddPath(s, environment)
if err != nil {
return nil, err
}
resp, err := services.ApiAdd(s.GetClient(), environment, new(Environment), path)
if err != nil {
return nil, err
}
return resp.(*Environment), nil
}
// Get returns a collection of environments based on the criteria defined by
// its input query parameter. If an error occurs, an empty collection is
// returned along with the associated error.
//
// Deprecated: use environments.Get
func (s *EnvironmentService) Get(environmentsQuery EnvironmentsQuery) (*resources.Resources[*Environment], error) {
path, err := s.GetURITemplate().Expand(environmentsQuery)
if err != nil {
return &resources.Resources[*Environment]{}, err
}
response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Environment]), path)
if err != nil {
return &resources.Resources[*Environment]{}, err
}
return response.(*resources.Resources[*Environment]), nil
}
// GetAll returns all environments. If none can be found or an error occurs, it
// returns an empty collection.
//
// Deprecated: use environments.GetAll
func (s *EnvironmentService) GetAll() ([]*Environment, error) {
items := []*Environment{}
path, err := services.GetAllPath(s)
if err != nil {
return items, err
}
_, err = api.ApiGet(s.GetClient(), &items, path)
return items, err
}
// GetByID returns the environment that matches the input ID. If one cannot be
// found, it returns nil and an error.
//
// Deprecated: use environments.GetByID
func (s *EnvironmentService) GetByID(id string) (*Environment, error) {
if internal.IsEmpty(id) {
return nil, internal.CreateInvalidParameterError(constants.OperationGetByID, constants.ParameterID)
}
path, err := services.GetByIDPath(s, id)
if err != nil {
return nil, err
}
resp, err := api.ApiGet(s.GetClient(), new(Environment), path)
if err != nil {
return nil, err
}
return resp.(*Environment), nil
}
// GetByIDs returns the environments that match the input IDs.
func (s *EnvironmentService) GetByIDs(ids []string) ([]*Environment, error) {
if len(ids) == 0 {
return []*Environment{}, nil
}
path, err := services.GetByIDsPath(s, ids)
if err != nil {
return []*Environment{}, err
}
return services.GetPagedResponse[Environment](s, path)
}
// GetByName returns the environments with a matching partial name.
func (s *EnvironmentService) GetByName(name string) ([]*Environment, error) {
if internal.IsEmpty(name) {
return []*Environment{}, internal.CreateInvalidParameterError("GetByName", "name")
}
path, err := services.GetByNamePath(s, name)
if err != nil {
return []*Environment{}, err
}
return services.GetPagedResponse[Environment](s, path)
}
// GetByPartialName performs a lookup and returns enironments with a matching
// partial name.
func (s *EnvironmentService) GetByPartialName(partialName string) ([]*Environment, error) {
if internal.IsEmpty(partialName) {
return []*Environment{}, internal.CreateInvalidParameterError(constants.OperationGetByPartialName, constants.ParameterPartialName)
}
path, err := services.GetByPartialNamePath(s, partialName)
if err != nil {
return []*Environment{}, err
}
return services.GetPagedResponse[Environment](s, path)
}
// Update modifies an environment based on the one provided as input.
//
// Deprecated: use environments.Update
func (s *EnvironmentService) Update(environment *Environment) (*Environment, error) {
if environment == nil {
return nil, internal.CreateInvalidParameterError(constants.OperationUpdate, constants.ParameterEnvironment)
}
path, err := services.GetUpdatePath(s, environment)
if err != nil {
return nil, err
}
resp, err := services.ApiUpdate(s.GetClient(), environment, new(Environment), path)
if err != nil {
return nil, err
}
return resp.(*Environment), nil
}
// --- new ---
const template = "/api/{spaceId}/environments{/id}{?name,skip,ids,take,partialName}"
// Get returns a collection of environments based on the criteria defined by
// its input query parameter. If an error occurs, an empty collection is
// returned along with the associated error.
func Get(client newclient.Client, spaceID string, environmentsQuery EnvironmentsQuery) (*resources.Resources[*Environment], error) {
return newclient.GetByQuery[Environment](client, template, spaceID, environmentsQuery)
}
// Add creates a new environment.
func Add(client newclient.Client, environment *Environment) (*Environment, error) {
return newclient.Add[Environment](client, template, environment.SpaceID, environment)
}
// DeleteById deletes the environment based on the ID provided as input.
func DeleteByID(client newclient.Client, spaceID string, ID string) error {
return newclient.DeleteByID(client, template, spaceID, ID)
}
// GetByID returns the environment that matches the input ID. If one cannot be
// found, it returns nil and an error.
func GetByID(client newclient.Client, spaceID string, ID string) (*Environment, error) {
return newclient.GetByID[Environment](client, template, spaceID, ID)
}
// Update modifies an environment based on the one provided as input.
func Update(client newclient.Client, environment *Environment) (*Environment, error) {
return newclient.Update[Environment](client, template, environment.SpaceID, environment.ID, environment)
}
// GetAll returns all environments. If an error occurs, it returns nil.
func GetAll(client newclient.Client, spaceID string) ([]*Environment, error) {
return newclient.GetAll[Environment](client, template, spaceID)
}