Skip to content

Commit

Permalink
chore(stack): embed flow-style env manifest in template
Browse files Browse the repository at this point in the history
Related aws#3522
  • Loading branch information
efekarakus committed May 10, 2022
1 parent 2d16694 commit 09c1ef6
Show file tree
Hide file tree
Showing 10 changed files with 948 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration
// +build integration

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Expand Down
6 changes: 5 additions & 1 deletion internal/pkg/deploy/cloudformation/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cloudformation
import (
"errors"
"fmt"
"io"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -100,7 +101,10 @@ func TestCloudFormation_UpgradeEnvironment(t *testing.T) {
})
})
s3 := mocks.NewMocks3Client(ctrl)
s3.EXPECT().Upload("mockbucket", "manual/templates/phonetool-test/6806328b58e482e354cfd1879fecc310113fcf1b95c6bdf058c486f440087847.yml", gomock.Any()).Return("url", nil)
s3.EXPECT().Upload("mockbucket", gomock.Any(), gomock.Any()).DoAndReturn(func(bucket, key string, data io.Reader) (string, error) {
require.Contains(t, key, "manual/templates/phonetool-test/")
return "url", nil
})

return &CloudFormation{
cfnClient: m,
Expand Down
12 changes: 11 additions & 1 deletion internal/pkg/deploy/cloudformation/stack/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/cloudformation"
"gopkg.in/yaml.v3"

"github.com/aws/copilot-cli/internal/pkg/aws/s3"
"github.com/aws/copilot-cli/internal/pkg/config"
Expand Down Expand Up @@ -82,6 +83,14 @@ func (e *EnvStackConfig) Template() (string, error) {
if err != nil {
return "", err
}
var mft string
if e.in.Mft != nil {
out, err := yaml.Marshal(e.in.Mft)
if err != nil {
return "", fmt.Errorf("marshal environment manifest to embed in template: %v", err)
}
mft = string(out)
}

content, err := e.parser.ParseEnv(&template.EnvOpts{
AppName: e.in.App.Name,
Expand All @@ -92,12 +101,13 @@ func (e *EnvStackConfig) Template() (string, error) {
ArtifactBucketARN: e.in.ArtifactBucketARN,
ArtifactBucketKeyARN: e.in.ArtifactBucketKeyARN,

ImportCertARNs: e.in.ImportCertARNs,
ImportCertARNs: e.importCertARNs(),
VPCConfig: e.vpcConfig(),
Telemetry: e.telemetryConfig(),

Version: e.in.Version,
LatestVersion: deploy.LatestEnvTemplateVersion,
Manifest: mft,
}, template.WithFuncs(map[string]interface{}{
"inc": template.IncFunc,
}))
Expand Down
87 changes: 87 additions & 0 deletions internal/pkg/deploy/cloudformation/stack/env_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//go:build integration || localintegration

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package stack_test

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/aws/copilot-cli/internal/pkg/manifest"

"github.com/aws/copilot-cli/internal/pkg/template"

"gopkg.in/yaml.v3"

"github.com/aws/copilot-cli/internal/pkg/deploy"
"github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation/stack"
"github.com/stretchr/testify/require"
)

func TestEnvStack_Template(t *testing.T) {
testCases := map[string]struct {
input *deploy.CreateEnvironmentInput
wantedFileName string
}{
"generate template with embedded manifest file with container insights and imported certificates": {
input: func() *deploy.CreateEnvironmentInput {
var mft manifest.Environment
err := yaml.Unmarshal([]byte(`
name: test
type: Environment
# Create the public ALB with certificates attached.
# All these comments should be deleted.
http:
public:
certificates:
- cert-1
- cert-2
observability:
container_insights: true # Enable container insights.
`), &mft)
require.NoError(t, err)
return &deploy.CreateEnvironmentInput{
Version: "1.x",
App: deploy.AppInformation{
AccountPrincipalARN: "arn:aws:iam::000000000:root",
Name: "demo",
},
Name: "test",
ArtifactBucketARN: "arn:aws:s3:::mockbucket",
ArtifactBucketKeyARN: "arn:aws:kms:us-west-2:000000000:key/1234abcd-12ab-34cd-56ef-1234567890ab",
CustomResourcesURLs: map[string]string{
template.DNSCertValidatorFileName: "https://mockbucket.s3-us-west-2.amazonaws.com/dns-cert-validator",
template.DNSDelegationFileName: "https://mockbucket.s3-us-west-2.amazonaws.com/dns-delegation",
template.CustomDomainFileName: "https://mockbucket.s3-us-west-2.amazonaws.com/custom-domain",
},
Mft: &mft,
}
}(),
wantedFileName: "template-with-imported-certs-observability.yml",
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
// GIVEN
wanted, err := os.ReadFile(filepath.Join("testdata", "environments", tc.wantedFileName))
require.NoError(t, err, "read wanted template")
wantedObj := make(map[any]any)
require.NoError(t, yaml.Unmarshal(wanted, wantedObj))

// WHEN
envStack := stack.NewEnvStackConfig(tc.input)
actual, err := envStack.Template()
require.NoError(t, err, "serialize template")
fmt.Println(actual)
actualObj := make(map[any]any)
require.NoError(t, yaml.Unmarshal([]byte(actual), actualObj))

// THEN
require.Equal(t, wantedObj, actualObj)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build integration || localintegration
// +build integration localintegration

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Expand Down
Loading

0 comments on commit 09c1ef6

Please sign in to comment.