Skip to content

Commit

Permalink
*: removed provider global in favor of tf
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Mar 20, 2020
1 parent 74c404f commit b1d5063
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion _examples/aws.star
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions _examples/backend.star
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
b = backend("gcs")
b.bucket = "tf-state-prod"
b.prefix = "terraform/state"
tf.backend = backend("gcs")
tf.backend.bucket = "tf-state-prod"
tf.backend.prefix = "terraform/state"
6 changes: 2 additions & 4 deletions _examples/docker.star
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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))
foo.image = golang.version(full=True)
2 changes: 1 addition & 1 deletion _examples/ignition.star
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ignition = provider("ignition", "1.1.0")
ignition = tf.provider("ignition", "1.1.0")

user = ignition.data.user()
user.name = "foo"
Expand Down
13 changes: 11 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
}
1 change: 0 additions & 1 deletion starlark/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions starlark/types/fixtures/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ resource "aws_vpc" "id_2" {
cidr_block = "172.16.0.0/16"
tags = { Name = "tf-example" }
}

1 change: 0 additions & 1 deletion starlark/types/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions starlark/types/testdata/computed.star
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion starlark/types/testdata/hcl.star
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions starlark/types/testdata/hcl_integration.star
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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"))
2 changes: 1 addition & 1 deletion starlark/types/testdata/nested.star
Original file line number Diff line number Diff line change
@@ -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<nested.filter>")
Expand Down
6 changes: 3 additions & 3 deletions starlark/types/testdata/provider.star
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions starlark/types/testdata/resource.star
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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")

Expand Down

0 comments on commit b1d5063

Please sign in to comment.