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

func: draft implementation of catalog repo visibility on creation #25

Draft
wants to merge 41 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
aec2feb
func: draft implementation of catalog repo visibility on creation
fhacloid Dec 4, 2024
5d6bb05
misc: formatting
fhacloid Dec 4, 2024
e774a79
func: migrate makefile to mage
fhacloid Dec 18, 2024
797b0ad
misc: add nix support for NixOs nerds
fhacloid Dec 18, 2024
15332ea
fix: added all missing ingredient in OpenAPI for code gen
fhacloid Dec 18, 2024
6a3e8be
misc: generated new files
fhacloid Dec 18, 2024
482fd6a
misc: update go mod
fhacloid Dec 18, 2024
3ede18e
func: managed to mapped visibility and team to on_create_x
fhacloid Dec 18, 2024
3e68544
misc: add init org script
fhacloid Dec 31, 2024
8991445
func: update docker compose and dev testing stuff
fhacloid Jan 2, 2025
0bf69bc
misc: add dev magefile
fhacloid Jan 2, 2025
e60488c
func: add tests
fhacloid Jan 2, 2025
346519a
misc: update go.mod
fhacloid Jan 2, 2025
c73ac23
misc: fix tests scripts
fhacloid Jan 3, 2025
2e0e8e6
misc: update swagger func
fhacloid Jan 3, 2025
efccafc
func: add working test suite
fhacloid Jan 3, 2025
217c201
func: add test utils
fhacloid Jan 3, 2025
48f63e4
func: add test setup / teardown
fhacloid Jan 3, 2025
6b7f411
fix: fix backend test setup
fhacloid Jan 3, 2025
6f8e69b
misc: fix some tests quirks and add test configuration
fhacloid Jan 6, 2025
5af44b5
remove secrets
fhacloid Jan 7, 2025
203553c
misc: update readme
fhacloid Jan 7, 2025
6ca1144
misc: add test config docs
fhacloid Jan 7, 2025
1bedcf5
misc: update go.mod
fhacloid Jan 7, 2025
568405e
misc: migrate magefiles -> justfiles
fhacloid Jan 8, 2025
cef033c
fix: add some debug infos in dev env
fhacloid Jan 9, 2025
ad0a07c
tests: add tests for catalog repo
fhacloid Jan 9, 2025
9564a95
func: add stack datasource
fhacloid Jan 9, 2025
daade9c
func: add support for stack visibility
fhacloid Jan 9, 2025
3b93a71
fix: automate org creation for tests
fhacloid Jan 10, 2025
c105f23
cleanup: old test files
fhacloid Jan 10, 2025
2357b61
func: add datasource stack to provider
fhacloid Jan 10, 2025
9854344
misc: renamed catalog repo resource since manual modifications
fhacloid Jan 10, 2025
2f88404
misc: update go.mod
fhacloid Jan 10, 2025
6942e38
misc: remove comment
fhacloid Jan 10, 2025
1d5477a
misc: add output bin to .gitignore
fhacloid Jan 10, 2025
4e4218c
func: speed up test start and fix org / api key allocation for tests
fhacloid Jan 10, 2025
ca8a3db
misc: update tests
fhacloid Jan 10, 2025
798cc18
misc: remove useless things in compose
fhacloid Jan 10, 2025
eec38a5
wip: stack almost working
fhacloid Jan 10, 2025
1e74e07
misc: commit gen settings
fhacloid Jan 10, 2025
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
10 changes: 10 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="
fi

watch_file flake.nix
watch_file flake.lock
if ! use flake . --no-pure-eval
then
echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2
fi
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.tfstate*
main.tf
variables.tf
*.env
.direnv/
.devenv/
.ci/
terraform-provider-cycloid
60 changes: 58 additions & 2 deletions DEVELOPING_TIPS.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
# Tips

## Dev tooling

### Requirements

To make the dev environment work, you need those binaries installed on your machine:

- `go` -> v1.22.9
- `cy` -> Will be used to pull secrets from cycloid console, so you need to configure you access to the `cycloid` org.
- `jq`
- `docker` -> a recent version with `docker compose` plugin.

