From 60a51f12226c68d76327a063b75a5314e5acd6b8 Mon Sep 17 00:00:00 2001 From: joeduffy Date: Mon, 20 Feb 2017 17:41:24 -0800 Subject: [PATCH] Implement simple deletion functions --- lib/aws/ec2/instance.ts | 1 + lib/aws/provider/ec2/instance.go | 16 +++++++++++----- lib/aws/provider/ec2/security_group.go | 10 +++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/aws/ec2/instance.ts b/lib/aws/ec2/instance.ts index fdd808d50..12e31037d 100644 --- a/lib/aws/ec2/instance.ts +++ b/lib/aws/ec2/instance.ts @@ -19,6 +19,7 @@ export class Instance constructor(args: InstanceProperties) { super({ resource: "AWS::EC2::Instance", + properties: args, }); this.imageId = args.imageId; this.instanceType = args.instanceType; diff --git a/lib/aws/provider/ec2/instance.go b/lib/aws/provider/ec2/instance.go index 184d4d735..2b8b3d5fe 100644 --- a/lib/aws/provider/ec2/instance.go +++ b/lib/aws/provider/ec2/instance.go @@ -35,6 +35,7 @@ func (p *instanceProvider) Create(ctx context.Context, req *murpc.CreateRequest) props := resource.UnmarshalProperties(req.GetProperties()) // Read in the properties given by the request, validating as we go; if any fail, reject the request. + // TODO: validate additional properties (e.g., that AMI exists in this region). // TODO: this is a good example of a "benign" (StateOK) error; handle it accordingly. inst, err := newInstance(props, true) if err != nil { @@ -44,12 +45,10 @@ func (p *instanceProvider) Create(ctx context.Context, req *murpc.CreateRequest) // Create the create instances request object. var secgrpIDs []*string if inst.SecurityGroupIDs != nil { - for _, sid := range *inst.SecurityGroupIDs { - secgrpIDs = append(secgrpIDs, &sid) - } + secgrpIDs = aws.StringSlice(*inst.SecurityGroupIDs) } create := &ec2.RunInstancesInput{ - ImageId: &inst.ImageID, + ImageId: aws.String(inst.ImageID), InstanceType: inst.InstanceType, SecurityGroupIds: secgrpIDs, KeyName: inst.KeyName, @@ -91,7 +90,14 @@ func (p *instanceProvider) Update(ctx context.Context, req *murpc.UpdateRequest) // Delete tears down an existing resource with the given ID. If it fails, the resource is assumed to still exist. func (p *instanceProvider) Delete(ctx context.Context, req *murpc.DeleteRequest) (*pbempty.Empty, error) { contract.Assert(req.GetType() == string(Instance)) - return nil, errors.New("Not yet implemented") + delete := &ec2.TerminateInstancesInput{ + InstanceIds: []*string{aws.String(req.GetId())}, + } + if _, err := p.ctx.EC2().TerminateInstances(delete); err != nil { + return nil, err + } + // TODO: wait for termination to complete. + return &pbempty.Empty{}, nil } // instance represents the state associated with an instance. diff --git a/lib/aws/provider/ec2/security_group.go b/lib/aws/provider/ec2/security_group.go index 73a22a0fe..b8596d900 100644 --- a/lib/aws/provider/ec2/security_group.go +++ b/lib/aws/provider/ec2/security_group.go @@ -5,6 +5,7 @@ package ec2 import ( "errors" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" pbempty "github.com/golang/protobuf/ptypes/empty" "github.com/marapongo/mu/pkg/resource" @@ -82,7 +83,14 @@ func (p *securityGroupProvider) Update(ctx context.Context, req *murpc.UpdateReq // Delete tears down an existing resource with the given ID. If it fails, the resource is assumed to still exist. func (p *securityGroupProvider) Delete(ctx context.Context, req *murpc.DeleteRequest) (*pbempty.Empty, error) { contract.Assert(req.GetType() == string(SecurityGroup)) - return nil, errors.New("Not yet implemented") + delete := &ec2.DeleteSecurityGroupInput{ + GroupId: aws.String(req.GetId()), + } + if _, err := p.ctx.EC2().DeleteSecurityGroup(delete); err != nil { + return nil, err + } + // TODO: wait for termination to complete. + return &pbempty.Empty{}, nil } // securityGroup represents the state associated with a security group.