Giter Club home page Giter Club logo

terraform-provider-sql's Introduction

Tests

View the docs on the Terraform Registry.

Terraform SQL Provider

This provider is an experiment using the new terraform-plugin-go SDK in order to utilize dynamic typing for its attributes.

terraform-provider-sql's People

Contributors

dependabot[bot] avatar paultyng 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

Watchers

 avatar  avatar  avatar  avatar  avatar

terraform-provider-sql's Issues

Provider configuration cannot read url argument

When trying terraform plan i get this error:

│ Error: Invalid provider configuration
│
│ Provider "registry.terraform.io/paultyng/sql" requires explicit
│ configuration. Add a provider block to the root module and configure the
│ provider's required arguments as described in the provider documentation.
│
╵
╷
│ Error: A `url` is required to connect to your database.
│
│   with provider["registry.terraform.io/paultyng/sql"],
│   on <empty> line 0:
│   (source code not available)
│
╵
ERRO[0016] 1 error occurred:
	* exit status 1

This is my code:

#sql.tf
terraform {
  required_providers {
    sql = {
      source = "paultyng/sql"
      version = "0.4.0"
    }
  }
}

provider "sql" {
  alias = "postgres"
  url = "postgres://postgres:password@localhost:5432/mydatabase"
}

resource "sql_migrate" "migrate" {
  migration {
    id = "create api_keys table"
    up = <<SQL
CREATE TABLE api_keys (
    api_key varchar(40) NOT NULL PRIMARY KEY,
    shop_id integer NOT NULL,
    portal varchar(20) NOT NULL
);
SQL

    down = "DROP TABLE IF EXISTS api_keys;"
  }
}

It looks like the url argument is not readable.
When I use export SQL_URL="postgres://postgres:password@localhost:5432/mydatabase" it starts working. But it is not a solution for me. I need to manage database, which is created also with terraform. in GCP (should not be relevant to this issue).

versions:

Terraform v1.0.11
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v4.0.0
  • trying to initialize paultyng/sql v0.4.0

Could we set url to empty string if we don't use this provider in some condition

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Run terraform -v to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.

Affected Resource(s)

Please list the resources as a list, for example:

  • opc_instance
  • opc_storage_volume

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.

Debug Output

Please provider a link to a GitHub Gist containing the complete debug output: https://www.terraform.io/docs/internals/debugging.html. Please do NOT paste the debug output in the issue; just paste a link to the Gist.

Panic Output

If Terraform produced a panic, please provide a link to a GitHub Gist containing the output of the crash.log.

Expected Behavior

What should have happened?

Actual Behavior

What actually happened?

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply

Important Factoids

Are there anything atypical about your accounts that we should know? For example: Running in EC2 Classic? Custom version of OpenStack? Tight ACLs?

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:

  • GH-1234

Error connecting to aws aurora db (postgres)

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Run terraform -v to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.

Terraform v1.0.7
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.28.0
+ provider registry.terraform.io/paultyng/sql v0.4.0

Your version of Terraform is out of date! The latest version
is 1.2.9. You can update by downloading from https://www.terraform.io/downloads.html```
### Affected Resource(s)
Please list the resources as a list, for example:
provider "sql" {
  alias = "postgres"
  url   = var.db_connection_string
}

data "sql_query" "create_audit_role" {
  provider = sql.postgres
  query = var.create_audit_role_query
}

data "sql_query" "create_extension" {
  provider = sql.postgres
  query = var.create_pgaudit_extension
}

data "sql_query" "alter_db_audit" {
  provider = sql.postgres
  query = "set pgaudit.log='All';"
  depends_on = [
    data.sql_query.create_audit_role,
    data.sql_query.create_extension
  ]
}

Expected Behavior

Supposed to connect and run sql query

Actual Behavior

Received:

