Skip to content

Commit

Permalink
Merge pull request #10 from sasamuku/refactor_support
Browse files Browse the repository at this point in the history
Refactor support
  • Loading branch information
sasamuku authored Feb 27, 2022
2 parents 751a721 + dda9cec commit 807d8c3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
41 changes: 26 additions & 15 deletions aws/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -25,39 +26,49 @@ 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(),
config.WithRegion("us-east-1"))
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
}
50 changes: 50 additions & 0 deletions aws/support_test.go
Original file line number Diff line number Diff line change
@@ -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{"[email protected]"}
displayId := "1234567890"
language := "ja"
status := "opened"
subject := "Test Subject"
submittedBy := "[email protected]"
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)
}
}
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion slack/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
`
Expand Down
4 changes: 2 additions & 2 deletions slack/notice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_Notice(t *testing.T) {
{
Subject: "Test",
Status: "opened",
SubmitteBy: "[email protected]",
SubmittedBy: "[email protected]",
TimeCreated: "2021-03-01T01:43:57.974Z",
Url: "https://console.aws.amazon.com/support/home#/case/?displayId=12345%26language=ja",
},
Expand All @@ -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)
}
})
}
Expand Down

0 comments on commit 807d8c3

Please sign in to comment.