From cc42de8d2cc4844b677cb6e572db4a877c2a1967 Mon Sep 17 00:00:00 2001 From: ste Date: Sun, 22 Oct 2023 17:13:44 +0200 Subject: [PATCH 1/4] Simplify local setup --- .goreleaser.yml | 72 +++++++++++++++++++++------------------- GNUmakefile | 10 ++++-- scripts/local-release.sh | 9 ----- scripts/setup.sh | 18 ++++++++++ 4 files changed, 64 insertions(+), 45 deletions(-) delete mode 100755 scripts/local-release.sh create mode 100755 scripts/setup.sh diff --git a/.goreleaser.yml b/.goreleaser.yml index 27a37ffc..a68fc57b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -5,45 +5,48 @@ before: # this is just an example and not a requirement for provider building/publishing - go mod tidy builds: -- env: - # goreleaser does not work with CGO, it could also complicate - # usage by users in CI/CD systems like Terraform Cloud where - # they are unable to install libraries. - - CGO_ENABLED=0 - mod_timestamp: '{{ .CommitTimestamp }}' - flags: - - -trimpath - ldflags: - - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' - goos: - - freebsd - - windows - - linux - - darwin - goarch: - - amd64 - - '386' - - arm - - arm64 - ignore: - - goos: darwin - goarch: '386' - - goos: windows - goarch: arm64 - binary: '{{ .ProjectName }}_v{{ .Version }}' - hooks: - post: - - cmd: "sh ./scripts/local-release.sh {{ .Version }} {{ .Path }} {{.Os}} {{.Arch}}" + - env: + # goreleaser does not work with CGO, it could also complicate + # usage by users in CI/CD systems like Terraform Cloud where + # they are unable to install libraries. + - CGO_ENABLED=0 + mod_timestamp: '{{ .CommitTimestamp }}' + flags: + - -trimpath + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + goos: + - freebsd + - windows + - linux + - darwin + goarch: + - amd64 + - '386' + - arm + - arm64 + ignore: + - goos: darwin + goarch: '386' + - goos: windows + goarch: arm64 + binary: '{{ .ProjectName }}_v{{ .Version }}' + hooks: + post: + - cmd: "go install ." + archives: -- format: zip - name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' + - format: zip + name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' + checksum: name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS' algorithm: sha256 + signs: - artifacts: checksum args: - # if you are using this is a GitHub action or some other automated pipeline, you + # if you are using this is a GitHub action or some other automated pipeline, you # need to pass the batch flag to indicate its not interactive. - "--batch" - "--local-user" @@ -52,8 +55,9 @@ signs: - "${signature}" - "--detach-sign" - "${artifact}" -release: - # Visit your project's GitHub Releases page to publish this release. + +release: # Visit your project's GitHub Releases page to publish this release. draft: true + changelog: skip: true diff --git a/GNUmakefile b/GNUmakefile index d03fe7f7..108b315a 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -6,7 +6,13 @@ PKG_NAME=opsgenie default: build build: fmtcheck - goreleaser build --skip-validate --rm-dist + goreleaser build --skip-validate --clean + +dev: fmt + go install . + +setup: + @sh -c "'$(CURDIR)/scripts/setup.sh'" test: fmtcheck go test -i $(TEST) || exit 1 @@ -56,5 +62,5 @@ ifeq (,$(wildcard $(GOPATH)/src/$(WEBSITE_REPO))) endif @$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider-test PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME) -.PHONY: build test testacc vet fmt fmtcheck errcheck vendor-status test-compile website website-test +.PHONY: build dev test testacc vet fmt fmtcheck errcheck vendor-status test-compile website website-test diff --git a/scripts/local-release.sh b/scripts/local-release.sh deleted file mode 100755 index 5db76187..00000000 --- a/scripts/local-release.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -VERSION=$1 -FILE_PATH=$2 -OS=$3 -ARCH=$4 - -mkdir -p ~/terraform/providers/test.local/opsgenie/opsgenie/$VERSION/"$OS"_"$ARCH" -cp $FILE_PATH ~/terraform/providers/test.local/opsgenie/opsgenie/$VERSION/"$OS"_"$ARCH" \ No newline at end of file diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100755 index 00000000..1959ae69 --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +if [ -z "$GOPATH" ]; then + GOPATH="${GOPATH:-$HOME/go}" +fi + +if [ ! -d "$GOPATH" ]; then + mkdir -p "$GOPATH/bin" +fi + +cat >~/.terraformrc < Date: Sun, 22 Oct 2023 17:15:51 +0200 Subject: [PATCH 2/4] Add `make clean` to remove `~/.terraformrc` --- GNUmakefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/GNUmakefile b/GNUmakefile index 108b315a..83232926 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -14,6 +14,9 @@ dev: fmt setup: @sh -c "'$(CURDIR)/scripts/setup.sh'" +clean: + rm $(HOME)/.terraformrc + test: fmtcheck go test -i $(TEST) || exit 1 echo $(TEST) | \ From 7ef5d2c59a4dca6055a0c3ec9cc5be207d1f582f Mon Sep 17 00:00:00 2001 From: ste Date: Sun, 22 Oct 2023 18:04:40 +0200 Subject: [PATCH 3/4] Add missing phony commands to makefile --- GNUmakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GNUmakefile b/GNUmakefile index 83232926..346ffcf2 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -65,5 +65,5 @@ ifeq (,$(wildcard $(GOPATH)/src/$(WEBSITE_REPO))) endif @$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider-test PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME) -.PHONY: build dev test testacc vet fmt fmtcheck errcheck vendor-status test-compile website website-test +.PHONY: build dev setup clean test testacc vet fmt fmtcheck errcheck vendor-status test-compile website website-test From 2bea11e9e38bccd26b5a8e5ac72dd29a6ac88676 Mon Sep 17 00:00:00 2001 From: ste Date: Sun, 22 Oct 2023 18:00:18 +0200 Subject: [PATCH 4/4] Rewrite README development/setup instructions --- README.md | 168 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 101 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index c9771764..5b3ea7f8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ -Terraform Provider -================== +# Terraform Provider - Website: https://www.terraform.io - [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby) @@ -8,93 +7,128 @@ Terraform Provider -Requirements ------------- -- [Terraform](https://www.terraform.io/downloads.html) 0.12.x -- [Go](https://golang.org/doc/install) 1.14.2 (to build the provider plugin) +## Development -Building The Provider ---------------------- +Everything needed to start local development and testing of the Provider plugin -Clone repository to: `$GOPATH/src/github.com/opsgenie/terraform-provider-opsgenie` +### 1. Requirements -```sh -$ mkdir -p $GOPATH/src/github.com/opsgenie; cd $GOPATH/src/github.com/opsgenie -$ git clone git@github.com:opsgenie/terraform-provider-opsgenie -``` +- [Go](https://golang.org/doc/install) 1.18 (or higher, to build the provider plugin) +- [Terraform](https://www.terraform.io/downloads.html) 0.12.x (To test the plugin) -Enter the provider directory and build the provider -```sh -$ cd $GOPATH/src/github.com/opsgenie/terraform-provider-opsgenie -$ make build -``` +### 2. Development Setup -Using the provider ----------------------- -## Fill in for each provider +#### Cloning the project -Developing the Provider ---------------------------- +```bash +export GOPATH="${GOPATH:=$HOME/go}" -If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.8+ is *required*). You'll also need to correctly setup a [GOPATH](http://golang.org/doc/code.html#GOPATH), as well as adding `$GOPATH/bin` to your `$PATH`. +mkdir -p "$GOPATH/src/github.com/opsgenie" -To compile the provider, run `make build`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory. +cd "$GOPATH/src/github.com/opsgenie" -```sh -$ make build -... -$ $GOPATH/bin/terraform-provider-opsgenie -... +git clone git@github.com:opsgenie/terraform-provider-opsgenie ``` -In order to test the provider, you can simply run `make test`. + +### 3. Compiling The Provider ```sh -$ make test +export GOPATH="${GOPATH:=$HOME/go}" +cd "$GOPATH/src/github.com/opsgenie" + +# Compile all versions of the provider and install it in GOPATH. +make build + +# Only compile the local executable version (Faster) +make dev ``` -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. +#### Running tests on the Provider + +Run all local unit tests. ```sh -$ make testacc +make test ``` -Testing the Provider from Local Registry Version ------------------------------------------------- -* Create a `.terraformrc` file on your in your home folder using `vi ~/.terraformrc` -* Add the local provider registry conf in `.terraformrc` -``` -provider_installation { - filesystem_mirror { - path = "~/terraform/providers" - include = ["test.local/*/*"] - } - direct { - exclude = ["test.local/*/*"] - } -} -``` -* Run `make build` on local (it will internally trigger a hook to write to `test.local` registry located in your `~/terraform/providers` folder) -* You can create a terraform basic project of your own locally with `main.tf` file + +### 4. Using the Compiled Provider + +#### Configure Terraform to use the compiled provider. + +This configuration makes use of the [`dev_overrides`](https://developer.hashicorp.com/terraform/cli/config/config-file#development-overrides-for-provider-developers). + +See the [Manual Setup][Manual Setup] for more details. + +```bash +# Creates $HOME/.terraformrc +make setup ``` -terraform { - required_providers { - opsgenie = { - source = "test.local/opsgenie/opsgenie" - version = "" + +#### Manual setup + +
+ Click to expand + +1. Create the `.terraformrc` file on your in your home folder using `touch ~/.terraformrc` +2. Configure [`dev_overrides`](https://developer.hashicorp.com/terraform/cli/config/config-file#development-overrides-for-provider-developers) in your `~/.terraformrc` as show below: + ```hcl + provider_installation { + dev_overrides { + # Remember to replace with your username + "opsgenie/opsgenie" = "/home//go/bin" + } + direct {} } - } -} - -# Configure the Opsgenie Provider -provider "opsgenie" { - api_key = - api_url = "api.opsgenie.com" # can be a stage instance url for devs -} + ``` +3. Run `make build` + +
+ + +#### New OpsGenie Terraform project + +1. Create a basic terraform project locally with a `main.tf` file: + ```hcl + terraform { + required_providers { + opsgenie = { + source = "opsgenie/opsgenie" + version = ">=0.6.0" # version can be omitted + } + } + } + + # Configure the Opsgenie Provider + provider "opsgenie" { + api_key = "" # https://support.atlassian.com/opsgenie/docs/api-key-management/ + api_url = "api.opsgenie.com" # can be a stage instance url for devs + } + + resource "opsgenie_team" { + name = "Dev-Provider test team" + description = "New team made using in-development OpsGenie provider" + } + ``` + +2. And, Add respective terraform change files which you want to apply on your OG instance + +3. Run respective terraform commands to test the provider as per your convenience + Install the currently available provider with `tf init` + `terraform plan` and `terraform init` will use providers from the configured paths in `$HOME/.terraformrc` + `terraform` will output an error if no provider is found in the `dev_overrides` path. (`make build`) + + +#### Removing the 'dev_override' again + +This allows you to use the normal release versions of the `opsgenie/opsgenie` provider. + +*Note* Removes `$HOME/.terraformrc` + +```bash +make clean ``` -* And, Add respective terraform change files which you want to apply on your OG instance -* Run respective terraform commands to test the provider as per your convenience \ No newline at end of file