Error: Plugin error
│ 
│   with provider["registry.terraform.io/paultyng/sql"].postgres,
│   on main.tf line 1, in provider "sql":
│    1: provider "sql" {
│ 
│ The plugin returned an unexpected error from plugin.(*GRPCProvider).ConfigureProvider: rpc error: code = Unknown desc =
│ ConfigureProvider - unable to ping database: failed to connect to `host=test.cluster-asdafascads.us-east-1.rds.amazonaws.com
│ user=postgres database=postgrestest`: dial error (dial tcp xx.xxx.xxx.xxx:5432: connect: operation timed out)

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform plan

Feature - client key,cert (mTLS)

Hello,

Are you going to add client key/cert feature to start working with mutual TLS? In GCP Cloud SQL database works in that mode and that's why your provider now is useless :|1

Regards,
Maciek

Snowflake support

I've tried connecting to a Snowflake account, but it failed (provider version 0.4.0).

This was the template for the connection string, as suggested here: jdbc:snowflake://<account_identifier>.snowflakecomputing.com/?<connection_params>.

Got this error:

unexpected datasource name scheme: jdbc

Support setting url in sql_migrate ressource

Hi,

Great provider ! I'm really missing to be able to specify the url in the ressource.
Basically I want to be able to do something like this:

locals {
  data = {
      "DB_A" = {
        "url" = "sql url goes here"
         "up_script" = "a/b/up.sql"
         "down_script" = "a/b/down.sql"
       }
      "DB_B" = {
        "url" = "sql url goes here"
         "up_script" = "a/b/up.sql"
         "down_script" = "a/b/down.sql"
       }
  }
}
resource "sql_migrate" "schema" {
  for_each = local.data
  url = each.value.url
  migration {
    up   = each.value.sql_up 
    down = each.value.sql_down
 }
}

Thanks in advance :)

suggestion : disallow update queries

I was playing with your provider. It will meet some of my needs but I will use another for updating databases (I may have to make my own) .

I ran a TF plan and found that it ran my query - it was expecting it to be a read and returning data - but i had put a "create user" in it and it created the user. I can see that you were only intending use for getting data to feed terraform.

You may want to run a check for any query that changes the database in any way and put a safeguard in it to ensure it does not run by giving an error message "updates to your database are not allowed"

provider.url: Unmarshaling unknown values is not supported / postpone database connection

Hi there,

Terraform Version

1.3.2

Affected Resource(s)

  • sql provider configuration itself

Terraform Configuration Files

provider "sql" {
    url = "postgres://${local.postgresql.login}:${local.postgresql.password}@${local.postgresql.host}:${local.postgresql.port}/postgres?sslmode=${local.postgresql.sslmode}"
}

Expected Behavior

The provider should have been configured.

Actual Behavior

The provider fails to pass configuration invoking this error:

 Error: Plugin error
│ 
│ The plugin returned an unexpected error from plugin6.(*GRPCProvider).ConfigureProvider: rpc error: code = Unknown desc = ConfigureProvider - unable to read url:
│ unmarshaling unknown values is not supported

Steps to Reproduce

Use any variable in the url parameter with a value not known during the planning phase.

Important Factoids

The connection string cannot be passed in advance in my case because the PostgreSQL server is provisioned during apply phase.
Postgresql provider (https://registry.terraform.io/providers/cyrilgdn/postgresql/) is ok with it.

CloudSQL support and url overrides in resource

Hi @paultyng ,

I recently stumbled upon this provider and when I was looking into provisioning database instance users with terraform.
On GCP there is an API to create users on CloudSQL instances, however they will be created as "superusers".
For readonly-users or similar, you have to drop down to SQL. By default CloudSQL instances are not directly reachable, but can be reached through a proxy. Luckily the proxy can be embedded directly in Go and even be used as SQL driver.

I went ahead and implemented a minimal provider myself earlier: https://github.com/trevex/terraform-provider-cloudsql

But if you are open to it I can see if I can draft up a PR implementing CloudSQL-support in this provider.

URL parameter is not working in provider setup

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Run terraform -v to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.
v1.1.3

Affected Resource(s)

Please list the resources as a list, for example:
-data "sql_query"

Terraform Configuration Files

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
    
    sql = {
      source = "paultyng/sql"
      version = "0.4.0"
      
    }
  }
  required_version = ">= 0.14.9"
  backend "azurerm" {
    resource_group_name  = "blabla"
    storage_account_name = "blabal"
    container_name       = "tfstate"
    key                  = "bla.stage-1.tfstate"
  }
}

provider "azurerm" {
  features {}
}

provider "sql" {
  alias = "mysql"
  url   = "mysql://SfEuj2w4nrHeHoD6@my-server-name:<redacted password>@tcp(my-server-name:3306)/mysql?tls=true"
  max_idle_conns = 20
  max_open_conns = 4
}



data "sql_query" "test" {
  query = "CREATE USER 'test_user'@'%';"
}.

Debug Output

