-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tom Withers
committed
Jun 19, 2020
0 parents
commit 22a1886
Showing
7 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module terraform-workspace-cleanup | ||
|
||
go 1.14 | ||
|
||
require github.com/aws/aws-sdk-go v1.32.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
github.com/aws/aws-sdk-go v1.32.5 h1:Sz0C7deIoMu5lFGTVkIN92IEZrUz1AWIDDW+9p6n1Rk= | ||
github.com/aws/aws-sdk-go v1.32.5/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc= | ||
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= | ||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= | ||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | ||
"log" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func main() { | ||
flag.Usage = func() { | ||
fmt.Println("Usage: tf-workspace-cleanup -put <workspace>") | ||
fmt.Println("Usage: tf-workspace-cleanup -expired-workspaces true") | ||
flag.PrintDefaults() | ||
} | ||
var workspaceName string | ||
var expiredWorkspaces bool | ||
|
||
flag.StringVar(&workspaceName, "put", "", "workspace to register for deletion at later time") | ||
flag.BoolVar(&expiredWorkspaces, "expired-workspaces", false, "get list of expired workspaces for deletion") | ||
flag.Parse() | ||
|
||
if workspaceName == "" { | ||
fmt.Println("Error: Workspace not passed") | ||
flag.Usage() | ||
} else { | ||
PutWorkspace(&workspaceName) | ||
} | ||
|
||
if expiredWorkspaces { | ||
GetExpiredWorkspaces() | ||
} | ||
} | ||
|
||
func PutWorkspace(w *string) { | ||
|
||
sess, err := session.NewSession() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
RoleArn := "" | ||
// TODO - Allow AWS Account ID and Role name to be passed in via cli flag | ||
if len(os.Getenv("CI")) > 0 { | ||
RoleArn = "arn:aws:iam::288342028542:role/sirius-ci" | ||
} else { | ||
RoleArn = "arn:aws:iam::288342028542:role/operator" | ||
} | ||
|
||
creds := stscreds.NewCredentials(sess, RoleArn) | ||
awsConfig := aws.Config{Credentials: creds, Region: aws.String("eu-west-1")} | ||
|
||
svc := dynamodb.New(sess, &awsConfig) | ||
|
||
type Workspace struct { | ||
WorkspaceName string | ||
ExpiresTTL int64 | ||
} | ||
|
||
item := Workspace{ | ||
WorkspaceName: *w, | ||
ExpiresTTL: time.Now().AddDate(0, 0, 1).Unix(), | ||
} | ||
|
||
WorkspaceToPut, err := dynamodbattribute.MarshalMap(item) | ||
if err != nil { | ||
fmt.Println("Got error marshalling Workspace item:") | ||
fmt.Println(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
input := &dynamodb.PutItemInput{ | ||
Item: WorkspaceToPut, | ||
TableName: aws.String("WorkspaceCleanup"), | ||
} | ||
|
||
_, err = svc.PutItem(input) | ||
if err != nil { | ||
fmt.Println("Got error calling PutItem:") | ||
fmt.Println(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Successfully added '" + item.WorkspaceName + "' with TTL " + strconv.FormatInt(item.ExpiresTTL, 10) + " for workspace cleanup") | ||
} | ||
|
||
func GetExpiredWorkspaces() { | ||
fmt.Println("Not yet implemented") | ||
} |