Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: OpenTofu Support #129

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run: |
git diff --exit-code
gofmt:
name: "Ensure 'make fmt' has been run"
name: "Ensure 'go fmt' has been run"
runs-on: ubuntu-latest
steps:
- name: Checkout repo
Expand All @@ -31,4 +31,4 @@ jobs:
go fmt
- name: Validate No Changes
run: |
git diff --exit-code
git diff --exit-code
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ tests/

.idea
**/*.lock.hcl
**/*.backup
**/*.backup

.DS_Store
32 changes: 30 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HOSTNAME=codefresh.io
PKG_NAME=codefresh
NAMESPACE=app
BINARY=terraform-provider-${PKG_NAME}
OS_ARCH=darwin_amd64
OS_ARCH=$$(go env GOOS)_$$(go env GOARCH)
TFPLUGINDOCS_VERSION=v0.14.1

default: build
Expand All @@ -22,13 +22,41 @@ build: fmtcheck
install: build
mv ${BINARY} $(HOME)/go/bin/

equivalence: build
@echo "==> Preparing equivalence tests"
mkdir -p testing/equivalence/.plugins/registry.terraform.io/codefresh-io/codefresh/0.6.0/${OS_ARCH}/
cp terraform-provider-codefresh testing/equivalence/.plugins/registry.terraform.io/codefresh-io/codefresh/0.6.0/${OS_ARCH}/

cd testing/equivalence;\
./update-test-cases.sh;\

@echo "==> Running equivalence tests for terraform"
cd testing/equivalence;\
equivalence-testing update --binary=$$(which terraform) --goldens=results/terraform --tests=test_cases --rewrites=rewrites.jsonc

@echo "==> Running equivalence tests for opentofu"
cd testing/equivalence;\
equivalence-testing update --binary=$$(which tofu) --goldens=results/opentofu --tests=test_cases --rewrites=rewrites.jsonc

@echo "==> Comparing results"
cd testing/equivalence;\
./compare-results.sh;\

fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w $(GOFMT_FILES)

fmtcheck: SHELL:=/bin/bash
fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
@echo "==> Checking that code complies with gofmt requirements..."

@gofmt_files=$$(find . -name '*.go' | grep -v vendor | xargs gofmt -l -s); \
if [[ -n $${gofmt_files} ]]; then\
echo 'gofmt needs running on the following files:';\
echo "$${gofmt_files}";\
echo "You can use the command: \`make fmt\` to reformat code.";\
exit 1;\
fi;
lint:
@echo "==> Checking source code against linters..."
golangci-lint run ./...
Expand Down
41 changes: 18 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# Terraform Provider for Codefresh

This is the official Terraform Provider for Codefresh.
The official Terraform and OpenTofu Provider for [Codefresh](https://codefresh.io/).

Terraform Registry: [registry.terraform.io/providers/codefresh-io/codefresh](https://registry.terraform.io/providers/codefresh-io/codefresh/latest)

## Requirements

- [Terraform](https://www.terraform.io/downloads.html) `1.x.x`

## Download the Provider

Download and extract terraform-provider-codefresh from [releases](https://github.com/codefresh-io/terraform-provider-codefresh/releases)
- [Terraform](https://www.terraform.io/downloads.html) `1.x.x` or [OpenTofu](https://github.com/opentofu/opentofu/releases/latest) `1.x.x`.

## Using the Provider

Expand All @@ -21,12 +17,14 @@ terraform {
required_providers {
codefresh = {
version = "x.y.z" # Optional but recommended; replace with latest semantic version
source = "codefresh-io/codefresh"
source = "registry.terraform.io/codefresh-io/codefresh" # registry.terraform.io/ is optional for Terraform users, but required for OpenTofu users
}
}
}
```

You can also download and extract the provider binary (`terraform-provider-codefresh`) from [releases](https://github.com/codefresh-io/terraform-provider-codefresh/releases).

## Building the Provider Locally

```sh
Expand All @@ -39,37 +37,34 @@ The documentation is generated using [tfplugindocs](https://github.com/hashicorp

See: [CONTRIBUTING.md](./CONTRIBUTING.md#documentation)

## To configure Codefresh provider:
## Provider Configuration:

```hcl
provider "codefresh" {
api_url = "<MY API URL>" # Default value - https://g.codefresh.io/api
token = "<MY API TOKEN>" # If token isn't set the provider expects the $CODEFRESH_API_KEY env variable
}
```
See the [Provider Documentation](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs#schema).

Get an API key from [Codefresh](https://g.codefresh.io/user/settings) and set the following scopes:
The provider requires a Codefresh API in order to authenticate to the Codefresh API. Generate the API key [here](https://g.codefresh.io/user/settings) and set the scopes [according to the resources you wish to create](https://codefresh.io/docs/docs/integrations/codefresh-api/#access-scopes). Note that some resource require platform admin permissions and hence can only be created for on-prem installations and not our SaaS offering.

- Environments-V2
- Pipeline
- Project
- Repos
- Step-Type
- Step-Types
- View
The key can be set as an environment variable:

```bash
export CODEFRESH_API_KEY='xyz'
```

## Testing the Provider

**NOTE:** Acceptance tests create real resources, including admin resources (accounts, users) so make sure that `CODEFRESH_API_KEY` is set to an account that you are ok with being modified.
**NOTE:** Acceptance tests create real resources, including admin resources (accounts, users) so make sure that `CODEFRESH_API_KEY` is set to a Codefresh installation and an account that you are ok with being modified.

```bash
make testacc
```

## OpenTofu Support

This provider supports [OpenTofu](https://opentofu.org/).

[Equivalence Testing](https://github.com/opentofu/equivalence-testing) is performed on the `examples/` directory in order to ensure that the provider behaves identically when used by either `terraform` and `tofu` binaries.

The [OpenTofu Registry](https://registry.opentofu.org/) seems to be unpublished at time of writing. As of now, the provider is only published to the [Terraform Registry](https://registry.terraform.io/providers/codefresh-io/codefresh/latest).

## Contributors

<a href="https://github.com/codefresh-io/terraform-provider-codefresh/graphs/contributors">
Expand Down
20 changes: 19 additions & 1 deletion codefresh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,27 @@ steps:
matrix:
# The following will resolve to their latest patch version
environment:
- TF_VERSION=1.3.0
- TF_VERSION=1.4.0
- TF_VERSION=1.5.0
- TF_VERSION=1.6.0

equivalence_tests:
title: "Run tests"
stage: test
image: golang:1.18.10-alpine3.17
environment:
- TF_VERSION=1.6.0
commands:
- apk update
- apk add make
- go install github.com/warrensbox/[email protected]
- terraform-switcher --latest-stable ${TF_VERSION}
- apk add [email protected]_alpha2-r0 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/
- make equivalence
retry:
maxAttempts: 3
delay: 5
exponentialFactor: 2

prepare_env_vars:
title: "Preparing environment variables..."
Expand Down
2 changes: 1 addition & 1 deletion codefresh/internal/schemautil/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ func (o *ValidationOptions) setSummary(summary string) *ValidationOptions {
func (o *ValidationOptions) setDetailFormat(detailFormat string) *ValidationOptions {
o.detailFormat = detailFormat
return o
}
}
4 changes: 2 additions & 2 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ func TestAccCodefreshPipeline_CronTriggersInvalid(t *testing.T) {
var pipeline cfclient.Pipeline

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCodefreshPipelineBasicConfigCronTriggers(
Expand Down
13 changes: 6 additions & 7 deletions codefresh/resource_step_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package codefresh

import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
Expand Down Expand Up @@ -48,8 +47,8 @@ func TestCleanUpStepFromTransientValues(t *testing.T) {
}

func TestNormalizeYamlStringStepTypes(t *testing.T) {
testFile := "../test_data/step_types/testStepWithRuntimeData.yaml"
yamlString, err := ioutil.ReadFile(testFile)
testFile := "../testing/fixtures/testStepWithRuntimeData.yaml"
yamlString, err := os.ReadFile(testFile)
if err != nil {
t.Errorf("Unable to open test file %s. Err: #%v ", testFile, err)
}
Expand Down Expand Up @@ -84,8 +83,8 @@ func TestSortVersions(t *testing.T) {
}

func TestExtractSteps(t *testing.T) {
testFile := "../test_data/step_types/testStepTypesOrder.yaml"
yamlString, err := ioutil.ReadFile(testFile)
testFile := "../testing/fixtures/testStepTypesOrder.yaml"
yamlString, err := os.ReadFile(testFile)
if err != nil {
t.Errorf("Unable to read file %s", testFile)
}
Expand All @@ -112,11 +111,11 @@ func TestAccCodefreshStepTypes(t *testing.T) {
}
name := accountName + "/" + stepTypesNamePrefix + acctest.RandString(10)
resourceName := "codefresh_step_types.test"
contentStepsV1, err := ioutil.ReadFile("../test_data/step_types/testSteps.yaml")
contentStepsV1, err := os.ReadFile("../testing/fixtures/testSteps.yaml")
if err != nil {
log.Fatal(err)
}
contentStepsV2, err := ioutil.ReadFile("../test_data/step_types/testStepsTemplate.yaml")
contentStepsV2, err := os.ReadFile("../testing/fixtures/testStepsTemplate.yaml")
if err != nil {
log.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
variable api_url {
type = string
}

#
variable token {
type = string
default = ""
}

variable account_id {
type = string
default = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
codefresh = {
source = "codefresh-io/codefresh"
source = "registry.terraform.io/codefresh-io/codefresh"
version = "~> 0.1"
}
}
Expand Down
4 changes: 4 additions & 0 deletions examples/.modules/account_tokens/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Set of account names
variable accounts {
type = set(string)
}
8 changes: 8 additions & 0 deletions examples/.modules/account_tokens/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
codefresh = {
source = "registry.terraform.io/codefresh-io/codefresh"
version = "~> 0.1"
}
}
}
8 changes: 8 additions & 0 deletions examples/.modules/accounts_users/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
codefresh = {
source = "registry.terraform.io/codefresh-io/codefresh"
version = "~> 0.1"
}
}
}
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/.modules/teams/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable teams {
type = map(any)
}
8 changes: 8 additions & 0 deletions examples/.modules/teams/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
codefresh = {
source = "registry.terraform.io/codefresh-io/codefresh"
version = "~> 0.1"
}
}
}
10 changes: 3 additions & 7 deletions examples/abac_rules/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
data "codefresh_team" "admins" {
name = "admins"
}

data "codefresh_team" "users" {
name = "users"
}
Expand All @@ -12,10 +8,10 @@ resource "codefresh_abac_rules" "app_rule" {
actions = ["REFRESH", "SYNC", "TERMINATE_SYNC", "VIEW_POD_LOGS", "APP_ROLLBACK"]

attribute {
name = "LABEL"
key = "KEY"
name = "LABEL"
key = "KEY"
value = "VALUE"
}

tags = ["dev", "untagged"]
tags = ["dev", "untagged"]
}
5 changes: 0 additions & 5 deletions examples/abac_rules/provider.tf

This file was deleted.

3 changes: 0 additions & 3 deletions examples/abac_rules/terraform.tfvars

This file was deleted.

12 changes: 0 additions & 12 deletions examples/abac_rules/vars.tf

This file was deleted.

2 changes: 1 addition & 1 deletion examples/abac_rules/versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
codefresh = {
source = "codefresh-io/codefresh"
source = "registry.terraform.io/codefresh-io/codefresh"
version = "~> 0.1"
}
}
Expand Down
Loading
Loading