Created
July 15, 2015 02:01
-
-
Save caiush/b6fafeffeb6f927487bc to your computer and use it in GitHub Desktop.
Basic tool to grab list of all floating IPs of all tenancies a user has access too
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
import os | |
import requests | |
import sys | |
import json | |
import datetime | |
import logging | |
#logging.basicConfig(level=logging.DEBUG) | |
def get_config(): | |
r = dict( username = os.environ.get("OS_USERNAME"), | |
password = os.environ.get("OS_PASSWORD"), | |
tenant_name = os.environ.get("OS_TENANT_NAME"), | |
auth_url = os.environ.get("OS_AUTH_URL"), | |
debug=True ) | |
cacert = os.environ.get("OS_CACERT") | |
if cacert: | |
r["cacert"] = cacert | |
return r | |
def get_user_ips(user): | |
user_ips = [] | |
r = get_config() | |
payload = dict( auth = dict( passwordCredentials = dict(username = r["username"], password=r["password"]), tenantName=r["tenant_name"])) | |
headers = {"Content-Type" : "application/json"} | |
kr = requests.post(r["auth_url"] +"tokens", data=json.dumps(payload), verify=False, headers=headers) | |
if kr.status_code!=200: | |
raise Exception("Failed to auth") | |
response = json.loads( kr.text ) | |
# get token, tenant id and directory of services. | |
token_id = response["access"]["token"]["id"] | |
token_expires = datetime.datetime.strptime(response["access"]["token"]["expires"], # datetime object, parses 2015-07-14T18:49:12Z | |
"%Y-%m-%dT%H:%M:%SZ") | |
my_tenant_id = response["access"]["token"]["tenant"]["id"] | |
headers = {"Content-Type" : "application/json"} | |
ident_service = [service for service in response["access"]["serviceCatalog"] if service["type"]== "identity"][0] | |
compute_service = [service for service in response["access"]["serviceCatalog"] if service["type"]== "compute"][0] | |
admin_url= [endpoint for endpoint in ident_service["endpoints"] if "adminURL" in endpoint][0]["adminURL"] | |
compute_url = [endpoint for endpoint in compute_service["endpoints"] if "publicURL" in endpoint][0]["publicURL"] | |
# grab a list of ALL tenants | |
headers["X-Auth-Token"] = token_id | |
kr = requests.get(admin_url +"/tenants", verify=False, headers=headers) | |
response = json.loads(kr.text) | |
for tenant in response["tenants"]: | |
# for each tenant check to see if user is a memeber? | |
tenant_id = tenant["id"] | |
kr = requests.get(admin_url +"/tenants/%s/users/%s/roles" %(tenant_id, user), verify=False, headers=headers) | |
tenant_json = json.loads(kr.text) | |
if tenant_json["roles"]: # looks like the user has a role here! | |
# get a list of all VMs | |
kr = requests.get(compute_url.replace(my_tenant_id, tenant_id) + "/servers/detail", verify=False, headers=headers) | |
servers_json = json.loads(kr.text) | |
for server in servers_json["servers"]: | |
# for each network look for first fixed. | |
for network in server["addresses"]["fixed_0"]: | |
# grab the floating ip, if available | |
if network["OS-EXT-IPS:type"] == "floating": | |
user_ips.append( network["addr"] ) | |
return user_ips | |
if __name__ == "__main__": | |
print get_user_ips("563954a1c79744ceaae9e65e49237a1c") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment