diff --git a/aws/support.go b/aws/support.go index 06cd66e..c97b5af 100644 --- a/aws/support.go +++ b/aws/support.go @@ -6,12 +6,13 @@ import ( "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/support" + "github.com/aws/aws-sdk-go-v2/service/support/types" ) type Case struct { Subject string `json:"subject"` Status string `json:"status"` - SubmitteBy string `json:"submitteBy"` + SubmittedBy string `json:"SubmittedBy"` TimeCreated string `json:"timeCreated"` Url string `json:"url"` } @@ -25,7 +26,15 @@ func NewDescribeCasesInput(aftertime, beforetime, language string, include bool) } } -func GetCases(input *support.DescribeCasesInput) []*Case { +func GetCaseList(input *support.DescribeCasesInput) []*Case { + client := loadConfig() + output := outputCases(client, input) + caseDetails := extractCaseDetails(output) + caseList := makeCaseList(caseDetails) + return caseList +} + +func loadConfig() *support.Client { // Load the Shared AWS Configuration (~/.aws/config) cfg, err := config.LoadDefaultConfig( context.TODO(), @@ -33,31 +42,33 @@ func GetCases(input *support.DescribeCasesInput) []*Case { if err != nil { log.Fatal(err) } + return support.NewFromConfig(cfg) +} - client := support.NewFromConfig(cfg) - - output, err := client.DescribeCases(context.TODO(), input) +func outputCases(c *support.Client, i *support.DescribeCasesInput) *support.DescribeCasesOutput { + output, err := c.DescribeCases(context.TODO(), i) if err != nil { log.Fatal(err) } - - cases := ArrangeCases(output) - return cases + return output } -func ArrangeCases(output *support.DescribeCasesOutput) []*Case { - var cases []*Case +func extractCaseDetails(o *support.DescribeCasesOutput) []types.CaseDetails { + caseDetails := o.Cases + return caseDetails +} - caseDetails := output.Cases - for _, c := range caseDetails { +func makeCaseList(cd []types.CaseDetails) []*Case { + var caseList []*Case + for _, c := range cd { eachCase := Case{ Subject: *c.Subject, Status: *c.Status, - SubmitteBy: *c.SubmittedBy, + SubmittedBy: *c.SubmittedBy, TimeCreated: *c.TimeCreated, Url: "https://console.aws.amazon.com/support/home#/case/?displayId=" + *c.DisplayId + "%26language=" + *c.Language, } - cases = append(cases, &eachCase) + caseList = append(caseList, &eachCase) } - return cases + return caseList } diff --git a/aws/support_test.go b/aws/support_test.go new file mode 100644 index 0000000..ccd21df --- /dev/null +++ b/aws/support_test.go @@ -0,0 +1,50 @@ +package aws + +import ( + "testing" + + "github.com/aws/aws-sdk-go-v2/service/support/types" +) + +func Test_Support(t *testing.T) { + caseId := "case-12345678910-2013-c4c1d2bf33c5cf47" + ccEmailAddresses := []string{"cc@example.com"} + displayId := "1234567890" + language := "ja" + status := "opened" + subject := "Test Subject" + submittedBy := "test@example.com" + timeCreated := "2021-12-01T12:00:00.000Z" + url := "https://console.aws.amazon.com/support/home#/case/?displayId=1234567890%26language=ja" + + caseDetails := []types.CaseDetails{ + { + CaseId: &caseId, + CcEmailAddresses: ccEmailAddresses, + DisplayId: &displayId, + Language: &language, + Status: &status, + Subject: &subject, + SubmittedBy: &submittedBy, + TimeCreated: &timeCreated, + }, + } + caseList := makeCaseList(caseDetails) + for _, c := range caseList { + if c.Subject != subject { + t.Fatalf("Fail: got = %v, want = %v", c.Subject, subject) + } + if c.Status != status { + t.Fatalf("Fail: got = %v, want = %v", c.Status, status) + } + if c.SubmittedBy != submittedBy { + t.Fatalf("Fail: got = %v, want = %v", c.SubmittedBy, submittedBy) + } + if c.TimeCreated != timeCreated { + t.Fatalf("Fail: got = %v, want = %v", c.TimeCreated, timeCreated) + } + if c.Url != url { + t.Fatalf("Fail: got = %v, want = %v", c.Url, url) + } + } +} diff --git a/main.go b/main.go index 6bc998b..b9daf3d 100644 --- a/main.go +++ b/main.go @@ -25,9 +25,9 @@ func main() { } input := aws.NewDescribeCasesInput(aftertime, beforetime, language, include_resolved_cases_b) - cases := aws.GetCases(input) + caseList := aws.GetCaseList(input) - payload := slack.NewPayload("AWS Support Case Notice", slack.ConvertToNoticeFormat(cases)) + payload := slack.NewPayload("AWS Support Case Notice", slack.ConvertToNoticeFormat(caseList)) notice := slack.NewSlackNotice(webhookUrl, payload) notice.Run() } diff --git a/slack/notice.go b/slack/notice.go index 48108a6..d859b8d 100644 --- a/slack/notice.go +++ b/slack/notice.go @@ -24,7 +24,7 @@ type Payload struct { func ConvertToNoticeFormat(cases []*aws.Case) string { text := `Subject: {{ .Subject }} Status: {{ .Status }} -SubmitteBy: {{ .SubmitteBy }} +SubmittedBy: {{ .SubmittedBy }} TimeCreated: {{ .TimeCreated }} Url: {{ .Url }} ` diff --git a/slack/notice_test.go b/slack/notice_test.go index 301f000..8c52100 100644 --- a/slack/notice_test.go +++ b/slack/notice_test.go @@ -33,7 +33,7 @@ func Test_Notice(t *testing.T) { { Subject: "Test", Status: "opened", - SubmitteBy: "user@example.com", + SubmittedBy: "user@example.com", TimeCreated: "2021-03-01T01:43:57.974Z", Url: "https://console.aws.amazon.com/support/home#/case/?displayId=12345%26language=ja", }, @@ -53,7 +53,7 @@ func Test_Notice(t *testing.T) { payload := NewPayload("AWS Support Case Notice", ConvertToNoticeFormat(tt.cases)) notice := NewSlackNotice(tt.webhookUrl, payload) if statusCode := notice.Run(); statusCode != tt.wants.statusCode { - t.Fatalf("run() status: got = %v, want = %v", statusCode, tt.wants.statusCode) + t.Fatalf("Fail: got = %v, want = %v", statusCode, tt.wants.statusCode) } }) }