Skip to content

Commit

Permalink
Merge pull request #71 from betr-io/update-dependencies
Browse files Browse the repository at this point in the history
Maintenance
  • Loading branch information
magne authored Dec 29, 2023
2 parents eb5a75e + e5fb664 commit b5224f9
Show file tree
Hide file tree
Showing 18 changed files with 597 additions and 636 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ trim_trailing_whitespace = true
indent_size = 4

[Makefile]
indent_size = 8
indent_style = tab
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Run unit tests
run: make test

- name: Run acceptance tests
run: |
make docker-start
TESTARGS=-count=1 make testacc-local
make docker-stop
sh -c 'TESTARGS=-count=1 ./wait-for localhost:1433 -- make testacc-local'
make docker-stop
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.0] - 2023-12-29

### Changed

- Make minimum terraform version 1.5. Versions less than this are no longer supported ([endoflife.date](https://endoflife.date/terraform))
- Upgraded to go version 1.21.
- Upgraded dependencies.
- Replaced github.com/denisenkom/go-mssqldb with github.com/microsoft/go-mssqldb.
- Upgraded terraform dependencies.
- Improve Makefile.

## [0.2.7] - 2022-12-16

### Fixed
Expand Down
77 changes: 57 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,83 @@
SHELL := /bin/bash

OPERATING_SYSTEM=Linux
TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=betr.io
NAMESPACE=betr
NAME=mssql
BINARY=terraform-provider-${NAME}
VERSION=0.2.7
OS_ARCH=linux_amd64
TERRAFORM=terraform
VERSION = 0.3.0

TERRAFORM = terraform
TERRAFORM_VERSION = "~> 1.5"

GO = go
MODULE = $(shell env GO111MODULE=on $(GO) list -m)
PKGS = $(shell env GO111MODULE=on $(GO) list ./... | grep -v /vendor/)
TESTPKGS = $(shell env GO111MODULE=on $(GO) list -f \
'{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' \
$(PKGS))

ifeq ($(OS),Windows_NT)
OPERATING_SYSTEM=Windows
ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
OS_ARCH=windows_amd64
else
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
OS_ARCH=windows_amd64
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
OS_ARCH=windows_386
endif
endif
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
OPERATING_SYSTEM=Linux
_OS=linux
endif
ifeq ($(UNAME_S),Darwin)
OPERATING_SYSTEM=MacOS
_OS=darwin
endif
UNAME_P := $(shell uname -p)
ifeq ($(UNAME_P),x86_64)
OS_ARCH=$(_OS)_amd64
endif
ifneq ($(filter %86,$(UNAME_P)),)
OS_ARCH=$(_OS)_386
endif
ifneq ($(filter arm%,$(UNAME_P)),)
OS_ARCH=$(_OS)_arm
endif
endif

INSTALL_PATH=~/.terraform.d/plugins/$(shell basename $(shell dirname $(MODULE)))/$(shell basename $(MODULE) | cut -d'-' -f3)/${VERSION}/${OS_ARCH}

default: install

build:
go build -o ${BINARY}
CGO_ENABLED=0 $(GO) build -o $(shell basename $(MODULE))

release:
# Runs goreleaser locally (testrun)
goreleaser release --rm-dist --skip-sign --skip-publish

install: build
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mkdir -p $(INSTALL_PATH)
mv $(shell basename $(MODULE)) $(INSTALL_PATH)/

test:
go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
echo $(TESTPKGS) | xargs -t -n4 $(GO) test $(TESTARGS) -timeout=30s -parallel=4

testacc:
if [ -f .local.env ]; then source .local.env; fi && TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
if [ -f .local.env ]; then source .local.env; fi && TF_ACC=1 TERRAFORM_VERSION=$(TERRAFORM_VERSION) $(GO) test $(TESTPKGS) -v $(TESTARGS) -timeout 120m

testacc-local:
if [ -f .local.env ]; then source .local.env; fi && TF_ACC_LOCAL=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
if [ -f .local.env ]; then source .local.env; fi && TF_ACC_LOCAL=1 TERRAFORM_VERSION=$(TERRAFORM_VERSION) $(GO) test $(TESTPKGS) -v $(TESTARGS) -timeout 120m

docker-start:
cd test-fixtures/local && ${TERRAFORM} init && ${TERRAFORM} apply -auto-approve -var="operating_system=${OPERATING_SYSTEM}"
cd test-fixtures/local && export TERRAFORM_VERSION=$(TERRAFORM_VERSION) && ${TERRAFORM} init && ${TERRAFORM} apply -auto-approve -var="operating_system=${OPERATING_SYSTEM}"

docker-stop:
cd test-fixtures/local && ${TERRAFORM} destroy -auto-approve -var="operating_system=${OPERATING_SYSTEM}"
cd test-fixtures/local && TERRAFORM_VERSION=$(TERRAFORM_VERSION) ${TERRAFORM} destroy -auto-approve -var="operating_system=${OPERATING_SYSTEM}"

azure-create:
cd test-fixtures/all && ${TERRAFORM} init && ${TERRAFORM} apply -auto-approve -var="operating_system=${OPERATING_SYSTEM}"
cd test-fixtures/all && export TERRAFORM_VERSION=$(TERRAFORM_VERSION) && ${TERRAFORM} init && ${TERRAFORM} apply -auto-approve

azure-destroy:
cd test-fixtures/all && ${TERRAFORM} destroy -auto-approve -var="operating_system=${OPERATING_SYSTEM}"
cd test-fixtures/all && TERRAFORM_VERSION=$(TERRAFORM_VERSION) ${TERRAFORM} destroy -auto-approve
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
## Requirements

- [Terraform](https://www.terraform.io/downloads.html) 0.13.x
- [Go](https://golang.org/doc/install) 1.18 (to build the provider plugin)
- [Terraform](https://www.terraform.io/downloads.html) 1.5.x
- [Go](https://golang.org/doc/install) 1.21 (to build the provider plugin)

I recommend using [tfvm](https://github.com/cbuschka/tfvm) to manage Terraform versions. The `Makefile` assumes that `tfvm` is installed to use the correct version of Terraform when running tests.

## Usage

```hcl
terraform {
required_version = "~> 0.13"
required_version = "~> 1.5"
required_providers {
mssql = {
versions = "~> 0.2.2"
source = "betr.io/betr/mssql"
versions = "~> 0.2"
source = "betr-io/mssql"
}
}
}
Expand Down Expand Up @@ -46,7 +48,7 @@ make install

## Developing the provider

If you wish to work on the provider, you'll first need [Go](https://www.golang.org) installed on your machine (version 1.18+).
If you wish to work on the provider, you'll first need [Go](https://www.golang.org) installed on your machine (version 1.21+).

To compile the provider, run `make build`. This will build the provider.

Expand Down
78 changes: 32 additions & 46 deletions examples/azure/main.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
terraform {
required_version = "~> 0.13"
required_version = "~> 1.5"
required_providers {
azuread = {
source = "hashicorp/azuread"
version = "~> 1.0"
version = "~> 2.47"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.34.0"
version = "~> 3.85"
}
mssql = {
source = "betr-io/mssql"
version = "0.1.0"
version = "~> 0.2"
}
random = {
source = "hashicorp/random"
version = "~> 3.0.0"
version = "~> 3.6"
}
time = {
source = "hashicorp/time"
version = "0.6.0"
version = "~> 0.10"
}
}
}
Expand Down Expand Up @@ -78,57 +78,41 @@ locals {
prefix = "${var.prefix}-${substr(random_string.random.result, 0, 4)}"
}

resource "random_password" "sa" {
length = 32
special = true
keepers = {
name = "${local.prefix}-sa"
}
}

resource "random_password" "user" {
length = 32
special = true
keepers = {
name = "${local.prefix}-user"
}
}

# An Azure AD group assigned the role 'Directory Readers'. The Azure SQL Server needs to be assigned to this group to enable external logins.
data "azuread_group" "sql_servers" {
name = var.sql_servers_group
display_name = var.sql_servers_group
}

# An Azure AD service principal used as Azure Administrator for the Azure SQL Server resource
resource "azuread_application" "sa" {
name = random_password.sa.keepers.name
homepage = "https://test.example.com"
display_name = "${local.prefix}-sa"
web {
homepage_url = "https://test.example.com"
}
}

resource "azuread_service_principal" "sa" {
application_id = azuread_application.sa.application_id
client_id = azuread_application.sa.client_id
}

resource "azuread_service_principal_password" "sa" {
service_principal_id = azuread_service_principal.sa.id
value = random_password.sa.result
end_date_relative = "360h"
service_principal_id = azuread_service_principal.sa.object_id
}

# An Azure AD service principal used to test creating an external login to the Azure SQL server resource
resource "azuread_application" "user" {
name = random_password.user.keepers.name
homepage = "https://test.example.com"
display_name = "${local.prefix}-user"
web {
homepage_url = "https://test.example.com"
}
}

resource "azuread_service_principal" "user" {
application_id = azuread_application.user.application_id
client_id = azuread_application.user.client_id
}

resource "azuread_service_principal_password" "user" {
service_principal_id = azuread_service_principal.user.id
value = random_password.user.result
end_date_relative = "360h"
}

# Temporary resource group
Expand All @@ -145,11 +129,11 @@ resource "azurerm_mssql_server" "sql_server" {

version = "12.0"
administrator_login = "SuperAdministrator"
administrator_login_password = random_password.sa.result
administrator_login_password = azuread_service_principal_password.sa.value

azuread_administrator {
tenant_id = var.tenant_id
object_id = azuread_service_principal.sa.application_id
object_id = azuread_service_principal.sa.client_id
login_username = azuread_service_principal.sa.display_name
}

Expand All @@ -163,13 +147,12 @@ resource "azuread_group_member" "sql" {
member_object_id = azurerm_mssql_server.sql_server.identity[0].principal_id
}

resource "azurerm_sql_firewall_rule" "sql_server_fw_rule" {
count = length(var.local_ip_addresses)
name = "AllowIP ${count.index}"
resource_group_name = azurerm_mssql_server.sql_server.resource_group_name
server_name = azurerm_mssql_server.sql_server.name
start_ip_address = var.local_ip_addresses[count.index]
end_ip_address = var.local_ip_addresses[count.index]
resource "azurerm_mssql_firewall_rule" "sql_server_fw_rule" {
count = length(var.local_ip_addresses)
name = "AllowIP ${count.index}"
server_id = azurerm_mssql_server.sql_server.id
start_ip_address = var.local_ip_addresses[count.index]
end_ip_address = var.local_ip_addresses[count.index]
}

# The Azure SQL Database used in tests
Expand Down Expand Up @@ -230,6 +213,7 @@ output "instance" {
login_name = mssql_login.server.login_name,
password = mssql_login.server.password
}
sensitive = true
}


Expand All @@ -254,7 +238,7 @@ resource "mssql_user" "database" {
}
}
database = azurerm_mssql_database.db.name
username = random_password.database.keepers.username
username = "${local.prefix}-user"
password = random_password.database.result
}

Expand All @@ -263,6 +247,7 @@ output "database" {
username = mssql_user.database.username,
password = mssql_user.database.password
}
sensitive = true
}


Expand All @@ -275,7 +260,7 @@ resource "mssql_user" "external" {
host = azurerm_mssql_server.sql_server.fully_qualified_domain_name
azure_login {
tenant_id = var.tenant_id
client_id = azuread_service_principal.sa.application_id
client_id = azuread_service_principal.sa.client_id
client_secret = azuread_service_principal_password.sa.value
}
}
Expand All @@ -286,7 +271,8 @@ resource "mssql_user" "external" {
output "external" {
value = {
tenant_id = var.tenant_id
client_id = azuread_service_principal.user.application_id
client_id = azuread_service_principal.user.client_id
client_secret = azuread_service_principal_password.user.value
}
sensitive = true
}
Loading

0 comments on commit b5224f9

Please sign in to comment.