-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
83 lines (66 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// +build linux darwin
package main
import (
"fmt"
"os"
"flag"
"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"
"strconv"
"time"
)
func exitWithError(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func main() {
flag.Usage = func() {
fmt.Println("Usage: Put Workspace")
flag.PrintDefaults()
}
var WorkspaceName string
flag.StringVar(&WorkspaceName, "workspace", "", "name of workspace to mark for clean up")
flag.Parse()
sess, err := session.NewSession()
if err != nil {
log.Fatalln(err)
}
RoleArn := ""
if len(os.Getenv("CI")) > 0 {
RoleArn = "arn:aws:iam::367815980639:role/opg-use-an-lpa-ci"
} else {
RoleArn = "arn:aws:iam::367815980639: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: WorkspaceName,
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")
}