Skip to content

Commit

Permalink
Update mssql driver (#22)
Browse files Browse the repository at this point in the history
* fix setup (sqlcmd moved and changed in mssql image)

* switch from denisenkom mssql driver to microsoft official mssql driver (fork of denisenkom)

* update dev instructions

* run make generate

* update tf version matrix

* add setup for docker-compose in test workflow

* collect errors from retries during connection
  • Loading branch information
kristofferlind authored Sep 1, 2024
1 parent 590e771 commit 8927173
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 94 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use_nix
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ jobs:
matrix:
terraform:
- '1.0.*'
- '1.1.*'
- '1.8.*'
- '1.9.*'
steps:
- uses: KengoTODA/actions-setup-docker-compose@v1
with:
version: '1.29.2' # fairly old, matches docker-compose version from nix, update..
- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
Expand Down
24 changes: 1 addition & 23 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,5 @@
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
"cSpell.words": [
"denisenkom",
"ghaction",
"goarch",
"gomod",
"goreleaser",
"Gosec",
"hashicorp",
"kristofferlind",
"ldflags",
"azuresql",
"mssqldb",
"nvarchar",
"pinentry",
"securego",
"tflog",
"tfplugindocs",
"tfprotov",
"tfsdk",
"tftypes",
"trimpath"
]
}
}
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ NAME=azuresql
BINARY=terraform-provider-${NAME}
OS_ARCH=linux_amd64

VERSION=0.2.1
VERSION=0.3.0

build:
go mod download
go build -v

install: build
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}/${BINARY}_v${VERSION}

generate:
go mod tidy
Expand All @@ -33,11 +33,11 @@ test-release:
test-local-up:
docker-compose up -d
./scripts/wait-for-mssql.sh
docker-compose exec -T mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -Q "CREATE DATABASE test"
docker-compose exec -T mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -Q "CREATE DATABASE test2"
docker-compose exec -T mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -Q "sp_configure 'contained database authentication', 1; RECONFIGURE"
docker-compose exec -T mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -Q "CREATE DATABASE contained_test CONTAINMENT = PARTIAL"
docker-compose exec -T mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -Q "CREATE DATABASE contained_test2 CONTAINMENT = PARTIAL"
docker-compose exec -T mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -No -Q "CREATE DATABASE test"
docker-compose exec -T mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -No -Q "CREATE DATABASE test2"
docker-compose exec -T mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -No -Q "sp_configure 'contained database authentication', 1; RECONFIGURE"
docker-compose exec -T mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -No -Q "CREATE DATABASE contained_test CONTAINMENT = PARTIAL"
docker-compose exec -T mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'p@ssw0rd' -No -Q "CREATE DATABASE contained_test2 CONTAINMENT = PARTIAL"

test-local-down:
docker-compose down
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Using an unofficial terraform provider is pretty scary and it therefore needs to
This provider currently only has acceptance tests against a local docker based mssql instance and has received a little bit of manual testing against Azure SQL Server and Azure AD. It's my first time creating a terraform provider and I'm still a bit of a Golang newbie. **Do not use this unless you can first test all your changes in an environment that is ok to break**.

## Dependencies
Other than terraform boilerplate/plumbing this provider only utilizes an mssql driver, specifically github.com/denisenkom/go-mssqldb, which is also mentioned in Microsoft's documentation for working with mssql using golang.
Other than terraform boilerplate/plumbing this provider only utilizes an mssql driver, specifically github.com/microsoft/go-mssqldb, the official driver for MSSQL with Go from Microsoft.

## Development
This plugin is based on Terraform Plugin Framework and the documentation for that should therefore be a good source of information (https://www.terraform.io/plugin/framework).

If you have [direnv](https://direnv.net/) and [nix package manager](https://nixos.org/) installed. You should be able to just start using commands found in Makefile. If you don't want those you can look at shell.nix to see what's needed.

command | description
---|---
`nix-shell --pure` | open shell with all prerequisites installed
`make test` | build, install and execute tests

Check makefile for more options.
Expand All @@ -26,4 +27,4 @@ Check makefile for more options.
- aad_user is untested (feature is currently public preview)

## License
Based on [terraform-provider-scaffolding-framework](https://github.com/hashicorp/terraform-provider-scaffolding-framework) and therefore also has the same license.
Based on [terraform-provider-scaffolding-framework](https://github.com/hashicorp/terraform-provider-scaffolding-framework) and copied its license at time of creation.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
mssql:
image: mcr.microsoft.com/mssql/server
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "p@ssw0rd"
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ provider "azuresql" {
connection_string = "sqlserver://some-server.database.windows.net:1433?fedauth=ActiveDirectoryDefault"
}
# check https://github.com/denisenkom/go-mssqldb#azure-active-directory-authentication for other fedauth options
# check https://github.com/microsoft/go-mssqldb#azure-active-directory-authentication for other fedauth options
# You can manage multiple servers by utilizing provider aliases
provider "azuresql" {
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ provider "azuresql" {
connection_string = "sqlserver://some-server.database.windows.net:1433?fedauth=ActiveDirectoryDefault"
}

# check https://github.com/denisenkom/go-mssqldb#azure-active-directory-authentication for other fedauth options
# check https://github.com/microsoft/go-mssqldb#azure-active-directory-authentication for other fedauth options

# You can manage multiple servers by utilizing provider aliases
provider "azuresql" {
Expand Down
27 changes: 15 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ module github.com/kristofferlind/terraform-provider-azuresql
go 1.18

// non-terraform dependencies (created by running go mod init and go mod tidy in ./internal/manager)
require github.com/denisenkom/go-mssqldb v0.12.0
require github.com/microsoft/go-mssqldb v1.7.2

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0 // indirect
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

Expand All @@ -38,15 +41,15 @@ require (
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.4.0 // indirect
github.com/hashicorp/hc-install v0.3.1 // indirect
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
Expand All @@ -73,7 +76,7 @@ require (
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
Expand Down
Loading

0 comments on commit 8927173

Please sign in to comment.