Skip to content

Commit 5f77a09

Browse files
committed
json
1 parent 9e751fe commit 5f77a09

File tree

5 files changed

+211
-105
lines changed

5 files changed

+211
-105
lines changed

examples/main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ resource "iterative_machine" "machine-az" {
1717
}
1818
*/
1919

20+
2021
resource "iterative_runner" "runner-az" {
2122
token = "arszDpb3xtNdKaXmQ6vN"
2223
repo = "https://gitlab.com/DavidGOrtega/3_tensorboard"
2324
driver = "gitlab"
2425
labels = "tf"
25-
machine {
26-
cloud = "azure"
27-
region = "us-west"
28-
instance_type = "m"
29-
}
26+
27+
cloud = "azure"
28+
region = "us-west"
29+
instance_type = "m"
3030
}
3131

3232
/*

iterative/aws/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
//ResourceMachineCreate creates AWS instance
1616
func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) error {
17-
instanceName := d.Get("instance_name").(string)
17+
instanceName := d.Get("name").(string)
1818
hddSize := d.Get("instance_hdd_size").(int)
1919
region := getRegion(d.Get("region").(string))
2020
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))

iterative/azure/provider.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strings"
99
"time"
1010

11-
"terraform-provider-iterative/iterative/utils"
12-
1311
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
1412
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-11-01/network"
1513
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
@@ -23,19 +21,18 @@ import (
2321
//ResourceMachineCreate creates AWS instance
2422
func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) error {
2523
subscriptionID := os.Getenv("AZURE_SUBSCRIPTION_ID")
26-
maprefix := utils.MachinePrefix(d)
2724

2825
username := "ubuntu"
2926
//username := d.Get("ssh_user").(string)
3027

31-
customData := base64.StdEncoding.EncodeToString([]byte(d.Get(maprefix + "custom_data").(string)))
32-
region := getRegion(d.Get(maprefix + "region").(string))
33-
instanceType := getInstanceType(d.Get(maprefix+"instance_type").(string), d.Get(maprefix+"instance_gpu").(string))
34-
keyPublic := d.Get(maprefix + "ssh_public").(string)
35-
vmName := d.Get(maprefix + "instance_name").(string)
36-
hddSize := int32(d.Get(maprefix + "instance_hdd_size").(int))
28+
customData := base64.StdEncoding.EncodeToString([]byte(d.Get("custom_data").(string)))
29+
region := getRegion(d.Get("region").(string))
30+
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
31+
keyPublic := d.Get("ssh_public").(string)
32+
vmName := d.Get("name").(string)
33+
hddSize := int32(d.Get("instance_hdd_size").(int))
3734

38-
image := d.Get(maprefix + "image").(string)
35+
image := d.Get("image").(string)
3936
if image == "" {
4037
image = "Canonical:UbuntuServer:18.04-LTS:latest"
4138
//image = "iterative:CML:v1.0.0:1.0.0"
@@ -67,7 +64,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
6764
}
6865

6966
d.SetId(gpName)
70-
d.Set(maprefix+"instance_id", gpName)
67+
d.Set("instance_id", gpName)
7168

7269
// securityGroup
7370
nsgClient, _ := getNsgClient(subscriptionID)
@@ -257,8 +254,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
257254
return err
258255
}
259256

260-
d.Set(maprefix+"instance_ip", ip.IPAddress)
261-
d.Set(maprefix+"instance_launch_time", time.Now().String())
257+
d.Set("instance_ip", ip.IPAddress)
258+
d.Set("instance_launch_time", time.Now().String())
262259

263260
return nil
264261
}

iterative/resource_machine.go

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package iterative
33
import (
44
"context"
55
"fmt"
6-
"strconv"
76

87
"terraform-provider-iterative/iterative/aws"
98
"terraform-provider-iterative/iterative/azure"
@@ -19,13 +18,18 @@ func resourceMachine() *schema.Resource {
1918
CreateContext: resourceMachineCreate,
2019
DeleteContext: resourceMachineDelete,
2120
ReadContext: resourceMachineRead,
22-
UpdateContext: resourceMachineUpdate,
2321
Schema: *machineSchema(),
2422
}
2523
}
2624

2725
func machineSchema() *map[string]*schema.Schema {
2826
return &map[string]*schema.Schema{
27+
"name": &schema.Schema{
28+
Type: schema.TypeString,
29+
ForceNew: true,
30+
Optional: true,
31+
Default: "",
32+
},
2933
"cloud": &schema.Schema{
3034
Type: schema.TypeString,
3135
ForceNew: true,
@@ -34,81 +38,74 @@ func machineSchema() *map[string]*schema.Schema {
3438
},
3539
"region": &schema.Schema{
3640
Type: schema.TypeString,
37-
Optional: true,
3841
ForceNew: true,
42+
Optional: true,
3943
Default: "us-west",
4044
},
4145
"image": &schema.Schema{
4246
Type: schema.TypeString,
43-
Optional: true,
4447
ForceNew: true,
45-
Default: "",
46-
},
47-
"instance_name": &schema.Schema{
48-
Type: schema.TypeString,
4948
Optional: true,
50-
ForceNew: true,
5149
Default: "",
5250
},
5351
"instance_type": &schema.Schema{
5452
Type: schema.TypeString,
55-
Optional: true,
5653
ForceNew: true,
54+
Optional: true,
5755
Default: "m",
5856
},
5957
"instance_hdd_size": &schema.Schema{
6058
Type: schema.TypeInt,
61-
Optional: true,
6259
ForceNew: true,
60+
Optional: true,
6361
Default: 35,
6462
},
6563
"instance_gpu": &schema.Schema{
6664
Type: schema.TypeString,
67-
Optional: true,
6865
ForceNew: true,
66+
Optional: true,
6967
Default: "",
7068
},
7169
"instance_id": &schema.Schema{
7270
Type: schema.TypeString,
73-
Optional: true,
7471
Computed: true,
7572
},
7673
"instance_ip": &schema.Schema{
7774
Type: schema.TypeString,
78-
Optional: true,
7975
Computed: true,
8076
},
8177
"instance_launch_time": &schema.Schema{
8278
Type: schema.TypeString,
83-
Optional: true,
8479
Computed: true,
8580
},
8681
"ssh_public": &schema.Schema{
8782
Type: schema.TypeString,
88-
Optional: true,
8983
ForceNew: true,
84+
Optional: true,
9085
Default: "",
9186
},
9287
"ssh_private": &schema.Schema{
9388
Type: schema.TypeString,
89+
ForceNew: true,
9490
Optional: true,
95-
Computed: true,
91+
Default: "",
9692
},
9793
"ssh_name": &schema.Schema{
9894
Type: schema.TypeString,
99-
Optional: true,
10095
ForceNew: true,
96+
Optional: true,
10197
Default: "ubuntu",
10298
},
10399
"custom_data": &schema.Schema{
104100
Type: schema.TypeString,
105101
ForceNew: true,
106-
Computed: true,
102+
Optional: true,
103+
Default: "#!/bin/bash",
107104
},
108105
"aws_security_group": &schema.Schema{
109106
Type: schema.TypeString,
110-
Optional: true,
111107
ForceNew: true,
108+
Optional: true,
112109
Default: "",
113110
},
114111
}
@@ -117,50 +114,22 @@ func machineSchema() *map[string]*schema.Schema {
117114
func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
118115
var diags diag.Diagnostics
119116

120-
maprefix := utils.MachinePrefix(d)
121-
hasMachine := len(maprefix) > 0
122-
123-
keyPublic := d.Get(maprefix + "ssh_public").(string)
117+
keyPublic := d.Get("ssh_public").(string)
124118
if len(keyPublic) == 0 {
125119
public, private, _ := utils.SSHKeyPair()
126120

127-
d.Set(maprefix+"ssh_public", public)
128-
d.Set(maprefix+"ssh_private", private)
121+
d.Set("ssh_public", public)
122+
d.Set("ssh_private", private)
129123
}
130124

131-
sid, _ := shortid.New(1, shortid.DefaultABC, 2342)
132-
id, _ := sid.Generate()
133-
name := "iterative-" + id
134-
if hasMachine {
135-
136-
d.Set("name", name)
137-
d.Set(maprefix+"instance_name", name)
138-
139-
} else {
140-
instanceName := d.Get("instance_name").(string)
141-
if len(instanceName) == 0 {
142-
d.Set("instance_name", name)
143-
}
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)
144130
}
145131

146-
diags = append(diags, diag.Diagnostic{
147-
Severity: diag.Error,
148-
Summary: fmt.Sprintf("mapfrefix: %v", maprefix),
149-
})
150-
151-
diags = append(diags, diag.Diagnostic{
152-
Severity: diag.Error,
153-
Summary: strconv.FormatBool(hasMachine),
154-
})
155-
156-
diags = append(diags, diag.Diagnostic{
157-
Severity: diag.Error,
158-
Summary: d.Get(maprefix + "custom_data").(string),
159-
})
160-
161-
return diags
162-
163-
cloud := d.Get(maprefix + "cloud").(string)
132+
cloud := d.Get("cloud").(string)
164133
if cloud == "aws" {
165134
err := aws.ResourceMachineCreate(ctx, d, m)
166135
if err != nil {
@@ -191,9 +160,7 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
191160
func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
192161
var diags diag.Diagnostics
193162

194-
maprefix := utils.MachinePrefix(d)
195-
196-
cloud := d.Get(maprefix + "cloud").(string)
163+
cloud := d.Get("cloud").(string)
197164
if cloud == "aws" {
198165
err := aws.ResourceMachineDelete(ctx, d, m)
199166
if err != nil {
@@ -218,7 +185,3 @@ func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
218185
func resourceMachineRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
219186
return nil
220187
}
221-
222-
func resourceMachineUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
223-
return nil
224-
}

0 commit comments

Comments
 (0)