starhawking / python-terrascript

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

create multiple resources #141

Closed pathcl closed 3 years ago

pathcl commented 3 years ago

Hey there! thanks for making this project open source.

Im trying to create 2 resources out of the same provider however:

$ cat create-config.py 
import terrascript
import terrascript.resource

def create_shovel(vhosts):
    for vhost in vhosts:
        config = terrascript.Terrascript()
        config += terrascript.resource.rabbitmq_shovel("shovel",name="imyshovel",vhost=vhost, info={"source_uri": "ampq:///source", "destination_uri": "ampq:///test"})
        print(config)

create_shovel(['one','two'])

$ python3 create-config.py > config.tf.json
$ cat config.tf.json
{
  "resource": {
    "rabbitmq_shovel": {
      "shovel": {
        "name": "imyshovel",
        "vhost": "one",
        "info": {
          "source_uri": "ampq:///source",
          "destination_uri": "ampq:///test"
        }
      }
    }
  }
}
{
  "resource": {
    "rabbitmq_shovel": {
      "shovel": {
        "name": "imyshovel",
        "vhost": "two",
        "info": {
          "source_uri": "ampq:///source",
          "destination_uri": "ampq:///test"
        }
      }
    }
  }
}
$ terraform plan

Error: Extraneous data after value

  on config.tf.json line 15:
  15: {

Extra characters appear after the JSON value.

Is there an example I can imitate? I couldn't find anything

pathcl commented 3 years ago

I think I figured it out. Now I'd only need multiple providers maybe.

$ cat create-config.py
import terrascript
import terrascript.resource

config = terrascript.Terrascript()
for item in ['one', 'two']:
    config += terrascript.resource.rabbitmq_shovel(item, name="imyshovel",vhost=item, info={"source_uri": "ampq:///source", "destination_uri": "ampq:///test"})

print(config)

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # rabbitmq_shovel.one will be created
  + resource "rabbitmq_shovel" "one" {
      + id    = (known after apply)
      + name  = "imyshovel"
      + vhost = "one"

      + info {
          + ack_mode                         = "on-confirm"
          + destination_add_timestamp_header = false
          + destination_protocol             = "amqp091"
          + destination_uri                  = (sensitive value)
          + reconnect_delay                  = 1
          + source_protocol                  = "amqp091"
          + source_uri                       = (sensitive value)
        }
    }

  # rabbitmq_shovel.two will be created
  + resource "rabbitmq_shovel" "two" {
      + id    = (known after apply)
      + name  = "imyshovel"
      + vhost = "two"

      + info {
          + ack_mode                         = "on-confirm"
          + destination_add_timestamp_header = false
          + destination_protocol             = "amqp091"
          + destination_uri                  = (sensitive value)
          + reconnect_delay                  = 1
          + source_protocol                  = "amqp091"
          + source_uri                       = (sensitive value)
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.