All scripts are either wrote in POSIX sh (for MacOs compatibility) or go using [magefiles](https://magefile.org/) contained in [./magefiles](./magefiles).

### Backend

To spin up the backend for tests, use the `compose.yml` provider via the [`./ci/dc.sh`](./ci/dc.sh) script provider in this repository.

`dc.sh` will deal with environment sourcing and secret pulling from cycloid API.

### Testing

E2E testing has been implemented using terraform files.

To start the tests use `go test -v ./... -count=1`.

By default, tests will spin up the backend using the `./ci/dc.sh up_default` command, execute the tests, then exec `./ci/dc.sh clean`.

You can change this behavior with environment variables.

#### Test Configuration

If you need to add configuration to a test, it must be via environment variable and documented here.

##### Configuration

Value displayed are the default

```
/// TEST BEHAVIOR SECTION
/// This section allows you to change how test manage the backend.

// if true, tests will spin up a backend using docker compose - implies TEST_DC_CLEAN=true
TEST_DC_UP=true
// if true, tests will clean the backend after tests are runned
TEST_DC_CLEAN=true
// if true, tests will init admin user and API_KEY before testing
TEST_BACKEND_INIT=true

// You can provide yourself the backend licence, but it will fetch the staging one by default using cy
API_LICENCE_KEY=
```

#### Adding tests

see [the tests Readme](./tests/e2e/README.md) for more information on how to add e2e tests.

## How to work on it

Docs: https://developer.hashicorp.com/terraform/plugin/code-generation/openapi-generator#generator-config
Docs: https://developer.hashicorp.com/terraform/plugin/code-generation/openapi-generator#generator-config

We have 2 required files:
* `openapi.yaml`: Which is the Swagger spec of the Cycloid API
* `generator_config.yaml`: Which is the definition of the Provider spec. It basically says which parts of the `openapi.yaml` are part of the Provider when generating

Any change to the `openapi.yaml` or `generator_config.yaml` will require to run
Any change to the `openapi.yaml` or `generator_config.yaml` will require to run

```
tfplugingen-openapi generate --config generator_config.yml --output out_code_spec.json openapi.yaml
Expand Down
139 changes: 139 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/usr/bin/env just --justfile
# If you need help for this file format
# https://just.systems/man/en
## -- Variables and settings

set unstable := true
set shell := ["/usr/bin/env", "sh", "-c"]
set script-interpreter := ["/usr/bin/env", "sh"]
set dotenv-filename := ".env"
set dotenv-load := true

export WORKDIR := justfile_directory()

## -- Common section ##

# print the project help
help:
@just --list --list-submodules

# target to do whatever you want locally
playground: lint build
go test ./tests/utils
@just e2e-no-clean "-run=TestE2e/test_resource_catalog_use_stack_list_output"

[doc("install required dependencies")]
[script]
install:
deps='
github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi@latest
github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework@latest
github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
.
'
for dep in $deps; do echo "installing $dep"; go install "$dep"; done

[group("lint")]
lint: lint-sh lint-go

[group("lint")]
lint-sh:
@shellcheck ci/*.sh -x ci/lib.sh

[group("lint")]
lint-go:
@go vet -c 2

build:
@go build -gcflags "all=-N -l" -trimpath

# Test all the packages
[group("test")]
test *args:
go test -v ./... {{ args }}

alias e2e := test-e2e

# start e2e tests
[group("test")]
test-e2e *args:
go test -v ./tests/e2e -count=1 {{ args }}

alias e2e-no-clean := test-e2e-no-clean

# exec test without cleaning the backend afterwards, useful to speed up dev -> test loop
[group("test")]
[script]
test-e2e-no-clean *args:
export TEST_DC_UP=true
export TEST_DC_CLEAN=false
just test-e2e {{ args }}

alias e2e-manual := test-e2e-manual

# execute the e2e test to <folder> using `./ci/tf-test.sh`
[group("test")]
test-e2e-manual folder cmd="run_test":
test -d {{ folder }} || { echo "folder does not exists."; exit 1; }
TF_TEST_TARGET_DIR={{ folder }} ./ci/tf-test.sh "{{ cmd }}"

# clean the repo's generated files
clean:
rm -rf ./.ci

[doc("execute docker compose commmand using ./ci/dc.sh script")]
[group("docker")]
compose *cmd:
./ci/dc.sh cmd {{ cmd }}

[doc("execute ./ci/init-backend.sh script")]
[group("docker")]
init-backend *cmd:
./ci/init-backend.sh {{ cmd }}

[doc("start the backend and init the root org")]
[group("docker")]
start-backend:
@just compose up -d

alias logs := get-backend-logs

[group("docker")]
get-backend-logs:
@just compose logs -f

alias stop-backend := clean-backend

# stop the backend and clean volumes
[group("docker")]
clean-backend:
@just compose down -v

[group("docker")]
reset-backend: clean-backend start-backend

[group("docker")]
pull:
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 661913936052.dkr.ecr.eu-west-1.amazonaws.com

[doc("execute cy to the local backend with credentials set for `org` with `args`")]
test-cy org *args: start-backend
# Careful before editing, this function is used in test
CY_API_KEY="$(cat .ci/api-keys/{{ org }})" cy --insecure --api-url "http://127.0.0.1:3001" --org {{ org }} {{ args }}

# watch changes in repository and execute <cmd>
watch *cmd:
watchexec -w provider \
-w Justfile \
-w tests \
-w ci \
-e go -e tf -e sh \
-c -r {{ cmd }}

## -- Swagger section ##

import "swagger.just"

## -- Terraform section ##

import "tf.just"
36 changes: 4 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
.PHONY: help
help: ## Show this help
@grep -F -h "##" $(MAKEFILE_LIST) | grep -F -v fgrep | sed -e 's/:.*##/:##/' | column -t -s '##'

.PHONY: tf-generate
tf-generate: ## Will regenerate the new provider spec and models
@tfplugingen-openapi generate --config generator_config.yml --output out_code_spec.json openapi.yaml
@tfplugingen-framework generate resources --input ./out_code_spec.json --output .

.PHONY: new-resource
new-resource: ## Generates boilplate code for new resource R
@tfplugingen-framework scaffold resource --name $(R) --output-dir ./provider

.PHONY: install
install: ## Install the provider
@go install .

.PHONY: plan
plan: install ## Will run a plan
@terraform plan

.PHONY: apply
apply: install ## Will run an apply
@terraform apply -auto-approve

.PHONY: destroy
destroy: install ## Will run a destroy
@terraform destroy -auto-approve

.PHONY: docs
docs: ## Generates the provider docs
@tfplugindocs generate ./..
.DEFAULT:
@just $(MAKECMDGOALS)

default:
@just --list --list-submodules
12 changes: 12 additions & 0 deletions ci/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Tooling readme

This is where the project's tooling lies.

## Rules

- All scripts must be written in POSIX sh
- All scripts must be executed from repo's root
- Use the `.ci/` folder from repo's root as cache / temporary folder
- All functions/lib must be in the `lib.sh` file. Don't try to split it, it's too cumbersome in sh.
- Every script should print an help when called with `./script.sh -h`
- All CI procedure MUST be in specific `ci-` prefixed scripts
57 changes: 57 additions & 0 deletions ci/dc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#! /usr/bin/env sh
# shellcheck disable=SC2317

. ci/lib.sh

set -eu
test -n "${CI_DEBUG-}" && set -x

_requires_prog cy jq docker

_register_cmd cmd "docker compose cli wrapper"
cmd() {
_get_secrets
docker compose --env-file ".ci/secrets.env" "$@"
}

_register_cmd up "docker compose up with default services"
# shellcheck disable=SC2086
up() {
cmd up "$@"

}
_register_cmd up_default "docker compose up with default flags for tests"
# shellcheck disable=SC2086
up_default() {
cmd up -d
}

_register_cmd down "destroy project"
down() {
cmd down "$@"
}

_register_cmd clean "destroy project and remove volumes"
clean() {
_clean_secrets
cmd down -v "$@" -t 2
}

_register_cmd get_secrets "pull secrets required for this repo"
get_secrets() {
_clean_secrets
_get_secrets
}

_register_cmd logs "see logs"
logs() {
cmd logs -f "$@"
}

_register_cmd print_env "dump the env after script evaluation"
print_env() {
env | grep TEST_
env | grep ^COMPOSE_
}

_parse_subcommand "$@"
Loading