Skip to content

Commit 49aca85

Browse files
authored
feat(storage): Add new storage classes in addons to enable template execution. (#1025)
This PR completes the implementation of `storage init`'s Execute() method. Notable changes: * Adds S3 and DynamoDB structs to the addons package which behave similarly to those in the `manifest.LoadBalancedWebService` and `manifest.BackendService` structs. * Leverages the new AddonsWriter interface (with tiny modification) in `workspace` to enable template parsing for DDB and S3. * Resolves several typos in the DDB template which would have rendered it undeployable Unit tests forthcoming. <!-- Issue number, if available. E.g. "Fixes #31", "Addresses #42, 77" --> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 08dd91c commit 49aca85

43 files changed

Lines changed: 596 additions & 211 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ gen-mocks: tools
146146
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/aws/s3/mocks/mock_s3.go -source=./internal/pkg/aws/s3/s3.go
147147
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/aws/cloudformation/mocks/mock_cloudformation.go -source=./internal/pkg/aws/cloudformation/interfaces.go
148148
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/aws/cloudformation/stackset/mocks/mock_stackset.go -source=./internal/pkg/aws/cloudformation/stackset/stackset.go
149-
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/addons/mocks/mock_addons.go -source=./internal/pkg/addons/addons.go
149+
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/addon/mocks/mock_addons.go -source=./internal/pkg/addon/addons.go
150150
${GOBIN}/mockgen -package=mocks -source=./internal/pkg/docker/docker.go -destination=./internal/pkg/docker/mocks/mock_docker.go
151151
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/deploy/cloudformation/mocks/mock_cloudformation.go -source=./internal/pkg/deploy/cloudformation/cloudformation.go
152152
${GOBIN}/mockgen -package=mocks -destination=./internal/pkg/deploy/cloudformation/stack/mocks/mock_lb_web_svc.go -source=./internal/pkg/deploy/cloudformation/stack/lb_web_svc.go

cmd/copilot/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func buildRootCmd() *cobra.Command {
5959
cmd.AddCommand(cli.BuildEnvCmd())
6060
cmd.AddCommand(cli.BuildSvcCmd())
6161

62-
cmd.AddCommand(cli.BuildStorageCmd())
62+
// "Addons" command group
63+
//cmd.AddCommand(cli.BuildStorageCmd())
6364

6465
// "Settings" command group.
6566
cmd.AddCommand(cli.BuildVersionCmd())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// Package addons contains the service to manage addons.
5-
package addons
4+
// Package addon contains the service to manage addons.
5+
package addon
66

77
import (
88
"fmt"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package addons
4+
package addon
55

66
import (
77
"errors"
88
"io/ioutil"
99
"path/filepath"
1010
"testing"
1111

12-
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/addons/mocks"
12+
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/addon/mocks"
1313
"github.com/golang/mock/gomock"
1414
"github.com/stretchr/testify/require"
1515
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package addons
4+
package addon
55

66
import (
77
"errors"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package addons
4+
package addon
55

66
import (
77
"errors"
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package addons
4+
package addon
55

66
import (
77
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package addons
4+
package addon
55

66
import (
77
"io/ioutil"

internal/pkg/addon/storage.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package addon contains the service to manage addons.
5+
package addon
6+
7+
import (
8+
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/template"
9+
)
10+
11+
const (
12+
dynamoDbAddonPath = "addons/ddb/cf.yml"
13+
s3AddonPath = "addons/s3/cf.yml"
14+
)
15+
16+
type storage struct {
17+
ResourceName *string
18+
Name *string
19+
}
20+
21+
// DynamoDB contains configuration options which fully descibe a DynamoDB table.
22+
// Implements the encoding.BinaryMarshaler interface.
23+
type DynamoDB struct {
24+
DynamoDBProps
25+
26+
parser template.Parser
27+
}
28+
29+
// S3 contains configuration options which fully describe an S3 bucket.
30+
// Implements the encoding.BinaryMarshaler interface.
31+
type S3 struct {
32+
S3Props
33+
34+
parser template.Parser
35+
}
36+
37+
// StorageProps holds basic input properties for addon.NewDynamoDB() or addon.NewS3().
38+
type StorageProps struct {
39+
Name string
40+
ResourceName string
41+
}
42+
43+
// S3Props contains S3-specific properties for addon.NewS3().
44+
type S3Props struct {
45+
*StorageProps
46+
}
47+
48+
// DynamoDBProps contains DynamoDB-specific properties for addon.NewDynamoDB().
49+
type DynamoDBProps struct {
50+
*StorageProps
51+
Attributes []DDBAttribute
52+
LSIs []DDBLocalSecondaryIndex
53+
SortKey *string
54+
PartitionKey *string
55+
HasLSI bool
56+
}
57+
58+
// DDBAttribute holds the attribute definition of a DynamoDB attribute (keys, local secondary indices).
59+
type DDBAttribute struct {
60+
Name *string
61+
DataType *string // Must be one of "N", "S", "B"
62+
}
63+
64+
// DDBLocalSecondaryIndex holds a representation of an LSI.
65+
type DDBLocalSecondaryIndex struct {
66+
PartitionKey *string
67+
SortKey *string
68+
Name *string
69+
}
70+
71+
// MarshalBinary serializes the DynamoDB object into a binary YAML CF template.
72+
// Implements the encoding.BinaryMarshaler interface.
73+
func (d *DynamoDB) MarshalBinary() ([]byte, error) {
74+
content, err := d.parser.Parse(dynamoDbAddonPath, *d)
75+
if err != nil {
76+
return nil, err
77+
}
78+
return content.Bytes(), nil
79+
}
80+
81+
// NewDynamoDB creates a DynamoDB cloudformation template specifying attributes,
82+
// primary key schema, and local secondary index configuration.
83+
func NewDynamoDB(input *DynamoDBProps) *DynamoDB {
84+
return &DynamoDB{
85+
DynamoDBProps: *input,
86+
87+
parser: template.New(),
88+
}
89+
}
90+
91+
// MarshalBinary serializes the S3 object into a binary YAML CF template.
92+
// Implements the encoding.BinaryMarshaler interface.
93+
func (s *S3) MarshalBinary() ([]byte, error) {
94+
content, err := s.parser.Parse(s3AddonPath, *s)
95+
if err != nil {
96+
return nil, err
97+
}
98+
return content.Bytes(), nil
99+
}
100+
101+
// NewS3 creates a new S3 marshaler which can be used to write CF via addonWriter.
102+
func NewS3(input *S3Props) *S3 {
103+
return &S3{
104+
S3Props: *input,
105+
106+
parser: template.New(),
107+
}
108+
}

0 commit comments

Comments
 (0)