Skip to content

Commit 245af62

Browse files
committed
no sleep
1 parent f23c9c6 commit 245af62

File tree

4 files changed

+85
-56
lines changed

4 files changed

+85
-56
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ NAMESPACE=iterative
44
NAME=iterative
55
VERSION=0.1
66
OS_ARCH=linux_amd64
7+
# OS_ARCH=darwin_amd64
78
BINARY=terraform-provider-${NAME}
89

910
default: install

examples/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ provider "iterative" {}
1212
resource "iterative_machine" "machine" {
1313
region = "us-west-1"
1414
instance_ami = "ami-03ba3948f6c37a4b0"
15+
aws_security_group = "default"
16+
key_public = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCcztk+/ibMWJH7LTjcw5KhlDKW1y/gJVB3ivk3D0YMa84ylL5pc8/3zd4PkAvX4eJX0Yi/dE9r4dY+8+ws/1pIuAO9y9Fu3ev+Cj8CKbFbgxaLDlaWHCV/y295rhmgfArtTB4KCbQ2EihXgodzddA9FGiyPoeYWilUUNDsi9wBTsouGahaFpiDVeAkexkUgGtGUWTW7OcTgvagGmKoNogWEpo9VBU3gGEoWj/I1TecQmOs09NFMyj1DdtRfsKYhQUfYz1W38ht0zCuPHKOnVGLDK4vd3nI2KzKJu0/CcFbjlJNqPrHHooIGJtmQRQIsSyI6hCUPK3ZCI7o+viaGqP+Awbo7XSKyexqd81bhgha98jqy6304jOG5qSUewgeK7VNq2FEXQ0D7ox0Yci/TgM7w+XVpjOf6XEUjkUyoLoL1xkxcINdZozWzeXK/dykvfXo+nwALT4UhjMx7fk46e2lRyExBuD4L0ah8rDT1ZUORsDkEVvmCx/tJqO1drrUPLT846Cb0E6oebcYCUCN9r8qo2BeipG44VkX0jL9BLB2IZeP5BpXFT+bo3zjXqEtX3l/5iJ42jOJodmw70gaf/7c2NWumydR2STuDQLUSvJC2Xtka5M/CHfNX3ShssrJaR/oKacU8F5DaPqTH9RSJ3oSs8Kr247E20i1BzDTwrWicQ== g.ortega.david@gmail.com"
1517
}
18+

iterative/provider.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
55
)
66

