Skip to content

Commit

Permalink
adding methods to parse and format timestamps instead pf all across t…
Browse files Browse the repository at this point in the history
…he code/test suite
  • Loading branch information
ina-stoyanova committed Oct 9, 2020
1 parent 34cae6f commit 022855d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 18 additions & 2 deletions aws/ecs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func getAllEcsClustersOlderThan(awsSession *session.Session, region string, excl
}

if firstSeenTime.IsZero() {
err := tagEcsClusterWhenFirstSeen(awsSession, clusterArn, time.Now().UTC().Format(time.RFC3339))
err := tagEcsClusterWhenFirstSeen(awsSession, clusterArn, formatTimestampTag(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 @@ -118,7 +118,8 @@ func getClusterTag(awsSession *session.Session, clusterArn *string, tagKey strin
for _, tag := range clusterTags.Tags {
if aws.StringValue(tag.Key) == tagKey {

firstSeenTime, err := time.Parse(time.RFC3339, aws.StringValue(tag.Value))
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 All @@ -129,3 +130,18 @@ func getClusterTag(awsSession *session.Session, clusterArn *string, tagKey strin
}
return firstSeenTime, nil
}

func parseTimestampTag(timestamp string) (time.Time, error) {
parsed, err := time.Parse(time.RFC3339, timestamp)

if err != nil {
logging.Logger.Errorf("Error parsing the timestamp into a `RFC3339` Time format")
return parsed, errors.WithStackTrace(err)

}
return parsed, nil
}

func formatTimestampTag(timestamp time.Time) string {
return timestamp.Format(time.RFC3339)
}
7 changes: 5 additions & 2 deletions aws/ecs_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ func TestCanTagEcsClusters(t *testing.T) {
cluster := createEcsFargateCluster(t, awsSession, util.UniqueID())
defer deleteEcsCluster(awsSession, cluster)

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

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

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

assert.Equal(t, returnedTag.Format(time.RFC3339), tagValue)
parsedOriginalTagValue, parseErr := parseTimestampTag(tagValue)
require.NoError(t, parseErr)

assert.Equal(t, parsedOriginalTagValue, returnedTag)
}

// Test we can get all ECS clusters younger than < X time based on tags
Expand Down

0 comments on commit 022855d

Please sign in to comment.