puppetlabs / puppetlabs-terraform

Bolt Terraform plugin
Apache License 2.0
6 stars 18 forks source link

(WIP) Add a lookup task for plugin use #20

Open donoghuc opened 4 years ago

donoghuc commented 4 years ago

This commit shows a general idea for adding a task based lookup plugin. Generally if a specific value is needed from a resource or if you want a list of attributes for a given resource this task allows you to look that up.

donoghuc commented 4 years ago

Opened this to guage interest in the idea. if accepted we can easily extend this to work for output also.

Example plan showing the two use cases (assuming the terraform state at the end):

plan terraform_lookup(){
  ## Show how to index into a specific value
  $path_example = {
    '_plugin' => 'task',
    'task' => 'terraform::tfstate_lookup',
    'parameters' => {
      'dir' => '/home/cas/working_dir/dynamic-inventory-demo',
      'resource_type' => 'docker_container.sshd',
      'attribute_path' => '0.ports.0.ip'
    }
  }
  $path_example_output = resolve_references($path_example)
  out::message(String($path_example_output))

  ## Show how to just read data for all resources that match
  $match_example = {
    '_plugin' => 'task',
    'task' => 'terraform::tfstate_lookup',
    'parameters' => {
      'dir' => '/home/cas/working_dir/dynamic-inventory-demo',
      'resource_type' => 'docker_container.sshd'
    }
  }
  $match_example_output = resolve_references($match_example)
  out::message(String($match_example_output))
}

output:

cas@cas-ThinkPad-T460p:~/working_dir/dynamic-inventory-demo$ bolt plan run terraform_lookup
Starting: plan terraform_lookup
0.0.0.0
[{'attach' => false, 'bridge' => '', 'capabilities' => [], 'command' => undef, 'container_logs' => undef, 'cpu_set' => undef, 'cpu_shares' => undef, 'destroy_grace_seconds' => undef, 'devices' => [], 'dns' => undef, 'dns_opts' => undef, 'dns_search' => undef, 'domainname' => undef, 'entrypoint' => undef, 'env' => undef, 'exit_code' => undef, 'gateway' => '172.17.0.1', 'group_add' => undef, 'healthcheck' => [], 'host' => [], 'hostname' => undef, 'id' => '27a87a28b8d1a20d9cc2103e2d12c50a59d591e01f1339a68aa54b0d3fe40096', 'image' => 'sha256:49533628fb371c9f1952c06cedf912c78a81fbe3914901334673c369376e077e', 'ip_address' => '172.17.0.2', 'ip_prefix_length' => 16, 'ipc_mode' => undef, 'labels' => undef, 'links' => undef, 'log_driver' => 'json-file', 'log_opts' => undef, 'logs' => false, 'max_retry_count' => undef, 'memory' => undef, 'memory_swap' => undef, 'mounts' => [], 'must_run' => true, 'name' => 'docker_target_0', 'network_alias' => undef, 'network_data' => [{'gateway' => '172.17.0.1', 'ip_address' => '172.17.0.2', 'ip_prefix_length' => 16, 'network_name' => 'bridge'}], 'network_mode' => undef, 'networks' => undef, 'networks_advanced' => [], 'pid_mode' => undef, 'ports' => [{'external' => 2200, 'internal' => 22, 'ip' => '0.0.0.0', 'protocol' => 'tcp'}], 'privileged' => undef, 'publish_all_ports' => undef, 'restart' => 'no', 'rm' => false, 'shm_size' => undef, 'start' => true, 'sysctls' => undef, 'tmpfs' => undef, 'ulimit' => [], 'upload' => [], 'user' => undef, 'userns_mode' => undef, 'volumes' => [], 'working_dir' => undef}]
Finished: plan terraform_lookup in 0.39 sec
Plan completed successfully with no result

statefile for this example

{
  "version": 4,
  "terraform_version": "0.12.28",
  "serial": 282,
  "lineage": "f4c7f41c-5e35-ceac-b717-d13da76dd5ef",
  "outputs": {
    "terraform_output": {
      "value": {
        "clivar": "override",
        "tfstatevar": "foo"
      },
      "type": [
        "object",
        {
          "clivar": "string",
          "tfstatevar": "string"
        }
      ]
    }
  },
  "resources": [
    {
      "mode": "managed",
      "type": "docker_container",
      "name": "sshd",
      "each": "list",
      "provider": "provider.docker",
      "instances": [
        {
          "index_key": 0,
          "schema_version": 1,
          "attributes": {
            "attach": false,
            "bridge": "",
            "capabilities": [],
            "command": null,
            "container_logs": null,
            "cpu_set": null,
            "cpu_shares": null,
            "destroy_grace_seconds": null,
            "devices": [],
            "dns": null,
            "dns_opts": null,
            "dns_search": null,
            "domainname": null,
            "entrypoint": null,
            "env": null,
            "exit_code": null,
            "gateway": "172.17.0.1",
            "group_add": null,
            "healthcheck": [],
            "host": [],
            "hostname": null,
            "id": "27a87a28b8d1a20d9cc2103e2d12c50a59d591e01f1339a68aa54b0d3fe40096",
            "image": "sha256:49533628fb371c9f1952c06cedf912c78a81fbe3914901334673c369376e077e",
            "ip_address": "172.17.0.2",
            "ip_prefix_length": 16,
            "ipc_mode": null,
            "labels": null,
            "links": null,
            "log_driver": "json-file",
            "log_opts": null,
            "logs": false,
            "max_retry_count": null,
            "memory": null,
            "memory_swap": null,
            "mounts": [],
            "must_run": true,
            "name": "docker_target_0",
            "network_alias": null,
            "network_data": [
              {
                "gateway": "172.17.0.1",
                "ip_address": "172.17.0.2",
                "ip_prefix_length": 16,
                "network_name": "bridge"
              }
            ],
            "network_mode": null,
            "networks": null,
            "networks_advanced": [],
            "pid_mode": null,
            "ports": [
              {
                "external": 2200,
                "internal": 22,
                "ip": "0.0.0.0",
                "protocol": "tcp"
              }
            ],
            "privileged": null,
            "publish_all_ports": null,
            "restart": "no",
            "rm": false,
            "shm_size": null,
            "start": true,
            "sysctls": null,
            "tmpfs": null,
            "ulimit": [],
            "upload": [],
            "user": null,
            "userns_mode": null,
            "volumes": [],
            "working_dir": null
          },
          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjEifQ==",
          "dependencies": [
            "docker_image.sshd"
          ]
        }
      ]
    },
    {
      "mode": "managed",
      "type": "docker_image",
      "name": "sshd",
      "provider": "provider.docker",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "sha256:49533628fb371c9f1952c06cedf912c78a81fbe3914901334673c369376e077erastasheep/ubuntu-sshd",
            "keep_locally": null,
            "latest": "sha256:49533628fb371c9f1952c06cedf912c78a81fbe3914901334673c369376e077e",
            "name": "rastasheep/ubuntu-sshd",
            "pull_trigger": null,
            "pull_triggers": null
          },
          "private": "bnVsbA=="
        }
      ]
    }
  ]
}
CLAassistant commented 2 years ago

CLA assistant check
All committers have signed the CLA.