-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.txt
138 lines (124 loc) · 4.48 KB
/
test.txt
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
data "aws_iam_policy_document" "lambda-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "lambda_policy" {
statement {
sid = "AllowActions"
actions = [
"ec2:CreateNetworkInterface",
"ec2:DetachNetworkInterface",
"autoscaling:CompleteLifecycleAction",
"ec2:ModifyNetworkInterfaceAttribute",
"ec2:DeleteNetworkInterface",
"autoscaling:PutLifecycleHook",
"autoscaling:DetachLoadBalancerTargetGroups",
"ec2:AttachNetworkInterface",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"autoscaling:AttachLoadBalancerTargetGroups",
"ec2:DeleteTags",
"ec2:CreateTags",
]
resources = [
"arn:aws:autoscaling:*:xxx:autoScalingGroup:*:autoScalingGroupName/*",
"arn:aws:ec2:*:xxx:instance/*",
"arn:aws:ec2:*:xxx:network-interface/*",
"arn:aws:ec2:*:xxx:subnet/*",
"arn:aws:ec2:*:xxx:security-group/*"
]
}
statement {
sid = "AllowActionsLogs"
actions = [
"logs:*"
]
resources = [
"arn:aws:logs:*:*:*"
]
}
statement {
sid = "DescribeActions"
actions = [
"ec2:DescribeInstances",
"ec2:DescribeNetworkInterfaces",
"ec2:DescribeAvailabilityZones",
"ec2:DescribeNetworkInterfaceAttribute",
"ec2:DescribeSubnets"
]
resources = [
"*"
]
}
}
resource "aws_iam_policy" "lambda_policy" {
name = "jenkins_lc_policy"
policy = data.aws_iam_policy_document.lambda_policy.json
}
resource "aws_iam_role" "lambda_role" {
name = "lambda_role"
path = "/system/"
depends_on = [
aws_iam_policy.lambda_policy,
]
assume_role_policy = data.aws_iam_policy_document.lambda-assume-role-policy.json
managed_policy_arns = [aws_iam_policy.lambda_policy.arn]
}
get_mgmt_sg_id = ec2.describe_security_groups(
Filters=[
{
'Name': 'tag:Name',
'Values': [
'lambda-test',
]
},
]
)
mgmt_sg_id = get_mgmt_sg_id['SecurityGroups'][0]['GroupId']
logger.info('SECURITY GROUP ID TO ATTACH TO THE ENI: {}'.format(mgmt_sg_id))
import requests
import xml.etree.ElementTree as ET
URL = "https://xxx/api/?type=op&cmd=<show><devices><summary></summary></devices></show>&key=xxx"
r = requests.get(URL,verify=False)
root = ET.fromstring(r.content)
# for child in root.iter('*'):
# child.tag = child.text.encode('utf8')
# print(child.text)
for child in root.iter('*'):
print(child.tag, child.attrib)
# xmlDict = {}
# for sitemap in root:
# children = sitemap.getchildren()
# xmlDict[children[0].text] = children[1].text
# print (xmlDict)
def send_sns_notification(event):
try:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
asg_name=event['detail']['AutoScalingGroupName']
event_type = event['detail']['LifecycleTransition']
instance_id = event['detail']['EC2InstanceId']
if event['detail']['LifecycleTransition'] == "autoscaling:EC2_INSTANCE_LAUNCHING":
subject = "AutoScaling Scale-Out Event Triggered on {} auotscaling Group".format(asg_name)
body = "Palo Alto Instance {} has been provisioned and added to the GWLB Target Group at {}".format(instance_id,current_time)
publish_message = sns.publish(
TargetArn = "arn:aws:sns:us-east-1:552777967030:asg-test",
Message=json.dumps({'default': body}),
Subject=subject,
MessageStructure='json'
)
elif event['detail']['LifecycleTransition'] == "autoscaling:EC2_INSTANCE_TERMINATING":
subject = "AutoScaling Scale-In Event Triggered on {} auotscaling Group".format(asg_name)
body = "Palo Alto Instance with ID: {} has been deleted and de-registered from the GWLB Target Group at {}".format(instance_id,current_time)
publish_message = sns.publish(
TargetArn = "arn:aws:sns:us-east-1:552777967030:asg-test",
Message=json.dumps({'default': body}),
Subject=subject,
MessageStructure='json'
)
except Exception as e:
raise Exception(e)