Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource tagging support #599

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ resource "iterative_task" "example" {
- `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](<https://en.wikipedia.org/wiki/Glob_(programming)>) name to import all matching variables.
- `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`)".
Expand Down
14 changes: 14 additions & 0 deletions iterative/resource_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 := ""
Expand Down