/
c3mc-ec2create-describe-vpcs
executable file
·62 lines (47 loc) · 1.61 KB
/
c3mc-ec2create-describe-vpcs
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
#!/usr/bin/env /data/Software/mydan/python3/bin/python3
# -*- coding: utf-8 -*-
import sys
import json
import subprocess
import boto3
def describe_vpcs(access_id, access_key, region):
"""
查询vpc列表
"""
client = boto3.client(
"ec2",
aws_access_key_id=access_id,
aws_secret_access_key=access_key,
region_name=region
)
page_size = 100
result = []
response = client.describe_vpcs(MaxResults=page_size)
result.extend(response["Vpcs"])
marker = response["NextToken"] if "NextToken" in response else ""
while marker != "":
response = client.describe_vpcs(MaxResults=page_size, NextToken=marker)
result.extend(response["Vpcs"])
marker = response["NextToken"] if "NextToken" in response else ""
for item in result:
if "Tags" in item:
for tag in item["Tags"]:
if tag["Key"].lower() == "name":
item["Name"] = tag["Value"]
break
return sorted(result, key=lambda x: (x.get("Name", "")), reverse=False)
def main(params):
account = params["account"]
region = params["region"]
key_info = json.loads(
subprocess.getoutput(f"c3mc-get-account-info aws {account}")
)
vpc_list = describe_vpcs(
key_info["access_id"], key_info["access_key"], region)
for vpc in vpc_list:
print(json.dumps(vpc, default=str))
if __name__ == '__main__':
l = list(sys.stdin)
if not l or len(l) > 1:
raise type('WrongInputData', (Exception,), {})('数据格式不对, 需要一行json字符串"')
main(json.loads(l[0]))