Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from jhernand/initial_prototype
Browse files Browse the repository at this point in the history
Initial prototype
  • Loading branch information
jhernand authored Nov 10, 2021
2 parents fad4c20 + 1170a37 commit f2b2bfc
Show file tree
Hide file tree
Showing 21 changed files with 2,551 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/check-pull-request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: Check pull request

on:
pull_request:
branches:
- main

jobs:

test:
name: Test
strategy:
matrix:
platform:
- ubuntu-latest
- macos-latest
- windows-latest
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout the source
uses: actions/checkout@v2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Setup Ginkgo
run: go install github.com/onsi/ginkgo/[email protected]

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_wrapper: false

- name: Run the tests
run: make tests
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
.terraform.d/
.terraform.lock.hcl
.terraform/
terraform.tfstate
4 changes: 4 additions & 0 deletions CHANGES.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Changes

This document describes the relevant changes between releases of the Terraform
provider for OCM.
52 changes: 52 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Disable CGO so that we always generate static binaries:
export CGO_ENABLED=0

# Version of the provider:
version:=0.1.0

.PHONY: build
build:
platform=$$(terraform version -json | jq -r .platform); \
ext=""; \
if [[ "$${platform}" =~ ^windows_.*$$ ]]; then \
ext=".exe"; \
fi; \
dir=".terraform.d/plugins/localhost/redhat/ocm/$(version)/$${platform}"; \
file="terraform-provider-ocm$${ext}"; \
mkdir -p "$${dir}"; \
go build -o "$${dir}/$${file}"

.PHONY: test tests
test tests: build
ginkgo -r tests

.PHONY: fmt_go
fmt_go:
gofmt -s -l -w $$(find . -name '*.go')

.PHONY: fmt_tf
fmt_tf:
terraform fmt -recursive examples

.PHONY: fmt
fmt: fmt_go fmt_tf

.PHONY: clean
clean:
rm -rf .terraform.d
82 changes: 82 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
= Terraform provider for OCM

== Build

To build the provider use the `make` command.

== Use

Currently the provider isn't available in any public registry, so you will need
to build it:

....
$ make build
....

This will create a local Terraform plugin registry in the directory
`.terraform.d/plugins` of the project. Assuming that you have the project
checked out in `/files/projects/terraform-provider-ocm/repository` you will
need to add something like this to your Terraform CLI configuration file:

....
provider_installation {
filesystem_mirror {
path = "/files/projects/terraform-provider-ocm/repository/.terraform.d/plugins"
include = ["localhost/*/*"]
}
}
....

If you don't want to change your global CLI configuation file you can put this
in any file you like and then use the `TF_CLI_CONFIG_FILE` environment variable
to point to it. For example, put the configuration in
`/files/projects/terraform-provider-ocm/terraform.rc` and then set the
environment variable pointing to it:

....
$ cat >/files/projects/terraform-provider-ocm/terraform.rc <<.
provider_installation {
filesystem_mirror {
path = "/files/projects/terraform-provider-ocm/repository/.terraform.d/plugins"
include = ["localhost/*/*"]
}
}
.
$ export TF_CLI_CONFIG_FILE=/files/projects/terraform-provider-ocm/terraform.rc
....

Once your configuration is ready you can go to the directory containing the
Terraform `.tf` files and run the `terraform init` and `terraform apply`
commands:

....
$ terraform init
$ terraform apply
....

To see the debug log of the provider set the `TF_LOG` environment variable to
`DEBUG` before running the `terraform apply` command:

....
$ export TF_LOG=DEBUG
$ terraform apply
....

== Examples

The `examples` directory contains some examples, together with _Makefiles_ to
simplify their use. For example, to run the `create_cluster` example:

....
$ make build
$ cd examples/create_cluster
$ make
....

Note that this examples expect that you have the `ocm` command line utility
installed, and use it to get a valid access token (with `ocm token`) and put it
in the `OCM_TOKEN` environment variable where the provider gets the token by
default.

Use the `make clean` command to remove the `.terraform` and `terraform.tfstate`
files so that the next time you run the example it starts from scratch.
33 changes: 33 additions & 0 deletions examples/create_cluster/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

.PHONY: apply
apply: init
OCM_TOKEN=$$(ocm token) \
terraform apply

.PHONY: destroy
destroy: init
OCM_TOKEN=$$(ocm token) \
terraform destroy

.PHONY: init
init:
terraform init

.PHONY:
clean:
rm -rf .terraform .terraform.lock.hcl terraform.tfstate
4 changes: 4 additions & 0 deletions examples/create_cluster/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Cluster creation example

This example shows how to create a cluster. To run it use adjust the description
of the cluster in the `main.tf` file and then use the `make` command.
37 changes: 37 additions & 0 deletions examples/create_cluster/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

terraform {
required_providers {
ocm = {
version = "0.1"
source = "localhost/redhat/ocm"
}
}
}

provider "ocm" {
url = "https://api.stage.openshift.com"
}

resource "ocm_cluster" "my_cluster" {
name = "my-cluster"
cloud_provider = "aws"
cloud_region = "us-west-1"
properties = {
fake_cluster = "true"
}
}
28 changes: 28 additions & 0 deletions examples/list_cloud_providers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

.PHONY: apply
apply: init
OCM_TOKEN=$$(ocm token) \
terraform apply

.PHONY: init
init:
terraform init

.PHONY:
clean:
rm -rf .terraform .terraform.lock.hcl terraform.tfstate
5 changes: 5 additions & 0 deletions examples/list_cloud_providers/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Cloud provider list example

This example shows how to use the cloud providers data source to get a list of
the identifiers of the supported cloud providers. To run it use the `make`
command.
36 changes: 36 additions & 0 deletions examples/list_cloud_providers/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright (c) 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

terraform {
required_providers {
ocm = {
version = "0.1"
source = "localhost/redhat/ocm"
}
}
}

provider "ocm" {
url = "https://api.stage.openshift.com"
}

data "ocm_cloud_providers" "all" {
}

output "cloud_provider_ids" {
description = "Cloud providers"
value = data.ocm_cloud_providers.all.ids
}
Loading

0 comments on commit f2b2bfc

Please sign in to comment.