Skip to content

Commit

Permalink
New Resource: aws_cloud9_environment_ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 8, 2018
1 parent afd0201 commit 6081204
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/aws/aws-sdk-go/service/cloud9"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/cloudtrail"
Expand Down Expand Up @@ -138,6 +139,7 @@ type Config struct {

type AWSClient struct {
cfconn *cloudformation.CloudFormation
cloud9conn *cloud9.Cloud9
cloudfrontconn *cloudfront.CloudFront
cloudtrailconn *cloudtrail.CloudTrail
cloudwatchconn *cloudwatch.CloudWatch
Expand Down Expand Up @@ -398,6 +400,7 @@ func (c *Config) Client() (interface{}, error) {
client.apigateway = apigateway.New(awsApigatewaySess)
client.appautoscalingconn = applicationautoscaling.New(sess)
client.autoscalingconn = autoscaling.New(sess)
client.cloud9conn = cloud9.New(sess)
client.cfconn = cloudformation.New(awsCfSess)
client.cloudfrontconn = cloudfront.New(sess)
client.cloudtrailconn = cloudtrail.New(sess)
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func Provider() terraform.ResourceProvider {
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_cloud9_environment_ec2": resourceAwsCloud9EnvironmentEc2(),
"aws_cloudformation_stack": resourceAwsCloudFormationStack(),
"aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(),
"aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(),
Expand Down
217 changes: 217 additions & 0 deletions aws/resource_aws_cloud9_environment_ec2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloud9"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsCloud9EnvironmentEc2() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloud9EnvironmentEc2Create,
Read: resourceAwsCloud9EnvironmentEc2Read,
Update: resourceAwsCloud9EnvironmentEc2Update,
Delete: resourceAwsCloud9EnvironmentEc2Delete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"instance_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"automatic_stop_time_minutes": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"owner_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsCloud9EnvironmentEc2Create(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn

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

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

out, err := conn.CreateEnvironmentEC2(params)
if err != nil {
return err
}
d.SetId(*out.EnvironmentId)

stateConf := resource.StateChangeConf{
Pending: []string{
cloud9.EnvironmentStatusConnecting,
cloud9.EnvironmentStatusCreating,
},
Target: []string{
cloud9.EnvironmentStatusReady,
},
Timeout: 10 * time.Minute,
Refresh: func() (interface{}, string, error) {
out, err := conn.DescribeEnvironmentStatus(&cloud9.DescribeEnvironmentStatusInput{
EnvironmentId: aws.String(d.Id()),
})
if err != nil {
return 42, "", err
}

status := *out.Status
var sErr error
if status == cloud9.EnvironmentStatusError && out.Message != nil {
sErr = fmt.Errorf("Reason: %s", *out.Message)
}

return out, status, sErr
},
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}

return resourceAwsCloud9EnvironmentEc2Read(d, meta)
}

func resourceAwsCloud9EnvironmentEc2Read(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn

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

out, err := conn.DescribeEnvironments(&cloud9.DescribeEnvironmentsInput{
EnvironmentIds: []*string{aws.String(d.Id())},
})
if err != nil {
if isAWSErr(err, cloud9.ErrCodeNotFoundException, "") {
log.Printf("[WARN] Cloud9 Environment EC2 (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
if len(out.Environments) == 0 {
log.Printf("[WARN] Cloud9 Environment EC2 (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
env := out.Environments[0]

d.Set("arn", env.Arn)
d.Set("description", env.Description)
d.Set("name", env.Name)
d.Set("owner_arn", env.OwnerArn)
d.Set("type", env.Type)

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

return nil
}

func resourceAwsCloud9EnvironmentEc2Update(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn

input := cloud9.UpdateEnvironmentInput{
Description: aws.String(d.Get("description").(string)),
EnvironmentId: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
}

log.Printf("[INFO] Updating Cloud9 Environment EC2: %s", input)

out, err := conn.UpdateEnvironment(&input)
if err != nil {
return err
}

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

return resourceAwsCloud9EnvironmentEc2Read(d, meta)
}

func resourceAwsCloud9EnvironmentEc2Delete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloud9conn

_, err := conn.DeleteEnvironment(&cloud9.DeleteEnvironmentInput{
EnvironmentId: aws.String(d.Id()),
})
if err != nil {
return err
}
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
out, err := conn.DescribeEnvironments(&cloud9.DescribeEnvironmentsInput{
EnvironmentIds: []*string{aws.String(d.Id())},
})
if err != nil {
if isAWSErr(err, cloud9.ErrCodeNotFoundException, "") {
return nil
}
// :'-(
if isAWSErr(err, "AccessDeniedException", "is not authorized to access this resource") {
return nil
}
return resource.NonRetryableError(err)
}
if len(out.Environments) == 0 {
return nil
}
return resource.RetryableError(fmt.Errorf("Cloud9 EC2 Environment %q still exists", d.Id()))
})
if err != nil {
return err
}

return err
}
Loading

0 comments on commit 6081204

Please sign in to comment.