│ Error: Invalid provider configuration
│ 
│ Provider "registry.terraform.io/paultyng/sql" requires explicit configuration. Add a provider block to the root module and configure the provider's required arguments as described in
│ the provider documentation.
│ 
╵
╷
│ Error: A `url` is required to connect to your database.
│ 
│   with provider["registry.terraform.io/paultyng/sql"],
│   on <empty> line 0:
│   (source code not available)

Steps to Reproduce

set up provider similar to above without setting SQL_URL environment variable
terraform apply
get error
set SQL_URL environment variable
everything runs just fine

Important Factoids

Database is on Azure, requires SSL, running on my local in Windows WSL Linux

MySQL Syntax error on multiple SQL command

Hi there,

I have an issue with multiple insert by using the migration directory or without it.
I always getting MySQL syntax error if I have multiple line and any command but if I remove the rest and each file I add just one MySQL command it works.

INSERT INTO ReferenceData.topics (topicName,isTenor,batchSize,intervalTime,lambdaId,description) VALUES('derivedData',0,1500,10,NULL,'Data generated by KDA derived from the fiat topics'),('M1',1,1500,5,NULL,'Forward tenor'),('M10',1,1500,10,NULL,'Forward tenor'),('M11',1,1500,10,NULL,'Forward tenor'),('M12',1,1500,10,NULL,'Forward tenor'),('M18',1,1500,60,NULL,'Forward tenor'),('M2',1,1500,10,NULL,'Forward tenor'),('M21',1,1500,300,NULL,'Forward tenor'),('M3',1,1500,10,NULL,'Forward tenor'),('M4',1,1500,10,NULL,'Forward tenor');
INSERT INTO ReferenceData.topics (topicName,isTenor,batchSize,intervalTime,lambdaId,description) VALUES('M5',1,1500,10,NULL,'Forward tenor'),('M6',1,1500,10,NULL,'Forward tenor'),('M7',1,1500,10,NULL,'Forward tenor'),('M8',1,1500,10,NULL,'Forward tenor'),('M9',1,1500,10,NULL,'Forward tenor'),('ON',1,1500,5,NULL,'Forward tenor'),('SN',1,1500,5,NULL,'Forward tenor'),('SPT',1,1500,1,NULL,'Spot data'),('TN',1,1500,5,NULL,'Forward tenor'),('W1',1,1500,5,NULL,'Forward tenor');
INSERT INTO ReferenceData.topics (topicName,isTenor,batchSize,intervalTime,lambdaId,description) VALUES('W2',1,1500,5,NULL,'Forward tenor'),('W3',1,1500,5,NULL,'Forward tenor'),('Y2',1,1500,600,NULL,'Forward tenor'),('Y3',1,1500,6000,NULL,'Forward tenor'),('Y4',1,1500,6000,NULL,'Forward tenor'),('Y5',1,1500,6000,NULL,'Forward tenor');

Terraform Version

v1.3.6

Panic Output

│ Error: Plugin error
│
│ The plugin returned an unexpected error from plugin6.(*GRPCProvider).ApplyResourceChange: rpc error: code = Unknown
│ desc = Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
│ version for the right syntax to use near 'INSERT INTO ReferenceData.topics
│ (topicName,isTenor,batchSize,intervalTime,lambd' at line 2

data.query thought error sql.RawBytes about data type

I'm having a problem with Aurora Cluster Mysql 5.7 data type

Terraform Version

Terraform v1.1.7
on linux_amd64

Affected Resource(s)

Please list the resources as a list, for example:

  • data.sql_query

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

data "sql_query" "users" {

  query = "select `user` from mysql.`user`"
  provider = sql.mysql_db_management
}
output "test"{
  value = data.sql_query.users.result
}

Debug Output

mysql_grant.devops_all_schemas_grant: Refreshing state... [id=test.user@%:`gk`]
╷
│ Error: unable to convert value from database: unable to determine type for "user": unexpected type for "user": "CHAR" (slice sql.RawBytes)
│ 
│   with data.sql_query.users,
│   on terrafile.tf line 40, in data "sql_query" "users":
│   40: data "sql_query" "users" {
│ 
╵

but if I change the query for

data "sql_query" "users" {

  query ="select CAST(`user` AS CHAR CHARACTER SET utf8) as user_name from mysql.`user`"
  provider = sql.mysql_db_management
}
output "test"{
  value = data.sql_query.users.result
}

Debug Output

  + test = [
      + {
          + user_name = "user.user"
        },
      + {
          + user_name = "airflow_dag"
        },
.....

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.