Skip to content

Commit

Permalink
add notifications to manual judgement (#55)
Browse files Browse the repository at this point in the history
* add notifications to manual judgement

* fix manual judgement serializing in snakecase
  • Loading branch information
jgramoll committed Dec 20, 2019
1 parent 50f8f47 commit 3ed0849
Show file tree
Hide file tree
Showing 36 changed files with 643 additions and 158 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ resource "spinnaker_pipeline_manual_judgment_stage" "main" {
"commit",
"rollback",
]
notification {
address = "#my-slack-channel"
message {
manual_judgment_continue = "Manual judgement continue"
manual_judgment_stop = "Manual judgement stop"
}
type = "slack"
when {
manual_judgment = true
manual_judgment_continue = true
manual_judgment_stop = true
}
}
}
resource "spinnaker_pipeline_deploy_stage" "deploy" {
Expand Down Expand Up @@ -244,10 +258,10 @@ resource "spinnaker_pipeline_destroy_server_group_stage" "deploy" {
}
resource "spinnaker_pipeline_evaluate_variables_stage" "s%v" {
pipeline = "${spinnaker_pipeline.test.id}"
name = "Stage %v"
pipeline = "${spinnaker_pipeline.test.id}"
name = "Stage %v"
variables {
variables {
foo = "bar"
baz = "qux"
}
Expand Down
4 changes: 3 additions & 1 deletion client/manual_judgment_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type JudgmentInputs struct {
Value string `json:"value"`
}

// ManualJudgmentStage for manual judgements
type ManualJudgmentStage struct {
BaseStage `mapstructure:",squash"`

Expand All @@ -26,7 +27,8 @@ type ManualJudgmentStage struct {
// NewManualJudgmentStage for pipeline
func NewManualJudgmentStage() *ManualJudgmentStage {
return &ManualJudgmentStage{
BaseStage: *newBaseStage(ManualJudgmentStageType),
BaseStage: *newBaseStage(ManualJudgmentStageType),
JudgmentInputs: []JudgmentInputs{},
}
}

Expand Down
6 changes: 6 additions & 0 deletions client/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type Message interface {

SetStartingText(string)
StartingText() string

SetManualJudgmentContinueText(string)
ManualJudgmentContinueText() string

SetManualJudgmentStopText(string)
ManualJudgmentStopText() string
}

// NewMessage new message
Expand Down
20 changes: 20 additions & 0 deletions client/pipeline_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ func (m *PipelineMessage) StartingText() string {
}
return ""
}

// SetManualJudgmentContinueText for Message interface
func (m *PipelineMessage) SetManualJudgmentContinueText(text string) {
panic("NOT IMPLEMENTED")
}

// ManualJudgmentContinueText for Message interface
func (m *PipelineMessage) ManualJudgmentContinueText() string {
panic("NOT IMPLEMENTED")
}

// SetManualJudgmentStopText for Message interface
func (m *PipelineMessage) SetManualJudgmentStopText(text string) {
panic("NOT IMPLEMENTED")
}

// ManualJudgmentStopText for Message interface
func (m *PipelineMessage) ManualJudgmentStopText() string {
panic("NOT IMPLEMENTED")
}
2 changes: 0 additions & 2 deletions client/run_job_manifest_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ func TestRunJobManifestStageDeserialize(t *testing.T) {
}
stageInterface, err := parseRunJobManifestStage(stageMap)
if err != nil {
println("2")
t.Fatal(err)
}
stage := stageInterface.(*RunJobManifestStage)
if string(stage.Manifest) != runJobManifestYaml {
println("3")
t.Fatalf("Manifest should be text")
}
}
Expand Down
38 changes: 38 additions & 0 deletions client/stage_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const StageFailedKey = "stage.failed"
// StageStartingKey for pipeline starting
const StageStartingKey = "stage.starting"

// ManualJudgmentKey for manual judgement
const ManualJudgmentKey = "manualJudgment"

// ManualJudgmentContinueKey for manual judgment continue
const ManualJudgmentContinueKey = "manualJudgmentContinue"

// ManualJudgmentStopKey for manual judgement stop
const ManualJudgmentStopKey = "manualJudgmentStop"

func init() {
messageFactories[NotificationLevelStage] = func() Message {
return &StageMessage{}
Expand All @@ -22,6 +31,9 @@ type StageMessage struct {
Complete *MessageText `json:"stage.complete" mapstructure:"stage.complete"`
Failed *MessageText `json:"stage.failed" mapstructure:"stage.failed"`
Starting *MessageText `json:"stage.starting" mapstructure:"stage.starting"`

ManualJudgmentContinue *MessageText `json:"manualJudgmentContinue"`
ManualJudgmentStop *MessageText `json:"manualJudgmentStop"`
}

// SetCompleteText for Message interface
Expand Down Expand Up @@ -62,3 +74,29 @@ func (m *StageMessage) StartingText() string {
}
return ""
}

// SetManualJudgmentContinueText for Message interface
func (m *StageMessage) SetManualJudgmentContinueText(text string) {
m.ManualJudgmentContinue = &MessageText{Text: text}
}

// ManualJudgmentContinueText for Message interface
func (m *StageMessage) ManualJudgmentContinueText() string {
if m.ManualJudgmentContinue != nil {
return m.ManualJudgmentContinue.Text
}
return ""
}

// SetManualJudgmentStopText for Message interface
func (m *StageMessage) SetManualJudgmentStopText(text string) {
m.ManualJudgmentStop = &MessageText{Text: text}
}

// ManualJudgmentStopText for Message interface
func (m *StageMessage) ManualJudgmentStopText() string {
if m.ManualJudgmentStop != nil {
return m.ManualJudgmentStop.Text
}
return ""
}
59 changes: 59 additions & 0 deletions provider/default_notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package provider

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/jgramoll/terraform-provider-spinnaker/client"
)

type defaultNotification struct {
ID string `mapstructure:"-"`
Address string `mapstructure:"address"`
Message *[]*message `mapstructure:"message"`
Type string `mapstructure:"type"`
When *[]*when `mapstructure:"when"`
}

func newDefaultNotification() *defaultNotification {
return &defaultNotification{}
}

func (n *defaultNotification) toClientNotification(level client.NotificationLevel) (*client.Notification, error) {
message, err := toClientMessage(level, n.Message)
if err != nil {
return nil, err
}
return &client.Notification{
ID: n.ID,
Address: n.Address,
Level: level,
Type: n.Type,
When: *toClientWhen(level, (*n.When)[0]),
Message: message,
}, nil
}

func (n *defaultNotification) fromClientNotification(cn *client.Notification) notification {
return &defaultNotification{
ID: cn.ID,
Address: cn.Address,
Message: &[]*message{fromClientMessage(cn.Message)},
Type: cn.Type,
When: &[]*when{fromClientWhen(cn)},
}
}

func (n *defaultNotification) setNotificationResourceData(d *schema.ResourceData) error {
err := d.Set("address", n.Address)
if err != nil {
return err
}
err = d.Set("message", n.Message)
if err != nil {
return err
}
err = d.Set("type", n.Type)
if err != nil {
return err
}
return d.Set("when", n.When)
}
64 changes: 64 additions & 0 deletions provider/manual_judgement_notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package provider

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/jgramoll/terraform-provider-spinnaker/client"
)

type manualJudgementNotification struct {
ID string `mapstructure:"-"`
Address string `mapstructure:"address"`
Type string `mapstructure:"type"`

Message *[]*manualJudgementNotificationMessage `mapstructure:"message"`
When *[]*manualJudgementNotificationWhen `mapstructure:"when"`
}

func newManualJudgementNotification() *manualJudgementNotification {
return &manualJudgementNotification{}
}

func newManualJudgementNotificationInterface() notification {
return newManualJudgementNotification()
}

func (n *manualJudgementNotification) toClientNotification(level client.NotificationLevel) (*client.Notification, error) {
message, err := toClientManualJudgementNotificationMessage(level, n.Message)
if err != nil {
return nil, err
}
return &client.Notification{
ID: n.ID,
Address: n.Address,
Level: level,
Type: n.Type,
When: *toClientManualJudgementNotificationWhen(level, (*n.When)[0]),
Message: message,
}, nil
}

func (n *manualJudgementNotification) fromClientNotification(cn *client.Notification) notification {
return &manualJudgementNotification{
ID: cn.ID,
Address: cn.Address,
Message: &[]*manualJudgementNotificationMessage{fromClientManualJudgementNotificationMessage(cn.Message)},
Type: cn.Type,
When: &[]*manualJudgementNotificationWhen{fromClientManualJudgementNotificationWhen(cn)},
}
}

func (n *manualJudgementNotification) setNotificationResourceData(d *schema.ResourceData) error {
err := d.Set("address", n.Address)
if err != nil {
return err
}
err = d.Set("message", n.Message)
if err != nil {
return err
}
err = d.Set("type", n.Type)
if err != nil {
return err
}
return d.Set("when", n.When)
}
48 changes: 48 additions & 0 deletions provider/manual_judgement_notification_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package provider

import (
"github.com/jgramoll/terraform-provider-spinnaker/client"
)

type manualJudgementNotificationMessage struct {
ManualJudgmentContinue string `mapstructure:"manual_judgment_continue"`
ManualJudgmentStop string `mapstructure:"manual_judgment_stop"`
}

func toClientManualJudgementNotificationMessage(level client.NotificationLevel, m *[]*manualJudgementNotificationMessage) (client.Message, error) {
if m == nil || len(*m) == 0 {
return nil, nil
}
message := (*m)[0]
if message == nil {
return nil, nil
}

newMessage, err := client.NewMessage(level)
if err != nil {
return nil, err
}

if message.ManualJudgmentContinue != "" {
newMessage.SetManualJudgmentContinueText(message.ManualJudgmentContinue)
}
if message.ManualJudgmentStop != "" {
newMessage.SetManualJudgmentStopText(message.ManualJudgmentStop)
}
return newMessage, nil
}

func fromClientManualJudgementNotificationMessage(cm client.Message) *manualJudgementNotificationMessage {
if cm == nil {
return nil
}

newMessage := manualJudgementNotificationMessage{}
if cm.ManualJudgmentContinueText() != "" {
newMessage.ManualJudgmentContinue = cm.ManualJudgmentContinueText()
}
if cm.ManualJudgmentStopText() != "" {
newMessage.ManualJudgmentStop = cm.ManualJudgmentStopText()
}
return &newMessage
}
62 changes: 62 additions & 0 deletions provider/manual_judgement_notification_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package provider

import (
"github.com/hashicorp/terraform/helper/schema"
)

func manualJudementNotificationResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Description: "Address of the notification (slack channel, email, etc)",
Required: true,
},
"message": {
Type: schema.TypeList,
Description: "Custom messages",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"manual_judgment_continue": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"manual_judgment_stop": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"type": &schema.Schema{
Type: schema.TypeString,
Description: "Type of notification (slack, email, etc)",
Required: true,
},
"when": &schema.Schema{
Type: schema.TypeList,
Description: "When to send notification (started, completed, failed)",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"manual_judgment": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"manual_judgment_continue": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"manual_judgment_stop": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},
},
},
},
}
}
Loading

0 comments on commit 3ed0849

Please sign in to comment.