Skip to content

Commit

Permalink
moving formatting to logic, not tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ina-stoyanova committed Oct 9, 2020
1 parent 022855d commit 3501dea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
10 changes: 6 additions & 4 deletions aws/ecs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import (
)

// Tag an ECS cluster identified by the given cluster ARN when it's first seen by cloud-nuke
func tagEcsClusterWhenFirstSeen(awsSession *session.Session, clusterArn *string, tagValue string) error {
func tagEcsClusterWhenFirstSeen(awsSession *session.Session, clusterArn *string, timestamp time.Time) error {
svc := ecs.New(awsSession)

firstSeenTime := formatTimestampTag(timestamp)

input := &ecs.TagResourceInput{
ResourceArn: clusterArn,
Tags: []*ecs.Tag{
{
Key: aws.String(firstSeenTagKey),
Value: aws.String(tagValue),
Value: aws.String(firstSeenTime),
},
},
}
Expand Down Expand Up @@ -53,7 +56,7 @@ func getAllEcsClustersOlderThan(awsSession *session.Session, region string, excl
}

if firstSeenTime.IsZero() {
err := tagEcsClusterWhenFirstSeen(awsSession, clusterArn, formatTimestampTag(time.Now().UTC()))
err := tagEcsClusterWhenFirstSeen(awsSession, clusterArn, time.Now().UTC())
if err != nil {
logging.Logger.Errorf("Error tagigng the ECS cluster with ARN %s", aws.StringValue(clusterArn))
return nil, errors.WithStackTrace(err)
Expand Down Expand Up @@ -119,7 +122,6 @@ func getClusterTag(awsSession *session.Session, clusterArn *string, tagKey strin
if aws.StringValue(tag.Key) == tagKey {

firstSeenTime, err := parseTimestampTag(aws.StringValue(tag.Value))

if err != nil {
logging.Logger.Errorf("Error parsing the `cloud-nuke-first-seen` tag into a `RFC3339` Time format for ECS cluster with ARN %s", aws.StringValue(clusterArn))
return firstSeenTime, errors.WithStackTrace(err)
Expand Down
21 changes: 12 additions & 9 deletions aws/ecs_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ func TestCanTagEcsClusters(t *testing.T) {
cluster := createEcsFargateCluster(t, awsSession, util.UniqueID())
defer deleteEcsCluster(awsSession, cluster)

tagValue := formatTimestampTag(time.Now().UTC())
tagValue := time.Now().UTC()

tagErr := tagEcsClusterWhenFirstSeen(awsSession, cluster.ClusterArn, tagValue)
require.NoError(t, tagErr)

returnedTag, err := getClusterTag(awsSession, cluster.ClusterArn, firstSeenTagKey)
require.NoError(t, err)

parsedOriginalTagValue, parseErr := parseTimestampTag(tagValue)
require.NoError(t, parseErr)
parsedTagValue, parseErr1 := parseTimestampTag(formatTimestampTag(tagValue))
require.NoError(t, parseErr1)

assert.Equal(t, parsedOriginalTagValue, returnedTag)
parsedReturnValue, parseErr2 := parseTimestampTag(formatTimestampTag(returnedTag))
require.NoError(t, parseErr2)

assert.Equal(t, parsedTagValue, parsedReturnValue)
}

// Test we can get all ECS clusters younger than < X time based on tags
Expand All @@ -54,8 +57,8 @@ func TestCanListAllEcsClustersOlderThan24hours(t *testing.T) {
defer deleteEcsCluster(awsSession, cluster2)

now := time.Now().UTC()
var olderClusterTagValue = now.Add(time.Hour * time.Duration(-48)).Format(time.RFC3339)
var youngerClusterTagValue = now.Add(time.Hour * time.Duration(-23)).Format(time.RFC3339)
var olderClusterTagValue = now.Add(time.Hour * time.Duration(-48))
var youngerClusterTagValue = now.Add(time.Hour * time.Duration(-23))

err1 := tagEcsClusterWhenFirstSeen(awsSession, cluster1.ClusterArn, olderClusterTagValue)
require.NoError(t, err1)
Expand Down Expand Up @@ -86,9 +89,9 @@ func TestCanNukeAllEcsClustersOlderThan24Hours(t *testing.T) {
defer deleteEcsCluster(awsSession, cluster3)

now := time.Now().UTC()
var oldClusterTagValue1 = now.Add(time.Hour * time.Duration(-48)).Format(time.RFC3339)
var youngClusterTagValue = now.Format(time.RFC3339)
var oldClusterTagValue2 = now.Add(time.Hour * time.Duration(-27)).Format(time.RFC3339)
var oldClusterTagValue1 = now.Add(time.Hour * time.Duration(-48))
var youngClusterTagValue = now
var oldClusterTagValue2 = now.Add(time.Hour * time.Duration(-27))

err1 := tagEcsClusterWhenFirstSeen(awsSession, cluster1.ClusterArn, oldClusterTagValue1)
require.NoError(t, err1)
Expand Down

0 comments on commit 3501dea

Please sign in to comment.