Skip to content

Commit

Permalink
Other prep work for the Bubble Tea implementation (#98)
Browse files Browse the repository at this point in the history
* Ensure new VPC gets created when requested.

Append unique string to VPC name when creating, to prevent collisions.
Add nil checks on security group and subnet parameters.
New ValidateInteger function, for input validation on plainText questions type.

Co-authored-by: Gavin <[email protected]>

* Pass in stack name, to avoid name mismatch from generating a UUID

The UUID generation is internal to CreateStackAndGetResources, meaning we cannot mock it. Easier to pass in a particular value and avoid the UUID generation code altogether.

Co-authored-by: Gavin <[email protected]>
  • Loading branch information
snay2 and GavinBurris42 authored Aug 22, 2022
1 parent 8d11504 commit f7b037b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pkg/cfn/cfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/google/uuid"
)

const DefaultStackName = "simple-ec2"
Expand Down Expand Up @@ -51,7 +52,8 @@ func (c Cfn) CreateStackAndGetResources(availabilityZones []*ec2.AvailabilityZon
stackName *string, template string) (vpcId *string, subnetIds []string, instanceId *string,
stackResources []*cloudformation.StackResource, err error) {
if stackName == nil {
stackName = aws.String(DefaultStackName)
stackIdentifier := uuid.New()
stackName = aws.String(fmt.Sprintf("%s%s", DefaultStackName, stackIdentifier))
}

zonesToUse := []*ec2.AvailabilityZone{}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cfn/cfn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestCreateStackAndGetResources_Success(t *testing.T) {
StackEvents: mockedEvents,
}

vpcId, subnetIds, instanceId, _, err := testCfn.CreateStackAndGetResources(testAzs, nil, "")
vpcId, subnetIds, instanceId, _, err := testCfn.CreateStackAndGetResources(testAzs, aws.String(cfn.DefaultStackName), "")
th.Ok(t, err)
th.Equals(t, testVpcId, *vpcId)
th.Equals(t, testSubnetIds, subnetIds)
Expand All @@ -106,7 +106,7 @@ func TestCreateStackAndGetResources_DescribeStackEventsPagesError(t *testing.T)
DescribeStackEventsPagesError: errors.New("Test error"),
}

_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, nil, "")
_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, aws.String(cfn.DefaultStackName), "")
th.Nok(t, err)
}

Expand All @@ -118,7 +118,7 @@ func TestCreateStackAndGetResources_DescribeStackResourcesError(t *testing.T) {
DescribeStackResourcesError: errors.New("Test error"),
}

_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, nil, "")
_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, aws.String(cfn.DefaultStackName), "")
th.Nok(t, err)
}

Expand All @@ -133,7 +133,7 @@ func TestCreateStackAndGetResources_NoSubnet(t *testing.T) {
StackEvents: mockedEvents,
}

_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, nil, "")
_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, aws.String(cfn.DefaultStackName), "")
th.Nok(t, err)
}

Expand All @@ -152,7 +152,7 @@ func TestCreateStackAndGetResources_NoVpc(t *testing.T) {
StackEvents: mockedEvents,
}

_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, nil, "")
_, _, _, _, err := testCfn.CreateStackAndGetResources(testAzs, aws.String(cfn.DefaultStackName), "")
th.Nok(t, err)
}

Expand Down
26 changes: 24 additions & 2 deletions pkg/ec2helper/ec2helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"os"
"sort"
"strconv"
"strings"

"simple-ec2/pkg/cfn"
Expand Down Expand Up @@ -1117,6 +1118,14 @@ func (h *EC2Helper) LaunchSpotInstance(simpleConfig *config.SimpleInfo, detailed
if simpleConfig.LaunchTemplateId != "" {
_, err = h.LaunchFleet(aws.String(simpleConfig.LaunchTemplateId))
} else {
// Create new stack, if specified.
if simpleConfig.NewVPC {
err := h.createNetworkConfiguration(simpleConfig, nil)
if err != nil {
return err
}
}

template, err := h.CreateLaunchTemplate(simpleConfig, detailedConfig)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
Expand Down Expand Up @@ -1171,7 +1180,9 @@ func (h *EC2Helper) createNetworkConfiguration(simpleConfig *config.SimpleInfo,
return errors.New("No subnet with the selected availability zone found")
}

input.SubnetId = selectedSubnetId
if input != nil {
input.SubnetId = selectedSubnetId
}

/*
Get the security group.
Expand Down Expand Up @@ -1208,7 +1219,9 @@ func (h *EC2Helper) createNetworkConfiguration(simpleConfig *config.SimpleInfo,
return errors.New("No security group available for stack")
}

input.SecurityGroupIds = aws.StringSlice(selectedSecurityGroupIds)
if input != nil {
input.SecurityGroupIds = aws.StringSlice(selectedSecurityGroupIds)
}

// Update simpleConfig for config saving
simpleConfig.NewVPC = false
Expand Down Expand Up @@ -1284,6 +1297,15 @@ func ValidateTags(h *EC2Helper, userTags string) bool {
return true
}

// ValidateInteger checks if a given string is an integer
func ValidateInteger(h *EC2Helper, intString string) bool {
_, err := strconv.Atoi(intString)
if err != nil {
return false
}
return true
}

// Given an AWS platform string, tell if it's a Linux platform
func IsLinux(platform string) bool {
return platform == ec2.CapacityReservationInstancePlatformLinuxUnix ||
Expand Down
20 changes: 20 additions & 0 deletions pkg/ec2helper/ec2helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,26 @@ func TestValidateTags_False(t *testing.T) {
th.Equals(t, false, result)
}

func TestValidateInteger_True(t *testing.T) {
testUserInput := "123"
result := ec2helper.ValidateInteger(testEC2, testUserInput)
th.Equals(t, true, result)
}

func TestValidateInteger_False(t *testing.T) {
floatInput := "123.456"
floatResult := ec2helper.ValidateInteger(testEC2, floatInput)
th.Equals(t, false, floatResult)

stringInput := "abcdefg"
stringResult := ec2helper.ValidateInteger(testEC2, stringInput)
th.Equals(t, false, stringResult)

alphanumericInput := "123abc"
alphanumericResult := ec2helper.ValidateInteger(testEC2, alphanumericInput)
th.Equals(t, false, alphanumericResult)
}

func TestIsLinux_True(t *testing.T) {
actualIsLinux := ec2helper.IsLinux(ec2.CapacityReservationInstancePlatformLinuxUnix)
th.Equals(t, true, actualIsLinux)
Expand Down

0 comments on commit f7b037b

Please sign in to comment.