Skip to content

Commit 77a5ffc

Browse files
authored
Merge pull request #65 from scottwinkler/feature/registry
Update docs
2 parents 6d62ca6 + c1d1789 commit 77a5ffc

File tree

6 files changed

+44
-1437
lines changed

6 files changed

+44
-1437
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ signs:
4949
- "${artifact}"
5050
release:
5151
# Visit your project's GitHub Releases page to publish this release.
52-
draft: true
52+
draft: false
5353
changelog:
5454
skip: true

README.md

Lines changed: 27 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -6,171 +6,6 @@ Since this provider is rather different than most other provider, it is recommen
66

77
**Note:** many people use this provider for wrapping APIs of resources that are not supported by existing providers. For an example of using this provider to manage a Github repo resource, see `examples/github-repo`
88

9-
## Configuring the Provider
10-
The provider can be configured with optional `environment` and `sensitive_environment` attributes. If these are set, then they will be used to configure all resources which rely on them (without triggering a force new update!)
11-
12-
```
13-
provider "shell" {
14-
environment = {
15-
AWS_ACCESS_KEY = var.access_key
16-
AWS_DEFAULT_REGION = var.region
17-
}
18-
sensitive_environment = {
19-
AWS_SECRET_ACCESS_KEY = var.secret_key
20-
}
21-
}
22-
```
23-
24-
Additionally, you can configure the provider with an optional `interpreter` and `enable_parallelism` flags. If you do not specify an interpreter, then the default shell for your machine will be used. Meanwhile, `enable_parallelism` defaults to false but can be turned on if you want to speed things up.
25-
26-
```
27-
provider "shell" {
28-
interpreter = ["/bin/bash", "-c"]
29-
enable_parallelism = true
30-
}
31-
Data Sources
32-
------------
33-
34-
The simplest example is the data source which implements only Read(). Any output to stdout or stderr will show up in the logs, but to save state, you must output a JSON payload to stdout. The last JSON object printed to stdout will be taken to be the output state. The JSON can be a complex nested JSON, but will be flattened into a `map[string]string`. The reason for this is that your JSON payload variables can be accessed from the output map of this resource and used like a normal terraform output, so the value must be a string. You can use the built-in jsondecode() function to read nested JSON values if you really need to.
35-
36-
Below is an example of using the data source. The output of `whoami` is stored in a JSON object for the key `user`
37-
38-
```
39-
data "shell_script" "user" {
40-
lifecycle_commands {
41-
read = <<-EOF
42-
echo "{\"user\": \"$(whoami)\"}"
43-
EOF
44-
}
45-
}
46-
# "user" can be accessed like a normal Terraform map
47-
output "user" {
48-
value = data.shell_script.user.output["user"]
49-
}
50-
```
51-
52-
An apply would output the following:
53-
54-
```
55-
shell_script.user: Creating...
56-
shell_script.user: Creation complete after 0s [id=bpcs8j5grkris295e4qg]
57-
58-
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
59-
60-
Outputs:
61-
62-
user = swinkler
63-
```
64-
**Note:** the above example can be a very valuable way to get environment variables or other environment specific information into normal Terraform variables!
65-
66-
Another data source example, this time to get the weather in San Francisco:
67-
68-
```
69-
data "shell_script" "weather" {
70-
lifecycle_commands {
71-
read = <<-EOF
72-
echo "{\"SanFrancisco\": \"$(curl wttr.in/SanFrancisco?format="%l:+%c+%t")\"}"
73-
EOF
74-
}
75-
}
76-
77-
output "weather" {
78-
value = data.shell_script.weather.output["SanFrancisco"]
79-
}
80-
```
81-
82-
An apply would output the following:
83-
84-
```
85-
shell_script.weather: Creating...
86-
shell_script.weather: Creation complete after 0s [id=bpcs8j5grkris295e4qg]
87-
88-
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
89-
90-
Outputs:
91-
92-
weather = SanFrancisco: ⛅️ +54°F
93-
```
94-
95-
Resources
96-
------------
97-
98-
Resources are a bit more complicated. At a minimum, you must implement the `CREATE`, and `DELETE` lifecycle commands. `READ` and `UPDATE` are optional arguments.
99-
100-
* If you choose not to implement the `READ` command, then `CREATE` (and `UPDATE` if you are using it) must output JSON. The local state will not be synced with the actual state, but for many applications that is not a problem.
101-
102-
* If you choose not to implement `UPDATE`, then if a change occurs that would trigger an update, the resource will be instead be destroyed and then recreated - same as `ForceNew`. For many applications this is not a problem.
103-
104-
I suggest starting off with just `CREATE` and `DELETE` and then implementing `READ` and `UPDATE` as needed. If you choose to implement `READ`, then you must output the state in the form of a properly formatted JSON, it should not alter the resource it is reading, and you should not output the state in either the create or update scripts (otherwise it will be overridden). See the examples in the test folder for how to do each of these.
105-
106-
A complete example that uses all four lifecycle commands is shown below:
107-
```
108-
variable "oauth_token" {
109-
type = string
110-
}
111-
112-
provider "shell" {
113-
environment = {
114-
GO_PATH = "/Users/Admin/go"
115-
}
116-
sensitive_environment = {
117-
OAUTH_TOKEN = var.oauth_token
118-
}
119-
interpreter = ["/bin/sh", "-c"]
120-
enable_parallelism = false
121-
}
122-
123-
resource "shell_script" "github_repository" {
124-
lifecycle_commands {
125-
//I suggest having these command be as separate files if they are non-trivial
126-
create = file("${path.module}/scripts/create.sh")
127-
read = file("${path.module}/scripts/read.sh")
128-
update = file("${path.module}/scripts/update.sh")
129-
delete = file("${path.module}/scripts/delete.sh")
130-
}
131-
132-
environment = {
133-
//changes to one of these will trigger an update
134-
NAME = "HELLO-WORLD"
135-
DESCRIPTION = "description"
136-
}
137-
138-
139-
//sensitive environment variables are exactly the
140-
//same as environment variables except they don't
141-
//show up in log files
142-
sensitive_environment = {
143-
USERNAME = var.username
144-
PASSWORD = var.password
145-
}
146-
147-
//this overrides the provider supplied interpreter
148-
//if you do not specify this then the default for your
149-
//machine will be used (/bin/sh for linux/mac and cmd for windows)
150-
interpreter = ["/bin/bash", "-c"]
151-
152-
//sets current working directory
153-
working_directory = path.module
154-
155-
//triggers a force new update if value changes, like null_resource
156-
triggers = {
157-
when_value_changed = var.some_value
158-
}
159-
}
160-
161-
output "id" {
162-
value = shell_script.github_repository.output["id"]
163-
}
164-
```
165-
166-
Stdout and stderr stream to log files. You can get this by setting:
167-
168-
```
169-
export TF_LOG=1
170-
```
171-
**Note:** if you are using sensitive_environment to set sensitive environment variables, these values won't show up in the logs
172-
173-
1749
Requirements
17510
------------
17611

@@ -206,7 +41,33 @@ Then commit the changes to `go.mod` and `go.sum`.
20641
Using the provider
20742
----------------------
20843

209-
Fill this in for each provider
44+
You can use this provider to make custom external resources and data sources:
45+
46+
```
47+
variable "oauth_token" {
48+
type = string
49+
}
50+
51+
provider "shell" {
52+
sensitive_environment = {
53+
OAUTH_TOKEN = var.oauth_token
54+
}
55+
}
56+
57+
resource "shell_script" "github_repository" {
58+
lifecycle_commands {
59+
create = file("${path.module}/scripts/create.sh")
60+
read = file("${path.module}/scripts/read.sh")
61+
update = file("${path.module}/scripts/update.sh")
62+
delete = file("${path.module}/scripts/delete.sh")
63+
}
64+
65+
environment = {
66+
NAME = "HELLO-WORLD"
67+
DESCRIPTION = "description"
68+
}
69+
}
70+
```
21071

21172
Developing the Provider
21273
---------------------------

go.mod

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,21 @@ module github.com/scottwinkler/terraform-provider-shell
33
go 1.12
44

55
require (
6-
github.com/Azure/azure-sdk-for-go v40.0.0+incompatible // indirect
7-
github.com/Azure/go-autorest/autorest v0.10.0 // indirect
8-
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2
9-
github.com/coreos/etcd v3.3.18+incompatible // indirect
10-
github.com/dylanmei/iso8601 v0.1.0 // indirect
11-
github.com/hashicorp/atlas-go v0.0.0-20170808163836-8261ea080105 // indirect
12-
github.com/hashicorp/consul/api v1.4.0 // indirect
13-
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
14-
github.com/hashicorp/go-retryablehttp v0.6.4 // indirect
15-
github.com/hashicorp/go-tfe v0.5.0 // indirect
16-
github.com/hashicorp/hcl2 v0.0.0-20190821123243-0c888d1241f6 // indirect
17-
github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 // indirect
18-
github.com/hashicorp/logutils v1.0.0
19-
github.com/hashicorp/terraform v0.11.11
6+
github.com/fatih/color v1.9.0 // indirect
7+
github.com/hashicorp/go-hclog v0.12.0 // indirect
8+
github.com/hashicorp/hcl v1.0.0 // indirect
209
github.com/hashicorp/terraform-plugin-sdk v1.7.0
21-
github.com/joyent/triton-go v1.7.0 // indirect
22-
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
23-
github.com/lusis/go-artifactory v0.0.0-20180304164534-a47f63f234b2 // indirect
24-
github.com/masterzen/winrm v0.0.0-20190308153735-1d17eaf15943 // indirect
25-
github.com/mattn/go-shellwords v1.0.10 // indirect
10+
github.com/kr/pretty v0.2.0 // indirect
11+
github.com/mattn/go-isatty v0.0.12 // indirect
2612
github.com/mitchellh/go-linereader v0.0.0-20190213213312-1b945b3263eb
27-
github.com/mitchellh/panicwrap v1.0.0 // indirect
28-
github.com/mitchellh/prefixedio v0.0.0-20190213213902-5733675afd51 // indirect
29-
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
30-
github.com/packer-community/winrmcp v0.0.0-20180921211025-c76d91c1e7db // indirect
3113
github.com/rs/xid v1.2.1
32-
github.com/ryanuber/columnize v2.1.0+incompatible // indirect
3314
github.com/stretchr/testify v1.4.0
34-
github.com/terraform-providers/terraform-provider-aws v1.60.0 // indirect
35-
github.com/terraform-providers/terraform-provider-openstack v1.26.0 // indirect
3615
github.com/tidwall/gjson v1.6.0
37-
github.com/xanzy/ssh-agent v0.2.1 // indirect
38-
github.com/xlab/treeprint v1.0.0 // indirect
16+
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 // indirect
17+
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect
18+
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
19+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
20+
gopkg.in/yaml.v2 v2.2.8 // indirect
3921
)
4022

4123
replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999

0 commit comments

Comments
 (0)