Created
January 22, 2018 23:39
-
-
Save bortels/2bece938daa490d0e08c64399a68e994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Update pod inventory pages""" | |
import requests | |
from requests.auth import HTTPBasicAuth | |
import json | |
from pprint import pprint | |
import datetime | |
import gzip | |
import yaml | |
from bs4 import BeautifulSoup | |
def tagdict(data): | |
tags = {} | |
tagdata = data.get("Tags") | |
if tagdata: | |
for t in tagdata: | |
tags[t['Key']] = t['Value'] | |
return tags | |
with gzip.open("ec2.json.gz", 'rb') as f: | |
data = json.loads(f.read()) | |
PAGE = { | |
"10.141.10": "390398350", | |
"10.141.11": "391479391", | |
"10.131.10": "389415358", | |
"10.131.11": "391446854" | |
} | |
TABLES = {} | |
for i in data.keys(): | |
d = data[i] | |
ip = d.get('PrivateIpAddress') | |
if ip: | |
subnet = ip[:ip.rfind(".")] | |
pageid = PAGE.get(subnet) | |
tags = tagdict(d) | |
name = tags.get("Name") | |
if pageid: | |
if pageid not in TABLES: | |
TABLES[pageid] = [] | |
TABLES[pageid].append(f'<tr><td>{name}</td><td>{ip}</td></tr>') | |
for pageid in TABLES.keys(): | |
token = 'CONFLUENCE_TOKEN_GO_GET_ONE' | |
login = 'CONFLUENCE_EMAIL_LOGIN' | |
api = 'https://SOMEPLACE.atlassian.net/wiki/rest/api' | |
user = HTTPBasicAuth(login, token) | |
url = f'{api}/content/{pageid}' | |
geturl = f'{url}?expand=version,body.storage,ancestors' | |
response = requests.get(geturl, auth=user) | |
j = response.json() | |
version = j['version']['number'] | |
orig = j['body']['storage']['value'] | |
soup = BeautifulSoup(''.join(orig), 'html.parser') | |
oldsoup = str(soup) | |
TABLES[pageid].sort() | |
content = BeautifulSoup("<tbody><tr><th>Name</th><th>IP Address</th></tr>" + ''.join(TABLES[pageid]) + "</tbody>", 'html.parser') | |
soup.find('tbody').replaceWith(content) | |
newsoup = str(soup) | |
if oldsoup == newsoup: | |
print(f'No changes for {pageid}, skipping update') | |
continue | |
# update the wiki | |
newdata = { | |
'version': { | |
'number': version + 1, | |
}, | |
'id': j['id'], | |
'type': j['type'], | |
'title': j['title'], | |
'body': { | |
'storage': { | |
'representation': 'storage', | |
'value': str(soup) | |
} | |
} | |
} | |
r = requests.put(url, json=newdata, auth=user) | |
if r.status_code != 200: | |
print(r.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment