Last active
October 31, 2018 18:25
-
-
Save jbd/8a5f691e75509dc5397243a105dc61fa 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
#!/usr/bin/env python | |
from __future__ import print_function | |
import os | |
import sys | |
import stat | |
import argparse | |
import ConfigParser | |
import sqlite3 | |
import urllib3 | |
import isi_sdk_8_1_0 as isi_sdk | |
from isi_sdk_8_1_0.rest import ApiException | |
ERR_BAD_CMDLINE = 100 | |
ERR_BAD_CRED_FILE = 101 | |
ERR_PARSE_CRED_FILE = 102 | |
ERR_BAD_CRED_PERMS = 103 | |
ERR_QUOTA_API_ERR = 104 | |
def print_error(*args, **kwargs): | |
print(*args, file=sys.stderr, **kwargs) | |
def get_isilon_api_client(options): | |
# Configure HTTP basic authorization: basicAuth | |
configuration = isi_sdk.Configuration() | |
# configure username and password | |
configuration.username = os.getenv("ISI_ONEFS_USER") or options.credentials['user'] | |
configuration.password = os.getenv("ISI_ONEFS_PASS") or options.credentials['password'] | |
configuration.host = os.getenv("ISI_ONEFS_HOST") or options.credentials['host'] | |
configuration.verify_ssl = os.getenv("ISI_ONEFS_VERIFYSSL") or options.credentials['verify_ssl'] | |
if not configuration.verify_ssl: | |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
# create an instance of the API class | |
api_client = isi_sdk.ApiClient(configuration) | |
return api_client | |
def read_credentials(filename): | |
""" | |
Read credentials from an ini-formatted file. | |
Something like: | |
[credentials] | |
host = https://yourhost:8080 | |
user = your_user | |
password = your_password | |
verify_ssl = False | |
""" | |
if not os.access(filename, os.R_OK): | |
print_error("Cannot read %s" % filename) | |
sys.exit(ERR_BAD_CRED_FILE) | |
# this file contains sensible credential and should not be world readable | |
if bool(os.stat(filename).st_mode & stat.S_IROTH): | |
print_error("%s is world readable. Please correct permissions." % filename) | |
sys.exit(ERR_BAD_CRED_PERMS) | |
# sane default, but should be overriden in the credential file | |
config = ConfigParser.RawConfigParser({'verify_ssl': True}) | |
config.read(filename) | |
creds = dict() | |
creds['host'] = config.get("credentials", "host") | |
creds['user'] = config.get("credentials", "user") | |
creds['password'] = config.get("credentials", "password") | |
creds['verify_ssl'] = config.getboolean("credentials", "verify_ssl") | |
return creds | |
def parse_options(): | |
""" | |
command line parsing | |
""" | |
parser = argparse.ArgumentParser(description="Build sqlite3 database from Isilon Quotas") | |
parser.add_argument("credentials", | |
help="credential file in ini format (You can also" \ | |
"provide ISI_ONEFS_USER, ISI_ONEFS_PASS" \ | |
"and ISI_ONEFS_HOST environment variables)") | |
parser.add_argument("path") | |
options = parser.parse_args() | |
try: | |
options.credentials = read_credentials(options.credentials) | |
except ConfigParser.Error, err: | |
print_error("Error reading %s: %s" % (options.credentials, err)) | |
sys.exit(ERR_PARSE_CRED_FILE) | |
return options | |
def main(): | |
""" | |
Let's parse a bunch of xml report file and put some data inside an sqlite database ! | |
""" | |
options = parse_options() | |
api_instance = get_isilon_api_client(options) | |
ns_api = isi_sdk.NamespaceApi(api_instance) | |
try: | |
api_response = ns_api.get_directory_contents(options.path) # does not work | |
print(api_response) | |
api_response = ns_api.get_directory_contents(options.path, limit=8192) # does not work | |
print(api_response) | |
api_response = ns_api.get_directory_contents(options.path, limit=1) # work | |
print(api_response) | |
except ApiException as e: | |
print("Exception when calling NamespaceApi->create_access_point: %s\n" % e) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment