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

fix(CLOUDDEV-414): fixed makefile and readme instructions #41

Merged
merged 1 commit into from
Jan 17, 2024
Merged
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ terraform.tfstate

### bin
bin/
.local.env

### env
**/*.env

### other
local-files
Expand Down
62 changes: 29 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,35 @@ TAG = $(shell git describe --tags)
VERSION = $(shell git describe --tags $(LAST_TAG_COMMIT) | sed "s/^$(TAG_PREFIX)//")
PLUGIN_PATH = ~/.terraform.d/plugins/local.edgecenter.ru/repo/edgecenter/$(VERSION)/$(OS)_$(ARCH)


create_bin:
mkdir -p $(BIN_DIR)

install_jq:
if test "$(OS)" = "linux"; then \
curl -L -o $(BIN_DIR)/jq https://github.com/stedolan/jq/releases/download/jq-1.7/jq-linux64; \
elif test "$(ARCH)" = "arm64"; then \
curl -L -o $(BIN_DIR)/jq https://github.com/stedolan/jq/releases/download/jq-1.7/jq-macos-arm64; \
else \
curl -L -o $(BIN_DIR)/jq https://github.com/stedolan/jq/releases/download/jq-1.7/jq-osx-amd64; \
fi
chmod +x $(BIN_DIR)/jq


install_godotenv:
go install github.com/joho/godotenv/cmd/godotenv@latest


download_env_file:
@if [ -z "${VAULT_TOKEN}" ] || [ -z "${VAULT_ADDR}" ]; then \
echo "ERROR: Vault environment is not set, please setup VAULT_ADDR and VAULT_TOKEN environment variables" && exit 1;\
fi
vault kv get -format=json --field data /CLOUD/terraform | $(BIN_DIR)/jq -r 'to_entries|map("\(.key)=\(.value)")|.[]' > $(ENV_TESTS_FILE)

tidy:
go mod tidy

init: create_bin install_jq install_godotenv download_env_file tidy
# BUILD
build: tidy
mkdir -p $(PLUGIN_PATH)
Expand All @@ -38,10 +64,6 @@ build_debug: tidy
go build -o $(PLUGIN_PATH)/$(BINARY_NAME)_v$(VERSION) -gcflags '-N -l'
go build -o bin/$(BINARY_NAME) -gcflags '-N -l'

# CHECKS
err_check:
@sh -c "'$(PROJECT_DIR)/scripts/errcheck.sh'"

linters:
@test -f $(BIN_DIR)/golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.54.2
@$(BIN_DIR)/golangci-lint run
Expand All @@ -50,41 +72,15 @@ linters_docker: # for windows
docker run --rm -v $(PROJECT_DIR):/app -w /app golangci/golangci-lint:v1.54.2 golangci-lint run -v

# TESTS
envs_reader:
go install github.com/joho/godotenv/cmd/godotenv@latest

test_cloud_data_source: envs_reader
test_cloud_data_source: install_godotenv
godotenv -f $(ENV_TESTS_FILE) go test $(TEST_DIR) -tags cloud_data_source -short -timeout=20m

test_cloud_resource: envs_reader
test_cloud_resource: install_godotenv
godotenv -f $(ENV_TESTS_FILE) go test $(TEST_DIR) -tags cloud_resource -short -timeout=20m

test_not_cloud: envs_reader
test_not_cloud: install_godotenv
godotenv -f $(ENV_TESTS_FILE) go test $(TEST_DIR) -tags dns storage cdn -v -timeout=5m

# local test run (need to export VAULT_TOKEN env)
install_jq:
if test "$(OS)" = "linux"; then \
curl -L -o $(BIN_DIR)/jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64; \
else \
curl -L -o $(BIN_DIR)/jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64; \
fi
chmod +x $(BIN_DIR)/jq

install_vault:
curl -L -o vault.zip https://releases.hashicorp.com/vault/1.13.3/vault_1.13.3_$(OS)_$(ARCH).zip
unzip vault.zip && rm -f vault.zip && chmod +x vault
mv vault $(BIN_DIR)/

download_env_file: envs_reader
godotenv -f $(ENV_TESTS_FILE) $(BIN_DIR)/vault login -method=token $(VAULT_TOKEN)
godotenv -f $(ENV_TESTS_FILE) $(BIN_DIR)/vault kv get -format=json --field data /CLOUD/terraform | $(BIN_DIR)/jq -r 'to_entries|map("\(.key)=\(.value)")|.[]' >> $(ENV_TESTS_FILE)

test_local_data_source: envs_reader
godotenv -f .local.env go test $(TEST_DIR) -tags cloud_data_source -short -timeout=5m -v

test_local_resource: envs_reader
godotenv -f .local.env go test $(TEST_DIR) -tags cloud_resource -short -timeout=10m -v

# DOCS
docs_fmt:
Expand Down
220 changes: 155 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,102 +1,217 @@
Terraform EdgeCenter Provider
------------------------------

<img src="https://edgecenter.ru/img/logo.svg" data-src="https://edgecenter.ru/img/logo.svg" alt="EdgeCenter" width="500px" width="500px">
<img alt="EdgeCenter" src="https://edgecenter.ru/files/marketing/logo_EC_black.svg" width="500px"/>
====================================================================================

- [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby)
- Mailing list: [Google Groups](http://groups.google.com/group/terraform-tool)

Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.13.x
- [Go](https://golang.org/doc/install) 1.19 (to build the provider plugin)

Latest provider
------------
- [edge-center provider](https://registry.terraform.io/providers/Edge-Center/edgecenter/latest)

Building the provider
---------------------
```sh
$ mkdir -p $GOPATH/src/github.com/terraform-providers
$ cd $GOPATH/src/github.com/terraform-providers
$ git clone https://github.com/Edge-Center/terraform-provider-edgecenter.git
$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-edgecenter
$ make build
```

### Override Terraform provider
Using the provider
------------------
### Requirements


To override terraform provider for development goals you do next steps:
- [Terraform](https://www.terraform.io/downloads.html) >= 0.13

create Terraform configuration file
### Configuring terraform

#### Create Terraform configuration file
```shell
$ touch ~/.terraformrc
```
For using terraform without VPN, you should configure mirror in `~/.terraformrc` file:

```terraform
provider_installation {

network_mirror {
url = "https://terraform-mirror.yandexcloud.net/"
include = ["registry.terraform.io/*/*"]
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {
exclude = ["registry.terraform.io/*/*"]
}
}
```
### Project initializing

point provider to development path
```shell
#### Prepare module configuration file
It is necessary to add provider settings according to the instructions https://developer.hashicorp.com/terraform/language/providers/requirements:
Each Terraform module must declare which providers it requires, so that Terraform can install and use them.

