Skip to content

Instantly share code, notes, and snippets.

@marklap
Last active August 29, 2015 14:15
Show Gist options
  • Save marklap/ee2f01c71a36e266f803 to your computer and use it in GitHub Desktop.
Save marklap/ee2f01c71a36e266f803 to your computer and use it in GitHub Desktop.
################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015 Mark LaPerriere
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
import os
import sys
import json
import requests
from pprint import pprint
do_token = 'secret'
do_service = 'https://api.digitalocean.com'
do_base_url = 'v2'
target_domain = 'your.domain.com'
target_record_name = 'record-name'
external_ip_service = 'http://httpbin.org/ip'
_session = None
def session():
global _session
if _session is None:
_session = requests.Session()
_session.headers.update(
{
'Authorization': 'Bearer {0}'.format(do_token),
'Content-Type': 'application/json',
}
)
return _session
def url(endpoint):
return '{0}/{1}/{2}'.format(do_service, do_base_url, endpoint)
def get_record_id(domain, record_name):
http = session()
resp = http.get(url('domains/{0}/records'.format(domain)))
resp.raise_for_status()
record_id = None
for record in resp.json()['domain_records']:
if record['name'] == record_name:
record_id = record['id']
return record_id
def update_record(domain, record_id, ip):
http = session()
resp = http.put(url('domains/{0}/records/{1}'.format(domain, record_id)), data=json.dumps({'data': ip}))
resp.raise_for_status()
return resp
def get_external_ip():
http = session()
resp = http.get(external_ip_service)
resp.raise_for_status()
return resp.json()['origin']
def main():
external_ip = get_external_ip()
record_id = get_record_id(target_domain, target_record_name)
resp = update_record(target_domain, record_id, external_ip)
pprint(resp.json())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment