raymondbutcher / pretf

Generate Terraform code with Python
https://pretf.readthedocs.io/
MIT License
104 stars 14 forks source link

Unable to yield backend #52

Closed Dyllaann closed 4 years ago

Dyllaann commented 4 years ago

Heyo! I am now up and running with the latest version, and the flatten structure seems to be working. However, due to the flatten structure I have multiple states, and those have their own place to live. With Jinjaform, I was able to have this as my backend.tf

terraform {
  backend "azurerm" {
    storage_account_name = "<<Removed>>"
    container_name       = "<<Removed>>"
    key                  = "{{ var.domain }}/{{ var.environment }}/{{ var.customer }}/terraform.tfstate"

    access_key = "{{ env('TF_VAR_remoteStateAccessKey') }}"
  }
}

As far as I can see, there is no real way to support this in pretf as it's not really a block, am I right? I did found the aws provider, but I think that mostly has to do with authentication. Azure just has a static key to interact with the storage account.

raymondbutcher commented 4 years ago

Yes, this is a block too. You translate the HCL into a Python dict almost exactly as-is but when you see 2 labels (such as backend "azurerm") you add another level of nesting there too.

This should work:

from pretf.api import block

def pretf_blocks(var):
    yield block("terraform", {
        "backend": {
            "azurerm": {
                "storage_account_name": var.storage_account_name,
                "storage_account_name": var.container_name,
                "key": f"{var.domain}/{var.environment}/{var.customer}/terraform.tfstate",
                "access_key": var.remoteStateAccessKey,
            },
        },
    })

In the unreleased version in master, this should work:

from pretf.blocks import terraform

def pretf_blocks(var):
    yield terraform.backend.azurerm(
        storage_account_name=var.storage_account_name,
        storage_account_name=var.container_name,
        key=f"{var.domain}/{var.environment}/{var.customer}/terraform.tfstate",
        access_key=var.remoteStateAccessKey,
    )