Skip to content

Commit

Permalink
fix cloudformation source deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Nov 12, 2020
1 parent 4c81c7d commit 004f65b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
39 changes: 39 additions & 0 deletions client/deploy_cloudformation_source.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
package client

import (
"fmt"
)

// DeployCloudformationSource source type
type DeployCloudformationSource int

const (
// DeployCloudformationSourceUnknown default unknown
DeployCloudformationSourceUnknown DeployCloudformationSource = iota
// DeployCloudformationSourceText text
DeployCloudformationSourceText
// DeployCloudformationSourceArtifact artifact
DeployCloudformationSourceArtifact
)

func (s DeployCloudformationSource) String() string {
return [...]string{"unknown", "text", "artifact"}[s]
}

// ParseDeployCloudformationSource parse source
func ParseDeployCloudformationSource(s string) (DeployCloudformationSource, error) {
switch s {
default:
return DeployCloudformationSourceUnknown, fmt.Errorf("Unknown source %s", s)
case "text":
return DeployCloudformationSourceText, nil
case "artifact":
return DeployCloudformationSourceArtifact, nil
}
}

// MarshalText source to text
func (s DeployCloudformationSource) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}

// UnmarshalText source from text
func (s *DeployCloudformationSource) UnmarshalText(text []byte) error {
parsedSource, err := ParseDeployCloudformationSource(string(text))
if err != nil {
return err
}
*s = parsedSource
return nil
}
14 changes: 14 additions & 0 deletions client/deploy_cloudformation_stage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"fmt"

"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -35,6 +37,7 @@ type DeployCloudformationStage struct {
func NewDeployCloudformationStage() *DeployCloudformationStage {
return &DeployCloudformationStage{
BaseStage: *newBaseStage(DeployCloudformationStageType),
Source: DeployCloudformationSourceText,
}
}

Expand All @@ -44,6 +47,17 @@ func parseDeployCloudformationStage(stageMap map[string]interface{}) (Stage, err
return nil, err
}

sourceString, ok := stageMap["source"].(string)
if !ok {
return nil, fmt.Errorf("Could not parse cloudformation source %v", stageMap["source"])
}
source, err := ParseDeployCloudformationSource(sourceString)
if err != nil {
return nil, err
}
stage.Source = source
delete(stageMap, "source")

if err := mapstructure.Decode(stageMap, stage); err != nil {
return nil, err
}
Expand Down
71 changes: 68 additions & 3 deletions client/deploy_cloudformation_stage_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,93 @@
package client

import (
"encoding/json"
"testing"

"github.com/sergi/go-diff/diffmatchpatch"
)

var deployCloudformationStage DeployCloudformationStage

func init() {
deployCloudformationStage = *NewDeployCloudformationStage()
deployCloudformationStage.Name = "New Deploy Cloudformation"
}

func TestDeployCloudformationStageGetName(t *testing.T) {
name := "New Deploy Cloudformation"
deployCloudformationStage.Name = name
if deployCloudformationStage.GetName() != name {
t.Fatalf("Deploy Cloudformation stage GetName() should be %s, not \"%s\"", name, deployCloudformationStage.GetName())
}
}

func TestDeployCloudformationStageGetType(t *testing.T) {
if deployCloudformationStage.GetType() != DeployCloudformationStageType {
t.Fatalf("Delete Manifest stage GetType() should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.GetType())
t.Fatalf("Deploy Cloudformation stage GetType() should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.GetType())
}
if deployCloudformationStage.Type != DeployCloudformationStageType {
t.Fatalf("Delete Manifest stage Type should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.Type)
t.Fatalf("Deploy Cloudformation stage Type should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.Type)
}
}

func TestDeployCloudformationStageSerialize(t *testing.T) {
b, err := json.MarshalIndent(deployCloudformationStage, "", "\t")
if err != nil {
t.Fatal(err)
}
result := string(b)
if result != deployCloudformationJSON {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(deployCloudformationJSON, result, true)
t.Fatalf("Deploy Cloudformation not as expected: %s", dmp.DiffPrettyText(diffs))
}
}

func TestDeployCloudformationStageDeserialize(t *testing.T) {
var stageMap map[string]interface{}
err := json.Unmarshal([]byte(deployCloudformationJSON), &stageMap)
if err != nil {
t.Fatal(err)
}
stageInterface, err := parseDeployCloudformationStage(stageMap)
if err != nil {
t.Fatal(err)
}
stage := stageInterface.(*DeployCloudformationStage)
if stage.Source != DeployCloudformationSourceText {
t.Fatalf("Should have source type text")
}
}

var deployCloudformationJSON = `{
"name": "New Deploy Cloudformation",
"refId": "",
"type": "deployCloudFormation",
"requisiteStageRefIds": [],
"sendNotifications": false,
"stageEnabled": null,
"completeOtherBranchesThenFail": false,
"continuePipeline": false,
"failOnFailedExpressions": false,
"failPipeline": true,
"overrideTimeout": false,
"restrictExecutionDuringTimeWindow": false,
"restrictedExecutionWindow": null,
"notifications": null,
"expectedArtifacts": [],
"requiredArtifactIds": [],
"actionOnReplacement": "",
"capabilities": null,
"changeSetName": "",
"credentials": "",
"executeChangeSet": false,
"isChangeSet": false,
"parameters": null,
"regions": null,
"roleARN": "",
"source": "text",
"stackArtifact": null,
"stackName": "",
"tags": null,
"templateBody": null
}`
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func testAccPipelineDeployCloudformationStageConfigBasic(pipeName string, stackN
for i := 1; i <= count; i++ {
stages += fmt.Sprintf(`
resource "spinnaker_pipeline_deploy_cloudformation_stage" "s%v" {
pipeline = "spinnaker_pipeline.test.id"
pipeline = spinnaker_pipeline.test.id
name = "Stage %v"
credentials = "my-aws-account"
Expand Down

0 comments on commit 004f65b

Please sign in to comment.