From c9fb391ace6ea0c0ca50f7217f5b9e19edb05e80 Mon Sep 17 00:00:00 2001 From: Helio Machado <0x2b3bfa0+git@googlemail.com> Date: Thu, 2 Jun 2022 13:53:00 +0000 Subject: [PATCH 1/2] Add resource tagging support --- docs/resources/task.md | 1 + iterative/resource_task.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/docs/resources/task.md b/docs/resources/task.md index 3cc30f1b..72c82e98 100644 --- a/docs/resources/task.md +++ b/docs/resources/task.md @@ -64,6 +64,7 @@ resource "iterative_task" "example" { - `storage.workdir` - (Optional) Local working directory to upload and use as the `script` working directory. - `storage.output` - (Optional) Results directory (**relative to `workdir`**) to download (default: no download). - `environment` - (Optional) Map of environment variable names and values for the task script. Empty string values are replaced with local environment values. Empty values may also be combined with a [glob]() name to import all matching variables. +- `tags` - (Optional) Map of tags for the created cloud resources. - `timeout` - (Optional) Maximum number of seconds to run before instances are force-terminated. The countdown is reset each time TPI auto-respawns a spot instance. - `name` - (Optional) _Discouraged and may be removed in future - change the resource name instead, i.e. `resource "iterative_task" "some_other_example_name"`._ Deterministic task name (e.g. `name="Hello, World!"` always produces `id="tpi-hello-world-5kz6ldls-57wo7rsp"`). diff --git a/iterative/resource_task.go b/iterative/resource_task.go index 2686a50d..349d9776 100644 --- a/iterative/resource_task.go +++ b/iterative/resource_task.go @@ -156,6 +156,14 @@ func resourceTask() *schema.Resource { Type: schema.TypeString, }, }, + "tags": { + Type: schema.TypeMap, + ForceNew: true, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "timeout": { Type: schema.TypeInt, ForceNew: true, @@ -283,6 +291,11 @@ func resourceTaskDelete(ctx context.Context, d *schema.ResourceData, m interface } func resourceTaskBuild(ctx context.Context, d *schema.ResourceData, m interface{}) (task.Task, error) { + tags := make(map[string]string) + for name, value := range d.Get("tags").(map[string]interface{}) { + tags[name] = value.(string) + } + v := make(map[string]*string) for name, value := range d.Get("environment").(map[string]interface{}) { v[name] = nil @@ -309,6 +322,7 @@ func resourceTaskBuild(ctx context.Context, d *schema.ResourceData, m interface{ Update: d.Timeout(schema.TimeoutUpdate), Delete: d.Timeout(schema.TimeoutDelete), }, + Tags: tags, } directory := "" From f6d9631b7c241e114736482b0c3ba9ab1a308de9 Mon Sep 17 00:00:00 2001 From: Helio Machado <0x2b3bfa0+git@googlemail.com> Date: Thu, 2 Jun 2022 16:17:50 +0200 Subject: [PATCH 2/2] Apply arguable suggestions from code review Co-authored-by: Casper da Costa-Luis --- docs/resources/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/task.md b/docs/resources/task.md index 72c82e98..026d810e 100644 --- a/docs/resources/task.md +++ b/docs/resources/task.md @@ -64,8 +64,8 @@ resource "iterative_task" "example" { - `storage.workdir` - (Optional) Local working directory to upload and use as the `script` working directory. - `storage.output` - (Optional) Results directory (**relative to `workdir`**) to download (default: no download). - `environment` - (Optional) Map of environment variable names and values for the task script. Empty string values are replaced with local environment values. Empty values may also be combined with a [glob]() name to import all matching variables. -- `tags` - (Optional) Map of tags for the created cloud resources. - `timeout` - (Optional) Maximum number of seconds to run before instances are force-terminated. The countdown is reset each time TPI auto-respawns a spot instance. +- `tags` - (Optional) Map of tags for the created cloud resources. - `name` - (Optional) _Discouraged and may be removed in future - change the resource name instead, i.e. `resource "iterative_task" "some_other_example_name"`._ Deterministic task name (e.g. `name="Hello, World!"` always produces `id="tpi-hello-world-5kz6ldls-57wo7rsp"`). -> **Note:** `output` is relative to `workdir`, so `storage { workdir = "foo", output = "bar" }` means "upload `./foo/`, change working directory to the uploaded folder, run `script`, and download `bar` (i.e. `./foo/bar`)".