pulumi/pkg/engine/events.go

167 lines
3.8 KiB
Go
Raw Normal View History

// Copyright 2017, Pulumi Corporation. All rights reserved.
package engine
import (
"fmt"
"time"
"github.com/pulumi/pulumi/pkg/diag"
"github.com/pulumi/pulumi/pkg/diag/colors"
"github.com/pulumi/pulumi/pkg/resource/config"
"github.com/pulumi/pulumi/pkg/util/contract"
)
// Event represents an event generated by the engine during an operation. The underlying
// type for the `Payload` field will differ depending on the value of the `Type` field
type Event struct {
Type EventType
Payload interface{}
}
// EventType is the kind of event being emitted.
type EventType string
const (
2017-10-23 00:52:00 +02:00
CancelEvent EventType = "cancel"
StdoutColorEvent EventType = "stdoutcolor"
2017-10-23 00:52:00 +02:00
DiagEvent EventType = "diag"
PreludeEvent EventType = "prelude"
SummaryEvent EventType = "summary"
)
2017-10-23 00:52:00 +02:00
func cancelEvent() Event {
return Event{Type: CancelEvent}
}
// DiagEventPayload is the payload for an event with type `diag`
type DiagEventPayload struct {
Message string
Color colors.Colorization
Severity diag.Severity
}
type StdoutEventPayload struct {
Message string
Color colors.Colorization
}
type PreludeEventPayload struct {
IsPreview bool // true if this prelude is for a plan operation
Config map[string]string // the keys and values for config. For encrypted config, the values may be blinded
}
type SummaryEventPayload struct {
IsPreview bool // true if this summary is for a plan operation
MaybeCorrupt bool // true if one or more resources may be corrupt
Duration time.Duration // the duration of the entire update operation (zero values for previews)
ResourceChanges ResourceChanges // count of changed resources, useful for reporting
}
func preludeEvent(isPreview bool, cfg config.Map) Event {
configStringMap := make(map[string]string, len(cfg))
for k, v := range cfg {
keyString := k.String()
valueString, err := v.Value(config.NewBlindingDecrypter())
contract.AssertNoError(err)
configStringMap[keyString] = valueString
}
return Event{
Type: PreludeEvent,
Payload: PreludeEventPayload{
IsPreview: isPreview,
Config: configStringMap,
},
}
}
func previewSummaryEvent(resourceChanges ResourceChanges) Event {
return Event{
Type: SummaryEvent,
Payload: SummaryEventPayload{
IsPreview: true,
MaybeCorrupt: false,
Duration: 0,
ResourceChanges: resourceChanges,
},
}
}
func updateSummaryEvent(maybeCorrupt bool, duration time.Duration, resourceChanges ResourceChanges) Event {
return Event{
Type: SummaryEvent,
Payload: SummaryEventPayload{
IsPreview: false,
MaybeCorrupt: maybeCorrupt,
Duration: duration,
ResourceChanges: resourceChanges,
},
}
}
func stdOutEventWithColor(s fmt.Stringer) Event {
return Event{
Type: StdoutColorEvent,
Payload: StdoutEventPayload{
Message: s.String(),
Color: colors.Raw,
},
}
}
func diagDebugEvent(msg string) Event {
return Event{
Type: DiagEvent,
Payload: DiagEventPayload{
Message: msg,
Color: colors.Raw,
Severity: diag.Debug,
},
}
}
func diagInfoEvent(msg string) Event {
return Event{
Type: DiagEvent,
Payload: DiagEventPayload{
Message: msg,
Color: colors.Raw,
Severity: diag.Info,
},
}
}
func diagInfoerrEvent(msg string) Event {
return Event{
Type: DiagEvent,
Payload: DiagEventPayload{
Message: msg,
Color: colors.Raw,
Severity: diag.Infoerr,
},
}
}
func diagErrorEvent(msg string) Event {
return Event{
Type: DiagEvent,
Payload: DiagEventPayload{
Message: msg,
Color: colors.Raw,
Severity: diag.Error,
},
}
}
func diagWarningEvent(msg string) Event {
return Event{
Type: DiagEvent,
Payload: DiagEventPayload{
Message: msg,
Color: colors.Raw,
Severity: diag.Warning,
},
}
}