-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodule_version.go
106 lines (87 loc) · 2.76 KB
/
module_version.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ ModuleVersions = (*moduleVersions)(nil)
// ModuleVersions describes all the run related methods that the Scalr API supports.
type ModuleVersions interface {
// List all the module versions within a module.
List(ctx context.Context, options ModuleVersionListOptions) (*ModuleVersionList, error)
// Read a module version by its ID.
Read(ctx context.Context, moduleVersionID string) (*ModuleVersion, error)
}
// moduleVersions implements ModuleVersions.
type moduleVersions struct {
client *Client
}
// ModuleVersionList represents a list of module versions.
type ModuleVersionList struct {
*Pagination
Items []*ModuleVersion
}
// ModuleVersion represents a Scalr module version.
type ModuleVersion struct {
ID string `jsonapi:"primary,module-versions"`
IsRootModule bool `jsonapi:"attr,is-root-module"`
Status ModuleVersionStatus `jsonapi:"attr,status"`
Version string `jsonapi:"attr,version"`
}
type ModuleVersionStatus string
const (
ModuleVersionNotUploaded ModuleVersionStatus = "not_uploaded"
ModuleVersionPending ModuleVersionStatus = "pending"
ModuleVersionOk ModuleVersionStatus = "ok"
ModuleVersionErrored ModuleVersionStatus = "errored"
ModuleVersionPendingDelete ModuleVersionStatus = "pending_delete"
)
type ModuleVersionListOptions struct {
ListOptions
Module string `url:"filter[module]"`
Status *string `url:"filter[status],omitempty"`
Version *string `url:"filter[version],omitempty"`
Sort *string `url:"sort,omitempty"`
Include string `url:"include,omitempty"`
}
func (o ModuleVersionListOptions) validate() error {
if o.Module == "" {
return errors.New("filter[module] is required")
}
return nil
}
// Read a module version by its ID.
func (s *moduleVersions) Read(ctx context.Context, moduleVersionID string) (*ModuleVersion, error) {
if !validStringID(&moduleVersionID) {
return nil, errors.New("invalid value for module version ID")
}
u := fmt.Sprintf("module-versions/%s", url.QueryEscape(moduleVersionID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
m := &ModuleVersion{}
err = s.client.do(ctx, req, m)
if err != nil {
return nil, err
}
return m, err
}
// List the list of module versions
func (s *moduleVersions) List(ctx context.Context, options ModuleVersionListOptions) (*ModuleVersionList, error) {
if err := options.validate(); err != nil {
return nil, err
}
req, err := s.client.newRequest("GET", "module-versions", &options)
if err != nil {
return nil, err
}
mv := &ModuleVersionList{}
err = s.client.do(ctx, req, mv)
if err != nil {
return nil, err
}
return mv, nil
}