From 75453ade25017d38ecec85d1ae7e444281ba93b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fern=C3=A1ndez?= <7312236+fernandezcuesta@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:41:14 +0100 Subject: [PATCH] fix: issue #66 (#71) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com> --- .github/workflows/ci.yml | 153 ++++++++++++++++++++++++++++---------- .gitmodules | 2 +- .golangci.yml | 41 +++++----- Makefile | 116 ++++++++++++++++++++++++----- build | 2 +- config/business/config.go | 7 +- config/common/common.go | 56 ++++++++++++++ config/team/config.go | 9 ++- 8 files changed, 300 insertions(+), 86 deletions(-) create mode 100644 config/common/common.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8914f8..70b7406 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,13 +11,17 @@ on: env: # Common versions GO_VERSION: '1.21' - GOLANGCI_VERSION: 'v1.54.0' + GOLANGCI_VERSION: 'v1.61.0' DOCKER_BUILDX_VERSION: 'v0.8.2' - XPKG_ACCESS_ID: ${{ secrets.XPKG_ACCESS_ID }} + + # Common users. We can't run a step 'if secrets.XXX != ""' but we can run a + # step 'if env.XXX != ""', so we copy these to succinctly test whether + # credentials have been provided before trying to run steps that need them. + UPBOUND_MARKETPLACE_PUSH_ROBOT_USR: ${{ secrets.UPBOUND_MARKETPLACE_PUSH_ROBOT_USR }} jobs: detect-noop: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 outputs: noop: ${{ steps.noop.outputs.should_skip }} steps: @@ -29,8 +33,35 @@ jobs: paths_ignore: '["**.md", "**.png", "**.jpg"]' do_not_skip: '["workflow_dispatch", "schedule", "push"]' + report-breaking-changes: + runs-on: ubuntu-24.04 + needs: detect-noop + if: needs.detect-noop.outputs.noop != 'true' + steps: + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 + with: + submodules: true + + - name: Get modified CRDs + id: modified-crds + uses: tj-actions/changed-files@v45 + with: + files: | + package/crds/** + - name: Report breaking CRD OpenAPI v3 schema changes + if: steps.modified-crds.outputs.any_changed == 'true' + env: + MODIFIED_CRD_LIST: ${{ steps.modified-crds.outputs.all_changed_files }} + run: | + make crddiff + - name: Report native schema version changes + if: ${{ inputs.upjet-based-provider }} + run: | + make schema-version-diff + lint: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 needs: detect-noop if: needs.detect-noop.outputs.noop != 'true' @@ -47,17 +78,17 @@ jobs: - name: Find the Go Build Cache id: go - run: echo "::set-output name=cache::$(make go.cachedir)" + run: echo "cache=$(make go.cachedir)" >> $GITHUB_OUTPUT - name: Cache the Go Build Cache - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: ${{ steps.go.outputs.cache }} key: ${{ runner.os }}-build-lint-${{ hashFiles('**/go.sum') }} restore-keys: ${{ runner.os }}-build-lint- - name: Cache Go Dependencies - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: .work/pkg key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }} @@ -69,12 +100,12 @@ jobs: # We could run 'make lint' but we prefer this action because it leaves # 'annotations' (i.e. it comments on PRs to point out linter violations). - name: Lint - uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # v6 + uses: golangci/golangci-lint-action@v6 with: version: ${{ env.GOLANGCI_VERSION }} check-diff: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 needs: detect-noop if: needs.detect-noop.outputs.noop != 'true' @@ -94,17 +125,17 @@ jobs: - name: Find the Go Build Cache id: go - run: echo "::set-output name=cache::$(make go.cachedir)" + run: echo "cache=$(make go.cachedir)" >> $GITHUB_OUTPUT - name: Cache the Go Build Cache - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: ${{ steps.go.outputs.cache }} key: ${{ runner.os }}-build-check-diff-${{ hashFiles('**/go.sum') }} restore-keys: ${{ runner.os }}-build-check-diff- - name: Cache Go Dependencies - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: .work/pkg key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }} @@ -117,7 +148,7 @@ jobs: run: make check-diff unit-tests: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 needs: detect-noop if: needs.detect-noop.outputs.noop != 'true' @@ -137,17 +168,17 @@ jobs: - name: Find the Go Build Cache id: go - run: echo "::set-output name=cache::$(make go.cachedir)" + run: echo "cache=$(make go.cachedir)" >> $GITHUB_OUTPUT - name: Cache the Go Build Cache - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: ${{ steps.go.outputs.cache }} key: ${{ runner.os }}-build-unit-tests-${{ hashFiles('**/go.sum') }} restore-keys: ${{ runner.os }}-build-unit-tests- - name: Cache Go Dependencies - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: .work/pkg key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }} @@ -160,16 +191,65 @@ jobs: run: make -j2 test - name: Publish Unit Test Coverage - uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4 + uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4 with: flags: unittests file: _output/tests/linux_amd64/coverage.txt - publish-artifacts: - runs-on: ubuntu-22.04 + local-deploy: + runs-on: ubuntu-24.04 needs: detect-noop if: needs.detect-noop.outputs.noop != 'true' + steps: + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 + with: + submodules: true + + - name: Fetch History + run: git fetch --prune --unshallow + + - name: Setup Go + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Find the Go Build Cache + id: go + run: echo "cache=$(make go.cachedir)" >> $GITHUB_OUTPUT + + - name: Cache the Go Build Cache + uses: actions/cache@v4 + with: + path: ${{ steps.go.outputs.cache }} + key: ${{ runner.os }}-build-unit-tests-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-build-unit-tests- + + - name: Cache Go Dependencies + uses: actions/cache@v4 + with: + path: .work/pkg + key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-pkg- + + - name: Vendor Dependencies + run: make vendor vendor.check + + - name: Deploying locally built provider package + run: make local-deploy + + publish-artifacts: + runs-on: ubuntu-24.04 + needs: + - detect-noop + - report-breaking-changes + - lint + - check-diff + - unit-tests + - local-deploy + if: needs.detect-noop.outputs.noop != 'true' + steps: - name: Setup QEMU uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3 @@ -177,11 +257,19 @@ jobs: platforms: all - name: Setup Docker Buildx - uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3 + uses: docker/setup-buildx-action@v3 with: version: ${{ env.DOCKER_BUILDX_VERSION }} install: true + - name: Login to Upbound + uses: docker/login-action@v3 + if: env.UPBOUND_MARKETPLACE_PUSH_ROBOT_USR != '' + with: + registry: xpkg.upbound.io + username: ${{ secrets.UPBOUND_MARKETPLACE_PUSH_ROBOT_USR }} + password: ${{ secrets.UPBOUND_MARKETPLACE_PUSH_ROBOT_PSW }} + - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 with: @@ -197,17 +285,17 @@ jobs: - name: Find the Go Build Cache id: go - run: echo "::set-output name=cache::$(make go.cachedir)" + run: echo "cache=$(make go.cachedir)" >> $GITHUB_OUTPUT - name: Cache the Go Build Cache - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: ${{ steps.go.outputs.cache }} key: ${{ runner.os }}-build-publish-artifacts-${{ hashFiles('**/go.sum') }} restore-keys: ${{ runner.os }}-build-publish-artifacts- - name: Cache Go Dependencies - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + uses: actions/cache@v4 with: path: .work/pkg key: ${{ runner.os }}-pkg-${{ hashFiles('**/go.sum') }} @@ -222,24 +310,13 @@ jobs: # We're using docker buildx, which doesn't actually load the images it # builds by default. Specifying --load does so. BUILD_ARGS: "--load" - - - name: Publish Artifacts to GitHub - uses: actions/upload-artifact@834a144ee995460fba8ed112a2fc961b36a5ec5a # v4 + + - name: Upload Artifacts to GitHub + uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4 with: name: output path: _output/** - - name: Login to Upbound - uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3 - if: env.XPKG_ACCESS_ID != '' - with: - registry: xpkg.upbound.io - username: ${{ secrets.XPKG_ACCESS_ID }} - password: ${{ secrets.XPKG_TOKEN }} - - name: Publish Artifacts + if: env.UPBOUND_MARKETPLACE_PUSH_ROBOT_USR != '' run: make publish BRANCH_NAME=${GITHUB_REF##*/} - if: env.XPKG_ACCESS_ID != '' - env: - UPBOUND_MARKETPLACE_PUSH_ROBOT_USR: ${{ secrets.XPKG_ACCESS_ID }} - UPBOUND_MARKETPLACE_PUSH_ROBOT_PSW: ${{ secrets.XPKG_TOKEN }} diff --git a/.gitmodules b/.gitmodules index c2fad47..8f84209 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "build"] path = build - url = https://github.com/upbound/build + url = https://github.com/crossplane/build diff --git a/.golangci.yml b/.golangci.yml index 71c9575..67778f4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,12 +1,9 @@ run: - deadline: 10m - - skip-files: - - "zz_\\..+\\.go$" + timeout: 10m output: - # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" - format: colored-line-number + formats: + - format: colored-line-number linters-settings: errcheck: @@ -21,15 +18,14 @@ linters-settings: # [deprecated] comma-separated list of pairs of the form pkg:regex # the regex is used to ignore names within pkg. (default "fmt:.*"). # see https://github.com/kisielk/errcheck#the-deprecated-method for details - ignore: fmt:.*,io/ioutil:^Read.* + exclude-functions: + - fmt:.* + - io/ioutil:^Read.* govet: # report about shadowed variables - check-shadowing: false - - golint: - # minimal confidence for issues, default is 0.8 - min-confidence: 0.8 + disable: + - shadow gofmt: # simplify code: gofmt with `-s` option, true by default @@ -38,16 +34,12 @@ linters-settings: goimports: # put imports beginning with prefix after 3rd-party packages; # it's a comma-separated list of prefixes - local-prefixes: github.com/crossplane-contrib/provider-jet-template + local-prefixes: github.com/upbound/upjet-provider-template gocyclo: # minimal code complexity to report, 30 by default (but we recommend 10-20) min-complexity: 10 - maligned: - # print struct with more effective memory layout or not, false by default - suggest-new: true - dupl: # tokens count to trigger issue, 150 by default threshold: 100 @@ -67,8 +59,8 @@ linters-settings: # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: # if it's called for subdir of a project it can't find funcs usages. All text editor integrations # with golangci-lint call it on a directory with the changed file. - check-exported: false - + exported-fields-are-used: false + unparam: # Inspect exported functions, default is false. Set to true if no external program/library imports your code. # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: @@ -104,16 +96,16 @@ linters-settings: linters: enable: - - megacheck + - gosimple + - staticcheck + - unused - govet - gocyclo - gocritic - - interfacer - goconst - goimports - gofmt # We enable this as well as goimports for its simplify mode. - prealloc - - golint - unconvert - misspell - nakedret @@ -125,6 +117,9 @@ linters: issues: + exclude-files: + - "zz_\\..+\\.go$" + # Excluding configuration per-path and per-linter exclude-rules: # Exclude some linters from running on tests files. @@ -189,7 +184,7 @@ issues: new: false # Maximum issues count per one linter. Set to 0 to disable. Default is 50. - max-per-linter: 0 + max-issues-per-linter: 0 # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. max-same-issues: 0 diff --git a/Makefile b/Makefile index 5e059e5..13eb2cb 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,20 @@ # ==================================================================================== # Setup Project -PROJECT_NAME := provider-pagerduty -PROJECT_REPO := github.com/crossplane-contrib/$(PROJECT_NAME) +PROJECT_NAME ?= provider-pagerduty +PROJECT_REPO ?= github.com/crossplane-contrib/$(PROJECT_NAME) -export TERRAFORM_VERSION := 1.5.5 +export TERRAFORM_VERSION ?= 1.5.7 -export TERRAFORM_PROVIDER_SOURCE := PagerDuty/pagerduty -export TERRAFORM_PROVIDER_VERSION := 3.16.0 -export TERRAFORM_PROVIDER_DOWNLOAD_NAME := terraform-provider-pagerduty -export TERRAFORM_PROVIDER_DOWNLOAD_URL_PREFIX := https://github.com/PagerDuty/terraform-provider-pagerduty/releases/download/v3.16.0 -export TERRAFORM_PROVIDER_REPO ?= https://github.com/PagerDuty/terraform-provider-pagerduty +# Do not allow a version of terraform greater than 1.5.x, due to versions 1.6+ being +# licensed under BSL, which is not permitted. +TERRAFORM_VERSION_VALID := $(shell [ "$(TERRAFORM_VERSION)" = "`printf "$(TERRAFORM_VERSION)\n1.6" | sort -V | head -n1`" ] && echo 1 || echo 0) + +export TERRAFORM_PROVIDER_SOURCE ?= PagerDuty/pagerduty +export TERRAFORM_PROVIDER_VERSION ?= 3.16.0 +export TERRAFORM_PROVIDER_DOWNLOAD_NAME ?= terraform-provider-pagerduty +export TERRAFORM_PROVIDER_REPO ?= https://github.com/PagerDuty/$(TERRAFORM_PROVIDER_DOWNLOAD_NAME) +export TERRAFORM_PROVIDER_DOWNLOAD_URL_PREFIX := $(TERRAFORM_PROVIDER_REPO)/releases/download/v$(TERRAFORM_PROVIDER_VERSION) export TERRAFORM_DOCS_PATH := website/docs/r PLATFORMS ?= linux_amd64 linux_arm64 @@ -39,6 +43,7 @@ NPROCS ?= 1 GO_TEST_PARALLEL := $(shell echo $$(( $(NPROCS) / 2 ))) GO_REQUIRED_VERSION ?= 1.22 +GOLANGCILINT_VERSION ?= 1.61.0 GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/provider $(GO_PROJECT)/cmd/generator GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.Version=$(VERSION) GO_SUBDIRS += cmd internal apis @@ -48,9 +53,10 @@ GO111MODULE = on # ==================================================================================== # Setup Kubernetes tools -KIND_VERSION = v0.15.0 -UP_VERSION = v0.32.1 +KIND_VERSION = v0.24.0 +UP_VERSION = v0.35.0 UP_CHANNEL = stable +UPTEST_VERSION = v1.2.0 -include build/makelib/k8s_tools.mk # ==================================================================================== @@ -70,14 +76,6 @@ XPKG_REG_ORGS_NO_PROMOTE ?= xpkg.upbound.io/crossplane-contrib XPKGS = $(PROJECT_NAME) -include build/makelib/xpkg.mk -# NOTE(hasheddan): we force image building to happen prior to xpkg build so that -# we ensure image is present in daemon. -xpkg.build.provider-pagerduty: do.build.images - -# NOTE(hasheddan): we ensure up is installed prior to running platform-specific -# build steps in parallel to avoid encountering an installation race condition. -build.init: $(UP) - # ==================================================================================== # Fallthrough @@ -92,13 +90,26 @@ fallthrough: submodules @echo Initial setup complete. Running make again . . . @make +# NOTE(hasheddan): we force image building to happen prior to xpkg build so that +# we ensure image is present in daemon. +xpkg.build.provider-mongodbatlas: do.build.images + +# NOTE(hasheddan): we ensure up is installed prior to running platform-specific +# build steps in parallel to avoid encountering an installation race condition. +build.init: $(UP) check-terraform-version + # ==================================================================================== # Setup Terraform for fetching provider schema TERRAFORM := $(TOOLS_HOST_DIR)/terraform-$(TERRAFORM_VERSION) TERRAFORM_WORKDIR := $(WORK_DIR)/terraform TERRAFORM_PROVIDER_SCHEMA := config/schema.json -$(TERRAFORM): +check-terraform-version: +ifneq ($(TERRAFORM_VERSION_VALID),1) + $(error invalid TERRAFORM_VERSION $(TERRAFORM_VERSION), must be less than 1.6.0 since that version introduced a not permitted BSL license)) +endif + +$(TERRAFORM): check-terraform-version @$(INFO) installing terraform $(HOSTOS)-$(HOSTARCH) @mkdir -p $(TOOLS_HOST_DIR)/tmp-terraform @curl -fsSL https://releases.hashicorp.com/terraform/$(TERRAFORM_VERSION)/terraform_$(TERRAFORM_VERSION)_$(SAFEHOST_PLATFORM).zip -o $(TOOLS_HOST_DIR)/tmp-terraform/terraform.zip @@ -124,7 +135,7 @@ pull-docs: generate.init: $(TERRAFORM_PROVIDER_SCHEMA) pull-docs -.PHONY: $(TERRAFORM_PROVIDER_SCHEMA) pull-docs +.PHONY: $(TERRAFORM_PROVIDER_SCHEMA) pull-docs check-terraform-version # ==================================================================================== # Targets @@ -160,6 +171,67 @@ run: go.build @# To see other arguments that can be provided, run the command with --help instead UPBOUND_CONTEXT="local" $(GO_OUT_DIR)/provider --debug +# ==================================================================================== +# End to End Testing +CROSSPLANE_VERSION = 1.16.0 +CROSSPLANE_NAMESPACE = upbound-system +-include build/makelib/local.xpkg.mk +-include build/makelib/controlplane.mk + +# This target requires the following environment variables to be set: +# - UPTEST_EXAMPLE_LIST, a comma-separated list of examples to test +# To ensure the proper functioning of the end-to-end test resource pre-deletion hook, it is crucial to arrange your resources appropriately. +# You can check the basic implementation here: https://github.com/crossplane/uptest/blob/main/internal/templates/03-delete.yaml.tmpl. +# - UPTEST_CLOUD_CREDENTIALS (optional), multiple sets of AWS IAM User credentials specified as key=value pairs. +# The support keys are currently `DEFAULT` and `PEER`. So, an example for the value of this env. variable is: +# DEFAULT='[default] +# aws_access_key_id = REDACTED +# aws_secret_access_key = REDACTED' +# PEER='[default] +# aws_access_key_id = REDACTED +# aws_secret_access_key = REDACTED' +# The associated `ProviderConfig`s will be named as `default` and `peer`. +# - UPTEST_DATASOURCE_PATH (optional), please see https://github.com/crossplane/uptest#injecting-dynamic-values-and-datasource +uptest: $(UPTEST) $(KUBECTL) $(KUTTL) + @$(INFO) running automated tests + @KUBECTL=$(KUBECTL) KUTTL=$(KUTTL) $(UPTEST) e2e "${UPTEST_EXAMPLE_LIST}" --data-source="${UPTEST_DATASOURCE_PATH}" --setup-script=cluster/test/setup.sh --default-conditions="Test" || $(FAIL) + @$(OK) running automated tests + +local-deploy: build controlplane.up local.xpkg.deploy.provider.$(PROJECT_NAME) + @$(INFO) running locally built provider + @$(KUBECTL) wait provider.pkg $(PROJECT_NAME) --for condition=Healthy --timeout 5m + @$(KUBECTL) -n upbound-system wait --for=condition=Available deployment --all --timeout=5m + @$(OK) running locally built provider + +e2e: local-deploy uptest + +crddiff: $(UPTEST) + @$(INFO) Checking breaking CRD schema changes + @for crd in $${MODIFIED_CRD_LIST}; do \ + if ! git cat-file -e "$${GITHUB_BASE_REF}:$${crd}" 2>/dev/null; then \ + echo "CRD $${crd} does not exist in the $${GITHUB_BASE_REF} branch. Skipping..." ; \ + continue ; \ + fi ; \ + echo "Checking $${crd} for breaking API changes..." ; \ + changes_detected=$$($(UPTEST) crddiff revision <(git cat-file -p "$${GITHUB_BASE_REF}:$${crd}") "$${crd}" 2>&1) ; \ + if [[ $$? != 0 ]] ; then \ + printf "\033[31m"; echo "Breaking change detected!"; printf "\033[0m" ; \ + echo "$${changes_detected}" ; \ + echo ; \ + fi ; \ + done + @$(OK) Checking breaking CRD schema changes + +schema-version-diff: + @$(INFO) Checking for native state schema version changes + @export PREV_PROVIDER_VERSION=$$(git cat-file -p "${GITHUB_BASE_REF}:Makefile" | sed -nr 's/^export[[:space:]]*TERRAFORM_PROVIDER_VERSION[[:space:]]*:=[[:space:]]*(.+)/\1/p'); \ + echo Detected previous Terraform provider version: $${PREV_PROVIDER_VERSION}; \ + echo Current Terraform provider version: $${TERRAFORM_PROVIDER_VERSION}; \ + mkdir -p $(WORK_DIR); \ + git cat-file -p "$${GITHUB_BASE_REF}:config/schema.json" > "$(WORK_DIR)/schema.json.$${PREV_PROVIDER_VERSION}"; \ + ./scripts/version_diff.py config/generated.lst "$(WORK_DIR)/schema.json.$${PREV_PROVIDER_VERSION}" config/schema.json + @$(OK) Checking for native state schema version changes + .PHONY: cobertura submodules fallthrough run crds.clean # ==================================================================================== @@ -182,3 +254,7 @@ crossplane.help: help-special: crossplane.help .PHONY: crossplane.help help-special + +# TODO(negz): Update CI to use these targets. +vendor: modules.download +vendor.check: modules.check diff --git a/build b/build index 75a9fe3..1ed1933 160000 --- a/build +++ b/build @@ -1 +1 @@ -Subproject commit 75a9fe3ae6b6de82c5f7ddc6a267617940f16b83 +Subproject commit 1ed19332b947c449795fd016f3c21ee0a64930fd diff --git a/config/business/config.go b/config/business/config.go index 9aa6a03..3e13993 100644 --- a/config/business/config.go +++ b/config/business/config.go @@ -1,6 +1,9 @@ package business -import "github.com/crossplane/upjet/pkg/config" +import ( + c "github.com/crossplane-contrib/provider-pagerduty/config/common" + "github.com/crossplane/upjet/pkg/config" +) // Configure configures individual resources by adding custom ResourceConfigurators. func Configure(p *config.Provider) { @@ -12,6 +15,8 @@ func Configure(p *config.Provider) { Type: "github.com/crossplane-contrib/provider-pagerduty/apis/team/v1alpha1.Team", }, } + r.ExternalName.GetExternalNameFn = c.GetExternalName + r.ExternalName.GetIDFn = c.GetFakeID // Deprecated if s, ok := r.TerraformResource.Schema["type"]; ok { s.Optional = false diff --git a/config/common/common.go b/config/common/common.go new file mode 100644 index 0000000..34c3a45 --- /dev/null +++ b/config/common/common.go @@ -0,0 +1,56 @@ +package commmon + +import ( + "context" + "strings" + + "github.com/crossplane/upjet/pkg/config" + "github.com/pkg/errors" +) + +// Errors +const ( + ErrFmtNoAttribute = `attribute not found: %s` + ErrFmtUnexpectedType = `unexpected type for attribute %s: Expecting a string` + Managed = `managed` +) + +func GetExternalName(tfstate map[string]any) (string, error) { + id, ok := tfstate["id"] + if !ok { + return "", errors.Errorf(ErrFmtNoAttribute, "id") + } + idStr, ok := id.(string) + if !ok { + return "", errors.Errorf(ErrFmtUnexpectedType, "id") + } + return idStr, nil +} + +func GetFakeID(_ context.Context, externalName string, _ map[string]any, _ map[string]any) (string, error) { + if externalName == "" { + return Managed, nil + } + return externalName, nil +} + +func SplitExternalNameFromId() config.ExternalName { + e := config.IdentifierFromProvider + e.GetExternalNameFn = func(tfstate map[string]interface{}) (string, error) { + id, ok := tfstate["id"] + if !ok { + return "", errors.New("id in tfstate cannot be empty") + } + w := strings.Split(id.(string), ":") + return w[len(w)-1], nil + } + return e +} + +func ExternalNameFromParams(params []string) config.ExternalName { + e := config.IdentifierFromProvider + e.GetExternalNameFn = func(tfstate map[string]interface{}) (string, error) { + return strings.Join(params, ":"), nil + } + return e +} diff --git a/config/team/config.go b/config/team/config.go index 3ae41a0..fdf6183 100644 --- a/config/team/config.go +++ b/config/team/config.go @@ -1,11 +1,16 @@ package team -import "github.com/crossplane/upjet/pkg/config" +import ( + c "github.com/crossplane-contrib/provider-pagerduty/config/common" + "github.com/crossplane/upjet/pkg/config" +) // Configure configures individual resources by adding custom ResourceConfigurators. func Configure(p *config.Provider) { p.AddResourceConfigurator("pagerduty_team", func(r *config.Resource) { - + r.ExternalName = config.IdentifierFromProvider + r.ExternalName.GetExternalNameFn = c.GetExternalName + r.ExternalName.GetIDFn = c.GetFakeID r.ShortGroup = "team" })