diff --git a/client/deploy_cloudformation_source.go b/client/deploy_cloudformation_source.go index 07caedc..1a9179e 100644 --- a/client/deploy_cloudformation_source.go +++ b/client/deploy_cloudformation_source.go @@ -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 +} diff --git a/client/deploy_cloudformation_stage.go b/client/deploy_cloudformation_stage.go index 3f654e6..16d839c 100644 --- a/client/deploy_cloudformation_stage.go +++ b/client/deploy_cloudformation_stage.go @@ -1,6 +1,8 @@ package client import ( + "fmt" + "github.com/mitchellh/mapstructure" ) @@ -35,6 +37,7 @@ type DeployCloudformationStage struct { func NewDeployCloudformationStage() *DeployCloudformationStage { return &DeployCloudformationStage{ BaseStage: *newBaseStage(DeployCloudformationStageType), + Source: DeployCloudformationSourceText, } } @@ -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 } diff --git a/client/deploy_cloudformation_stage_test.go b/client/deploy_cloudformation_stage_test.go index 1a3609a..4aa6fb3 100644 --- a/client/deploy_cloudformation_stage_test.go +++ b/client/deploy_cloudformation_stage_test.go @@ -1,18 +1,21 @@ 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()) } @@ -20,9 +23,71 @@ func TestDeployCloudformationStageGetName(t *testing.T) { 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 +}` diff --git a/provider/pipeline_deploy_cloudformation_stage_resource_test.go b/provider/pipeline_deploy_cloudformation_stage_resource_test.go index a5e76a2..b0072b0 100644 --- a/provider/pipeline_deploy_cloudformation_stage_resource_test.go +++ b/provider/pipeline_deploy_cloudformation_stage_resource_test.go @@ -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"