From b1d50635de7a5ddfc390868878cfd72bb297425a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Sat, 21 Mar 2020 00:10:22 +0100 Subject: [PATCH] *: removed provider global in favor of tf --- README.md | 4 ++-- _examples/aws.star | 2 +- _examples/backend.star | 6 +++--- _examples/docker.star | 6 ++---- _examples/ignition.star | 2 +- cmd/run.go | 13 +++++++++++-- starlark/runtime/runtime.go | 1 - starlark/types/fixtures/aws.tf | 1 + starlark/types/provider_test.go | 1 - starlark/types/testdata/computed.star | 4 ++-- starlark/types/testdata/hcl.star | 2 +- starlark/types/testdata/hcl_integration.star | 4 ++-- starlark/types/testdata/nested.star | 2 +- starlark/types/testdata/provider.star | 6 +++--- starlark/types/testdata/resource.star | 4 ++-- 15 files changed, 32 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index b5f638c..fd6dbb8 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Terraform is a great tool, with support for almost everything you can imagine, m Creating am Amazon EC2 Instance is as easy as: ```pyhon -aws = provider("aws", "2.13.0") +aws = tf.provider("aws", "2.13.0") aws.region = "us-west-2" aws.resource.instance(instance_type ="t2.micro", ami="ami-2757f631") @@ -28,7 +28,7 @@ aws.resource.instance(instance_type ="t2.micro", ami="ami-2757f631") In this example we create 40 instances, 20 using ubuntu and 20 using ECS. ```python -aws = provider("aws") +aws = tf.provider("aws") aws.region = "us-west-2" # It creates a new instance for the given name, distro and type. diff --git a/_examples/aws.star b/_examples/aws.star index 5fdab34..2348aad 100644 --- a/_examples/aws.star +++ b/_examples/aws.star @@ -1,4 +1,4 @@ -aws = provider("aws") +aws = tf.provider("aws") aws.region = "us-west-2" # It creates a new instance for the given name, distro and type. diff --git a/_examples/backend.star b/_examples/backend.star index 8cf6bfd..045acdf 100644 --- a/_examples/backend.star +++ b/_examples/backend.star @@ -1,3 +1,3 @@ -b = backend("gcs") -b.bucket = "tf-state-prod" -b.prefix = "terraform/state" \ No newline at end of file +tf.backend = backend("gcs") +tf.backend.bucket = "tf-state-prod" +tf.backend.prefix = "terraform/state" \ No newline at end of file diff --git a/_examples/docker.star b/_examples/docker.star index 16e4e53..bbf6653 100644 --- a/_examples/docker.star +++ b/_examples/docker.star @@ -1,6 +1,6 @@ load("experimental/docker", "docker") -p = provider("docker", "2.7.0", "foo") +p = tf.provider("docker", "2.7.0", "foo") # using docker.image semver can be used to choose the docker image, ` golang = docker.image("golang", "1.13.x") @@ -9,6 +9,4 @@ foo = p.resource.container("foo") foo.name = "foo" # version queries the docker repository and returns the correct tag. -foo.image = golang.version(full=True) - -print(hcl(p)) \ No newline at end of file +foo.image = golang.version(full=True) \ No newline at end of file diff --git a/_examples/ignition.star b/_examples/ignition.star index d51e95c..dfac907 100644 --- a/_examples/ignition.star +++ b/_examples/ignition.star @@ -1,4 +1,4 @@ -ignition = provider("ignition", "1.1.0") +ignition = tf.provider("ignition", "1.1.0") user = ignition.data.user() user.name = "foo" diff --git a/cmd/run.go b/cmd/run.go index 2f311fe..96ca0ab 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -23,7 +23,8 @@ const ( type RunCmd struct { commonCmd - ToHCL string `long:"to-hcl" description:"dumps context resources to a hcl file"` + ToHCL string `long:"to-hcl" description:"dumps resources to a hcl file"` + PrintHCL bool `long:"print-hcl" description:"print resources to a hcl file"` PositionalArgs struct { File string `positional-arg-name:"file" description:"starlark source file"` } `positional-args:"true" required:"1"` @@ -47,12 +48,20 @@ func (c *RunCmd) Execute(args []string) error { } func (c *RunCmd) dumpToHCL(ctx starlark.StringDict) error { - if c.ToHCL == "" { + if c.ToHCL == "" && !c.PrintHCL { return nil } f := hclwrite.NewEmptyFile() c.runtime.Terraform.ToHCL(f.Body()) + if c.PrintHCL { + os.Stdout.Write(f.Bytes()) + } + + if c.ToHCL == "" { + return nil + } + return ioutil.WriteFile(c.ToHCL, f.Bytes(), 0644) } diff --git a/starlark/runtime/runtime.go b/starlark/runtime/runtime.go index 6a91d48..137225c 100644 --- a/starlark/runtime/runtime.go +++ b/starlark/runtime/runtime.go @@ -58,7 +58,6 @@ func NewRuntime(pm *terraform.PluginManager) *Runtime { }, predeclared: starlark.StringDict{ "tf": tf, - "provider": types.BuiltinProvider(pm), "provisioner": types.BuiltinProvisioner(pm), "backend": types.BuiltinBackend(pm), "hcl": types.BuiltinHCL(), diff --git a/starlark/types/fixtures/aws.tf b/starlark/types/fixtures/aws.tf index 6eb05dc..7fce85e 100644 --- a/starlark/types/fixtures/aws.tf +++ b/starlark/types/fixtures/aws.tf @@ -99,3 +99,4 @@ resource "aws_vpc" "id_2" { cidr_block = "172.16.0.0/16" tags = { Name = "tf-example" } } + diff --git a/starlark/types/provider_test.go b/starlark/types/provider_test.go index 7c1ac07..2e95838 100644 --- a/starlark/types/provider_test.go +++ b/starlark/types/provider_test.go @@ -64,7 +64,6 @@ func doTest(t *testing.T, filename string) { pm := &terraform.PluginManager{".providers"} predeclared := starlark.StringDict{ - "provider": BuiltinProvider(pm), "provisioner": BuiltinProvisioner(pm), "backend": BuiltinBackend(pm), "hcl": BuiltinHCL(), diff --git a/starlark/types/testdata/computed.star b/starlark/types/testdata/computed.star index c56bf0d..373b19f 100644 --- a/starlark/types/testdata/computed.star +++ b/starlark/types/testdata/computed.star @@ -1,6 +1,6 @@ load("assert.star", "assert") -aws = provider("aws", "2.13.0") +aws = tf.provider("aws", "2.13.0") # compute of scalar web = aws.resource.instance() @@ -30,7 +30,7 @@ assert.eq(str(aws.resource.instance().id), '"${aws_instance.id_7.id}"') # compute on resource assert.eq(str(aws.data.ami().id), '"${data.aws_ami.id_8.id}"') -gcp = provider("google", "3.13.0") +gcp = tf.provider("google", "3.13.0") # computed on list with MaxItem:1 cluster = gcp.resource.container_cluster("foo") diff --git a/starlark/types/testdata/hcl.star b/starlark/types/testdata/hcl.star index 8d58f32..a63d617 100644 --- a/starlark/types/testdata/hcl.star +++ b/starlark/types/testdata/hcl.star @@ -1,6 +1,6 @@ load("assert.star", "assert") -helm = provider("helm", "1.0.0", "default") +helm = tf.provider("helm", "1.0.0", "default") helm.kubernetes.token = "foo" # hcl diff --git a/starlark/types/testdata/hcl_integration.star b/starlark/types/testdata/hcl_integration.star index aae49a2..f497364 100644 --- a/starlark/types/testdata/hcl_integration.star +++ b/starlark/types/testdata/hcl_integration.star @@ -1,7 +1,7 @@ load("os", "os") load("assert.star", "assert") -aws = provider("aws", "2.13.0") +aws = tf.provider("aws", "2.13.0") aws.region = "us-west-2" # Based on: @@ -71,4 +71,4 @@ example.ami = "ami-2757f631" example.instance_type = "t2.micro" example.depends_on(bucket) -assert.eq(hcl(aws), os.read_file("fixtures/aws.tf")) +assert.eq(hcl(tf), os.read_file("fixtures/aws.tf")) diff --git a/starlark/types/testdata/nested.star b/starlark/types/testdata/nested.star index 097c209..aeba13e 100644 --- a/starlark/types/testdata/nested.star +++ b/starlark/types/testdata/nested.star @@ -1,6 +1,6 @@ load("assert.star", "assert") -p = provider("aws", "2.13.0") +p = tf.provider("aws", "2.13.0") d = p.data.ami() assert.eq(type(d.filter), "ResourceCollection") diff --git a/starlark/types/testdata/provider.star b/starlark/types/testdata/provider.star index 9a68cbf..a5d6a4f 100644 --- a/starlark/types/testdata/provider.star +++ b/starlark/types/testdata/provider.star @@ -1,6 +1,6 @@ load("assert.star", "assert") -p = provider("aws", "2.13.0") +p = tf.provider("aws", "2.13.0") assert.eq(p.version, "2.13.0") assert.eq(len(dir(p.data)), 131) @@ -19,11 +19,11 @@ assert.eq(len(p.resource.instance), 2) p.region = "us-west-2" assert.eq(p.region, "us-west-2") -alias = provider("aws", "2.13.0", "alias") +alias = tf.provider("aws", "2.13.0", "alias") assert.eq(alias.alias, "alias") assert.eq(alias.version, "2.13.0") -kwargs = provider("aws", region="foo") +kwargs = tf.provider("aws", region="foo") assert.eq(kwargs.region, "foo") # compare diff --git a/starlark/types/testdata/resource.star b/starlark/types/testdata/resource.star index 013345f..21d64eb 100644 --- a/starlark/types/testdata/resource.star +++ b/starlark/types/testdata/resource.star @@ -1,6 +1,6 @@ load("assert.star", "assert") -p = provider("ignition", "1.1.0") +p = tf.provider("ignition", "1.1.0") # attr qux = p.data.user() @@ -21,7 +21,7 @@ assert.eq(type(qux.id), "Computed") assert.eq(str(qux.id), '"${data.ignition_user.id_2.id}"') # attr output assignation -aws = provider("aws", "2.13.0") +aws = tf.provider("aws", "2.13.0") def invalidOutput(): aws.data.instance().public_dns = "foo" assert.fails(invalidOutput, "aws_instance: can't set computed public_dns attribute")