Skip to content

Instantly share code, notes, and snippets.

@frodoslaw
Forked from andymotta/update_confluence.py
Created April 25, 2019 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frodoslaw/7f668fdb847041528f834ba3cffeec78 to your computer and use it in GitHub Desktop.
Save frodoslaw/7f668fdb847041528f834ba3cffeec78 to your computer and use it in GitHub Desktop.
Update/create a page containing a table w/ Confluence REST API
#!/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