-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjira.go
171 lines (141 loc) · 5.22 KB
/
jira.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/andygrunwald/go-jira"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
)
func tagsToJiraIssue(instance *ec2.Instance) string {
arr := []string{"||Tag || Value ||"}
for _, tag := range instance.Tags {
arr = append(arr, "| "+*tag.Key+" | "+*tag.Value+" |")
}
return strings.Join(arr, "\n")
}
// securityGroupsToJiraIssue returns jiira formatted table of security groups for an instance
func securityGroupsToJiraIssue(instance *ec2.Instance) string {
arr := []string{"||Name || Group Id ||"}
for _, g := range instance.SecurityGroups {
arr = append(arr, "| "+*g.GroupName+" | "+*g.GroupId+" |")
}
return strings.Join(arr, "\n")
}
func buildInstanceTicket(event ec2.InstanceStatusEvent, instance ec2.InstanceStatus, region string) *issue {
instanceData, err := getInstanceData(region, *instance.InstanceId)
if err != nil {
log.WithFields(log.Fields{
"instanceID": *instance.InstanceId,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": region,
}).Error(err)
}
// Get instance name by tag
instanceName := getInstanceTagValue(instanceData, "Name", "unknown")
// Get instance environment from tag
instanceEnvironment := getInstanceTagValue(instanceData, "Environment Type", "unknown")
// Trim instance description
trimedInstanceDescription := trimEventDescription(*event.Description, region, *instance.InstanceId)
// Build issue summary
summary := "Instance: " + instanceName + " - (" + *instanceData.InstanceId + ") " + trimedInstanceDescription
// Build issue description
desciption := "[Event Panel|https://" + region + ".console.aws.amazon.com/ec2/v2/home?region=" + region + "#Events]\n\n-------\n\n" +
"|| || ||\n" +
"| **Instance Name** | [" + instanceName + "|https://" + region + ".console.aws.amazon.com/ec2/v2/home?region=" + region + "#Instances:tag:Name=" + instanceName + "] |\n" +
"| **Instance** | [" + *instance.InstanceId + "|https://" + region + ".console.aws.amazon.com/ec2/v2/home?region=" + region + "#Instances:instanceId=" + *instance.InstanceId + "]\n" +
"| **Instance Type** | " + *instanceData.InstanceType + "|\n" +
"| Security Groups | {panel}\n" +
securityGroupsToJiraIssue(instanceData) +
"|{panel}|\n" +
"| Region | [" + region + "|https://console.aws.amazon.com/ec2/v2/home?region=" + region + "#/home]\n" +
"| State | " + *instance.InstanceState.Name + "|\n" +
"| Instance Status | " + *instance.InstanceStatus.Status + "|\n" +
"| Availability Zone | [" + *instance.AvailabilityZone + "|https://" + region + ".console.aws.amazon.com/ec2/v2/home?region=" + region + "#Instances:availabilityZone=" + *instance.AvailabilityZone + "]|\n" +
"| Tags | {panel}\n" +
tagsToJiraIssue(instanceData) +
"|{panel}|\n" +
"| Event Code | " + *event.Code + " | \n" +
"| Event Description | " + *event.Description + " | \n"
// "| Event Description | " + *event.NotBefore.Format("2006-01-02 15:04:05") + " | \n"
issue := &issue{
Description: desciption,
Summary: summary,
Tags: []string{"aws", "aws_maintenance", fmt.Sprintf("aws_" + instanceEnvironment)},
InstanceID: *instanceData.InstanceId,
awsRegion: region,
awsEnvironment: instanceEnvironment,
}
return issue
}
// Based on the environment it sets the priority
func getPriority(env string) *jira.Priority {
switch env {
case "production":
return &jira.Priority{
Name: "P2",
}
case "integration":
return &jira.Priority{
Name: "P3",
}
default:
return &jira.Priority{
Name: "P3",
}
}
}
func createJiraIssue(anIssue *issue) string {
requestLogger := log.WithFields(log.Fields{
"instanceID": anIssue.InstanceID,
"awsProfile": application.Config.AWS.Profile,
"awsRegion": anIssue.awsRegion,
})
base := application.Config.Jira.Protocol + "://" +
application.Config.Jira.Host +
application.Config.Jira.Path
tp := jira.CookieAuthTransport{
Username: application.Config.Jira.Username,
Password: application.Config.Jira.Password,
AuthURL: fmt.Sprintf("%s/rest/auth/1/session", base),
}
jiraClient, err := jira.NewClient(tp.Client(), base)
if err != nil {
panic(err)
}
i := jira.Issue{
Fields: &jira.IssueFields{
// Assignee: &jira.User{
// Name: "johndye",
// },
Description: anIssue.Description,
Type: jira.IssueType{
Name: application.Config.Jira.IssueType,
},
Project: jira.Project{
Key: application.Config.Jira.Project,
},
Priority: getPriority(anIssue.awsEnvironment),
Labels: anIssue.Tags,
Summary: anIssue.Summary,
},
}
issue, response, err := jiraClient.Issue.Create(&i)
// log.WithFields(log.Fields{
// "instanceID": anIssue.InstanceID,
// "awsProfile": application.Config.AWS.Profile,
// "awsRegion": anIssue.awsRegion,
// }).Debug(fmt.Printf("%+v", &i))
if err != nil {
requestLogger.Error("'" + err.Error() + "'")
os.Exit(2)
}
bodyBytes, _ := ioutil.ReadAll(response.Body)
if response.StatusCode != 201 {
requestLogger.Warnf("Response Code: [%d], Respomnse body: %s", response.StatusCode, string(bodyBytes))
} else {
requestLogger.Infof("Issue created: %s://%s/%s/browse/%s", application.Config.Jira.Protocol, application.Config.Jira.Host, application.Config.Jira.Path, issue.Key)
}
return issue.Key
}