-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.go
118 lines (103 loc) · 4.1 KB
/
aws.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
package duat
// Utility functions for interacting with AWS
import (
"net/url"
"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/ecr"
"github.com/go-stack/stack" // Forked copy of https://github.com/go-stack/stack
"github.com/jjeffery/kv" // Forked copy of https://github.com/jjeffery/kv
)
func GetECRToken() (token string, err kv.Error) {
sess, errGo := session.NewSession()
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
svc := ecr.New(sess)
input := &ecr.GetAuthorizationTokenInput{}
result, errGo := svc.GetAuthorizationToken(input)
if errGo != nil {
if aerr, ok := errGo.(awserr.Error); ok {
switch aerr.Code() {
case ecr.ErrCodeServerException:
return "", kv.NewError(ecr.ErrCodeServerException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
case ecr.ErrCodeInvalidParameterException:
return "", kv.NewError(ecr.ErrCodeInvalidParameterException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
default:
return "", kv.Wrap(aerr).With("stack", stack.Trace().TrimRuntime())
}
} else {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
}
if len(result.AuthorizationData) != 1 {
return "", kv.NewError("aws auth data is in an unknown format").With("stack", stack.Trace().TrimRuntime())
}
return *result.AuthorizationData[0].AuthorizationToken, nil
}
func CreateECRRepo(repo string) (err kv.Error) {
sess, errGo := session.NewSession()
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
svc := ecr.New(sess)
input := &ecr.CreateRepositoryInput{
RepositoryName: aws.String(repo),
}
if _, errGo := svc.CreateRepository(input); errGo != nil {
if aerr, ok := errGo.(awserr.Error); ok {
switch aerr.Code() {
case ecr.ErrCodeServerException:
return kv.NewError(ecr.ErrCodeServerException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
case ecr.ErrCodeInvalidParameterException:
return kv.NewError(ecr.ErrCodeInvalidParameterException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
case ecr.ErrCodeRepositoryAlreadyExistsException:
return kv.Wrap(aerr).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
case ecr.ErrCodeLimitExceededException:
return kv.NewError(ecr.ErrCodeLimitExceededException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
default:
return kv.Wrap(aerr).With("stack", stack.Trace().TrimRuntime())
}
} else {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
}
return nil
}
func GetECRDefaultURL() (urlOut *url.URL, err kv.Error) {
sess, errGo := session.NewSession()
if errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
svc := ecr.New(sess)
input := &ecr.DescribeRepositoriesInput{MaxResults: aws.Int64(1)}
result, errGo := svc.DescribeRepositories(input)
if errGo != nil {
if aerr, ok := errGo.(awserr.Error); ok {
switch aerr.Code() {
case ecr.ErrCodeServerException:
return nil, kv.NewError(ecr.ErrCodeServerException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
case ecr.ErrCodeInvalidParameterException:
return nil, kv.NewError(ecr.ErrCodeInvalidParameterException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
case ecr.ErrCodeRepositoryNotFoundException:
return nil, kv.NewError(ecr.ErrCodeRepositoryNotFoundException).With("cause", aerr.Error()).With("stack", stack.Trace().TrimRuntime())
default:
return nil, kv.Wrap(aerr).With("stack", stack.Trace().TrimRuntime())
}
} else {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
}
for _, repo := range result.Repositories {
if repo.RepositoryUri == nil {
continue
}
urlOut, errGo = url.Parse("https://" + *repo.RepositoryUri)
if errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
return urlOut, nil
}
return nil, nil
}