Coder Social home page Coder Social logo

python-terrascript's People

Contributors

alanyee avatar austin-millan avatar benediktkr avatar bkez322 avatar byjg avatar farsonic avatar hugovk avatar ilons avatar jackluo923 avatar maorfr avatar mjuenema avatar nielsonsantana avatar ryanwalder avatar sharkofmirkwood avatar starhawking avatar vfoucault avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-terrascript's Issues

Problem when using resource in a loop

Hi,

I'm using terrascript to dump existing datadog monitors into terraform code.

Here's some sample code:

import terrascript
from datadog import initialize, api
from terrascript.datadog.r import datadog_monitor


def get_all():
    """ get all monitors """
    initialize()
    response = api.Monitor.get_all()
    return response


def dump_as_tf(monitor):
    """ Dump the specified monitor as terraform code """
    kwargs = {}
    kwargs['type'] = monitor['type']
    datadog_monitor(
       monitor['name'],
       **kwargs
    )
    print(terrascript.dump())


def dump_all_as_tf():
    """ get all monitors and dump as terraform code """
    response = get_all()
    # response is an array of hashes, one hash for each monitor
    for monitor in response:
        dump_as_tf(monitor)

What I see is that the datadog_monitor resource doesn't clean up between invocations so I end up with this sort of output:

{
  "resource": {
    "datadog_monitor": {
      "name of first monitor": {
        "type": "query alert"
      }
    }
  }
}
{
  "resource": {
    "datadog_monitor": {
      "name of second monitor": {
        "type": "service check"
      },
      "name of first monitor": {
        "type": "query alert"
      }
    }
  }
}
{
  "resource": {
    "datadog_monitor": {
      "name of second monitor": {
        "type": "service check"
      },
      "name of third monitor": {
        "type": "service check"
      },
      "name of first monitor": {
        "type": "query alert"
      }
    }
  }
}

Am I doing something wrong here?

makecode.py problems

consul

Traceback (most recent call last):
  File "./makecode.py", line 48, in <module>
    if regex.match(h.path)][0]
IndexError: list index out of range

do

do
Traceback (most recent call last):
  File "./makecode.py", line 42, in <module>
    branch = repo.branch(repo.default_branch)
AttributeError: 'NoneType' object has no attribute 'branch'

Acess Azure built in role definition permissions.actions

