From d864d2257a2ae274e7f490896db65cced30e84f3 Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Wed, 11 Dec 2019 09:47:43 +0100 Subject: [PATCH 1/2] add github actions pipeline --- .editorconfig | 26 ++++++++++ .github/workflows/main.yml | 39 +++++++++++++++ .github/workflows/prerelease.yml | 38 +++++++++++++++ .gitignore | 1 + GNUmakefile | 79 ++++++++++++++++++++++++++++--- bin/test | 10 ---- ccloud/resource_kafka_cluster.go | 4 +- go.mod | 4 +- go.sum | 4 ++ scripts/build/build-all-osarch.sh | 72 ++++++++++++++++++++++++++++ scripts/test.sh | 14 ++++++ 11 files changed, 269 insertions(+), 22 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/main.yml create mode 100644 .github/workflows/prerelease.yml delete mode 100755 bin/test create mode 100755 scripts/build/build-all-osarch.sh create mode 100755 scripts/test.sh diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d43d445 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,26 @@ +# http://editorconfig.org + +# this file is the top-most editorconfig file +root = true + +# all files +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.go] +indent_style = tab +indent_size = 4 + +[json*.golden] +insert_final_newline = false + +[*.{yaml,yml}] +indent_size = 2 + +[Makefile] +indent_style = tab \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..2cf7883 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,39 @@ +name: build +on: [push] + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.12 + uses: actions/setup-go@v1 + with: + go-version: 1.12 + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Run tests + run: make test + + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.12 + uses: actions/setup-go@v1 + with: + go-version: 1.12 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Build binary for current OS/ARCH + run: make build diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml new file mode 100644 index 0000000..23e306e --- /dev/null +++ b/.github/workflows/prerelease.yml @@ -0,0 +1,38 @@ +name: prerelease + +on: + push: + tags: + - "v*.*.*" + +jobs: + prerelease: + name: Pre-Release + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.12 + uses: actions/setup-go@v1 + with: + go-version: 1.12 + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Build binaries for all OS/ARCH platforms + run: | + export PATH=$PATH:$(go env GOPATH)/bin + make gox build-all + - name: Release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + files: bin/terraform-provider-confluentcloud-v* + draft: false + prerelease: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 14fbcae..c196fc0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ !/examples/main.tf /examples/* +bin diff --git a/GNUmakefile b/GNUmakefile index ac5adf8..8d40d58 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,15 +1,80 @@ TEST?=./... -GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) -default: build +# Project variables +NAME := terraform-provider-confluentcloud +# Build variables +BUILD_DIR := bin +VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(COMMIT_HASH)") +# Go variables +GOCMD := GO111MODULE=on go +GOBUILD ?= CGO_ENABLED=0 $(GOCMD) build +GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) +GOFILES ?= $(shell find . -type f -name '*.go' -not -path "./vendor/*") -build: - go install +.PHONY: all +all: clean test lint build + +.PHONY: checkfmt +checkfmt: RESULT = $(shell goimports -l $(GOFILES) | tee >(if [ "$$(wc -l)" = 0 ]; then echo "OK"; fi)) +checkfmt: SHELL := /usr/bin/env bash +checkfmt: ## Check formatting of all go files + @ echo "$(RESULT)" + @ if [ "$(RESULT)" != "OK" ]; then exit 1; fi + +.PHONY: fmt +fmt: ## Format all go files + @ $(MAKE) --no-print-directory log-$@ + goimports -w $(GOFILES) + +.PHONY: lint +lint: ## Run linter + @ $(MAKE) --no-print-directory log-$@ + GO111MODULE=on golangci-lint run ./... + +.PHONY: clean +clean: ## Clean workspace + @ $(MAKE) --no-print-directory log-$@ + rm -rf ./$(BUILD_DIR) + +.PHONY: build +build: clean ## Build binary for current OS/ARCH + @ $(MAKE) --no-print-directory log-$@ + $(GOBUILD) -o ./$(BUILD_DIR)/$(GOOS)-$(GOARCH)/$(NAME) + +.PHONY: build-all +build-all: GOOS = linux darwin +build-all: GOARCH = amd64 +build-all: clean ## Build binary for all OS/ARCH + @ $(MAKE) --no-print-directory log-$ + @ ./scripts/build/build-all-osarch.sh "$(BUILD_DIR)" "$(NAME)" "$(VERSION)" "$(GOOS)" "$(GOARCH)" + +.PHONY: test test: - go test ./... + $(GOCMD) test ./... +.PHONY: testacc testacc: - TF_LOG=debug TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m + TF_LOG=debug TF_ACC=1 $(GOCMD) test $(TEST) -v $(TESTARGS) -timeout 120m -.PHONY: build test testacc +.PHONY: gox +gox: + GO111MODULE=off go get -u github.com/mitchellh/gox + +.PHONY: goimports +goimports: + GO111MODULE=off go get -u golang.org/x/tools/cmd/goimports + +.PHONY: golangci +golangci: + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(shell go env GOPATH)/bin $(GOLANGCI_VERSION) + + +.PHONY: tools +tools: ## Install required tools + @ $(MAKE) --no-print-directory log-$@ + @ $(MAKE) --no-print-directory goimports golangci gox + +log-%: + @ grep -h -E '^$*:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m==> %s\033[0m\n", $$2}' \ No newline at end of file diff --git a/bin/test b/bin/test deleted file mode 100755 index 6a0406d..0000000 --- a/bin/test +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -set -ex - -go build -mv terraform-provider-confluent-cloud ~/.terraform.d/plugins/darwin_amd64/terraform-provider-confluentcloud -cd examples -terraform init -terraform plan -terraform output diff --git a/ccloud/resource_kafka_cluster.go b/ccloud/resource_kafka_cluster.go index 4ce1410..058785d 100644 --- a/ccloud/resource_kafka_cluster.go +++ b/ccloud/resource_kafka_cluster.go @@ -47,8 +47,8 @@ func kafkaClusterResource() *schema.Resource { Description: "LOW(single-zone) or HIGH(multi-zone)", ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { v := val.(string) - if val != "LOW" || val != "HIGH" { - errs = append(errs, fmt.Errorf("%q must be `LOW` or `HIGH`, got: %d", key, v)) + if val != "LOW" && val != "HIGH" { + errs = append(errs, fmt.Errorf("%q must be `LOW` or `HIGH`, got: %s", key, v)) } return }, diff --git a/go.mod b/go.mod index a82ba8f..77882ef 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,6 @@ module github.com/Mongey/terraform-provider-confluent-cloud go 1.12 require ( - github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20191204162755-5bbf166f5417 + github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20191211092310-656dfa7ae169 github.com/hashicorp/terraform v0.12.1 ) - -replace github.com/cgroschupp/go-client-confluent-cloud => ../go-client-confluent-cloud diff --git a/go.sum b/go.sum index 2d46b13..0ec1e80 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBT github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k= github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20191204162755-5bbf166f5417 h1:qFpYAU2U8vodmGSpL32L979KEJ+64nAcxq9EsymNZag= github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20191204162755-5bbf166f5417/go.mod h1:4qz2Pftxeus+mcGJkiysWrDwf/uQL1+jEI/Yu5BpS60= +github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20191211092310-656dfa7ae169 h1:psVjoO+FaSJA5aD7sy2VdVgRGvylUh86aKj9vi9jjcA= +github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20191211092310-656dfa7ae169/go.mod h1:4qz2Pftxeus+mcGJkiysWrDwf/uQL1+jEI/Yu5BpS60= github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20161106042343-c914be64f07d/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= @@ -330,6 +332,7 @@ golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190502183928-7f726cade0ab h1:9RfW3ktsOZxgo9YNbBAjq1FWzc/igwEcUzZz8IXgSbk= golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU= golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -354,6 +357,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82 h1:vsphBvatvfbhlb4PO1BYSr9dzugGxJ/SQHoNufZJq1w= golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/scripts/build/build-all-osarch.sh b/scripts/build/build-all-osarch.sh new file mode 100755 index 0000000..2855168 --- /dev/null +++ b/scripts/build/build-all-osarch.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash + +set -e + +BUILD_DIR="${1:-bin}" +NAME="${2:-terraform-provider-confluentcloud}" +VERSION="$3" +GOOS="${4:-"linux darwin windows freebsd"}" +GOARCH="${5:-"amd64 arm"}" + +if [ -z "${VERSION}" ]; then + echo "Error: VERSION is missing. e.g. ./compress.sh " + exit 1 +fi + +PWD=$(cd $(dirname "$0") && pwd -P) +BUILD_DIR="${PWD}/../../${BUILD_DIR}" + +CGO_ENABLED=0 gox \ + -verbose \ + -ldflags "${GOLDFLAGS}" \ + -gcflags=-trimpath=`go env GOPATH` \ + -os="${GOOS}" \ + -arch="${GOARCH}" \ + -osarch="!darwin/arm" \ + -output="${BUILD_DIR}/{{.OS}}-{{.Arch}}/${NAME}" ${PWD}/../../ + +printf "\033[36m==> Compress binary\033[0m\n" + +for platform in $(find ${BUILD_DIR} -mindepth 1 -maxdepth 1 -type d); do + OSARCH=$(basename ${platform}) + FULLNAME="${NAME}-${VERSION}-${OSARCH}" + + case "${OSARCH}" in + "windows"*) + if ! command -v zip >/dev/null; then + echo "Error: cannot compress, 'zip' not found" + exit 1 + fi + + zip -q -j ${BUILD_DIR}/${FULLNAME}.zip ${platform}/${NAME}.exe + printf -- "--> %15s: bin/%s\n" "${OSARCH}" "${FULLNAME}.zip" + + ;; + *) + if ! command -v tar >/dev/null; then + echo "Error: cannot compress, 'tar' not found" + exit 1 + fi + + tar -czf ${BUILD_DIR}/${FULLNAME}.tar.gz --directory ${platform}/ ${NAME} + printf -- "--> %15s: bin/%s\n" "${OSARCH}" "${FULLNAME}.tar.gz" + + ;; + esac +done + +cd ${BUILD_DIR} +touch ${NAME}-${VERSION}.sha256sum + +for binary in $(find . -mindepth 1 -maxdepth 1 -type f | grep -v "${NAME}-${VERSION}.sha256sum" | sort); do + binary=$(basename ${binary}) + + if command -v sha256sum >/dev/null; then + sha256sum ${binary} >>${NAME}-${VERSION}.sha256sum + elif command -v shasum >/dev/null; then + shasum -a256 ${binary} >>${NAME}-${VERSION}.sha256sum + fi +done + +cd - >/dev/null 2>&1 +printf -- "\n--> %15s: bin/%s\n" "sha256sum" "${NAME}-${VERSION}.sha256sum" diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..91c4416 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -ex + +ARCH=go env GOARCH +OS=go env GOOS + +go build + +mv bin/${OS}-${ARCH}/terraform-provider-confluentcloud ~/.terraform.d/plugins/${OS}_${ARCH}/ +cd examples +terraform init +terraform plan +terraform output From 64a512f0b7eb2d292545e4fd5c6c9162cf3ded10 Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Wed, 11 Dec 2019 15:16:15 +0100 Subject: [PATCH 2/2] build only on master --- .github/workflows/main.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2cf7883..f6233e0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,5 +1,9 @@ name: build -on: [push] +on: + push: + branches: + - master + pull_request: jobs: test: