/
event.go
139 lines (121 loc) · 3.19 KB
/
event.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package event
import (
"errors"
"fmt"
"time"
"github.com/hashicorp/consul-terraform-sync/logging"
"github.com/hashicorp/go-uuid"
)
const (
logSystemName = "event"
)
// Event captures the series of actions that needs to happen to update network
// infrastructure for a given task when it receives a service change from Consul.
// An event should encompass: rendering the task’s templates, creating/updating
// resources, and executing any handlers.
type Event struct {
ID string `json:"id"`
Success bool `json:"success"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
TaskName string `json:"task_name"`
EventError *Error `json:"error"`
// Config is deprecated in v0.5. This is configuration details about the
// task rather than status information. Users should switch to using the
// Get Task API to request the task's config information.
// - Config should be removed in 0.8
Config *Config `json:"config"`
}
// Error captures an event's error information
type Error struct {
// Code string `json:"code"` TODO: future work
Message string `json:"message"`
}
// Config provides details on an event's task configuration. It is deprecated
// in v0.5 and should be removed in 0.8
type Config struct {
Providers []string `json:"providers"`
Services []string `json:"services"`
Source string `json:"source"`
}
// NewEvent configures a new event with a task name and any relevant information
// that the task is configured with
func NewEvent(taskName string, config *Config) (*Event, error) {
if taskName == "" {
return nil, errors.New("error creating new event: taskname cannot be empty")
}
id, err := uuid.GenerateUUID()
if err != nil {
return nil, err
}
return &Event{
ID: id,
TaskName: taskName,
Config: config,
}, nil
}
// Start sets the start time on an event. Can only be called once.
func (e *Event) Start() {
if !e.StartTime.IsZero() {
logging.Global().Named(logSystemName).Warn("event already started. unable to restart")
return
}
e.StartTime = time.Now()
}
// End sets the end time and captures any end results e.g. error, success status.
// Can only be called once
func (e *Event) End(err error) {
if !e.EndTime.IsZero() {
logging.Global().Named(logSystemName).Warn("event already ended. unable to re-end")
return
}
e.EndTime = time.Now()
if err == nil {
e.Success = true
return
}
e.Success = false
e.EventError = &Error{
Message: err.Error(),
}
}
// GoString defines the printable version of this struct.
func (c *Config) GoString() string {
if c == nil {
return "(*Config)(nil)"
}
return fmt.Sprintf("&Config{"+
"Providers:%s, "+
"Services:%s, "+
"Source:%s"+
"}",
c.Providers,
c.Services,
c.Source,
)
}
// GoString defines the printable version of this struct.
func (e *Event) GoString() string {
if e == nil {
return "(*Event)(nil)"
}
return fmt.Sprintf("&Event{"+
"ID:%s, "+
"TaskName:%s, "+
"Success:%t, "+
"StartTime:%s, "+
"EndTime:%s, "+
"EventError:%s, "+
"Config:%s"+
"}",
e.ID,
e.TaskName,
e.Success,
e.StartTime,
e.EndTime,
e.EventError,
e.Config.GoString(),
)
}