Skip to content

Commit

Permalink
Merge pull request #63 from dikhan/refactor/remove-symlinks
Browse files Browse the repository at this point in the history
Refactor/remove symlinks
  • Loading branch information
dikhan authored Oct 7, 2018
2 parents 0023591 + 2a88b13 commit 0250626
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 99 deletions.
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM golang:1.8

WORKDIR /openapi

ENV TERRAFORM_VERSION=0.11.8

RUN apt-get update && \
apt-get install unzip openssl ca-certificates && \
cd /tmp && \
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \
unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/bin && \
rm -rf /tmp/* && \
rm -rf /var/cache/apk/* && \
rm -rf /var/tmp/*

# provision openapi plugins
RUN export PROVIDER_NAME=goa && curl -fsSL https://raw.githubusercontent.com/dikhan/terraform-provider-openapi/master/scripts/install.sh | bash -s -- --provider-name $PROVIDER_NAME
RUN export PROVIDER_NAME=swaggercodegen && curl -fsSL https://raw.githubusercontent.com/dikhan/terraform-provider-openapi/master/scripts/install.sh | bash -s -- --provider-name $PROVIDER_NAME

# copy examples including terraform configurations
COPY examples/ .

# move plugin config file set up with openapi providers configuration to terraform plugins folder
RUN mv terraform-provider-openapi.yaml /root/.terraform.d/plugins/

CMD ["bash"]
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ local-env-down: fmt
# make local-env
local-env: fmt
@echo "[INFO] Bringing up local environment"
@docker-compose -f ./build/docker-compose.yml up --detach --build --force-recreate
@docker-compose -f ./build/docker-compose.yml up --detach --build --force-recreate swaggercodegen-service-provider-api swagger-ui-swaggercodegen goa-service-provider-api

# make examples-container
examples-container: local-env
@echo "[INFO] Bringing up container with OpenAPI providers examples"
@docker-compose -f ./build/docker-compose.yml up --detach --build --force-recreate terraform-provider-openapi-examples
@docker-compose -f ./build/docker-compose.yml run terraform-provider-openapi-examples

# [TF_CMD=apply] make run-terraform-example-swaggercodegen
run-terraform-example-swaggercodegen: build pre-requirements
Expand Down Expand Up @@ -88,9 +94,8 @@ release-version:
define install_plugin
@$(eval TF_PROVIDER_PLUGIN_NAME := $(TF_PROVIDER_NAMING_CONVENTION)$(1))

@echo "[INFO] Installing $(TF_PROVIDER_PLUGIN_NAME) binary in -> $(TF_INSTALLED_PLUGINS_PATH)"
@mv ./$(TF_OPENAPI_PROVIDER_PLUGIN_NAME) $(TF_INSTALLED_PLUGINS_PATH)
@ln -sF $(TF_INSTALLED_PLUGINS_PATH)/$(TF_OPENAPI_PROVIDER_PLUGIN_NAME) $(TF_INSTALLED_PLUGINS_PATH)/$(TF_PROVIDER_PLUGIN_NAME)
@echo "[INFO] Installing $(TF_PROVIDER_PLUGIN_NAME) binary in -> $(TF_INSTALLED_PLUGINS_PATH)/$(TF_PROVIDER_PLUGIN_NAME)"
@mv ./$(TF_OPENAPI_PROVIDER_PLUGIN_NAME) $(TF_INSTALLED_PLUGINS_PATH)/$(TF_PROVIDER_PLUGIN_NAME)
endef

define run_terraform_example
Expand Down
98 changes: 46 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,17 @@ the suggested options:
page.
- Extract contents of tar ball and copy the terraform-provider-openapi binary into your ````~/.terraform.d/plugins````
folder as described in the [Terraform documentation on how to install plugins](https://www.terraform.io/docs/extend/how-terraform-works.html#discovery).
- After installing the plugin, you have two options. Either:
- After installing the plugin, rename the binary file to have your provider's name:

- Rename the binary file to have your provider's name:

````
$ cd ~/.terraform.d/plugins
$ mv terraform-provider-openapi terraform-provider-<your_provider_name>
$ ls -la
total 29656
drwxr-xr-x 4 dikhan staff 128 3 Jul 15:13 .
drwxr-xr-x 4 dikhan staff 128 3 Jul 13:53 ..
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-<your_provider_name>
````
- Create a symlink pointing at the terraform-provider-openapi binary. The latter is recommended so the same compiled binary
'terraform-provider-openapi' can be reused by multiple openapi providers and also reduces the number of providers to support.
````
$ cd ~/.terraform.d/plugins
$ ln -sF terraform-provider-openapi terraform-provider-<your_provider_name>
$ ls -la
total 29656
drwxr-xr-x 4 dikhan staff 128 3 Jul 15:13 .
drwxr-xr-x 4 dikhan staff 128 3 Jul 13:53 ..
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-openapi
lrwxr-xr-x 1 dikhan staff 63 3 Jul 15:11 terraform-provider-<your_provider_name> -> /Users/dikhan/.terraform.d/plugins/terraform-provider-openapi
````
````
$ cd ~/.terraform.d/plugins
$ mv terraform-provider-openapi terraform-provider-<your_provider_name>
$ ls -la
total 29656
drwxr-xr-x 4 dikhan staff 128 3 Jul 15:13 .
drwxr-xr-x 4 dikhan staff 128 3 Jul 13:53 ..
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-<your_provider_name>
````

Where ````<your_provider_name>```` should be replaced with your provider's name. This is the name that will also be used
in the Terraform tf files to refer to the provider resources.
Expand Down Expand Up @@ -126,16 +110,15 @@ $ export PROVIDER_NAME=goa && curl -fsSL https://raw.githubusercontent.com/dikha
````

The install script will download the most recent [terraform-provider-openapi release](https://github.com/dikhan/terraform-provider-openapi/releases)
and install it in the terraform plugins folder ````~/.terraform.d/plugins```` as described above. The terraform plugins
folder should contain both the terraform-provider-openapi provider and the provider's symlink pointing at it.
and install it in the terraform plugins folder ````~/.terraform.d/plugins```` as described above. The terraform plugins
folder should contain the newly installed open api customer terraform provider with the name provider in the installation (PROVIDER_NAME=goa) ```terraform-provider-goa```.

````
$ ls -la ~/.terraform.d/plugins
total 29656
drwxr-xr-x 4 dikhan staff 128 3 Jul 15:13 .
drwxr-xr-x 4 dikhan staff 128 3 Jul 13:53 ..
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-openapi
lrwxr-xr-x 1 dikhan staff 63 3 Jul 15:11 terraform-provider-goa -> /Users/dikhan/.terraform.d/plugins/terraform-provider-openapi
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-goa
````

### Using the OpenAPI Terraform provider
Expand Down Expand Up @@ -163,39 +146,39 @@ to the plugin in two different ways:
Terraform will need to be executed passing in the OTF_VAR_<provider_name>_SWAGGER_URL environment variable pointing at the location
where the swagger file is hosted, where````<your_provider_name>```` should be replaced with your provider's name.

```
$ terraform init && OTF_VAR_goa_SWAGGER_URL="https://some-domain-where-swagger-is-served.com/swagger.yaml" terraform plan
```
```
$ terraform init && OTF_VAR_goa_SWAGGER_URL="https://some-domain-where-swagger-is-served.com/swagger.yaml" terraform plan
```

#### OpenAPI plugin configuration file

A configuration file will need to be created in terraform plugins folder ```~/.terraform.d/plugins``` following [OpenAPI v1 plugin configuration specification](https://github.com/dikhan/terraform-provider-openapi/blob/master/docs/plugin_configuration_schema.md).
An example is described below:

```
$ pwd
/Users/dikhan/.terraform.d/plugins
$ cat terraform-provider-openapi.yaml
version: '1'
services:
monitor:
swagger-url: http://monitor-api.com/swagger.json
insecure_skip_verify: true
cdn:
swagger-url: /Users/user/go/src/github.com/dikhan/terraform-provider-openapi/examples/swaggercodegen/api/resources/swagger.yaml
vm:
swagger-url: http://vm-api.com/swagger.json
goa:
swagger-url: https://some-domain-where-swagger-is-served.com/swagger.yaml
```
```
$ pwd
/Users/dikhan/.terraform.d/plugins
$ cat terraform-provider-openapi.yaml
version: '1'
services:
monitor:
swagger-url: http://monitor-api.com/swagger.json
insecure_skip_verify: true
cdn:
swagger-url: /Users/user/go/src/github.com/dikhan/terraform-provider-openapi/examples/swaggercodegen/api/resources/swagger.yaml
vm:
swagger-url: http://vm-api.com/swagger.json
goa:
swagger-url: https://some-domain-where-swagger-is-served.com/swagger.yaml
```

This option is the recommended one when the user is managing resources provided by multiple OpenAPI providers (e,g: goa and swaggercodegen),
since it minimizes the configuration needed when calling terraform. Hence, terraform could be executed as usual without
having to pass in any special environment variables like OTF_VAR_<provider_name>_SWAGGER_URL:

```
$ terraform init && terraform plan
```
```
$ terraform init && terraform plan
```

## Examples

Expand All @@ -208,6 +191,17 @@ This API exposes a resource called 'bottles'
- [swaggercodegen](https://github.com/dikhan/terraform-provider-openapi/tree/master/examples/swaggercodegen): Example
created using swaggercodegen. This API exposes a resource called 'cdns'

A convenient make target ``make examples-container`` is provided to bring up a container initialised with terraform and
the example OpenAPI terraform providers (goa and swaggercodegen) already installed. This enables users of this provider to
play around with the OpenAPI providers without messing with their local environments. The following
command will bring up the example APIs, and a container that you can interact with:

````
$ make examples-container
$ root@6d7ac292eebd:/openapi# cd goa/
$ root@6d7ac292eebd:/openapi/goa# terraform init && terraform plan
````

For more information refer to [How to set up the local environment?](./docs/local_environment.md) which contains instructions
for learning how to bring up the example APIs and run terraform against them.

Expand Down
29 changes: 27 additions & 2 deletions build/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '3'
services:
swaggercodegen-service-provider-api:
build: ../examples/swaggercodegen/api
Expand All @@ -7,6 +7,8 @@ services:
ports:
- "8080:80"
- "8443:443"
networks:
- example

swagger-ui-swaggercodegen:
image: swaggerapi/swagger-ui:latest
Expand All @@ -17,10 +19,33 @@ services:
- SWAGGER_JSON=/app/resources/swagger.yaml
volumes:
- ../examples/swaggercodegen/api/resources:/app/resources
networks:
- example

goa-service-provider-api:
build: ../examples/goa/api
image: goa-service-provider-api:latest
container_name: goa-service-provider-api
ports:
- "9090:9090"
- "9090:9090"
networks:
- example

terraform-provider-openapi-examples:
build: ../.
image: terraform-provider-openapi-examples:latest
container_name: terraform-provider-openapi-examples
links:
- swaggercodegen-service-provider-api:swaggercodegen-service-provider-api.com
- goa-service-provider-api:goa-service-provider-api.com
networks:
- example
depends_on:
- swaggercodegen-service-provider-api
- goa-service-provider-api
stdin_open: true
tty: true
entrypoint: bash

networks:
example:
32 changes: 14 additions & 18 deletions docs/local_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Internally, the make target 'local-env' uses the following [docker-compose](http
file that contains the definitions for the [example API services](https://github.com/dikhan/terraform-provider-openapi/tree/master/examples).

Additionally, it will also render a UI from the swagger file exposed by the 'swaggercodegen-service-provider-api' API
server that can be accessed from the browser at ``https://localhost:8443``.
server that can be accessed from the browser at ``http://localhost:8082/``.

The UI rendered feeds from the swagger file located at [docker-compose](https://github.com/dikhan/terraform-provider-openapi/blob/master/service_provider_example/resources/swagger.yaml)
The UI rendered feeds from the swagger file located at [docker-compose](https://github.com/dikhan/terraform-provider-openapi/blob/master/examples/swaggercodegen/api/resources/swagger.yaml)

## Trying out the service provider example

Expand All @@ -36,23 +36,20 @@ the openapi terraform provider binary:
$ PROVIDER_NAME="<provider_name>" make install
[INFO] Building terraform-provider-openapi binary
[INFO] Creating /Users/dikhan/.terraform.d/plugins if it does not exist
[INFO] Installing terraform-provider-<provider_name> binary in -> /Users/dikhan/.terraform.d/plugins
[INFO] Installing terraform-provider-<provider_name> binary in -> /Users/dikhan/.terraform.d/plugins/terraform-provider-<provider_name>
```

Where ````<your_provider_name>```` should be replaced with your provider's name.

The above ```make install``` command will compile the provider from the source code, install the compiled binary terraform-provider-openapi
in the terraform plugin folder ````~/.terraform.d/plugins```` and create a symlink from terraform-provider-goa to the
binary compiled. The reason why a symlink is created is so the same compiled binary can be reused by multiple openapi providers
and also reduces the number of providers to support.
The above ```make install``` command will compile the provider from the source code, install the openapi provider binary ```terraform-provider-goa```
in the terraform plugin folder ````~/.terraform.d/plugins````:

````
$ ls -la ~/.terraform.d/plugins
total 29656
drwxr-xr-x 4 dikhan staff 128 3 Jul 15:13 .
drwxr-xr-x 4 dikhan staff 128 3 Jul 13:53 ..
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-openapi
lrwxr-xr-x 1 dikhan staff 63 3 Jul 15:11 terraform-provider-<provider_name> -> /Users/dikhan/.terraform.d/plugins/terraform-provider-openapi
-rwxr-xr-x 1 dikhan staff 15182644 29 Jun 16:21 terraform-provider-<provider_name>
````

For the sake of the example, let's pick the swaggercodegen service example and install the plugin:
Expand All @@ -71,8 +68,7 @@ ls -la /Users/dikhan/.terraform.d/plugins
total 44120
drwxr-xr-x 5 dikhan staff 160 5 Jul 16:02 .
drwxr-xr-x 5 dikhan staff 160 4 Jul 13:19 ..
-rwxr-xr-x 1 dikhan staff 22270452 5 Jul 16:01 terraform-provider-openapi
lrwxr-xr-x 1 dikhan staff 63 5 Jul 16:01 terraform-provider-swaggercodegen -> /Users/dkhanram/.terraform.d/plugins/terraform-provider-openapi
-rwxr-xr-x 1 dikhan staff 22270452 5 Jul 16:01 terraform-provider-swaggercodegen
````

### Running the openapi terraform provider
Expand Down Expand Up @@ -102,7 +98,7 @@ Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
+ swaggercodegen_cdns_v1.my_cdn
+ swaggercodegen_cdn_v1.my_cdn
id: <computed>
example_boolean: "true"
example_int: "12"
Expand Down Expand Up @@ -133,15 +129,15 @@ the name of the provider specified in the binary when compiling the plugin - 'sw
When defining the env variable, {PROVIDER_NAME} can be lower case or upper case.

This means that the plugin was able to read the swagger file exposed by the service provider example, load it
up and set up the terraform provider dynamically with the resources exposed by 'cdn-service-provider-api' being one of
up and set up the terraform provider dynamically with the resources exposed by 'swaggercodegen-service-provider-api' being one of
them 'cdns'.

#### Executing terraform apply

Now we can run terraform apply to see the plugin do its magic:

```
$ terraform init && OTF_INSECURE_SKIP_VERIFY="true" OTF_VAR_swaggercodegensp_SWAGGER_URL="https://localhost:8443/swagger.yaml" terraform apply
$ terraform init && OTF_INSECURE_SKIP_VERIFY="true" OTF_VAR_swaggercodegen_SWAGGER_URL="https://localhost:8443/swagger.yaml" terraform apply
Initializing provider plugins...
Expand All @@ -161,7 +157,7 @@ Resource actions are indicated with the following symbols:
Terraform will perform the following actions:
+ swaggercodegen_cdns_v1.my_cdn
+ swaggercodegen_cdn_v1.my_cdn
id: <computed>
example_boolean: "true"
example_int: "12"
Expand All @@ -181,7 +177,7 @@ Do you want to perform these actions?
Enter a value: yes
swaggercodegen_cdns_v1.my_cdn: Creating...
swaggercodegen_cdn_v1.my_cdn: Creating...
example_boolean: "" => "true"
example_int: "" => "12"
example_number: "" => "1.12"
Expand Down Expand Up @@ -210,7 +206,7 @@ And a 'terraform.tfstate' should have been created by terraform containing the s
],
"outputs": {},
"resources": {
"swaggercodegen_cdns_v1.my_cdn": {
"swaggercodegen_cdn_v1.my_cdn": {
"type": "swaggercodegen_cdns_v1",
"depends_on": [],
"primary": {
Expand Down Expand Up @@ -253,5 +249,5 @@ The above command will bring up the example server API and install the binary pl
When calling terraform it will pass all the required environment variables mentioned above using the example values:

````
terraform init && OTF_INSECURE_SKIP_VERIFY="true" OTF_VAR_sp_SWAGGER_URL="https://localhost:8443/swagger.yaml" terraform plan
terraform init && OTF_INSECURE_SKIP_VERIFY="true" OTF_VAR_swaggercodegen_SWAGGER_URL="https://localhost:8443/swagger.yaml" terraform plan
````
6 changes: 3 additions & 3 deletions examples/goa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ $ make local-env

This command will bring up the example APIs provided for development purposes.

## Running terraform-provider-openapi against this API
## Running terraform-provider-goa against this API

- Install terraform-provider-api and the symlink for 'goa' provider:
- Install terraform-provider-goa provider:

````
$ cd $GOPATH/github.com/dikhan/terraform-provider-openapi/
Expand All @@ -120,7 +120,7 @@ $ PROVIDER_NAME="goa" make install
- Execute terraform passing the following env variables:

````
$ cd $GOPATH/github.com/dikhan/terraform-provider-openapi/examples/goa/api
$ cd $GOPATH/github.com/dikhan/terraform-provider-openapi/examples/goa/
$ terraform init && OTF_VAR_goa_SWAGGER_URL="http://localhost:9090/swagger/swagger.yaml" OTF_INSECURE_SKIP_VERIFY=true terraform plan
````

Expand Down
2 changes: 1 addition & 1 deletion examples/goa/api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ definitions:
type: string
title: 'Mediatype identifier: application/vnd.goa.error; view=default'
type: object
host: localhost:9090
#host: localhost:9090
info:
description: The wine review service
version: ""
Expand Down
Loading

0 comments on commit 0250626

Please sign in to comment.