-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathevents.go
167 lines (142 loc) · 5.82 KB
/
events.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
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package events
import (
"errors"
"fmt"
"log"
"strings"
"time"
cmdutil "github.com/aws/amazon-ec2-metadata-mock/pkg/cmd/cmdutil"
cfg "github.com/aws/amazon-ec2-metadata-mock/pkg/config"
e "github.com/aws/amazon-ec2-metadata-mock/pkg/error"
se "github.com/aws/amazon-ec2-metadata-mock/pkg/mock/events"
"github.com/spf13/cobra"
)
const (
cfgPrefix = "events."
// local flags
eventCodeFlagName = "code"
eventStateFlagName = "state"
notBeforeFlagName = "not-before"
notAfterFlagName = "not-after"
notBeforeDeadlineFlagName = "not-before-deadline"
// event codes
instanceReboot = "instance-reboot"
systemReboot = "system-reboot"
systemMaintenance = "system-maintenance"
instanceRetirement = "instance-retirement"
instanceStop = "instance-stop"
// event states
active = "active"
completed = "completed"
canceled = "canceled"
// default date diffs (in days) in metadata
notAfterDiff = 7
notBeforeDeadlineDiff = 9
)
var (
c cfg.Config
// Command represents the CLI command
Command *cobra.Command
// constraints
validEventCodes = []string{instanceReboot, systemReboot, systemMaintenance, instanceRetirement, instanceStop}
validEventStates = []string{active, completed, canceled}
constraints = []string{
"event-code can be one of the following: " + strings.Join(validEventCodes, ","),
"state can be one of the following: " + strings.Join(validEventStates, ","),
}
// defaults
defaultCfg = map[string]interface{}{
cfgPrefix + eventCodeFlagName: systemReboot,
cfgPrefix + eventStateFlagName: active,
cfgPrefix + notBeforeFlagName: time.Now().Format(time.RFC3339),
cfgPrefix + notAfterFlagName: time.Now().Add(time.Hour * 24 * notAfterDiff).Format(time.RFC3339),
cfgPrefix + notBeforeDeadlineFlagName: time.Now().Add(time.Hour * 24 * notBeforeDeadlineDiff).Format(time.RFC3339),
}
)
func init() {
cobra.OnInitialize(initConfig)
Command = newCmd()
}
func initConfig() {
cfg.LoadConfigFromDefaults(defaultCfg)
}
func newCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "events [--code CODE] [--state STATE] [--not-after] [--not-before-deadline]",
Aliases: []string{"se", "scheduledevents"},
PreRunE: preRun,
Example: fmt.Sprintf(" %s events -h \tevents help \n %s events -o instance-stop --state active -d\t\tmocks an active and upcoming scheduled event for instance stop with a deadline for the event start time", cmdutil.BinName, cmdutil.BinName),
Run: run,
Short: "Mock EC2 maintenance events",
Long: "Mock EC2 maintenance events",
}
// local flags
cmd.Flags().StringP(eventCodeFlagName, "o", "", "event code in the scheduled event (default: system-reboot)\nevent-code can be one of the following: "+strings.Join(validEventCodes, ","))
cmd.Flags().StringP(eventStateFlagName, "t", "", "state of the scheduled event (default: active)\nstate can be one of the following: "+strings.Join(validEventStates, ","))
cmd.Flags().StringP(notBeforeFlagName, "b", "", "the earliest start time for the scheduled event in RFC3339 format E.g. 2020-01-07T01:03:47Z (default: application start time in UTC)")
cmd.Flags().StringP(notAfterFlagName, "a", "", "the latest end time for the scheduled event in RFC3339 format E.g. 2020-01-07T01:03:47Z default: application start time + 7 days in UTC))")
cmd.Flags().StringP(notBeforeDeadlineFlagName, "l", "", "the deadline for starting the event in RFC3339 format E.g. 2020-01-07T01:03:47Z (default: application start time + 9 days in UTC)")
// bind local flags to config
cfg.BindFlagSetWithKeyPrefix(cmd.Flags(), cfgPrefix)
return cmd
}
// SetConfig sets the local config
func SetConfig(config cfg.Config) {
c = config
}
func preRun(cmd *cobra.Command, args []string) error {
if cfgErrors := ValidateLocalConfig(); cfgErrors != nil {
return errors.New(strings.Join(cfgErrors, ""))
}
return nil
}
// ValidateLocalConfig validates all local config and returns a slice of error messages
func ValidateLocalConfig() []string {
var errStrings []string
c := c.EventsConfig
// validate event code
if ok := cmdutil.Contains(validEventCodes, c.EventCode); !ok {
errStrings = append(errStrings, e.FlagValidationError{
FlagName: eventCodeFlagName,
Allowed: strings.Join(validEventCodes, ","),
InvalidValue: c.EventCode}.Error(),
)
}
// validate event status
if ok := cmdutil.Contains(validEventStates, c.EventState); !ok {
errStrings = append(errStrings, e.FlagValidationError{
FlagName: eventStateFlagName,
Allowed: strings.Join(validEventStates, ","),
InvalidValue: c.EventState}.Error(),
)
}
// validate time flags
if err := cmdutil.ValidateRFC3339TimeFormat(notBeforeFlagName, c.NotBefore); err != nil {
errStrings = append(errStrings, err.Error())
}
if err := cmdutil.ValidateRFC3339TimeFormat(notAfterFlagName, c.NotAfter); err != nil {
errStrings = append(errStrings, err.Error())
}
if err := cmdutil.ValidateRFC3339TimeFormat(notBeforeDeadlineFlagName, c.NotBeforeDeadline); err != nil {
errStrings = append(errStrings, err.Error())
}
return errStrings
}
func run(cmd *cobra.Command, args []string) {
log.Printf("Initiating %s for EC2 Events on port %s\n", cmdutil.BinName, c.Server.Port)
cmdutil.PrintFlags(cmd.Flags())
cmdutil.RegisterHandlers(cmd, c)
se.Mock(c)
}