Skip to content

Commit

Permalink
Use dev_override to allow for easier local development (#569)
Browse files Browse the repository at this point in the history
Update contribution guidelines to use terraform CLI configureation via ~/.terraformrc and the use of dev_override allowing a simpler onboarding to develop on the provider.

Squashed commits:
* Use dev_override to allow for easier local development environment
* fix typo
  • Loading branch information
Thiryn authored Oct 16, 2024
1 parent 81f3e7b commit 2cb12d6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
84 changes: 40 additions & 44 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,36 @@ Thanks for getting involved in the Terraform Provider for OpenNebula. Here are a
* [Install Go](https://go.dev/doc/install)
* [Install Terraform](https://learn.hashicorp.com/terraform/getting-started/install)

### Building from sources
### Local development of the provider

```shell
export tf_arch=darwin_arm64
export tf_one_version=0.0.1
To develop and use this provider locally, you can leverage the [`dev_override`](https://developer.hashicorp.com/terraform/cli/config/config-file#development-overrides-for-provider-developers) feature of the terraform CLI.
To do so, create a `$HOME/.terraformrc` file if it doesn't exist yet (see the [`terraform` CLI documentation](https://developer.hashicorp.com/terraform/cli/config/config-file#locations) for different consideration for Windows systems) with the following content:

# Clone terraform-provider-opennebula
git clone git@github.com:OpenNebula/terraform-provider-opennebula.git

# Create directory under Terraform plugins directory
mkdir -p ${HOME}/.terraform.d/plugins/one.test/one/opennebula/${tf_one_version}/${tf_arch}

# Create a link to the Provider binary
ln -s $(pwd)/terraform-provider-opennebula/terraform-provider-opennebula ${HOME}/.terraform.d/plugins/one.test/one/opennebula/${tf_one_version}/${tf_arch}
```hcl
provider_installation {
dev_overrides {
"OpenNebula/opennebula" = "[LOCAL_PATH_TO_THIS_REPO]"
}
direct {}
}
```

# Build the Provider
cd terraform-provider-opennebula
go build
This configuration will not apply any change for `terraform init` or your `.terraform.lock.hcl`. From the Terraform documentation:
```text
With development overrides in effect, the terraform init command will still attempt to select a suitable published
version of your provider to install and record in the dependency lock file for future use, but other commands like
terraform apply will disregard the lock file's entry and will use the given directory instead.
```

Now you can create a new `main.tf` file:
This configuration will check for the `terraform-provider-opennebula` binary at the given directory. You can generate it using `make build`

Example:
```hcl
terraform {
required_providers {
opennebula = {
source = "one.test/one/opennebula"
source = "OpenNebula/opennebula" # use the real provider as source
version = "1.4.0"
}
}
}
Expand All @@ -44,37 +47,30 @@ provider "opennebula" {
# ...
}
resource "opennebula_image" "image" {
data opennebula_datastore "my_datastore" {
# ...
}
```

During the `terraform init`, the provider should be initialized as `unauthenticated`:

```text
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of one.test/one/opennebula...
- Installing one.test/one/opennebula v0.0.1...
- Installed one.test/one/opennebula v0.0.1 (unauthenticated)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Applying changes for the above simple terraform module shows the override in action:
```txt
$> terraform apply
│ Warning: Provider development overrides are in effect
│ The following provider development overrides are set in the CLI configuration:
│ - opennebula/opennebula in [LOCAL_PATH_TO_THIS_REPO]
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
data.opennebula_datastore.my_datastore: Reading...
data.opennebula_datastore.my_datastore: Read complete after 0s
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
```

## Issues and Pull Requests
Expand Down
9 changes: 6 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
PKG_NAME=opennebula

default: build
default: install

build: fmtcheck
install: fmtcheck
go install

build: fmtcheck
go build -o terraform-provider-opennebula

test: fmtcheck
go test -i $(TEST) || exit 1
echo $(TEST) | \
Expand Down Expand Up @@ -57,5 +60,5 @@ ifeq (,$(wildcard $(GOPATH)/src/$(WEBSITE_REPO)))
endif
@$(MAKE) -C $(GOPATH)/src/$(WEBSITE_REPO) website-provider-test PROVIDER_PATH=$(shell pwd) PROVIDER_NAME=$(PKG_NAME)

.PHONY: build test testacc vet fmt fmtcheck errcheck test-compile website website-test
.PHONY: install build test testacc vet fmt fmtcheck errcheck test-compile website website-test

0 comments on commit 2cb12d6

Please sign in to comment.