Skip to content

Commit ae07edc

Browse files
authored
Merge pull request #645 from georgealton/learn-how-to-append-extensions
Adds optional Extension property to set extension on uploaded artifact
2 parents 4562561 + bc5ec09 commit ae07edc

File tree

8 files changed

+42
-5
lines changed

8 files changed

+42
-5
lines changed

cft/pkg/directives.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type s3Options struct {
3434
Zip bool `yaml:"Zip"`
3535
Format s3Format `yaml:"Format"`
3636
Run string `yaml:"Run"`
37+
Extension string `yaml:"Extension"`
3738
}
3839

3940
type directiveContext struct {
@@ -158,7 +159,7 @@ func handleS3(root string, options s3Options) (*yaml.Node, error) {
158159
}
159160
}
160161

161-
s, err := upload(root, options.Path, options.Zip)
162+
s, err := upload(root, options.Path, options.Zip, options.Extension)
162163
if err != nil {
163164
return nil, err
164165
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Resources:
2+
Test:
3+
Type: A::B::C
4+
Properties:
5+
TheS3URI: !Rain::S3
6+
Path: s3.txt
7+
Extension: txt

cft/pkg/util.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// `BucketProperty`: Name of returned property that will contain the bucket name
1616
// `KeyProperty`: Name of returned property that will contain the object key
1717
// `VersionProperty`: (optional) Name of returned property that will contain the object version
18+
// `Extension`: (optional) Extension appended to the end of the Object Key in S3
1819
package pkg
1920

2021
import (
@@ -111,7 +112,7 @@ func zipPath(root string) (string, error) {
111112

112113
// Upload a file or directory to S3.
113114
// If path is a directory, it will be zipped first.
114-
func upload(root, path string, force bool) (*s3Path, error) {
115+
func upload(root, path string, force bool, extension string) (*s3Path, error) {
115116
if !filepath.IsAbs(path) {
116117
path = filepath.Join(root, path)
117118
if abs, err := filepath.Abs(path); err == nil {
@@ -152,7 +153,7 @@ func upload(root, path string, force bool) (*s3Path, error) {
152153
}
153154

154155
bucket := s3.RainBucket(false)
155-
key, err := s3.Upload(bucket, content)
156+
key, err := s3.Upload(bucket, content, extension)
156157

157158
uploads[artifactName] = &s3Path{
158159
bucket: bucket,

docs/README.tmpl

+22
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ Resources:
241241
S3Key: 1b4844dacc843f09941c11c94f80981d3be8ae7578952c71e875ef7add37b1a7
242242
```
243243

244+
Sometimes you require that objects uploaded to S3 have a specific extension, use the `Extension` property to ensure the artifact in S3 ends .<Extension>.
245+
246+
```yaml
247+
Resources:
248+
Test:
249+
Type: A::B::C
250+
Properties:
251+
TheS3URI: !Rain::S3
252+
Path: test
253+
Extension: sh
254+
```
255+
256+
The packaged template:
257+
258+
```yaml
259+
Resources:
260+
Test:
261+
Type: A::B::C
262+
Properties:
263+
TheS3URI: s3://rain-artifacts-012345678912-us-east-1/a84b588aa54068ed4b027b6e06e5e0bb283f83cf0d5a6720002d36af2225dfc3.sh
264+
```
265+
244266
#### Metadata commands
245267

246268
You can add a metadata section to an `AWS::S3::Bucket` resource to take additional actions during deployment, such as running pre and post build scripts, uploading content to the bucket after stack deployment completes, and emptying the contents of the bucket when the stack is deleted.

docs/rain_pkg.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ You may use the following, rain-specific directives in templates packaged with "
2929
Format: Uri|Http Specify which format rain pkg should return the S3 location as.
3030
Do not specify this property if you supply BucketProperty and KeyProperty.
3131
The default Format is "Uri".
32+
Extension: <ext> If specified, Appends .<ext> to the artifact object in S3.
3233

3334
!Rain::Module <url> Supply a URL to a rain module, which is similar to a CloudFormation module,
3435
but allows for type inheritance. One of the resources in the module yaml file

internal/aws/cfn/cfn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func checkTemplate(template cft.Template) (string, error) {
7474

7575
bucket := s3.RainBucket(false)
7676

77-
key, err := s3.Upload(bucket, []byte(templateBody))
77+
key, err := s3.Upload(bucket, []byte(templateBody), "")
7878
region := aws.Config().Region
7979
if strings.HasPrefix(region, "cn-") {
8080
return fmt.Sprintf("https://%s.s3.%s.amazonaws.com.cn/%s", bucket, region, key), err

internal/aws/s3/s3.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func CreateBucket(bucketName string) error {
173173
}
174174

175175
// Upload uploads an artifact to the bucket with a unique name
176-
func Upload(bucketName string, content []byte) (string, error) {
176+
func Upload(bucketName string, content []byte, extension string) (string, error) {
177177
isBucketExists, errBucketExists := BucketExists(bucketName)
178178

179179
if errBucketExists != nil {
@@ -185,6 +185,9 @@ func Upload(bucketName string, content []byte) (string, error) {
185185
}
186186

187187
key := filepath.Join(BucketKeyPrefix, fmt.Sprintf("%x", sha256.Sum256(content)))
188+
if extension != "" {
189+
key = fmt.Sprintf("%s.%s", key, extension)
190+
}
188191

189192
accountId, err := getAccountId()
190193
if err != nil {

scripts/integ.sh

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ set -eoux pipefail
4747
./rain --profile rain pkg cft/pkg/tmpl/include-template.yaml
4848
./rain --profile rain pkg cft/pkg/tmpl/s3-template.yaml
4949
./rain --profile rain pkg cft/pkg/tmpl/s3http-template.yaml
50+
# Given a Template with an Extension value of txt, When Packaged, Then the S3 URI ends '.txt'
51+
./rain --profile rain pkg cft/pkg/tmpl/s3-extension-template.yaml | yq --exit-status '.Resources.Test.Properties.TheS3URI | test("\.txt$")'
5052

5153
# Make sure merge works
5254
./rain merge test/templates/merge-out-1.yaml test/templates/merge-out-2.yaml

0 commit comments

Comments
 (0)