starhawking / python-terrascript

Create Terraform files using Python scripts.
BSD 2-Clause "Simplified" License
516 stars 76 forks source link

Suggestion of syntax to terrascript: "ts.write" #33

Closed nielsonsantana closed 6 years ago

nielsonsantana commented 6 years ago

I started work on documentation to upgrade to version 0.5.0, and I'm think that declare ts += a bit tedious, specially when we will use the variable another time. In this case is necessary two lines.

ts = Terrascript()

var_access_key = variable('access_key')
ts += var_access_key
var_secret_key = variable('secret_key')
ts += var_secret_key
var_region = variable('region', default='us-east-1')
ts += var_region

Than, my suggestion is another syntax to Terrascript. Something explicit and more easy to use. I think that is possible take advantage of multiple assign of python. We would use a setter property, for example ts.write or ts.w(less verbose). I'm not advocating to remove the current syntax, but add another.

ts = Terrascript()

ts.write = var_access_key = variable('access_key')
ts.write = var_secret_key = variable('secret_key')
ts.write = var_region = variable('region', default='us-east-1')

To apply this syntax, Is need just add some lines:

class Terrascript(object):
    """Top-level container for Terraform configurations."""

    ....

    def get_write(self):
        return None

    def set_write(self, value):
        self += value

    write = property(get_write, set_write)

Full example for comparision

Syntax: ts +=

from terrascript import Terrascript
from terrascript import provider
from terrascript import output
from terrascript import variable
from terrascript.aws.r import aws_instance

ts = Terrascript()

var_access_key = variable('access_key')
ts += var_access_key
ts.write = var_secret_key = variable('secret_key')
ts += var_secret_key
ts.write = var_region = variable('region', default='us-east-1')
ts += var_region

ts += provider(
    'aws', access_key=var_access_key,
    secret_key=var_secret_key,
    region=var_region
)

example = aws_instance('example', ami='ami-2757f631', instance_type='t2.micro')
ts += example

ts += output('example_public_ip', value=example.public_ip, description='Public IP of example')

print(ts.dump())

Syntax: ts.write or ts.w

from terrascript import Terrascript
from terrascript import provider
from terrascript import output
from terrascript import variable
from terrascript.aws.r import aws_instance

ts = Terrascript()

ts.write = var_access_key = variable('access_key')
ts.write = var_secret_key = variable('secret_key')
ts.write = var_region = variable('region', default='us-east-1')

ts.write = aws_p = provider(
    'aws', access_key=var_access_key,
    secret_key=var_secret_key,
    region=var_region
)

ts.write = example = aws_instance('example', ami='ami-2757f631', instance_type='t2.micro')

ts.write = output('example_public_ip', value=example.public_ip, description='Public IP of example')

print(ts.dump())
mjuenema commented 6 years ago

I only added the += syntax in version 0.5.0 which was a major change. This is of course implemented as the Terrascript.__add__() method. There is actually a Terrascript.add() method which is simply an "alias" for Terrascript.__add__(). Since it is not documented I suggest changing the behaviour of Terrascript.add() so it can be used as follows.

from terrascript import Terrascript
import terrascript
ts = terrascript.Terrascript()
var_access_key = ts.add(terrascript.variable('access_key'))

The Terrascript.add() method would simply return its argument. This way you get += and variable assigment in one line.

mjuenema commented 6 years ago

I have a test case for what I suggested in my previous post: https://github.com/mjuenema/python-terrascript/blob/issue33/tests/test_issue33.py

nielsonsantana commented 6 years ago

Great!! I will use the syntax ts.add in the docs in this case.

nielsonsantana commented 6 years ago

I should make the pull request to branch issue33?

nielsonsantana commented 6 years ago

As this syntax was implemented on branch issue33, I'm closing this issue.