aidanmelen / terraform-provider-snowsql

Terraform SnowSQL provider
https://registry.terraform.io/providers/aidanmelen/snowsql/latest
Other
22 stars 10 forks source link

bug: import does not work #79

Closed aidanmelen closed 1 year ago

aidanmelen commented 1 year ago

The snowsql import does not import the resource statements correctly.

Terraform Version

n/a

Affected Resource(s)

Please list the resources as a list, for example:

Terraform Configuration Files

resource "snowsql_exec" "role" {
  name = "my_role"

  create {
    statements = "CREATE ROLE IF NOT EXISTS my_role"
  }

  read {
    statements = "SHOW ROLES LIKE 'my_role'"
  }

  delete {
    statements = "DROP ROLE IF EXISTS my_role"
  }
}

Expected Behavior

What should have happened?

the create, read, and delete statements should be in the resource state.

Actual Behavior

{
  "version": 4,
  "terraform_version": "1.0.5",
  "serial": 152,
  "lineage": "xxxxxxxxxxxxxxx",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "snowsql_exec",
      "name": "role",
      "provider": "provider[\"registry.terraform.io/aidanmelen/snowsql\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "create": [],
            "delete": [],
            "id": "my_role",
            "name": null,
            "read": [],
            "read_results": "",
            "update": []
          },
          "sensitive_attributes": [],
          "private": "xxxxxxx=="
        }
      ]
    }
  ]
}
aidanmelen commented 1 year ago

terraform provider docs for import: https://developer.hashicorp.com/terraform/tutorials/providers/provider-import#implement-import

aidanmelen commented 1 year ago

After looking further investigation, the import does work; however, it does not set any of the statements blocks. Therefore it would take either the user providing the statements in the resource id which would be gross OR we would need to know how to pull the statements from snowflake during the read which isn't going to happen.

Instead, we can customize how the create statements to behave by adding IF NOT EXIST/OR REPLACE. For example:

This terraform will only create the role when the role doesn't already exists:

resource "snowsql_exec" "role" {
  create {
    statements = "CREATE ROLE IF NOT EXISTS my_role"
  }

  delete {
    statements = "DROP ROLE IF EXISTS my_role"
  }
}

This is effectively a terraform import.