Skip to content

Commit

Permalink
Update Go examples for v1.9.1. (pulumi#527)
Browse files Browse the repository at this point in the history
* Update Go examples for v1.9.1.

* Simplify
  • Loading branch information
pgavlin committed Jan 30, 2020
1 parent e38cc4b commit b88f385
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 714 deletions.
26 changes: 22 additions & 4 deletions aws-go-s3-folder/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
/bin/
/node_modules/
/.pulumi/
Pulumi.*.yaml
*.swp
/vendor/
**/node_modules/
**/bin
**/.vscode/
**/.vs/
**/.ionide/
coverage.cov
*.coverprofile

/.idea/
*.iml

# VSCode creates this binary when running tests in the debugger
**/debug.test

# Go tests run "in tree" and this folder will linger if they fail (the integration test framework cleans
# it up when they pass.)
**/command-output/

# By default, we don't check in yarn.lock files
**/yarn.lock
38 changes: 38 additions & 0 deletions aws-go-s3-folder/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/pulumi/pulumi"
version = "1.9.1"

[[constraint]]
name = "github.com/pulumi/pulumi-aws"
branch = "tfgo"

[prune]
go-tests = true
unused-packages = true
56 changes: 23 additions & 33 deletions aws-go-s3-folder/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"mime"
"path"
"path/filepath"

"github.com/pulumi/pulumi-aws/sdk/go/aws/s3"
"github.com/pulumi/pulumi/sdk/go/pulumi"
"github.com/pulumi/pulumi/sdk/go/pulumi/asset"
)

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Create a bucket and expose a website index document
siteBucket, err := s3.NewBucket(ctx, "s3-website-bucket", &s3.BucketArgs{
Website: map[string]interface{}{
"indexDocument": "index.html",
Website: s3.BucketWebsiteArgs{
IndexDocument: pulumi.String("index.html"),
},
})
if err != nil {
Expand All @@ -34,47 +31,40 @@ func main() {
}
for _, item := range files {
name := item.Name()
filePath := filepath.Join(siteDir, name)
if _, err := s3.NewBucketObject(ctx, name, &s3.BucketObjectArgs{
Bucket: siteBucket.ID(), // reference to the s3.Bucket object
Source: asset.NewFileAsset(filePath), // use FileAsset to point to a file
ContentType: mime.TypeByExtension(path.Ext(name)), // set the MIME type of the file
Bucket: siteBucket.ID(), // reference to the s3.Bucket object
Source: pulumi.NewFileAsset(filepath.Join(siteDir, name)), // use FileAsset to point to a file
ContentType: pulumi.String(mime.TypeByExtension(path.Ext(name))), // set the MIME type of the file
}); err != nil {
return err
}
}

// Set the access policy for the bucket so all objects are readable
// Set the access policy for the bucket so all objects are readable.
if _, err := s3.NewBucketPolicy(ctx, "bucketPolicy", &s3.BucketPolicyArgs{
Bucket: siteBucket.ID(), // refer to the bucket created earlier
Policy: siteBucket.ID().Apply(publicReadPolicyForBucket), // use output property `siteBucket.bucket`
Bucket: siteBucket.ID(), // refer to the bucket created earlier
Policy: pulumi.Any(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{
"Effect": "Allow",
"Principal": "*",
"Action": []interface{}{
"s3:GetObject",
},
"Resource": []interface{}{
pulumi.Sprintf("arn:aws:s3:::%s/*", siteBucket.ID()), // policy refers to bucket name explicitly
},
},
},
}),
}); err != nil {
return err
}

// Stack exports
ctx.Export("bucketName", siteBucket.ID())
ctx.Export("websiteUrl", siteBucket.WebsiteEndpoint())
ctx.Export("websiteUrl", siteBucket.WebsiteEndpoint)
return nil
})
}

// Create an S3 Bucket Policy to allow public read of all objects in bucket.
func publicReadPolicyForBucket(bucketName pulumi.ID) (interface{}, error) {
policy, _ := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{
"Effect": "Allow",
"Principal": "*",
"Action": []string{
"s3:GetObject",
},
"Resource": []string{
fmt.Sprintf("arn:aws:s3:::%s/*", bucketName), // policy refers to bucket name explicitly
},
},
},
})
return string(policy), nil
}
22 changes: 22 additions & 0 deletions aws-go-webserver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*.swp
/vendor/
**/node_modules/
**/bin
**/.vscode/
**/.vs/
**/.ionide/
coverage.cov
*.coverprofile

/.idea/
*.iml

# VSCode creates this binary when running tests in the debugger
**/debug.test

# Go tests run "in tree" and this folder will linger if they fail (the integration test framework cleans
# it up when they pass.)
**/command-output/

# By default, we don't check in yarn.lock files
**/yarn.lock
38 changes: 38 additions & 0 deletions aws-go-webserver/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/pulumi/pulumi"
version = "1.9.1"

[[constraint]]
name = "github.com/pulumi/pulumi-aws"
branch = "tfgo"

[prune]
go-tests = true
unused-packages = true
8 changes: 0 additions & 8 deletions aws-go-webserver/go.mod

This file was deleted.

Loading

0 comments on commit b88f385

Please sign in to comment.