Created
December 7, 2017 20:07
-
-
Save niedbalski/f0482ffb84be5433daaeedd6d3fdb227 to your computer and use it in GitHub Desktop.
mr-provisioner find static ips
This file contains 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 | |
"""Show allocated static ips on mr-provisioner. | |
Usage of this script: | |
{0} provisioner-url token""" | |
from urllib.parse import urljoin | |
import requests | |
import sys | |
class AssignedIPList(object): | |
def __init__(self, host, token, api_version=1): | |
self.base_url = urljoin(host, "/api/v{0}/".format(api_version)) | |
self.default_headers = {'Authorization': token} | |
def do_request(self, url): | |
return requests.get(url, headers=self.default_headers).json() | |
@property | |
def machines(self): | |
return self.do_request(urljoin(self.base_url, "machine?show_all=true")) | |
def _machine_interfaces(self, machine_id): | |
return self.do_request(urljoin(self.base_url, | |
"machine/{0}/interface".format( | |
machine_id))) | |
@property | |
def static(self): | |
for machine in self.machines: | |
interfaces = self._machine_interfaces(machine.get('id')) | |
for interface in interfaces: | |
if interface.get('config_type_v4') == 'static': | |
yield interface.get('configured_ipv4') | |
def main(url, token): | |
ips = AssignedIPList(sys.argv[1], sys.argv[2]) | |
for ip in ips.static: | |
print(ip) | |
if __name__ == "__main__": | |
if len(sys.argv) <= 2: | |
print(__doc__.format(sys.argv[0])) | |
exit(-1) | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment