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

provider/aws: tag the spot instance. #4380

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 26 additions & 2 deletions builtin/providers/aws/resource_aws_spot_instance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,35 @@ func resourceAwsSpotInstanceRequestCreate(d *schema.ResourceData, meta interface
}

log.Printf("[DEBUG] waiting for spot bid to resolve... this may take several minutes.")
_, err = spotStateConf.WaitForState()
result, err := spotStateConf.WaitForState()

if err != nil {
return fmt.Errorf("Error while waiting for spot request (%s) to resolve: %s", sir, err)
}

sir = *result.(*ec2.SpotInstanceRequest)
d.Set("spot_instance_id", sir.InstanceId)

// Wait for the spot instance running
log.Printf(
"[DEBUG] Waiting for instance (%s) to become running",
*sir.InstanceId)

stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: "running",
Refresh: InstanceStateRefreshFunc(conn, *sir.InstanceId),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for instance (%s) to become ready: %s",
*sir.InstanceId, err)
}
}

return resourceAwsSpotInstanceRequestUpdate(d, meta)
Expand Down Expand Up @@ -254,7 +278,7 @@ func resourceAwsSpotInstanceRequestUpdate(d *schema.ResourceData, meta interface
conn := meta.(*AWSClient).ec2conn

d.Partial(true)
if err := setTags(conn, d); err != nil {
if err := setTagsSpotRequest(conn, d); err != nil {
return err
} else {
d.SetPartial("tags")
Expand Down
56 changes: 56 additions & 0 deletions builtin/providers/aws/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,62 @@ func setTags(conn *ec2.EC2, d *schema.ResourceData) error {
return nil
}

// setTagsSpotRequest also set the tags to the spot instance.
func setTagsSpotRequest(conn *ec2.EC2, d *schema.ResourceData) error {
if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTags(tagsFromMap(o), tagsFromMap(n))

// Set tags
if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v from %s", remove, d.Id())
_, err := conn.DeleteTags(&ec2.DeleteTagsInput{
Resources: []*string{aws.String(d.Id())},
Tags: remove,
})
if err != nil {
return err
}

if instanceId := d.Get("spot_instance_id").(string); instanceId != "" {
log.Printf("[DEBUG] Removing tags: %#v from %s", remove, instanceId)
_, err := conn.DeleteTags(&ec2.DeleteTagsInput{
Resources: []*string{aws.String(instanceId)},
Tags: remove,
})
if err != nil {
return err
}
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %s for %s", create, d.Id())
_, err := conn.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{aws.String(d.Id())},
Tags: create,
})
if err != nil {
return err
}

if instanceId := d.Get("spot_instance_id").(string); instanceId != "" {
log.Printf("[DEBUG] Creating tags: %s for %s", create, instanceId)
_, err := conn.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{aws.String(instanceId)},
Tags: create,
})
if err != nil {
return err
}
}
}
}

return nil
}

// diffTags takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must
// be destroyed.
Expand Down