-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_schedule_rule.go
172 lines (137 loc) · 4.87 KB
/
run_schedule_rule.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ RunScheduleRules = (*runScheduleRules)(nil)
// RunScheduleRules describes all the run schedule rule related methods that the Scalr API supports.
type RunScheduleRules interface {
List(ctx context.Context, options RunScheduleRuleListOptions) (*RunScheduleRulesList, error)
Create(ctx context.Context, options RunScheduleRuleCreateOptions) (*RunScheduleRule, error)
Read(ctx context.Context, ruleID string) (*RunScheduleRule, error)
Delete(ctx context.Context, ruleID string) error
Update(ctx context.Context, ruleID string, options RunScheduleRuleUpdateOptions) (*RunScheduleRule, error)
}
// runScheduleRules implements RunScheduleRules.
type runScheduleRules struct {
client *Client
}
// RunScheduleRulesList represents a list of run schedule rules.
type RunScheduleRulesList struct {
*Pagination
Items []*RunScheduleRule
}
// ScheduleMode represents the run-type that will be scheduled.
type ScheduleMode string
const (
ScheduleModeApply ScheduleMode = "apply"
ScheduleModeDestroy ScheduleMode = "destroy"
ScheduleModeRefresh ScheduleMode = "refresh"
)
// RunScheduleRule represents a Scalr run schedule rule.
type RunScheduleRule struct {
ID string `jsonapi:"primary,run-schedule-rules"`
Schedule string `jsonapi:"attr,schedule"`
ScheduleMode ScheduleMode `jsonapi:"attr,schedule-mode"`
Workspace *Workspace `jsonapi:"relation,workspace,omitempty"`
}
// RunScheduleRuleListOptions represents the options for listing run schedule rules.
type RunScheduleRuleListOptions struct {
ListOptions
Workspace string `url:"filter[workspace],omitempty"`
Include string `url:"include,omitempty"`
}
// List all run schedule rules in the workspace.
func (s *runScheduleRules) List(ctx context.Context, options RunScheduleRuleListOptions) (*RunScheduleRulesList, error) {
req, err := s.client.newRequest("GET", "run-schedule-rules", &options)
if err != nil {
return nil, err
}
runScheduleRulesList := &RunScheduleRulesList{}
err = s.client.do(ctx, req, runScheduleRulesList)
if err != nil {
return nil, err
}
return runScheduleRulesList, nil
}
// RunScheduleRuleCreateOptions represents the options for creating a new run schedule rule.
type RunScheduleRuleCreateOptions struct {
ID string `jsonapi:"primary,run-schedule-rules"`
Schedule string `jsonapi:"attr,schedule"`
ScheduleMode ScheduleMode `jsonapi:"attr,schedule-mode"`
Workspace *Workspace `jsonapi:"relation,workspace,omitempty"`
}
// Create is used to create a new run schedule rule.
func (s *runScheduleRules) Create(ctx context.Context, options RunScheduleRuleCreateOptions) (*RunScheduleRule, error) {
options.ID = ""
req, err := s.client.newRequest("POST", "run-schedule-rules", &options)
if err != nil {
return nil, err
}
rule := &RunScheduleRule{}
err = s.client.do(ctx, req, rule)
if err != nil {
return nil, err
}
return rule, nil
}
// Read a run schedule rule by ID.
func (s *runScheduleRules) Read(ctx context.Context, ruleID string) (*RunScheduleRule, error) {
if !validStringID(&ruleID) {
return nil, errors.New("invalid value for run schedule rule ID")
}
urlPath := fmt.Sprintf("run-schedule-rules/%s", url.QueryEscape(ruleID))
options := struct {
Include string `url:"include"`
}{
Include: "workspace",
}
req, err := s.client.newRequest("GET", urlPath, options)
if err != nil {
return nil, err
}
rule := &RunScheduleRule{}
err = s.client.do(ctx, req, rule)
if err != nil {
return nil, err
}
return rule, nil
}
// RunScheduleRuleUpdateOptions represents the options for updating a run schedule rule.
type RunScheduleRuleUpdateOptions struct {
ID string `jsonapi:"primary,run-schedule-rules"`
Schedule *string `jsonapi:"attr,schedule,omitempty"`
ScheduleMode *ScheduleMode `jsonapi:"attr,schedule-mode,omitempty"`
}
// Update an existing run schedule rule.
func (s *runScheduleRules) Update(ctx context.Context, ruleID string, options RunScheduleRuleUpdateOptions) (*RunScheduleRule, error) {
if !validStringID(&ruleID) {
return nil, errors.New("invalid value for run schedule rule ID")
}
urlPath := fmt.Sprintf("run-schedule-rules/%s", url.QueryEscape(ruleID))
req, err := s.client.newRequest("PATCH", urlPath, &options)
if err != nil {
return nil, err
}
rule := &RunScheduleRule{}
err = s.client.do(ctx, req, rule)
if err != nil {
return nil, err
}
return rule, nil
}
// Delete deletes a run schedule rule by its ID.
func (s *runScheduleRules) Delete(ctx context.Context, ruleID string) error {
if !validStringID(&ruleID) {
return errors.New("invalid value for run schedule rule ID")
}
urlPath := fmt.Sprintf("run-schedule-rules/%s", url.QueryEscape(ruleID))
req, err := s.client.newRequest("DELETE", urlPath, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}