Skip to content

Commit 137a99d

Browse files
committed
setname
1 parent cefb2c8 commit 137a99d

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

examples/main.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
terraform {
22
required_providers {
3-
iterative = {
4-
versions = ["0.6"]
5-
source = "github.com/iterative/iterative"
6-
}
7-
83
# iterative = {
9-
# source = "iterative/iterative"
10-
# version = "0.5.6"
4+
# versions = ["0.6"]
5+
# source = "github.com/iterative/iterative"
116
# }
7+
8+
iterative = {
9+
source = "iterative/iterative"
10+
version = "0.5.7"
11+
}
1212
}
1313
}
1414

iterative/resource_machine.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13-
"github.com/teris-io/shortid"
1413
)
1514

1615
func resourceMachine() *schema.Resource {
@@ -114,6 +113,8 @@ func machineSchema() *map[string]*schema.Schema {
114113
func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
115114
var diags diag.Diagnostics
116115

116+
utils.SetName(d)
117+
117118
keyPublic := d.Get("ssh_public").(string)
118119
if len(keyPublic) == 0 {
119120
public, private, _ := utils.SSHKeyPair()
@@ -122,13 +123,6 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
122123
d.Set("ssh_private", private)
123124
}
124125

125-
name := d.Get("name").(string)
126-
if len(name) == 0 {
127-
sid, _ := shortid.New(1, shortid.DefaultABC, 2342)
128-
id, _ := sid.Generate()
129-
d.Set("name", "iterative-"+id)
130-
}
131-
132126
cloud := d.Get("cloud").(string)
133127
if cloud == "aws" {
134128
err := aws.ResourceMachineCreate(ctx, d, m)

iterative/resource_runner.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"os"
1111
"strconv"
1212

13+
"terraform-provider-iterative/iterative/utils"
14+
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1517
)
@@ -134,6 +136,8 @@ func resourceRunner() *schema.Resource {
134136
func resourceRunnerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
135137
var diags diag.Diagnostics
136138

139+
utils.SetName(d)
140+
137141
customData, err := provisionerCode(d)
138142
if err != nil {
139143
diags = append(diags, diag.Diagnostic{

iterative/utils/helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package utils
22

33
import (
44
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
6+
"github.com/teris-io/shortid"
57
)
68

79
func MachinePrefix(d *schema.ResourceData) string {
@@ -12,3 +14,12 @@ func MachinePrefix(d *schema.ResourceData) string {
1214

1315
return prefix
1416
}
17+
18+
func SetName(d *schema.ResourceData) {
19+
name := d.Get("name").(string)
20+
if len(name) == 0 {
21+
sid, _ := shortid.New(1, shortid.DefaultABC, 2342)
22+
id, _ := sid.Generate()
23+
d.Set("name", "iterative-"+id)
24+
}
25+
}

0 commit comments

Comments
 (0)