diff --git a/Makefile b/Makefile index dff7c2e..9564a0d 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,59 @@ TESTARGS ?= TEST_COUNT ?= 1 TEST_TIMEOUT ?= 20m +BINARY = terraform-provider-unifi +VERSION = 99.0.0 +PLUGIN_DIR = registry.terraform.io/filipowm/unifi/$(VERSION) + +LOCAL_OS = $(shell go env GOOS) +LOCAL_ARCH = $(shell go env GOARCH) +LOCAL_PLATFORM = $(LOCAL_OS)_$(LOCAL_ARCH) + +DEPLOY_HOST ?= +TF_DIR ?= + .PHONY: default default: build +# Build for the local platform .PHONY: build build: - go install + go build -o $(BINARY) . + +# Build and install into local Terraform plugin directory +.PHONY: install +install: build + mkdir -p ~/.terraform.d/plugins/$(PLUGIN_DIR)/$(LOCAL_PLATFORM) + cp $(BINARY) ~/.terraform.d/plugins/$(PLUGIN_DIR)/$(LOCAL_PLATFORM)/$(BINARY) + +# Cross-compile for Linux ARM64 (UDM-SE, docker-host, etc.) +.PHONY: build-linux-arm64 +build-linux-arm64: + GOOS=linux GOARCH=arm64 go build -o $(BINARY)_linux_arm64 . + +# Cross-compile for Linux AMD64 +.PHONY: build-linux-amd64 +build-linux-amd64: + GOOS=linux GOARCH=amd64 go build -o $(BINARY)_linux_amd64 . + +# Deploy to a remote host via scp +# Usage: make deploy DEPLOY_HOST=root@192.168.1.1 +# make deploy DEPLOY_HOST=root@192.168.1.1 TF_DIR=/root/terraform +.PHONY: deploy +deploy: build-linux-arm64 + @if [ -z "$(DEPLOY_HOST)" ]; then echo "Error: set DEPLOY_HOST (e.g. make deploy DEPLOY_HOST=root@192.168.1.1)"; exit 1; fi + ssh $(DEPLOY_HOST) 'mkdir -p ~/.terraform.d/plugins/$(PLUGIN_DIR)/linux_arm64' + scp $(BINARY)_linux_arm64 $(DEPLOY_HOST):~/.terraform.d/plugins/$(PLUGIN_DIR)/linux_arm64/$(BINARY) + @if [ -n "$(TF_DIR)" ]; then \ + echo "Reinitializing Terraform on $(DEPLOY_HOST)..."; \ + ssh $(DEPLOY_HOST) 'cd $(TF_DIR) && rm -f .terraform.lock.hcl && terraform init'; \ + fi .PHONY: testacc testacc: go build ./... TF_ACC=1 go test $(TEST) -v -count $(TEST_COUNT) -timeout $(TEST_TIMEOUT) $(TESTARGS) + +.PHONY: clean +clean: + rm -f $(BINARY) $(BINARY)_linux_arm64 $(BINARY)_linux_amd64 diff --git a/README.md b/README.md index 4d98405..d1906e4 100644 --- a/README.md +++ b/README.md @@ -26,56 +26,76 @@ Use a hard-wired connection to your controller to use this provider. ## Installation -### From This Fork (Recommended) +### Building from Source -This fork includes bug fixes not yet in the upstream provider. To install: +This repo includes the `go-unifi` SDK as a local module (in `../go-unifi`). Both are built together — no external repo references needed. -1. Build the provider: - ```bash - git clone https://github.com/shadyeip/terraform-provider-unifi.git - cd terraform-provider-unifi - go build -o terraform-provider-unifi - ``` +**Prerequisites:** Go 1.23+, Terraform 1.0+ -2. Create the plugin directory and copy the binary: - ```bash - mkdir -p ~/.terraform.d/plugins/registry.terraform.io/filipowm/unifi/99.0.0/darwin_arm64 - cp terraform-provider-unifi ~/.terraform.d/plugins/registry.terraform.io/filipowm/unifi/99.0.0/darwin_arm64/ - ``` +#### Build and install locally (macOS) - Replace `darwin_arm64` with your platform: - - `darwin_amd64` - macOS Intel - - `linux_amd64` - Linux x86_64 - - `linux_arm64` - Linux ARM64 +```bash +make install +``` -3. Configure Terraform to use the local provider: - ```hcl - terraform { - required_providers { - unifi = { - source = "filipowm/unifi" - version = "99.0.0" - } - } - } - ``` +This builds the provider and installs it to `~/.terraform.d/plugins/` for local use. -4. Initialize Terraform: - ```bash - rm -f .terraform.lock.hcl - terraform init - ``` +#### Cross-compile for a remote host (e.g. UDM-SE, Linux ARM64 docker-host) -### From Terraform Registry +```bash +make build-linux-arm64 +``` -The provider is available in the [Terraform Registry](https://registry.terraform.io/providers/filipowm/unifi/latest). To use it in your Terraform configuration: +Then deploy to the remote host: + +```bash +make deploy DEPLOY_HOST=root@ +``` + +Or manually: + +```bash +scp terraform-provider-unifi_linux_arm64 root@:~/.terraform.d/plugins/registry.terraform.io/filipowm/unifi/99.0.0/linux_arm64/terraform-provider-unifi +``` + +#### All-in-one: build, deploy, and init on remote host + +```bash +make deploy DEPLOY_HOST=root@ TF_DIR=/path/to/terraform/configs +``` + +### Terraform Configuration + +Configure your `versions.tf` to use the local provider: ```hcl terraform { required_providers { unifi = { source = "filipowm/unifi" - version = "~> 1.0.0" # Use the latest version + version = "= 99.0.0" + } + } +} +``` + +After building/deploying, initialize Terraform: + +```bash +rm -f .terraform.lock.hcl +terraform init +``` + +### From Terraform Registry + +The upstream provider is available in the [Terraform Registry](https://registry.terraform.io/providers/filipowm/unifi/latest): + +```hcl +terraform { + required_providers { + unifi = { + source = "filipowm/unifi" + version = "~> 1.0.0" } } } diff --git a/go.mod b/go.mod index c6e3951..2127d7b 100644 --- a/go.mod +++ b/go.mod @@ -427,4 +427,4 @@ require ( tags.cncf.io/container-device-interface v0.8.1 // indirect ) -replace github.com/filipowm/go-unifi => github.com/shadyeip/go-unifi v1.8.1 +replace github.com/filipowm/go-unifi => ../go-unifi diff --git a/go.sum b/go.sum index f5d9b49..7754fc4 100644 --- a/go.sum +++ b/go.sum @@ -830,8 +830,6 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b h1:h+3JX2VoWTFuyQEo87pStk/a99dzIO1mM9KxIyLPGTU= github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b/go.mod h1:/yeG0My1xr/u+HZrFQ1tOQQQQrOawfyMUH13ai5brBc= -github.com/shadyeip/go-unifi v1.8.1 h1:sjigrGs7985w9IjbRoNGnRBeon1CsRfSN367fxaIaCM= -github.com/shadyeip/go-unifi v1.8.1/go.mod h1:hf0HZI8SX/h6vEa0BQzxY8Bpm4enbRmrPJ1DFFt6/4A= github.com/shibumi/go-pathspec v1.3.0 h1:QUyMZhFo0Md5B8zV8x2tesohbb5kfbpTi9rBnKh5dkI= github.com/shibumi/go-pathspec v1.3.0/go.mod h1:Xutfslp817l2I1cZvgcfeMQJG5QnU2lh5tVaaMCl3jE= github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=