Browse Source

Initial Commit

master
James Mills 4 days ago
commit
7abe835aee
Signed by: prologic
GPG Key ID: AC4C014F1440EBD6
  1. 6
      .gitignore
  2. 15
      README.md
  3. 17
      main.tf
  4. 8
      pvodier.tf
  5. 17
      shell_resources/helloworld/create.sh
  6. 11
      shell_resources/helloworld/delete.sh
  7. 13
      shell_resources/helloworld/read.sh
  8. 27
      shell_resources/helloworld/update.sh

6
.gitignore vendored

@ -0,0 +1,6 @@
*~
*.bak
*.tfstate*
**/.envrc
**/.terraform*

15
README.md

@ -0,0 +1,15 @@
# Example shell provider prototype
An example of using the [shell_resource]https://github.com/scottwinkler/terraform-provider-shell/blob/master/examples/test/scripts/read.sh) provider
to quickly prototype up a new Terraform provider and resource with full support
for:
- Create
- Delete
- Update (in-place)
- Read
The missing piece of documentation from the [shell provider](https://github.com/scottwinkler/terraform-provider-shell/blob/master/examples/test/scripts/read.sh) is the fact that the "previous output" is fed as input to the shell scripts
that implement Read, Update and Delete.
See: https://github.com/scottwinkler/terraform-provider-shell/issues/96

17
main.tf

@ -0,0 +1,17 @@
resource "shell_script" "helloworld" {
lifecycle_commands {
create = file("${path.module}/shell_resources/helloworld/create.sh")
read = file("${path.module}/shell_resources/helloworld/read.sh")
update = file("${path.module}/shell_resources/helloworld/update.sh")
delete = file("${path.module}/shell_resources/helloworld/delete.sh")
}
environment = {
NAME = "World"
}
}
output "hello" {
sensitive = true
value = shell_script.helloworld.output
}

8
pvodier.tf

@ -0,0 +1,8 @@
terraform {
required_providers {
shell = {
source = "scottwinkler/shell"
version = "1.7.7"
}
}
}

17
shell_resources/helloworld/create.sh

@ -0,0 +1,17 @@
#!/bin/sh
set -e
echo "Creating $NAME ..."
id="$(echo "$NAME" | sha256sum | cut -f 1 -d ' ')"
cat > "$id" <<EOF
Hello ${NAME}!
EOF
cat <<EOF
{
"id": "$id"
}
EOF

11
shell_resources/helloworld/delete.sh

@ -0,0 +1,11 @@
#!/bin/sh
set -e
# Read previous state into a temporary file
IN="$(cat)"
id="$(echo "$IN" | jq -r '.id')"
echo "Deleting $id ..." >&2
rm -f "$id"

13
shell_resources/helloworld/read.sh

@ -0,0 +1,13 @@
#!/bin/sh
set -e
echo "Reading $NAME ..." >&2
id="$(echo "$NAME" | sha256sum | cut -f 1 -d ' ')"
cat <<EOF
{
"id": "$id"
}
EOF

27
shell_resources/helloworld/update.sh

@ -0,0 +1,27 @@
#!/bin/sh
set -e
echo "Updating $NAME ..." >&2
IN="$(cat)"
oldId="$(echo "$IN" | jq -r '.id')"
newId="$(echo "$NAME" | sha256sum | cut -f 1 -d ' ')"
if [ "$oldId" = "$newId" ]; then
echo "Nothing to do..." >&2
exit 0
fi
echo "Removing $oldId ..." >&2
rm -rf "$oldId"
cat > "$newId" <<EOF
Hello ${NAME}!
EOF
cat <<EOF
{
"id": "$newId"
}
EOF
Loading…
Cancel
Save