-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefreshTG.py
More file actions
71 lines (55 loc) · 2.08 KB
/
Copy pathrefreshTG.py
File metadata and controls
71 lines (55 loc) · 2.08 KB
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
#!/usr/bin/env python
import json
import gspread
import boto3
import validators
import csv
import re
from googleapiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
spreadsheet_url = input("Enter the google spreadsheet ID (you can get it from the sheet URL or paste the URL here, we will extract it out): ")
#Validate the spreadsheet URL
url_validation_result=validators.url(spreadsheet_url)
url_string=str(spreadsheet_url)
if (url_validation_result):
spreadsheet_id= re.findall(r"/spreadsheets/d/([a-zA-Z0-9-_]+)", url_string)[0]
print(spreadsheet_id)
else:
print('Please enter a valid URL! It should be of the following format: https://docs.google.com/spreadsheets/d/<id>/edit#gid=0 ')
quit()
spreadsheet_name = input("Enter the spreadsheet name:")
#Initialize the elbv2 method
elbv2 = boto3.client('elbv2')
#Fetch target group info from AWS
response = elbv2.describe_target_groups(
TargetGroupArns=[]
)
json_key = json.load(open('credentials.json'))
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
# get email and key from creds
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
# authenticate with Google
file = gspread.authorize(credentials)
#Initialize the list
TGList=[]
for TG in response["TargetGroups"]:
TGList.append([TG['TargetGroupName'],TG['Port']])
TGList.sort(key=lambda elem:elem[1])
service = build('sheets', 'v4', credentials=credentials)
#Clear all the previous values from the sheet
rangeRemoval = '{0}!A2:Z'.format(spreadsheet_name)
body = {}
resultClear = service.spreadsheets( ).values( ).clear( spreadsheetId=spreadsheet_id, range=rangeRemoval,
body=body ).execute( )
# #Add the updated values
rangeAddition = "A1:B1"
resource = {
"majorDimension": "ROWS",
"values": TGList
}
service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id,
range=rangeAddition,
body=resource,
valueInputOption="USER_ENTERED"
).execute()