7-
// Provider -
87
func Provider() *schema.Provider {
98
return &schema.Provider{
109
ResourcesMap: map[string]*schema.Resource{

iterative/resource_machine.go

Lines changed: 81 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ func resourceMachine() *schema.Resource {
1919
//UpdateContext: resourceMachineUpdate,s
2020
DeleteContext: resourceMachineDelete,
2121
Schema: map[string]*schema.Schema{
22-
"key_name": &schema.Schema{
22+
"region": &schema.Schema{
2323
Type: schema.TypeString,
2424
Optional: true,
25-
Computed: true,
25+
ForceNew: true,
26+
Default: "us-west-2",
2627
},
27-
"private_key": &schema.Schema{
28+
"instance_ami": &schema.Schema{
2829
Type: schema.TypeString,
2930
Optional: true,
30-
Computed: true,
31+
ForceNew: true,
32+
Default: "ami-e7527ed7",
33+
},
34+
"instance_type": &schema.Schema{
35+
Type: schema.TypeString,
36+
Optional: true,
37+
ForceNew: true,
38+
Default: "t2.micro",
3139
},
3240
"instance_id": &schema.Schema{
3341
Type: schema.TypeString,
@@ -44,23 +52,27 @@ func resourceMachine() *schema.Resource {
4452
Optional: true,
4553
Computed: true,
4654
},
47-
"instance_ami": &schema.Schema{
55+
"key_name": &schema.Schema{
4856
Type: schema.TypeString,
4957
Optional: true,
50-
ForceNew: true,
51-
Default: "ami-e7527ed7",
58+
Computed: true,
5259
},
53-
"instance_type": &schema.Schema{
60+
"key_public": &schema.Schema{
5461
Type: schema.TypeString,
5562
Optional: true,
5663
ForceNew: true,
57-
Default: "t2.micro",
64+
Default: "",
5865
},
59-
"region": &schema.Schema{
66+
"key_private": &schema.Schema{
67+
Type: schema.TypeString,
68+
Optional: true,
69+
Computed: true,
70+
},
71+
"aws_security_group": &schema.Schema{
6072
Type: schema.TypeString,
6173
Optional: true,
6274
ForceNew: true,
63-
Default: "us-west-2",
75+
Default: "",
6476
},
6577
},
6678
}
@@ -69,42 +81,56 @@ func resourceMachine() *schema.Resource {
6981
func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
7082
var diags diag.Diagnostics
7183

84+
svc, errClient := awsClient(d)
85+
if errClient != nil {
86+
return diag.FromErr(errClient)
87+
}
88+
7289
sid, err := shortid.New(1, shortid.DefaultABC, 2342)
7390
id, _ := sid.Generate()
7491

75-
svc, _ := awsClient(d)
76-
ctxx := context.Background()
77-
78-
ami := d.Get("instance_ami").(string)
92+
instanceAmi := d.Get("instance_ami").(string)
7993
instanceType := d.Get("instance_type").(string)
94+
keyPublic := d.Get("key_public").(string)
95+
96+
securityGroup := d.Get("aws_security_group").(string)
97+
8098
pairName := "cml_" + id
81-
groupName := "cml"
99+
var keyMaterial string
82100

83-
keyResult, err := svc.CreateKeyPair(&ec2.CreateKeyPairInput{
84-
KeyName: aws.String(pairName),
85-
})
86-
if err != nil {
87-
return diag.FromErr(err)
101+
// key-pair
102+
if len(keyPublic) != 0 {
103+
_, errImportKeyPair := svc.ImportKeyPair(&ec2.ImportKeyPairInput{
104+
KeyName: aws.String(pairName),
105+
PublicKeyMaterial: []byte(keyPublic),
106+
})
107+
if errImportKeyPair != nil {
108+
return diag.FromErr(errImportKeyPair)
109+
}
110+
111+
} else {
112+
keyResult, err := svc.CreateKeyPair(&ec2.CreateKeyPairInput{
113+
KeyName: aws.String(pairName),
114+
})
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
keyMaterial = *keyResult.KeyMaterial
88119
}
89-
keyMaterial := *keyResult.KeyMaterial
90120

91-
vpcsDesc, _ := svc.DescribeVpcs(&ec2.DescribeVpcsInput{
92-
/* Filters: []*ec2.Filter{
93-
{
94-
Name: aws.String("tag:Name"),
95-
Values: []*string{aws.String("cml")},
96-
},
97-
}, */
98-
})
99-
vpc := vpcsDesc.Vpcs[0]
121+
if len(securityGroup) == 0 {
122+
securityGroup = "cml"
100123

101-
gpResult, ee := svc.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
102-
GroupName: aws.String(groupName),
103-
Description: aws.String("CML security group"),
104-
VpcId: aws.String(*vpc.VpcId),
105-
})
124+
vpcsDesc, _ := svc.DescribeVpcs(&ec2.DescribeVpcsInput{})
125+
vpc := vpcsDesc.Vpcs[0]
126+
vpcID := *vpc.VpcId
127+
128+
gpResult, ee := svc.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
129+
GroupName: aws.String(securityGroup),
130+
Description: aws.String("CML security group"),
131+
VpcId: aws.String(vpcID),
132+
})
106133

107-
if ee == nil {
108134
svc.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
109135
GroupId: aws.String(*gpResult.GroupId),
110136
IpPermissions: []*ec2.IpPermission{
@@ -130,18 +156,20 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
130156
}),
131157
},
132158
})
159+
if ee != nil {
160+
return diag.FromErr(err)
161+
}
133162
}
134163

135-
runResult, err := svc.RunInstancesWithContext(ctxx, &ec2.RunInstancesInput{
164+
runResult, err := svc.RunInstancesWithContext(ctx, &ec2.RunInstancesInput{
165+
ImageId: aws.String(instanceAmi),
136166
KeyName: aws.String(pairName),
137-
ImageId: aws.String(ami),
138167
InstanceType: aws.String(instanceType),
139168
MinCount: aws.Int64(1),
140169
MaxCount: aws.Int64(1),
141170
SecurityGroups: []*string{
142-
aws.String(groupName),
171+
aws.String(securityGroup),
143172
},
144-
145173
//CpuOptions: instanceOpts.CpuOptions,
146174
})
147175
if err != nil {
@@ -169,24 +197,24 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
169197
instanceIds[0] = &instanceID
170198
statusInput := ec2.DescribeInstancesInput{
171199
InstanceIds: instanceIds,
200+
Filters: []*ec2.Filter{
201+
{
202+
Name: aws.String("instance-state-name"),
203+
Values: []*string{aws.String("running")},
204+
},
205+
},
172206
}
173-
svc.WaitUntilInstanceExistsWithContext(ctxx, &statusInput)
207+
svc.WaitUntilInstanceExistsWithContext(ctx, &statusInput)
174208

175-
time.Sleep(50 * time.Second)
176-
177-
descResult, _ := svc.DescribeInstancesWithContext(ctxx, &statusInput)
209+
descResult, _ := svc.DescribeInstancesWithContext(ctx, &statusInput)
178210
instanceDesc := descResult.Reservations[0].Instances[0]
179211

180212
d.SetId(instanceID)
181213
d.Set("instance_id", instanceID)
182214
d.Set("instance_ip", instanceDesc.PublicIpAddress)
183215
d.Set("instance_launch_time", instanceDesc.LaunchTime.Format(time.RFC3339))
184216
d.Set("key_name", pairName)
185-
d.Set("private_key", keyMaterial)
186-
187-
/* if err := d.Set("instaceID", instanceID); err != nil {
188-
return diag.FromErr(err)
189-
} */
217+
d.Set("key_privates", keyMaterial)
190218

191219
return diags
192220
}
@@ -230,14 +258,12 @@ func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
230258
return diags
231259
}
232260

233-
func awsClient(d *schema.ResourceData) (*ec2.EC2, diag.Diagnostics) {
234-
var diags diag.Diagnostics
235-
261+
func awsClient(d *schema.ResourceData) (*ec2.EC2, error) {
236262
region := d.Get("region").(string)
237-
sess, _ := session.NewSession(&aws.Config{
263+
sess, err := session.NewSession(&aws.Config{
238264
Region: aws.String(region)},
239265
)
240266
svc := ec2.New(sess)
241267

242-
return svc, diags
268+
return svc, err
243269
}

0 commit comments

Comments
 (0)