Documentation
¶
Overview ¶
Package template defines helpers to make it easy to use Pulumi outputs as values with Go string templates.
This essentially takes a number of outputs and wraps the template with a call to ApplyT to ensure the template is only exeucted once the supplied values have been resolved.
See the example for NewJSON for an example of how to use this with Pulumi.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCompileError is raised via panic if the template generates an // error during the compile process. ErrCompileError = errors.New("template compile error") // ErrExecuteError is raised during panic if template generates an error // while it is being executed (eg. due to an incorrectly supplied argument) ErrExecuteError = errors.New("template execution error") // ErrInvalidJSON is raised during panic if the output from the template // does not validate as JSON. ErrInvalidJSON = errors.New("template produced invalid JSON") )
Functions ¶
func New ¶
func New(vars map[string]interface{}, templateText string) pulumi.StringOutput
New compiles a Go text/template and provides the specified variables to it, once they become available.
This is analogous to pulumi.Sprintf, except that you can supply a template instead of a string.
vars specifies a map of values to pass as data to the template; this may include any mix of regular values, or Pulumi outputs, which will have their values resolved before being supplied to the template.
Example ¶
package main import ( "github.com/gwatts/pulutil/template" "github.com/pulumi/pulumi/sdk/v2/go/pulumi" ) func main() { someStringOutput := pulumi.String("later-string").ToStringOutput() templateOutput := template.New(map[string]interface{}{ "Foo": "bar", "SomeID": someStringOutput, }, ` "Foo" is set to "{{ .Foo }}" "SomeID" is set to "{{ .SomeID }}" `) // templateOutput can now be supplied to a resource etc pulumi.Sprintf("template rendered to: %s", templateOutput) }
Output:
func NewJSON ¶
func NewJSON(vars map[string]interface{}, templateText string) pulumi.StringOutput
NewJSON wraps Template, but will panic if the rendered template does not parse as valid JSON.
Example ¶
package main import ( "fmt" "regexp" "sync" "github.com/gwatts/pulutil/template" "github.com/pulumi/pulumi-aws/sdk/v2/go/aws/s3" "github.com/pulumi/pulumi/sdk/v2/go/common/resource" "github.com/pulumi/pulumi/sdk/v2/go/pulumi" ) type mocks int func (mocks) NewResource(typeToken, name string, inputs resource.PropertyMap, provider, id string) (string, resource.PropertyMap, error) { return name + "_id", inputs, nil } func (mocks) Call(token string, args resource.PropertyMap, provider string) (resource.PropertyMap, error) { return args, nil } func main() { err := pulumi.RunErr(func(ctx *pulumi.Context) error { // create a new bucket - It's bucket.Arn field won't be known // until it's created, so is an output. bucket, err := s3.NewBucket(ctx, "mybucket", &s3.BucketArgs{}) if err != nil { return err } // Grant Cloudfront access to the bucket p, err := s3.NewBucketPolicy(ctx, "bucketPolicy", &s3.BucketPolicyArgs{ Bucket: bucket.Bucket, Policy: template.NewJSON(map[string]interface{}{ // parameters we want to pass to the template "BucketARN": bucket.Arn, "IdentityARN": "my-identity", }, `{ "Version": "2012-10-17", "Id": "PolicyForCloudFrontContent", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "{{ .IdentityARN }}" }, "Action": "s3:GetObject", "Resource": "{{ .BucketARN }}/*" } ] }`), }) if err != nil { return err } // The following is just a test to output the rendered template var wg sync.WaitGroup wg.Add(1) pulumi.All(p.Policy).ApplyT(func(all []interface{}) error { defer wg.Done() policy := all[0].(string) // strip the leading whitespace from each line for easier comparison var strip = regexp.MustCompile(`(?m)^\s+`) fmt.Println(strip.ReplaceAllLiteralString(policy, "")) return nil }) wg.Wait() return nil }, pulumi.WithMocks("project", "stack", mocks(0))) if err != nil { panic(fmt.Errorf("unexpected error: %v", err)) } }
Output: { "Version": "2012-10-17", "Id": "PolicyForCloudFrontContent", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "my-identity" }, "Action": "s3:GetObject", "Resource": "/*" } ] }
Types ¶
This section is empty.