Skip to content

Commit

Permalink
r/aws_cloud9_ac2_environment: Cosmetics.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jan 5, 2022
1 parent 818f1bc commit 3c7bbce
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions internal/service/cloud9/environment_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,32 @@ func resourceEnvironmentEC2Create(d *schema.ResourceData, meta interface{}) erro
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

params := &cloud9.CreateEnvironmentEC2Input{
InstanceType: aws.String(d.Get("instance_type").(string)),
Name: aws.String(d.Get("name").(string)),
name := d.Get("name").(string)
input := &cloud9.CreateEnvironmentEC2Input{
ClientRequestToken: aws.String(resource.UniqueId()),
InstanceType: aws.String(d.Get("instance_type").(string)),
Name: aws.String(name),
Tags: Tags(tags.IgnoreAWS()),
}

if v, ok := d.GetOk("automatic_stop_time_minutes"); ok {
params.AutomaticStopTimeMinutes = aws.Int64(int64(v.(int)))
input.AutomaticStopTimeMinutes = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("description"); ok {
params.Description = aws.String(v.(string))
input.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("owner_arn"); ok {
params.OwnerArn = aws.String(v.(string))
input.OwnerArn = aws.String(v.(string))
}
if v, ok := d.GetOk("subnet_id"); ok {
params.SubnetId = aws.String(v.(string))
input.SubnetId = aws.String(v.(string))
}

var out *cloud9.CreateEnvironmentEC2Output
log.Printf("[INFO] Creating Cloud9 EC2 Environment: %s", input)
var output *cloud9.CreateEnvironmentEC2Output
err := resource.Retry(tfiam.PropagationTimeout, func() *resource.RetryError {
var err error
out, err = conn.CreateEnvironmentEC2(params)
output, err = conn.CreateEnvironmentEC2(input)
if err != nil {
// NotFoundException: User arn:aws:iam::*******:user/****** does not exist.
if tfawserr.ErrMessageContains(err, cloud9.ErrCodeNotFoundException, "User") {
Expand All @@ -116,18 +118,21 @@ func resourceEnvironmentEC2Create(d *schema.ResourceData, meta interface{}) erro
}
return nil
})

if tfresource.TimedOut(err) {
out, err = conn.CreateEnvironmentEC2(params)
output, err = conn.CreateEnvironmentEC2(input)
}

if err != nil {
return fmt.Errorf("Error creating Cloud9 EC2 Environment: %w", err)
return fmt.Errorf("error creating Cloud9 EC2 Environment (%s): %w", name, err)
}
d.SetId(aws.StringValue(out.EnvironmentId))

d.SetId(aws.StringValue(output.EnvironmentId))

_, err = waitEnvironmentReady(conn, d.Id())

if err != nil {
return fmt.Errorf("error waiting for Cloud9 EC2 Environment (%s) to be ready: %w", d.Id(), err)
return fmt.Errorf("error waiting for Cloud9 EC2 Environment (%s) create: %w", d.Id(), err)
}

return resourceEnvironmentEC2Read(d, meta)
Expand All @@ -138,18 +143,16 @@ func resourceEnvironmentEC2Read(d *schema.ResourceData, meta interface{}) error
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

log.Printf("[INFO] Reading Cloud9 Environment EC2 %s", d.Id())

env, err := FindEnvironmentByID(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Cloud9 Environment EC2 (%s) not found, removing from state", d.Id())
log.Printf("[WARN] Cloud9 EC2 Environment (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading Cloud9 Environment EC2 (%s): %w", d.Id(), err)
return fmt.Errorf("error reading Cloud9 EC2 Environment (%s): %w", d.Id(), err)
}

arn := aws.StringValue(env.Arn)
Expand All @@ -176,8 +179,6 @@ func resourceEnvironmentEC2Read(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error setting tags_all: %w", err)
}

log.Printf("[DEBUG] Received Cloud9 Environment EC2: %s", env)

return nil
}

Expand All @@ -191,14 +192,12 @@ func resourceEnvironmentEC2Update(d *schema.ResourceData, meta interface{}) erro
Name: aws.String(d.Get("name").(string)),
}

log.Printf("[INFO] Updating Cloud9 Environment EC2: %s", input)
log.Printf("[INFO] Updating Cloud9 EC2 Environment: %s", input)
_, err := conn.UpdateEnvironment(&input)

out, err := conn.UpdateEnvironment(&input)
if err != nil {
return err
return fmt.Errorf("error updating Cloud9 EC2 Environment (%s): %w", d.Id(), err)
}

log.Printf("[DEBUG] Cloud9 Environment EC2 updated: %s", out)
}

if d.HasChange("tags_all") {
Expand All @@ -216,20 +215,23 @@ func resourceEnvironmentEC2Update(d *schema.ResourceData, meta interface{}) erro
func resourceEnvironmentEC2Delete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).Cloud9Conn

log.Printf("[INFO] Deleting Cloud9 EC2 Environment: %s", d.Id())
_, err := conn.DeleteEnvironment(&cloud9.DeleteEnvironmentInput{
EnvironmentId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, cloud9.ErrCodeNotFoundException) {
return nil
}

if err != nil {
return err
return fmt.Errorf("error deleting Cloud9 EC2 Environment (%s): %w", d.Id(), err)
}

_, err = waitEnvironmentDeleted(conn, d.Id())

if err != nil {
return fmt.Errorf("error waiting for Cloud9 EC2 Environment (%s) to be deleted: %w", d.Id(), err)
return fmt.Errorf("error waiting for Cloud9 EC2 Environment (%s) delete: %w", d.Id(), err)
}

return nil
Expand Down

0 comments on commit 3c7bbce

Please sign in to comment.