Skip to content

Commit

Permalink
Use randomly generated identifiers by default (#396)
Browse files Browse the repository at this point in the history
* Use randomly generated identifiers by default

Closes #379 and #390

* remove example in readme

Co-authored-by: DavidGOrtega <[email protected]>
  • Loading branch information
0x2b3bfa0 and DavidGOrtega authored Feb 17, 2022
1 parent 0e94e21 commit af2082e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
1 change: 0 additions & 1 deletion docs/guides/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ terraform {
}
provider "iterative" {}
resource "iterative_task" "task" {
name = "example"
cloud = "aws" # or any of: gcp, az, k8s
machine = "m"
Expand Down
1 change: 0 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ terraform {
}
provider "iterative" {}
resource "iterative_task" "task" {
name = "example"
cloud = "aws"
script = <<-END
Expand Down
3 changes: 1 addition & 2 deletions docs/resources/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This resource will:

```hcl
resource "iterative_task" "task" {
name = "example"
cloud = "aws"
environment = { GREETING = "Hello, world!" }
Expand All @@ -30,12 +29,12 @@ resource "iterative_task" "task" {

### Required

- `name` - (Required) Task name.
- `cloud` - (Required) Cloud provider to run the task on; valid values are `aws`, `gcp`, `az` and `k8s`.
- `script` - (Required) Script to run; must begin with a valid [shebang](<https://en.wikipedia.org/wiki/Shebang_(Unix)>).

### Optional

- `name` - (Optional) Deterministic task name.
- `region` - (Optional) [Cloud region/zone](#cloud-regions) to run the task on.
- `machine` - (Optional) See [Machine Types](#machine-types) below.
- `disk_size` - (Optional) Size of the ephemeral machine storage.
Expand Down
34 changes: 26 additions & 8 deletions iterative/resource_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"github.com/aohorodnyk/uid"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand All @@ -26,7 +28,7 @@ func resourceTask() *schema.Resource {
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Optional: true,
},
"cloud": {
Type: schema.TypeString,
Expand Down Expand Up @@ -162,14 +164,15 @@ func resourceTaskCreate(ctx context.Context, d *schema.ResourceData, m interface
return diagnostic(diags, err, diag.Error)
}

if err := task.Create(ctx); err == nil {
d.SetId(task.GetIdentifier(ctx).Long())
} else {
d.SetId(task.GetIdentifier(ctx).Long())

if err := task.Create(ctx); err != nil {
diags = diagnostic(diags, err, diag.Error)
if err := task.Delete(ctx); err == nil {
diags = diagnostic(diags, errors.New("failed to create"), diag.Error)
} else {
if err := task.Delete(ctx); err != nil {
diags = diagnostic(diags, err, diag.Error)
} else {
diags = diagnostic(diags, errors.New("failed to create"), diag.Error)
d.SetId("")
}
}

Expand Down Expand Up @@ -316,7 +319,22 @@ func resourceTaskBuild(ctx context.Context, d *schema.ResourceData, m interface{
Parallelism: uint16(d.Get("parallelism").(int)),
}

return task.New(ctx, c, common.Identifier(d.Get("name").(string)), t)
name := d.Id()
if name == "" {
if identifier := d.Get("name").(string); identifier != "" {
name = identifier
} else if identifier := os.Getenv("GITHUB_RUN_ID"); identifier != "" {
name = identifier
} else if identifier := os.Getenv("CI_PIPELINE_ID"); identifier != "" {
name = identifier
} else if identifier := os.Getenv("BITBUCKET_STEP_TRIGGERER_UUID"); identifier != "" {
name = identifier
} else {
name = uid.NewProvider36Size(8).MustGenerate().String()
}
}

return task.New(ctx, c, common.Identifier(name), t)
}

func diagnostic(diags diag.Diagnostics, err error, severity diag.Severity) diag.Diagnostics {
Expand Down

0 comments on commit af2082e

Please sign in to comment.