/
cloudfront.go
105 lines (91 loc) · 2.71 KB
/
cloudfront.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
package aws
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/config"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/client"
"github.com/slack-go/slack"
)
// command to trigger/start jenkins jobs
type cloudFrontCommand struct {
awsCommand
service *cloudfront.CloudFront
cfg []config.AwsCfDistribution
}
// NewAwsCommand is a command to interact with aws resources
func newCloudFrontCommands(cfg []config.AwsCfDistribution, base awsCommand) bot.Command {
svc := cloudfront.New(base.session)
return &cloudFrontCommand{base, svc, cfg}
}
func (c *cloudFrontCommand) GetMatcher() matcher.Matcher {
return matcher.NewGroupMatcher(
matcher.NewTextMatcher("aws cf list", c.distributions),
matcher.NewRegexpMatcher(`aws cf clean (?P<DIST>[\w|\d]+) at (?P<PATH>[\w|\d|\W]+)`, c.clearCache),
)
}
func (c *cloudFrontCommand) distributions(match matcher.Result, message msg.Message) {
// show list
blocks := []slack.Block{}
for _, v := range c.cfg {
txtObj := slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("\"%s\": %s\n", v.ID, v.Name), false, false)
if err := txtObj.Validate(); err != nil {
fmt.Println(err.Error())
return
}
blocks = append(blocks, slack.NewSectionBlock(txtObj, nil, nil))
}
c.SendBlockMessage(message, blocks)
}
func (c *cloudFrontCommand) clearCache(match matcher.Result, message msg.Message) {
dist := match.GetString("DIST")
paths := []*string{}
for _, v := range strings.Split(match.GetString("PATH"), ",") {
_tempVal := v
paths = append(paths, &_tempVal)
}
quantity := int64(len(paths))
ref := strconv.FormatInt(time.Now().Unix(), 10)
invalidation := &cloudfront.CreateInvalidationInput{
DistributionId: &dist,
InvalidationBatch: &cloudfront.InvalidationBatch{
CallerReference: &ref,
Paths: &cloudfront.Paths{
Items: paths,
Quantity: &quantity,
},
},
}
output, err := c.service.CreateInvalidation(invalidation)
if nil != output.Invalidation {
fmt.Println(output.String())
c.SendMessage(message, fmt.Sprintf("cache %s cleared", dist))
} else {
fmt.Println(err.Error())
blocks := []slack.Block{
client.GetTextBlock(
"Oops! Command `" + message.GetText() + "` failed...",
),
}
c.SendBlockMessage(message, blocks)
return
}
}
func (c *cloudFrontCommand) GetHelp() []bot.Help {
examples := []string{
"aws cf clean cache",
}
help := make([]bot.Help, 0)
help = append(help, bot.Help{
Command: "aws cf <sub command>",
Description: "interact with aws cf resources",
Examples: examples,
Category: category,
})
return help
}