Created
April 11, 2023 01:18
-
-
Save rocapp/39048d95d9fc7c29f07672ff22854dc4 to your computer and use it in GitHub Desktop.
Tiny CLI for IDrive E2
This file contains hidden or 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 | |
import requests | |
import json | |
import argparse | |
import configparser | |
from pathlib import Path | |
class AWSConfig: | |
def __init__(self): | |
self._obj = {} | |
self._creds, self._conf = None, None | |
self.parse_aws_config() | |
def parse_aws_config(self): | |
confpath = Path("~/.aws/config").expanduser() | |
credpath = Path("~/.aws/credentials").expanduser() | |
cparser = configparser.ConfigParser() | |
cparser.read_file(confpath.open()) | |
self._conf = cparser | |
rparser = configparser.ConfigParser() | |
rparser.read_file(credpath.open()) | |
self._creds = rparser | |
return self._conf, self._creds | |
def get_creds(self, profile=None, key_type=None): | |
if profile is None: | |
return self._creds | |
creds = self._creds[profile] | |
return creds if key_type is None else creds[key_type] | |
def get_conf(self, profile=None, key=None): | |
if profile is None: | |
return self._conf | |
conf = self._conf[profile] | |
return conf if key is None else conf[key] | |
def get_endpoints(self, profile=None): | |
endpoints = {profile: self._conf.get(profile, "s3")["endpoint_url"] for profile in self.profiles} | |
return endpoints if profile is None else endpoints[profile] | |
@property | |
def endpoints(self): | |
return self.get_endpoints() | |
@property | |
def profiles(self): | |
return [profile for profile, _ in self._creds.items() if profile != "DEFAULT"] | |
#: Init aws config | |
awsconf = AWSConfig() | |
def list_func(args): | |
print("Profiles:\n---------", "".join([f"\n - {profile}" for profile in awsconf.profiles]), "\n") | |
def print_func(args): | |
access_key = awsconf._creds.get(args.profile, "aws_access_key_id") | |
data = {"access_key": access_key} | |
e2_api_endpoint = "https://api.idrivee2.com/api/service/get_region_end_point" | |
resp = requests.post(e2_api_endpoint, data=data) | |
jdata = resp.json() | |
if args.with_key: | |
jdata["aws_access_key_id"] = access_key | |
if not args.url_only: | |
print(jdata) | |
else: | |
print(jdata["domain_name"]) | |
return resp | |
def main(): | |
parser = argparse.ArgumentParser("get-e2-info", description="Get information about IDrive E2, e.g. the endpoint URL for the specified region (corresponding access_key)") | |
subparsers = parser.add_subparsers(title="subcommands", required=True) | |
list_cmd = subparsers.add_parser("list-profiles", help="list help") | |
list_cmd.set_defaults(func=list_func) | |
print_cmd = subparsers.add_parser("print-profile", aliases=["print", ], help="print-profile help") | |
print_cmd.add_argument("profile", action="store", type=str, choices=awsconf.profiles) | |
print_cmd.add_argument("--url-only", action="store_true", default=False) | |
print_cmd.add_argument("--with-key", action="store_true", default=False) | |
print_cmd.set_defaults(func=print_func) | |
args = parser.parse_args() | |
return args.func(args) | |
if __name__ == "__main__": | |
resp = main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment