-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathenvironment_service.go
More file actions
160 lines (130 loc) · 4.73 KB
/
Copy pathenvironment_service.go
File metadata and controls
160 lines (130 loc) · 4.73 KB
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
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/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.
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.
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.
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.
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 environments 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.
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
}