To use the provider, prepare configuration file `provider.tf` in the module directory.

```terraform
terraform {
required_version = ">= 0.13.0"

required_providers {
edgecenter = {
source = "Edge-Center/edgecenter"
version = "{version_number}" # need to specify (choose from https://github.com/Edge-Center/terraform-provider-edgecenter/releases)
}
}
}


provider edgecenter {
edgecenter_platform_api = "https://api.edgecenter.ru/iam"
edgecenter_cloud_api = "https://api.edgecenter.ru/cloud"
permanent_api_token = "{your_permanent_token}" # need to specify (you can create it on the page https://accounts.edgecenter.ru/profile/api-tokens)
}
```
#### Initialize working directory
Run terraform init in the module directory.
```bash
terraform init
```
This command initializes a working directory containing Terraform configuration files. This is the first command that
should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times

### Writing modules files

Create module files using the examples provided in the `./examples` folder.


Development
-----------
### Requirements

- [Terraform](https://www.terraform.io/downloads.html) >= 0.13
- [vault](https://developer.hashicorp.com/vault/install)
- [Go](https://golang.org/doc/install) 1.20 (to build the provider plugin)



### Initialization of the project

#### Setting Vault envs
Before initializing the project you should to set VAULT_ADDR and VAULT_TOKEN envs:
```bash
export VAULT_ADDR=<you can ask address from Cloud team (matermost tag: @devcloud)>
export VAULT_TOKEN=<you can generate this token at the ${VAULT_ADDR} page>
```
#### Initialize
Initialize the project using this command:
```sh
$ make init
```

### Building the provider

```sh
$ make build
```

### Using built Terraform provider

To use the built provider from the previous step, you should add the dev_overrides option to the `~/.terraformrc` file:

```terraform
provider_installation {

dev_overrides {
"local.edgecenter.ru/repo/edgecenter" = "/<dev-path>/terraform-provider-edgecenter/bin"
}

network_mirror {
url = "https://terraform-mirror.yandexcloud.net/"
include = ["registry.terraform.io/*/*"]
}

# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
direct {
exclude = ["registry.terraform.io/*/*"]
}
}
```
Then you should use `"local.edgecenter.ru/repo/edgecenter"` source in required_provider settings in the `provider.tf` file:

add `local.edgecenter.ru/repo/edgecenter` to .tf configuration file
```shell
```terraform
terraform {
required_version = ">= 0.13.0"

required_providers {
edgecenter = {
source = "local.edgecenter.ru/repo/edgecenter"
version = "{version_number}" # need to specify
source = "local.edgecenter.ru/repo/edgecenter"
}
}
}
```

Using the provider
------------------
To use the provider, prepare configuration files based on examples

```sh
$ cp ./examples/... .
$ terraform init
provider edgecenter {
edgecenter_platform_api = "https://api.edgecenter.ru/iam"
edgecenter_cloud_api = "https://api.edgecenter.ru/cloud"
permanent_api_token = "{your_permanent_token}" # need to specify (you can create it on the page https://accounts.edgecenter.ru/profile/api-tokens)
}
```

Testing
------------------
Remote: Tests are run with provided secrets envs in the GitHub repository.
Local: execute the command `make test_local_data_source` and `make test_local_resource`. For this command to work, you need to:
* Create a `.local.env` file and fill it with the necessary envs.
* Run `make envs` to automatically fill the envs from Vault (don't forget to export `VAULT_TOKEN` to terminal).
* `make envs` requires the installation of `jq` and the `vault` binary. You can install them with the `make install_vault` and `make install_jq` commands, respectively.
### Remote
Tests are run with provided secrets envs in the GitHub repository.
### Local
For testing cloud data sources
```bash
make test_cloud_data_source
```
For testing cloud resources
```bash
make test_cloud_resource
```
For testing not cloud (storage, cdn, dns) data sources and resources:
```bash
make test_not_cloud
```

Lint
----------
To lint project code, use:
```bash
make linters
```

Docs generating
------------------
To generate Terraform documentation, use the command `make docs`. This command uses the `terraform-plugin-docs` library to create provider documentation with examples and places it in the `docs` folder. These docs can be viewed on the provider registry page.


Debugging
------------------
There are two ways to debug the provider:
### Goland debugging
1. Add new go build configuration with field ```program arguments```:
```-debug -address=local.edgecenter.ru/repo/edgecenter```
2. Run this configuration;
3. Export generated TF_REATTACH_PROVIDERS env from debug output:
```bash
export TF_REATTACH_PROVIDERS='{"local.edgecenter.ru/repo/edgecenter":{"Protocol":"grpc","ProtocolVersion":5,"Pid":65114,"Test":true,"Addr":{"Network":"unix","String":"/var/folders/g4/q8cpkx6n7gg_1cvr0lrtkd4w0000gq/T/plugin1422839680"}}}'
```
4. Set a breakpoint in your code and apply the Terraform config:
```bash
terraform apply
```
5. Debugging.

### VSCode debugging
1. Create a `launch.json` file:
* In the Run view, click `create a launch.json file`.
* Choose Go: Launch Package from the debug configuration drop-down menu.
* VS Code will create a `launch.json` file in a `.vscode` folder in your workspace.
2. Add a new configuration to `launch.json`:
* The `address` argument must be equal to the `source` field from your `provider.tf`.
``` {
```json
{
"version": "0.2.0",
"configurations": [
{
Expand All @@ -122,29 +237,4 @@ There are two ways to debug the provider:
5. Set a breakpoint in your code and apply the Terraform config: `terraform apply`.
6. Debugging.

### using delve
1. Install the Delve library - [installation](https://github.com/go-delve/delve/tree/master/Documentation/installation)
2. Build binary without optimization or use `make build_debug`
```shell
go build -o bin/$(BINARY_NAME) -gcflags '-N -l'
```
3. Open the first terminal:
* Run the binary with the debug option:
```shell
dlv exec bin/terraform-provider-edgecenter -- -debug
```
* Set a breakpoint for the create function with a resource that you want to debug, e.g,
```shell
break resourceFloatingIPCreate
```
* `continue`
* Copy `TF_REATTACH_PROVIDERS` with its value from output
4. Open the second terminal:
* Export `TF_REATTACH_PROVIDERS`:
```shell
export TF_REATTACH_PROVIDERS='{"local.edgecenter.ru/repo/edgecenter":{...'
```
* Launch ```terraform apply```
* Debug with the `continue` command in the first terminal via `delve`

Thank You
Loading