Created
April 10, 2019 19:42
-
-
Save andymotta/7ce6b90e9daa3c76f206138b43b530c1 to your computer and use it in GitHub Desktop.
Update/create a page containing a table w/ Confluence REST API
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 python | |
''' | |
Update/create a page containing a table w/ Confluence REST API | |
''' | |
import requests | |
import json | |
# Get api credentials from local config file | |
from configparser import ConfigParser | |
credentials = ConfigParser() | |
credentials.read('../.credentials') | |
user = credentials.get('confluence','user') | |
password = credentials.get('confluence','password') | |
auth = (user, password) # login and API token/password | |
# Requisite JSON headers for API | |
headers = { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
} | |
natip="10.10.0.2" | |
environment="dev" | |
table = { | |
"Environment URL" : "https://" + environment + ".myorg.org", | |
"Jumpbox SSH" : "ssh -p 2222 user@" + natip, | |
"Jumpbox VNC" : "vnc://user@" + natip + ":5901" | |
} | |
with open('confluence_table.html', 'w') as f1: | |
html = """<html> <h3>This page is generated via bootstrap automation</h3><table border="0">""" | |
for x in table: | |
html += "<tr>" | |
html += "<td>"+x+"</td>" | |
if str(table[x]).startswith(('http','vnc')): | |
html += "<td><a href=\""+str(table[x])+"\">"+str(table[x])+"</a></td>" | |
else: | |
html += "<td>"+str(table[x])+"</td>" | |
html += "</tr>" | |
html += "</table></html>" | |
f1.write(''.join(html)) | |
params = {'spaceKey': 'SPACE', 'title': environment} | |
result = requests.get("https://confluence.myorg.org/rest/api/content/", headers=headers, auth=auth, params=params) | |
json_output = json.loads(result.text) | |
if json_output['results']: | |
pid = json_output['results'][0]['id'] | |
print "Updating: https://confluence.myorg.org/display/SPACE/" + environment | |
else: | |
data = { | |
'title': environment, | |
'type': 'page', | |
'space': {'key': 'SPACE'}, | |
'ancestors': [{'id': '00000000'}] # ID of the parent page | |
} | |
result = requests.post("https://confluence.myorg.org/rest/api/content/", headers=headers, auth=auth, json=data) | |
json_output = json.loads(result.text) | |
pid = json_output['id'] | |
print "Creating: https://confluence.myorg.org/display/SPACE/" + environment | |
result = requests.get("https://confluence.myorg.org/rest/api/content/"+pid, headers=headers, auth=auth) | |
json_output = json.loads(result.text) | |
version = json_output['version']['number'] | |
data = { | |
'type': 'page', | |
'title': environment, | |
'body': { | |
'storage': { | |
'value': html, | |
'representation': 'storage', | |
} | |
}, | |
'version': { | |
'number': version + 1, | |
} | |
} | |
result = requests.put("https://confluence.myorg.org/rest/api/content/"+pid, headers=headers, auth=auth, json=data) | |
# Mac-ish | |
import subprocess | |
subprocess.call(["/usr/bin/open", "confluence_table.html"]) | |
subprocess.call(["/usr/bin/open", "https://confluence.myorg.org/display/SPACE/"+environment]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems support for python2 is not available. Received below error when running the program
atlassian/bamboo.py", line 73
**kwargs,