Created
June 12, 2019 20:41
-
-
Save JorgeFrancoIbanez/d0f286279c14392f2ca5829b9dc1abcf to your computer and use it in GitHub Desktop.
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 argparse | |
import requests #needs to be installed | |
import json | |
parser = argparse.ArgumentParser(description='Get data of your project from your object storage service.') | |
parser.add_argument('--user', type=str, required=True, help='Portal customer account') | |
parser.add_argument('--password', type=str, required=True, help='Portal customer password') | |
parser.add_argument('--project', type=str, required=True, help='User project ID. \n') | |
parser.add_argument('--endpoint', type=str, required=True, help='Endpoint where the request are going to be done.\n' | |
'Requires protocol and port. \n' | |
'example: https://enpoint.com:5000') | |
parser.add_argument('--port', type=str, required=True, help='Keystone authentication Port.\n' | |
'Requires protocol and port. \n' | |
'example: https://enpoint.com:5000') | |
args = parser.parse_args() | |
auth_payload = { | |
"auth": { | |
"identity": { | |
"methods": [ | |
"password" | |
], | |
"password": { | |
"user": { | |
"name": args.user, | |
"password": args.password | |
} | |
} | |
}, | |
"scope": { | |
"project": { | |
"id": args.project | |
} | |
} | |
} | |
} | |
def get_auth(): | |
token_url = args.endpoint+':'+args.port+'/v3/auth/tokens' | |
payload = json.dumps(auth_payload) | |
headers = {"Accept": "application/json", "Content-Type": "application/json"} | |
res = requests.post(token_url, data=payload, headers=headers) | |
return res | |
def get_swift_auth(authentication): | |
url = args.endpoint+':'+'6780/swift/v1/' | |
headers = {"X-Auth-Token": authentication["token"]["id"]} | |
return url, headers | |
def get_containers(authentication): | |
url, headers = get_swift_auth(authentication) | |
res = requests.get(url+args.project+"?format=json", headers=headers) | |
return res | |
def print_data(total_objects, total_bytes): | |
print("Total of GB used in project " + args.project) | |
print(round(int(total_bytes) / 1073741824), | |
"GB of ", int(107374182400 / 1048576), "GB (", | |
round(100.00 - (float(total_bytes) / 1073741824 / int(107374182400 / 1048576)) * 100, 2), "% Free)") | |
print(round(int(total_objects)), | |
"objects of ", 100000, "GB (", | |
round(100.00 - float(total_objects) / 100000 * 100, 2), "% Free)") | |
def main(): | |
authentication_token=get_auth().json() | |
stats=get_containers(authentication_token).headers | |
print(stats) | |
total_bytes=stats['x-account-bytes-used'] | |
total_objects=stats['x-account-object-count'] | |
print_data(total_objects, total_bytes) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment