-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodule.go
209 lines (173 loc) · 5.67 KB
/
module.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ Modules = (*modules)(nil)
// Modules describes all the module related methods that the Scalr API supports.
type Modules interface {
// List all the modules .
List(ctx context.Context, options ModuleListOptions) (*ModuleList, error)
// Create the module
Create(ctx context.Context, options ModuleCreateOptions) (*Module, error)
// Read a module by its ID.
Read(ctx context.Context, moduleID string) (*Module, error)
// ReadBySource Read the module by its Source.
ReadBySource(ctx context.Context, moduleSource string) (*Module, error)
// Delete a module by its ID.
Delete(ctx context.Context, moduleID string) error
}
// modules implements Modules.
type modules struct {
client *Client
}
type Module struct {
ID string `jsonapi:"primary,modules"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Name string `jsonapi:"attr,name"`
Provider string `jsonapi:"attr,provider"`
Source string `jsonapi:"attr,source"`
Description *string `jsonapi:"attr,description,omitempty"`
VCSRepo *ModuleVCSRepo `jsonapi:"attr,vcs-repo"`
Status ModuleStatus `jsonapi:"attr,status"`
// Relation
VcsProvider *VcsProvider `jsonapi:"relation,vcs-provider"`
Account *Account `jsonapi:"relation,account,omitempty"`
Environment *Environment `jsonapi:"relation,environment,omitempty"`
CreatedBy *User `jsonapi:"relation,created-by,omitempty"`
LatestModuleVersion *ModuleVersion `jsonapi:"relation,latest-module-version,omitempty"`
ModuleVersion *ModuleVersion `jsonapi:"relation,module-version,omitempty"`
}
// ModuleStatus represents a module state.
type ModuleStatus string
//List all available module statuses.
const (
ModuleNoVersionTags ModuleStatus = "no_version_tag"
ModulePending ModuleStatus = "pending"
ModuleSetupComplete ModuleStatus = "setup_complete"
ModuleErrored ModuleStatus = "errored"
)
// ModuleVCSRepo contains the configuration of a VCS integration.
type ModuleVCSRepo struct {
Identifier string `json:"identifier"`
Path *string `json:"path"`
TagPrefix *string `json:"tag-prefix,omitempty"`
}
// ModuleList represents a list of module.
type ModuleList struct {
*Pagination
Items []*Module
}
// ModuleListOptions represents the options for listing modules.
type ModuleListOptions struct {
ListOptions
Name *string `url:"filter[name],omitempty"`
Status *ModuleStatus `url:"filter[status],omitempty"`
Source *string `url:"filter[source],omitempty"`
Provider *string `url:"filter[provider],omitempty"`
Account *string `url:"filter[account],omitempty"`
Environment *string `url:"filter[environment],omitempty"`
}
// List all the modules
func (s *modules) List(ctx context.Context, options ModuleListOptions) (*ModuleList, error) {
req, err := s.client.newRequest("GET", "modules", &options)
if err != nil {
return nil, err
}
ml := &ModuleList{}
err = s.client.do(ctx, req, ml)
if err != nil {
return nil, err
}
return ml, nil
}
type ModuleCreateOptions struct {
//// For internal use only!
ID string `jsonapi:"primary,modules"`
// Settings for the module VCS repository.
VCSRepo *ModuleVCSRepo `jsonapi:"attr,vcs-repo"`
// Specifies the VcsProvider for module vcs-repo.
VcsProvider *VcsProvider `jsonapi:"relation,vcs-provider"`
// Specifies the Account for module
Account *Account `jsonapi:"relation,account,omitempty"`
// Specifies the Environment for module
Environment *Environment `jsonapi:"relation,environment,omitempty"`
}
func (o ModuleCreateOptions) valid() error {
if o.VCSRepo == nil {
return errors.New("vcs repo is required")
}
if o.VcsProvider == nil {
return errors.New("vcs provider is required")
}
return nil
}
// Create is used to create a new module.
func (s *modules) Create(ctx context.Context, options ModuleCreateOptions) (*Module, error) {
if err := options.valid(); err != nil {
return nil, err
}
//// Make sure we don't send a user provided ID.
options.ID = ""
req, err := s.client.newRequest("POST", "modules", &options)
if err != nil {
return nil, err
}
m := &Module{}
err = s.client.do(ctx, req, m)
if err != nil {
return nil, err
}
return m, nil
}
func (s *modules) Read(ctx context.Context, moduleID string) (*Module, error) {
if !validStringID(&moduleID) {
return nil, errors.New("invalid value for module ID")
}
u := fmt.Sprintf("modules/%s", url.QueryEscape(moduleID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
m := &Module{}
err = s.client.do(ctx, req, m)
if err != nil {
return nil, err
}
return m, nil
}
func (s *modules) ReadBySource(ctx context.Context, moduleSource string) (*Module, error) {
ms := &moduleSource
if !validString(ms) {
return nil, errors.New("invalid value for module source")
}
req, err := s.client.newRequest("GET", "modules", &ModuleListOptions{Source: ms})
if err != nil {
return nil, err
}
ml := &ModuleList{}
err = s.client.do(ctx, req, ml)
if err != nil {
return nil, err
}
if len(ml.Items) != 1 {
return nil, ResourceNotFoundError{Message: fmt.Sprintf("Module with source '%s' not found.", *ms)}
}
return ml.Items[0], nil
}
// Delete deletes a module by its ID.
func (s *modules) Delete(ctx context.Context, moduleID string) error {
if !validStringID(&moduleID) {
return errors.New("invalid value for module ID")
}
u := fmt.Sprintf("modules/%s", url.QueryEscape(moduleID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}