|
1 | 1 | [](https://travis-ci.com/burbon/terraform-provider-restapi)
|
2 | 2 | [](https://coveralls.io/github/burbon/terraform-provider-restapi?branch=master)
|
3 | 3 | [](https://goreportcard.com/report/github.com/burbon/terraform-provider-restapi)
|
4 |
| -# Terraform provider for generic REST APIs |
| 4 | + |
| 5 | +# Generic Terraform Provider for RESTful APIs |
5 | 6 |
|
6 | 7 | ## Maintenance Note
|
| 8 | + |
7 | 9 | This provider is largely feature-complete and in maintenance mode.
|
8 |
| -* It's not dead - it's just slow moving and updates must be done very carefully |
9 |
| -* We encourage community participation with open issues for usage and remain welcoming of pull requests |
| 10 | + |
| 11 | +* It's not dead - it's just slow-moving, and updates must be done very carefully |
| 12 | +* We encourage community participation with open issues for usage, and remain welcoming of pull requests |
10 | 13 | * Code updates happen sporadically throughout the year, driven primarily by security fixes and PRs
|
11 | 14 | * Because of the many API variations and flexibility of this provider, detailed per-API troubleshooting cannot be guaranteed
|
12 | 15 |
|
13 |
| - |
14 |
| - |
15 | 16 | ## About This Provider
|
16 |
| -This terraform provider allows you to interact with APIs that may not yet have a first-class provider available by implementing a "dumb" REST API client. |
17 | 17 |
|
18 |
| -This provider is essentially created to be a terraform-wrapped `cURL` client. Because of this, you need to know quite a bit about the API you are interacting with as opposed to full-featured terraform providers written with a specific API in mind. |
| 18 | +This terraform provider allows you to interact with APIs that may not yet have a first-class provider available by implementing a thin-wrapper of an REST API client. |
| 19 | + |
| 20 | +This provider is essentially created to be a terraform-wrapped cURL client. Because of this, you need to know quite a bit about the API you are interacting with, as opposed to full-featured terraform providers written with a specific API in mind. |
19 | 21 |
|
20 | 22 | There are a few requirements about how the API must work for this provider to be able to do its thing:
|
21 |
| -* The API is expected to support the following HTTP methods: |
22 |
| - * POST: create an object |
23 |
| - * GET: read an object |
24 |
| - * PUT: update an object |
25 |
| - * DELETE: remove an object |
26 |
| -* An "object" in the API has a unique identifier the API will return |
27 |
| -* Objects live under a distinct path such that for the path `/api/v1/things`... |
28 |
| - * POST on `/api/v1/things` creates a new object |
29 |
| - * GET, PUT and DELETE on `/api/v1/things/{id}` manages an existing object |
30 | 23 |
|
31 |
| -Have a look at the [examples directory](examples) for some use cases. |
| 24 | +* With the default provider configuration, The API must support the following HTTP methods: |
| 25 | + * POST: create an object |
| 26 | + * GET: read an object |
| 27 | + * PUT: update an object |
| 28 | + * DELETE: remove an object |
| 29 | +* An "object" in the API has a unique identifier the API will return upon CREATE as property `id` |
| 30 | +* Objects live under a distinct path, e.g., `/api/v1/things` where |
| 31 | + * POST on `/api/v1/things` creates a new object |
| 32 | + * GET, PUT and DELETE on `/api/v1/things/{id}` are read, update, and destroy operations |
32 | 33 |
|
33 |
| - |
| 34 | +Have a look at the [examples directory](./examples/) for some use cases. |
34 | 35 |
|
35 | 36 | ## Provider Documentation
|
| 37 | + |
36 | 38 | This provider has only a few moving components, but LOTS of configurable parameters:
|
| 39 | + |
37 | 40 | * [provider documentation](https://registry.terraform.io/providers/Mastercard/restapi/latest/docs)
|
38 | 41 | * [restapi_object resource documentation](https://registry.terraform.io/providers/Mastercard/restapi/latest/docs/resources/object)
|
39 | 42 | * [restapi_object datasource documentation](https://registry.terraform.io/providers/Mastercard/restapi/latest/docs/data-sources/object)
|
40 | 43 |
|
41 |
| - |
42 |
| - |
43 | 44 | ## Usage
|
| 45 | + |
44 | 46 | * Try to set as few parameters as possible to begin with. The more complicated the configuration gets, the more difficult troubleshooting can become.
|
45 | 47 | * Play with the [fakeserver cli tool](fakeservercli/) (included in releases) to get a feel for how this API client is expected to work. Also see the [examples directory](examples) directory for some working use cases with fakeserver.
|
46 | 48 | * By default, data isn't considered sensitive. If you want to hide the data this provider submits as well as the data returned by the API, you would need to set environment variable `API_DATA_IS_SENSITIVE=true`.
|
47 | 49 | * The `*_path` elements are for very specific use cases where one might initially create an object in one location, but read/update/delete it on another path. For this reason, they allow for substitution to be done by the provider internally by injecting the `id` somewhere along the path. This is similar to terraform's substitution syntax in the form of `${variable.name}`, but must be done within the provider due to structure. The only substitution available is to replace the string `{id}` with the internal (terraform) `id` of the object as learned by the `id_attribute`.
|
48 | 50 |
|
49 |
| - |
50 |
| - |
51 | 51 | ### Troubleshooting
|
| 52 | + |
52 | 53 | Because this provider is just a terraform-wrapped `cURL`, the API details and the go implementation of this client are often leaked to you.
|
53 | 54 | This means you, as the user, will have a bit more troubleshooting on your hands than would typically be required of a full-fledged provider if you experience issues.
|
54 | 55 |
|
55 | 56 | Here are some tips for troubleshooting that may be helpful...
|
56 | 57 |
|
57 |
| - |
58 |
| - |
59 | 58 | #### Debug log
|
| 59 | + |
60 | 60 | **Rely heavily on the debug log.** The debug log, enabled by setting the environment variable `TF_LOG=1` and enabling the `debug` parameter on the provider, is the best way to figure out what is happening.
|
61 | 61 |
|
62 | 62 | If an unexpected error occurs, enable debug log and review the output:
|
| 63 | + |
63 | 64 | * Does the API return an odd HTTP response code? This is common for bad requests to the API. Look closely at the HTTP request details.
|
64 | 65 | * Does an unexpected golang 'unmarshaling' error occur? Take a look at the debug log and see if anything other than a hash (for resources) or an array (for the datasource) is being returned. For example, the provider cannot cope with cases where a JSON object is requested, but an array of JSON objects is returned.
|
65 | 66 |
|
66 |
| - |
67 |
| - |
68 | 67 | ### Importing existing resources
|
69 |
| -This provider supports importing existing resources into the terraform state. Import is done according to the various provider/resource configuation settings to contact the API server and obtain data. That is: if a custom read method, path, or id attribute is defined, the provider will honor those settings to pull data in. |
| 68 | + |
| 69 | +This provider supports importing existing resources into the terraform state. Import is done according to the various provider/resource configuration settings to contact the API server and obtain data. That is, if a custom read method, path, or id attribute is defined, the provider will honor those settings to pull data in. |
70 | 70 |
|
71 | 71 | To import data:
|
72 | 72 | `terraform import restapi.Name /path/to/resource`.
|
73 | 73 |
|
74 | 74 | See a concrete example [here](examples/dummy_users_with_fakeserver.tf).
|
75 | 75 |
|
76 |
| - |
77 |
| - |
78 | 76 | ## Installation
|
| 77 | + |
79 | 78 | There are two standard methods of installing this provider detailed [in Terraform's documentation](https://www.terraform.io/docs/configuration/providers.html#third-party-plugins). You can place the file in the directory of your .tf file in `terraform.d/plugins/{OS}_{ARCH}/` or place it in your home directory at `~/.terraform.d/plugins/{OS}_{ARCH}/`.
|
80 | 79 |
|
81 | 80 | The released binaries are named `terraform-provider-restapi_vX.Y.Z-{OS}-{ARCH}` so you know which binary to install. You *may* need to rename the binary you use during installation to just `terraform-provider-restapi_vX.Y.Z`.
|
82 | 81 |
|
83 | 82 | Once downloaded, be sure to make the plugin executable by running `chmod +x terraform-provider-restapi_vX.Y.Z-{OS}-{ARCH}`.
|
84 | 83 |
|
85 |
| - |
86 |
| - |
87 | 84 | ## Contributing
|
| 85 | + |
88 | 86 | Pull requests are always welcome! Please be sure the following things are taken care of with your pull request:
|
| 87 | + |
89 | 88 | * `go fmt` is run before pushing
|
90 | 89 | * Be sure to add a test case for new functionality (or explain why this cannot be done)
|
91 | 90 | * Run the `scripts/test.sh` script to be sure everything works
|
92 | 91 | * Ensure new attributes can also be set by environment variables
|
93 | 92 |
|
94 |
| -#### Development environment requirements: |
| 93 | +#### Development environment requirements |
| 94 | + |
95 | 95 | * [Golang](https://golang.org/dl/) v1.11 or newer is installed and `go` is in your path
|
96 | 96 | * [Terraform](https://www.terraform.io/downloads.html) is installed and `terraform` is in your path
|
97 | 97 |
|
98 | 98 | To make development easy, you can use the Docker image [druggeri/tdk](https://hub.docker.com/r/druggeri/tdk) as a development environment:
|
99 |
| -``` |
| 99 | + |
| 100 | +```bash |
100 | 101 | docker run -it --name tdk --rm -v "$HOME/go":/root/go druggeri/tdk
|
101 | 102 | go get github.com/Mastercard/terraform-provider-restapi
|
102 | 103 | cd ~/go/src/github.com/Mastercard/terraform-provider-restapi
|
|
0 commit comments