-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildstatus.go
123 lines (104 loc) · 3 KB
/
buildstatus.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strconv"
)
func saveBuildStatus(ctx *gin.Context) {
clientId := ctx.GetHeader("X_CLIENT_ID")
jobId := ctx.Param("id")
// TODO validate inputs for security
var status BuildStatus
ctx.BindJSON(&status)
status.ClientID = clientId
status.JobID = jobId
createItem(status)
// TODO update return code for error messages
ctx.Status(http.StatusOK)
}
func createItem(status BuildStatus) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
if err != nil {
// TODO handle exception
log.Fatalln(err, err.Error())
}
svc := dynamodb.New(sess)
dynamoDocument, err := dynamodbattribute.MarshalMap(status)
if err != nil {
// TODO handle exception
log.Fatalln(err, err.Error())
}
putRequest := &dynamodb.PutItemInput{
Item: dynamoDocument,
TableName: aws.String("buildstatus"),
ConditionExpression: aws.String("attribute_not_exists (ClientID) or BuildNumber <= :buildno"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":buildno": {
N: aws.String(strconv.Itoa(status.BuildNumber)),
},
},
}
_, err = svc.PutItem(putRequest)
if err != nil {
// TODO: Handle Error, but we can ignore ConditionalCheckFailedException
fmt.Println(err.Error())
}
}
func getFailedBuilds(clientId string) []BuildStatus {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
if err != nil {
// TODO handle exception
log.Fatalln(err, err.Error())
}
svc := dynamodb.New(sess)
input := &dynamodb.ScanInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":clientId": {
S: aws.String(clientId),
},
":buildStatus": {
BOOL: aws.Bool(false),
},
},
FilterExpression: aws.String("ClientID = :clientId and BuildStatus = :buildStatus"),
TableName: aws.String("buildstatus"),
}
// TODO proper error handling
result, err := svc.Scan(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeProvisionedThroughputExceededException:
fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return nil
}
buildStats := []BuildStatus{}
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &buildStats)
if err != nil {
// TODO handle error
}
return buildStats
}