Skip to content

Commit

Permalink
sql_query data source
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Nov 17, 2020
1 parent f19b2bd commit 8a8d8d3
Show file tree
Hide file tree
Showing 28 changed files with 1,706 additions and 402 deletions.
36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Acceptance Tests",
"type": "go",
"request": "launch",
"mode": "test",
// this assumes your workspace is the root of the repo
"program": "${fileDirname}",
"env": {
"TF_ACC": "1",
},
"args": [],
},
// You could pair this configuration with an exec configuration that runs Terraform as
// a compound launch configuration:
// https://code.visualstudio.com/docs/editor/debugging#_compound-launch-configurations
{
"name": "Debug - Attach External CLI",
"type": "go",
"request": "launch",
"mode": "debug",
// this assumes your workspace is the root of the repo
"program": "${workspaceFolder}",
"env": {},
"args": [
// pass the debug flag for reattaching
"-debug",
],
}
]
}
67 changes: 6 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,10 @@
Terraform Provider Scaffolding
==================
# Terraform SQL Provider

This repository is a *template* for a [Terraform](https://www.terraform.io) provider. It is intended as a starting point for creating Terraform providers, containing:
This provider is an experiment using the new [terraform-plugin-go](https://github.com/hashicorp/terraform-plugin-go) SDK in order to utilize dynamic typing for its attributes.

- A resource, and a data source (`internal/provider/`),
- Documentation (`website/`),
- Miscellanious meta files.

These files contain boilerplate code that you will need to edit to create your own Terraform provider. A full guide to creating Terraform providers can be found at [Writing Custom Providers](https://www.terraform.io/docs/extend/writing-custom-providers.html).
Currently it only has a single data source (`sql_query`) which lets you execute a query against Microsoft SQL Server, PostreSQL, MySQL or other data base engines that are protocol compatible (Maria, CockroachDB, etc.).

Please see the [GitHub template repository documentation](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template) for how to create a new repository from this template on GitHub.
## TODO


Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) >= 0.12.x
- [Go](https://golang.org/doc/install) >= 1.12

Building The Provider
---------------------

1. Clone the repository
1. Enter the repository directory
1. Build the provider using the Go `install` command:
```sh
$ go install
```

Adding Dependencies
---------------------

This provider uses [Go modules](https://github.com/golang/go/wiki/Modules).
Please see the Go documentation for the most up to date information about using Go modules.

To add a new dependency `github.com/author/dependency` to your Terraform provider:

```
go get github.com/author/dependency
go mod tidy
```

Then commit the changes to `go.mod` and `go.sum`.


Using the provider
----------------------

Fill this in for each provider

Developing the Provider
---------------------------

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).

To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.

In order to run the full suite of Acceptance tests, run `make testacc`.

*Note:* Acceptance tests create real resources, and often cost money to run.

```sh
$ make testacc
```
* Better decimal handling (maybe not string? use big.float?)
* Convert JSON (or other structured db types) to native HCL types, not strings
41 changes: 41 additions & 0 deletions docs/data-sources/query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
page_title: "sql_query Data Source - terraform-provider-sql"
subcategory: ""
description: |-
The sql_query datasource allows you to execute a SQL query against the database of your choice.
---

# Data Source `sql_query`

The `sql_query` datasource allows you to execute a SQL query against the database of your choice.

## Example Usage

```terraform
data "sql_query" "test" {
query = "select 1 as number, 'foo' as string"
}
locals {
# The number column in this case is a Terraform "Number" type
# so you can use it as such:
math = 1 + data.sql_query.test.result[0].number
}
output "math" {
value = local.math
}
```

## Schema

### Required

- **query** (String, Required) The query to execute. The types in this query will be reflected in the typing of the `result` attribute.

### Read-only

- **id** (String, Read-only, Deprecated) This attribute is only present for some compatibility issues and should not be used. It will be removed in a future version.
- **result** (List of Dynamic, Read-only) The result of the query. This will be a list of objects. Each object will have attributes with names that match column names and types that match column types. The exact translation of types is dependent upon the database driver.


47 changes: 47 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
page_title: "sql Provider"
subcategory: ""
description: |-
---

# sql Provider



## Example Usage

```terraform
# connect to Microsoft SQL Server
provider "sql" {
alias = "mssql"
url = "sqlserver://sa:password@localhost:1433"
}
# connect to PostgreSQL
provider "sql" {
alias = "postgres"
url = "postgres://postgres:password@localhost:5432/mydatabase?sslmode=disable"
}
# connect to CockroachDB
provider "sql" {
alias = "cockroach"
# use the postgres driver for CockroachDB
url = "postgres://root@localhost:26257/events?sslmode=disable"
}
# connect to a MySQL server
provider "sql" {
alias = "mysql"
url = "mysql://root:password@tcp(localhost:3306)/mysql"
}
```

## Schema

### Optional

- **max_idle_conns** (Number, Optional) Sets the maximum number of connections in the idle connection pool. Default is `2`. See Go's documentation on [DB.SetMaxIdleConns](https://golang.org/pkg/database/sql/#DB.SetMaxIdleConns).
- **max_open_conns** (Number, Optional) Sets the maximum number of open connections to the database. Default is `0` (unlimited). See Go's documentation on [DB.SetMaxOpenConns](https://golang.org/pkg/database/sql/#DB.SetMaxOpenConns).
- **url** (String, Optional) Database connection strings are specified via URLs. The URL format is driver dependent but generally has the form: `dbdriver://username:password@host:port/dbname?param1=true&param2=false`. You can optionally set the `SQL_URL` environment variable instead.
13 changes: 13 additions & 0 deletions examples/data-sources/sql_query/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data "sql_query" "test" {
query = "select 1 as number, 'foo' as string"
}

locals {
# The number column in this case is a Terraform "Number" type
# so you can use it as such:
math = 1 + data.sql_query.test.result[0].number
}

output "math" {
value = local.math
}
24 changes: 24 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# connect to Microsoft SQL Server
provider "sql" {
alias = "mssql"
url = "sqlserver://sa:password@localhost:1433"
}

# connect to PostgreSQL
provider "sql" {
alias = "postgres"
url = "postgres://postgres:password@localhost:5432/mydatabase?sslmode=disable"
}

# connect to CockroachDB
provider "sql" {
alias = "cockroach"
# use the postgres driver for CockroachDB
url = "postgres://root@localhost:26257/events?sslmode=disable"
}

# connect to a MySQL server
provider "sql" {
alias = "mysql"
url = "mysql://root:password@tcp(localhost:3306)/mysql"
}
7 changes: 7 additions & 0 deletions examples/provider/tf.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
sql = {
source = "paultyng/sql"
}
}
}
34 changes: 31 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
module github.com/hashicorp/terraform-provider-scaffolding
module github.com/paultyng/terraform-provider-sql

go 1.12
go 1.15

require github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.1
require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.15 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect
github.com/denisenkom/go-mssqldb v0.9.0
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/hashicorp/go-plugin v1.3.0
github.com/hashicorp/terraform-plugin-docs v0.2.0
github.com/hashicorp/terraform-plugin-go v0.1.1-0.20201117024036-b9d161518a6d
github.com/hashicorp/terraform-plugin-sdk/v2 v2.2.0
github.com/jackc/pgx/v4 v4.9.2
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/ory/dockertest v3.3.5+incompatible
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gotest.tools v2.2.0+incompatible // indirect
)

// see https://github.com/ory/dockertest/issues/204
replace golang.org/x/sys => golang.org/x/sys v0.0.0-20190830141801-acfa387b8d69

// replace github.com/hashicorp/terraform-plugin-go => ../../hashicorp/terraform-plugin-go
Loading

0 comments on commit 8a8d8d3

Please sign in to comment.