Hi,
I am creating an Azure Terraform file for deployment (I'm at the "messing around with it" stage).

Specifically I'm trying to create a custom role with permissions based on a default Azure custom role.

As per the this terrafor documntation : https://www.terraform.io/docs/providers/azurerm/d/builtin_role_definition.html
I would like to acess the variable "${data.azurerm_builtin_role_definition.contributor.permissions.actions}"

data "azurerm_builtin_role_definition" "contributor" {
  name = "Contributor"
}

output "contributor_role_definition_id" {
  value = "${data.azurerm_builtin_role_definition.contributor.permissions.actions}"
}

hence here are my relevant terrascript lines:

builtinrole = ts.add(d.azurerm_builtin_role_definition("Contributor", name="Contributor"))
subscription = ts.add(d.subscription("primary"))

customRoleName = "testRole"
customRole = ts.add(azurerm_role_definition(customRoleName,
                                            name=customRoleName,
                                            role_definition_id=None,
                                            scope=subscription.id,
                                            description="This is a custom role created via Terraform with the same permissions as a Contributor",
                                            permissions={"actions": [builtinrole.permissions.actions],
                                                         "not_actions": [builtinrole.permissions.not_actions],
                                                         },
                                            # permissions=[builtinrole.permissions],
                                            assignable_scopes=[subscription.id],
                                            )
                    )

however this results in

Traceback (most recent call last):
  File "/home/guillaumedsde/PycharmProjects/champollion/champollion.py", line 273, in <module>
    permissions={"actions": [builtinrole.permissions.actions],
AttributeError: 'str' object has no attribute 'actions'

So I'm assuming that I can't really access elements of builtinrole.permissions.actions because it is essentially a string of the list of allowed actions if I understand this corretly?

So I'm essentially asking, how do I access builtinrole.permissions.actions instead of simply accessing builtinrole.permissions?

Terrascript doesn't support multiple providers properly

I run slightly modified example

from terrascript import Terrascript, provider, module
from terrascript.aws.r import aws_instance

ts = Terrascript()

aws_main = provider('aws', access_key='ACCESS_KEY_HERE',
               secret_key='SECRET_KEY_HERE', region='us-east-1')

aws_dr = provider('aws', alias="recovery", access_key='ACCESS_KEY_HERE',
               secret_key='SECRET_KEY_HERE', region='us-west-1')

ts += aws_main
ts += aws_dr

inst = ts.add(module('example',
                     source="[email protected]:repoorg/repo-name?ref=v0.1.2//modules/resource_group",
                     providers={
                         "aws": aws_main,
                         "aws_dr": aws_dr
                     },
                     instance_type='t2.micro')

              )

# Print the JSON-style configuration to stdout.
print(ts.dump())

and library generated following output

{
   "module": {
     "example": {
       "instance_type": "t2.micro",
       "providers": {
         "aws": "provider.__DEFAULT__",
         "aws_dr": "provider.recovery"
       },
       "source": "[email protected]:repoorg/repo-name?ref=v0.1.2//modules/resource_group"
     }
   },
   "provider": {
     "aws": {
       "__DEFAULT__": {
         "access_key": "ACCESS_KEY_HERE",
         "region": "us-east-1",
         "secret_key": "SECRET_KEY_HERE"
       },
       "recovery": {
         "access_key": "ACCESS_KEY_HERE",
         "alias": "recovery",
         "region": "us-west-1",
         "secret_key": "SECRET_KEY_HERE"
       }
     }
   }
 }

structure for providers is not proper, references passed to module cannot be found. Its problematic because we intensively uses modules and multiple providers. Expected output is described here
hashicorp/terraform#2015

Cheers,
Tomek

Is there any support for "null_resource" in terrascript?

Cool module!

I have a terraform file that has the following resource:

resource "null_resource" "configdrive" {
  provisioner "local-exec" {
    command = "python ../configdrive.py ${local_file.user_data.filename} ${var.node_name}"
  }

Is there anyway to express null_resources via terrascript?

Thanks!

Travis-CI: json.JSONDecodeError not available in Python 3.4

Change tests/shared.py:assert_deep_equal() to catch ValueError instead of json.JSONDecodeError as the latter has only been introduced in Python 3.4. Alternatively drop Travis-CI tests for Python3.4

Traceback (most recent call last):

  File "/home/travis/virtualenv/python3.4.8/lib/python3.4/site-packages/nose/case.py", line 198, in runTest

    self.test(*self.arg)

  File "/home/travis/build/mjuenema/python-terrascript/tests/test_004.py", line 15, in test

    assert_equals_json(config, 'test_004.tf.json')

  File "/home/travis/build/mjuenema/python-terrascript/tests/shared.py", line 24, in assert_deep_equal

    except json.decoder.JSONDecodeError:

AttributeError: 'module' object has no attribute 'JSONDecodeError'

exception json.JSONDecodeError(msg, doc, pos)

    Subclass of ValueError with the following additional attributes:

    msg
        The unformatted error message.

    doc
        The JSON document being parsed.

    pos
        The start index of doc where parsing failed.

    lineno
        The line corresponding to pos.

    colno
        The column corresponding to pos.

    New in version 3.5.

test_requirements.txt no such file

Downloading terrascript-0.2.0.tar.gz │
Complete output from command python setup.py egg_info: │
Traceback (most recent call last): │
File "", line 1, in │
File "/tmp/pip-build-h246gv2s/terrascript/setup.py", line 19, in │
r.strip() for r in open(join(dirname(file), 'test_requirements.txt')).readlines() │
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-build-h246gv2s/terrascript/test_requirements.tx│
t'

Build errors introduced on branch issue33

After update '.travis-ci.yml' and add Terraform versions (0.10.1, 0.10.2, 0.10.3, 0.10.4, 0.10.5), a build error was introducted. For this versions of terraform, the build aren't working(
https://travis-ci.org/mjuenema/python-terrascript/builds/372883420).

How we can solve this? Drop support for Terraform < 0.10.5?
Or the problem is the test implemented in TestModule?

Exemple of error:

python3 -m nose --failed --verbose --with-coverage --cover-package=terrascript --stop --no-byte-compile --logging-level=DEBUG --detailed-errors --with-id tests/test.py tests/test_providers.py  tests/test_issue22.py tests/test_issue26.py tests/test_issue31.py tests/test_issue33.py tests/test_issue3a.py tests/test_issue3b.py
#1 test.Test0Terraform.test ... ok
#2 test.Test0Terraform.test_backend ... ok
#3 test.Test1ConfigDefaults.test_data ... ok
#4 test.Test1ConfigDefaults.test_module ... ok
#5 test.Test1ConfigDefaults.test_resource ... ok
#6 test.Test1ConfigDefaults.test_variable ... ok
#7 test.Test1ConfigDefaults.test_wrongkey ... ok
#8 test.Test2Config.test_bool ... ok
#9 test.Test2Config.test_dict ... ok
#10 test.Test2Config.test_float ... ok
#11 test.Test2Config.test_int ... ok
#12 test.Test2Config.test_interpol_data_attr ... ok
#13 test.Test2Config.test_interpol_resource_attr ... ok
#14 test.Test2Config.test_list ... ok
#15 test.Test2Config.test_string ... ok
#16 test.TestFunctions.test_resource ... ok
#17 test.TestFunctions.test_string ... ok
#18 test.TestFunctions.test_synonyms ... ok
#19 test.TestFunctions.test_variable ... ok
#20 test.TestModule.test ... Error downloading modules: Error loading modules: error downloading 
'file:///tmp/tmpiktcfx9_/devops-workflow/ami-ids/aws': source path error: stat /tmp/tmpiktcfx9_/devops-workflow/ami-ids/aws: no such file or directory
FAIL

Make CONFIG a subclass of dict

CONFIG(dict):
    __getitem__('variable'):
        dynamically done

    dump()

    validate()

dump = CONFIG.dump

validate = CONFIG.validate

Work-around for data/JSON bug

This relates to hashicorp/terraform#13911.

The work-around suggested in the issue does not work. The underlying problem seems to be that at least with data sources the data source must have more than one argument.

Fails

{
  "data": {
    "aws_instance": {
      "NAME": {
        "first": {
          "a": 1,
          "b": "2",
          "c": true
        }
      }
    }
  }
}

Works

{
  "data": {
    "aws_instance": {
      "NAME": {
        "first": {
          "a": 1,
          "b": "2",
          "c": true
        },
        "second": "string"          <----- second argument.
      }
    }
  }
}

The work-around is to ensure that the data source always has a type (defaulting to string) and
an (empty) description.

Release 0.6.1?

Any thoughts about releasing a version 0.6.1? There's been a lot of stuff since 0.6.0. :^)

(Opening a ticket doesn't seem like the best way to suggest this, but I wasn't sure what else would be better.)

Support new AWS resource `aws_config_configuration_aggregator`

terraform-provider-aws version 1.22.0 supports AWS Config Aggregators, with a new aws_config_configuration_aggregator resource (https://www.terraform.io/docs/providers/aws/r/config_configuration_aggregator.html), but Terrascript doesn't seem to know about it:

Traceback (most recent call last):
  File "./terrascript-iam.py", line 133, in <module>
    ts += terrascript.aws.r.config_configuration_aggregator\
AttributeError: module 'terrascript.aws.r' has no attribute 'config_configuration_aggregator'

terrascript.terraform class vs module?

This may be a dumb question, but I can't figure out how to get it to work, so I figured I'd ask here.

I have a script like this:

#!/usr/bin/env python3

import terrascript

s3_backend = terrascript.backend(
    "s3",
    bucket = "mybucket",
    key = "my.tfstate",
    region = "us-east-1"
    )

ts = terrascript.Terrascript()
ts += terrascript.terraform(backend = s3_backend)

ts += terrascript.terraform.d.remote_state(
    "another",
    backend = "s3",
    config = {
        'bucket': "mybucket",
         'key': "another.tfstate",
        'region': "us-east-1"
        }
    )

If I run this, I get an error:

Traceback (most recent call last):
  File "terrascript-modules.py", line 15, in <module>
    ts += terrascript.terraform.d.remote_state(
AttributeError: type object 'terraform' has no attribute 'd'

At this point, terrascript.terraform is a class:

>>> terrascript.terraform
<class 'terrascript.terraform'>

If I add import terrascript.terraform.d, I get a different error:

Traceback (most recent call last):
  File "terrascript-modules.py", line 14, in <module>
    ts += terrascript.terraform(backend = s3_backend)
TypeError: 'module' object is not callable

At this point, terrascript.terraform is a module:

>>> terrascript.terraform
<module 'terrascript.terraform' from '/usr/local/lib/python3.5/dist-packages/terrascript/terraform/__init__.py'>

I've tried a bunch of different stuff, but can't figure it out. :^(

It seems like the problem is that terrascript.terraform is both a module and a class; if I rename the class to Terraform in .../terrascript/__init__.py, things seem to work.

Is there some other way to do this that I'm missing?

Simplify tests

Replace the _Base class and all the string, dict, etc. stuff. Instead code tests into each Test* class.

Support community providers

The current tools/makecode.py script is unable to generate code for community providers (https://www.terraform.io/docs/providers/type/community-index.html) as the Github URL to a provider is hard-coded. The tools/makecode.py script must be changed to read providers.yml replacing PROVIDERS. providers.yml can then contain a link to the Github repository for the provider.

- name: shell
  repository: https://github.com/scottwinkler/terraform-provider-shell

vSphere: missing some data sources and resources

Error:

vm_template = d.vsphere_virtual_machine('vm_template',
AttributeError: module 'terrascript.vsphere.d' has no attribute 'vsphere_virtual_machine'

python-terrascript/terrascript/vsphere/d.py is missing the declaration for a vsphere_virtual_machine data source.

This data source is required by Terraform for cloning from an existing template. Which means for Terraform, the vsphere_virtual_machine is required as both a resource and data source.

Fix:
Adding the following fixes this issue:

# ~/vsphere/d.py
class vsphere_virtual_machine(_data): pass
virtual_machine = vsphere_virtual_machine

Additional missing data sources:
vsphere_custom_attribute
vsphere_datastore_cluster

Additional missing resources
vsphere_custom_attribute
vsphere_datastore_cluster
vsphere_storage_drs_vm_override

Suggestion of syntax to terrascript: "ts.write"

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())

exithook

PRINT = True

exithook()
print json(_CONFIG)

rename CONFIG to _CONFIG

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.