def run_command(token, proxy, image_id, params, record): """ run command and process result """ method = 'POST' url = IMAGES + str(image_id) + '/actions' result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: headers = ['Fields', 'Values'] table = [['Id', result['action']['id']], ['Status', result['action']['status']], ['Type', result['action']['type']], ['Started at', result['action']['started_at']], ['Completed at', result['action']['completed_at']], ['Resource Id', result['action']['resource_id']], ['Resource Type', result['action']['resource_type']], ['Region', result['action']['region']]] data = {'headers': headers, 'table_data': table} cmd = 'Command: docli image-actions -a %d -i %d' % ( image_id, result['action']['id']) print_table(tablefmt, data, record) click.echo() click.echo( 'To get status update of above action execute following command.') click.echo(cmd)
def region_size(ctx, region, size, token, tablefmt, proxy): """ Get list of regions and droplet sizes available with Digital Ocean """ if (not ctx.params['region'] and not ctx.params['size']): return click.echo(ctx.get_help()) option_list = ['region', 'size'] if validate(ctx.params, option_list): if region: method = 'GET' url = REGIONS result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'region list' for dic in result['regions']: headers = ['Fields', 'Values'] table = [['Name', dic['name']], ['Slug', dic['slug']], ['Available', dic['available']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total regions: %d' % (result['meta']['total']) click.echo(total) if size: method = 'GET' url = SIZES result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'size list' for dic in result['sizes']: headers = ['Fields', 'Values'] table = [['Slug', dic['slug']], ['Memory', dic['memory']], ['VCPUS', dic['vcpus']], ['Disk', dic['disk']], ['Transfer', dic['transfer']], ['Price Monthly', dic['price_monthly']], ['Price Hourly', dic['price_hourly']], ['Available', dic['available']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total sizes: %d' % (result['meta']['total']) click.echo(total)
def image_actions(ctx, transfer, region, convert, action, action_id, token, tablefmt, proxy): """ Image actions are commands that can be given to a DigitalOcean image. """ if (not ctx.params['transfer'] and not ctx.params['region'] and not ctx.params['convert'] and not ctx.params['action'] and not ctx.params['action_id']): return click.echo(ctx.get_help()) option_list = ['transfer', 'convert', 'action'] if validate(ctx.params, option_list): if transfer: params = {"type": "transfer", "region": region} record = 'image transfer' return run_command(token, proxy, transfer, params, record) if convert: params = {"type": "convert"} record = 'image convert' return run_command(token, proxy, convert, params, record) if action: method = 'GET' url = IMAGES + str(action) + '/' + str(action_id) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'image action' headers = ['Fields', 'Values'] dic = result['action'] table = [['Id', dic['id']], ['Status', dic['status']], ['Type', click.style(dic['type'], fg='blue')], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource id', dic['resource_id']], ['Resource type', dic['resource_type']], ['Region', dic['region']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record)
def region_size(ctx, region, size, token, tablefmt, proxy): """ Get list of regions and droplet sizes available with Digital Ocean """ if (not ctx.params['region'] and not ctx.params['size']): return click.echo(ctx.get_help()) option_list = ['region', 'size'] if validate(ctx.params, option_list): if region: method = 'GET' url = REGIONS result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'region list' for dic in result['regions']: headers = ['Fields', 'Values'] table = [['Name', dic['name']], ['Slug', dic['slug']], ['Available', dic['available']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total regions: %d' % (result['meta']['total']) click.echo(total) if size: method = 'GET' url = SIZES result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'size list' for dic in result['sizes']: headers = ['Fields', 'Values'] table = [['Slug', dic['slug']], ['Memory', dic['memory']], ['VCPUS', dic['vcpus']], ['Disk', dic['disk']], ['Transfer', dic['transfer']], ['Price Monthly', dic['price_monthly']], ['Price Hourly', dic['price_hourly']], ['Available', dic['available']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total sizes: %d' % (result['meta']['total']) click.echo(total)
def account(token, tablefmt, proxy): """ get digital ocean account info """ method = 'GET' url = ACCOUNT_INFO result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'account' headers = ['Fields', 'Values'] table = [] for key in result['account'].keys(): table.append([key, result['account'][key]]) data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record)
def image_by_id_slug(token, proxy, record, url, tablefmt): """ get images by id or slug """ result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' % (result['error_message'])) else: headers = ['Fields', 'Values'] table = [['Id', result['image']['id']], ['Name', result['image']['name']], ['Distribution', result['image']['distribution']], ['Slug', result['image']['slug']], ['Public', result['image']['public']], ['Created at', result['image']['created_at']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record)
def image_actions(ctx, transfer, region, convert, action, action_id, token, tablefmt, proxy): """ Image actions are commands that can be given to a DigitalOcean image. """ if (not ctx.params['transfer'] and not ctx.params['region'] and not ctx.params['convert'] and not ctx.params['action'] and not ctx.params['action_id']): return click.echo(ctx.get_help()) option_list = ['transfer','convert','action'] if validate(ctx.params, option_list): if transfer: params = {"type":"transfer","region":region} record = 'image transfer' return run_command(token, proxy, transfer, params, record) if convert: params = {"type":"convert"} record = 'image convert' return run_command(token, proxy, convert, params, record) if action: method = 'GET' url = IMAGES + str(action) + '/' + str(action_id) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'image action' headers = ['Fields', 'Values'] dic = result['action'] table = [['Id', dic['id']], ['Status', dic['status']], ['Type', click.style(dic['type'], fg='blue')], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource id', dic['resource_id']], ['Resource type', dic['resource_type']], ['Region', dic['region']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record)
def run_command(token, proxy, record, url, tablefmt): """ run request and process result """ page = 1 has_page = True while has_page: if '?' in url: new_url = url + '&page=%d' % (page) else: new_url = url + '?page=%d' % (page) result = invoke_list(token, proxy, new_url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' % (result['error_message'])) else: headers = ['Fields', 'Values'] for dic in result['images']: table = [['Id', dic['id']], ['Name', dic['name']], ['Distribution', dic['distribution']], ['Slug', dic['slug']], ['Public', dic['public']], ['Created at', dic['created_at']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total images: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False
def run_command(droplet_id, params, record, token, proxy, tablefmt): """ run command and process result """ method = 'POST' url = DROPLETS + str(droplet_id) + '/actions' result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: headers = ['Fields', 'Values'] table = [['Id', result['action']['id']], ['Status', result['action']['status']], ['Type', result['action']['type']], ['Started at', result['action']['started_at']], ['Completed at', result['action']['completed_at']], ['Resource Id', result['action']['resource_id']], ['Resource Type', result['action']['resource_type']], ['Region', result['action']['region']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) click.echo() click.echo('To get status update of above action execute following command.') click.echo('Command: docli action -i %d' % droplet_id)
def domain(ctx, token, tablefmt, proxy, getlist, create, name, ip, detail, delete): """ Domains that you are managing through the DigitalOcean DNS interface. """ if (not ctx.params['getlist'] and not ctx.params['create'] and not ctx.params['name'] and not ctx.params['ip'] and not ctx.params['detail'] and not ctx.params['delete']): return click.echo(ctx.get_help()) option_list = ['getlist', 'create', 'detail', 'delete'] if validate(ctx.params, option_list): if getlist: method = 'GET' url = DOMAIN_LIST result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'domain' headers = ['Domain Name'] table = [] for domain in result['domains']: table.append([domain['name']]) data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) if create: method = 'POST' url = DOMAIN_LIST params = {'name': name, 'ip_address': ip} result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: click.echo() click.echo("Domain Created ", name) click.echo() if detail: method = 'GET' url = DOMAIN_LIST + detail result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: click.echo() click.echo("name: %s" %(result['domain']['name'])) click.echo("ttl: %s" %(result['domain']['ttl'])) click.echo("zone file: %s" %(result['domain']['zone_file'])) if delete: method = 'DELETE' url = DOMAIN_LIST + delete result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: click.echo() click.echo("Domain %s deleted" %(delete))
def record(ctx, token, tablefmt, proxy, getlist, create, recordid, delete, update, type, name, data, priority, port, weight): """ Individual DNS records configured for a domain. """ if not ctx.params['getlist'] and not ctx.params[ 'create'] and not ctx.params['delete'] and not ctx.params[ 'update'] and not ctx.params['type'] and not ctx.params[ 'name'] and not ctx.params['data'] and not ctx.params[ 'priority'] and not ctx.params[ 'port'] and not ctx.params[ 'weight'] and not ctx.params['recordid']: return click.echo(ctx.get_help()) if validate(ctx.params): if getlist: method = 'GET' url = DOMAIN_LIST + getlist + '/records' result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'domain record' headers = ['Fields', 'Values'] for dic in result['domain_records']: table = [['Id', dic['id']], ['Type', dic['type']], ['Name', dic['name']], ['Data', dic['data']], ['Priority', dic['priority']], ['Port', dic['port']], ['Weight', dic['weight']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total results: %d' % (result['meta']['total']) click.echo() click.echo(total) if create: method = 'POST' url = DOMAIN_LIST + create + '/records' params = {'type': type, 'name': name, 'data': data} if priority == 0: params.updated({'priority': 'null'}) else: params.update({'priority': priority}) if port == 0: params.updated({'port': 'null'}) else: params.update({'port': port}) if weight == 0: params.update({'weight': 'null'}) else: params.update({'weight': weight}) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'domain record' click.echo() click.echo("Domain record added for ", create) click.echo() headers = ['Fields', 'Values'] table = [['Id', result['domain_record']['id']], ['Type', result['domain_record']['type']], ['Name', result['domain_record']['name']], ['Data', result['domain_record']['data']], ['Priority', result['domain_record']['priority']], ['Port', result['domain_record']['port']], ['Weight', result['domain_record']['weight']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) if update: method = 'PUT' url = DOMAIN_LIST + update + '/records/' + recordid params = {} if name: params.update({'name': name}) if type: params.update({'type': type}) if data: params.update({'data': data}) if not priority == 0: params.update({'priority': priority}) if not port == 0: params.update({'port': port}) if not weight == 0: params.update({'weight': weight}) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'domain record' click.echo() click.echo("Domain record updated for ", update) click.echo() headers = ['Fields', 'Values'] table = [['Id', result['domain_record']['id']], ['Type', result['domain_record']['type']], ['Name', result['domain_record']['name']], ['Data', result['domain_record']['data']], ['Priority', result['domain_record']['priority']], ['Port', result['domain_record']['port']], ['Weight', result['domain_record']['weight']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) if delete: method = 'DELETE' url = DOMAIN_LIST + delete + '/records/' + recordid result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) else: click.echo() click.echo("Domain %s record id %s deleted" % (delete, recordid))
def droplet(ctx, create, getlist, retrieve, kernel, snapshot, listbackup, action, delete, name, region, size, image, sshkeys, backup, ipv6, private_networking, user_data, token, tablefmt, proxy): """ A Droplet is a DigitalOcean virtual machine. you can list, create, or delete Droplets. """ if (not ctx.params['create'] and not ctx.params['getlist'] and not ctx.params['retrieve'] and not ctx.params['kernel'] and not ctx.params['snapshot'] and not ctx.params['listbackup'] and not ctx.params['action'] and not ctx.params['delete'] and not ctx.params['name'] and not ctx.params['region'] and not ctx.params['size'] and not ctx.params['image'] and not ctx.params['sshkeys'] and not ctx.params['backup'] and not ctx.params['ipv6'] and not ctx.params['private_networking'] and not ctx.params['user_data']): return click.echo(ctx.get_help()) option_list = ['create','getlist','retrieve','kernel','snapshot','listbackup','action','delete'] if validate(ctx.params, option_list): if create: method = 'POST' url = DROPLETS params = {'name': name, 'region': region, 'size': size, 'image': image, 'ssh_keys': sshkeys, 'backups': backup, 'ipv6': ipv6, 'private_networking': private_networking, 'user_data': user_data} result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'droplet create' click.echo() click.echo("Creating Your Droplet ", name) click.echo() headers = ['Fields', 'Values'] table = [['Id', result['droplet']['id']], ['Name', result['droplet']['name']], ['Memory', result['droplet']['memory']], ['Vcpus', result['droplet']['vcpus']], ['Disk', result['droplet']['disk']], ['Locked', result['droplet']['locked']], ['Status', result['droplet']['status']], ['Kernal Id', result['droplet']['kernel']['id']], ['Kernel Name', result['droplet']['kernel']['name']], ['Kernel Version', result['droplet']['kernel']['version']], ['Created At', result['droplet']['created_at']], ['Features', result['droplet']['features']], ['Backup Id', result['droplet']['backup_ids']], ['SnapShot Id', result['droplet']['snapshot_ids']], ['Network', result['droplet']['networks']], ['Region', result['droplet']['region']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) if getlist: page = 1 has_page = True while has_page: url = DROPLETS + '?page=%d' % (page) result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'droplet list' headers = ['Fields', 'Values'] for dic in result['droplets']: droplet_id = dic['id'] name = dic['name'] memory = dic['memory'] created_at = dic['created_at'] if dic['networks']['v4']: network_v4 = dic['networks']['v4'][0]['ip_address'] else: network_v4 = None if dic['networks']['v6']: network_v6 = dic['networks']['v6'][0]['ip_address'] else: network_v6 = None table = [['Id', droplet_id], ['Name', name], ['Memory', memory], ['Created At', created_at], ['Network V4', network_v4], ['Network V6', network_v6]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total droplets: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False if retrieve: method = 'GET' url = DROPLETS + str(retrieve) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = 'retrieve droplet' headers = ['Fields', 'Values'] droplet_id = result['droplet']['id'] name = result['droplet']['name'] memory = result['droplet']['memory'] vcpus = result['droplet']['vcpus'] disk = result['droplet']['disk'] locked = result['droplet']['locked'] status = result['droplet']['status'] kernel_id = result['droplet']['kernel']['id'] kernel_name = result['droplet']['kernel']['name'] kernel_version = result['droplet']['kernel']['version'] created_at = result['droplet']['created_at'] features = result['droplet']['features'] backup_ids = result['droplet']['backup_ids'] snapshot_ids = result['droplet']['snapshot_ids'] if result['droplet']['networks']['v4']: network_v4 = result['droplet']['networks']['v4'][0]['ip_address'] else: network_v4 = None if result['droplet']['networks']['v6']: network_v6 = result['droplet']['networks']['v6'][0]['ip_address'] else: network_v6 = None region = result['droplet']['region']['name'] available = result['droplet']['region']['available'] table = [['Id', droplet_id],['Name', name],['Memory', memory], ['Vcpus', vcpus],['Disk', disk],['Locked', locked], ['Status', status],['Kernel Id', kernel_id], ['Kernel Name', kernel_name],['Kernel Version', kernel_version], ['Created At', created_at],['Features', features], ['Backup Id', backup_ids],['SnapShot Id', snapshot_ids], ['Network V4', network_v4],['Network V6', network_v6], ['Region', region],['Available', available]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) if kernel: page = 1 has_page = True while has_page: url = DROPLETS + str(kernel) + '/kernels?page=%d' % (page) result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = "droplet kernel" headers = ['Kernel Id', 'Kernel Name', 'Kernel Version'] table = [] for dic in result['kernels']: table_list = [dic['id'], dic['name'], dic['version']] table.append(table_list) data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total kernels: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False if snapshot: page = 1 has_page = True while has_page: url = DROPLETS + str(snapshot) + '/snapshots?page=%d' % (page) result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = "droplet snapshot" headers = ['Fields', 'Values'] for dic in result['snapshots']: table = [['SnapShot Id', dic['id']], ['SnapShot Name', dic['name']], ['Distribution', dic['distribution']], ['Public', dic['public']], ['Regions', dic['regions']], ['Created At', dic['created_at']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total snapshots: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False if listbackup: page = 1 has_page = True while has_page: url = DROPLETS + str(listbackup) + '/backups?page=%d' % (page) result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = "droplet backup" headers = ['Fields', 'Values'] for dic in result['backups']: table = [['Backup Id', dic['id']], ['Backup Name', dic['name']], ['Distribution', dic['distribution']], ['Public', dic['public']], ['Regions', dic['regions']], ['Created At', dic['created_at']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total backups: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False if action: page = 1 has_page = True while has_page: url = DROPLETS + str(action) + '/actions?page=%d' % (page) result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' %(result['error_message'])) else: record = "droplet action" headers = ['Fields', 'Values'] for dic in result['actions']: table = [['Id', dic['id']], ['Status', dic['status']], ['Type', dic['type']], ['Started At', dic['started_at']], ['Completed At', dic['completed_at']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total actions: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False if delete: method = 'DELETE' url = DROPLETS + str(delete) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) else: click.echo() click.echo("Droplet id %d deleted" % (delete))
def action(ctx, getlist, getid, token, proxy, tablefmt): """ Actions are records of events that have occurred on the resources """ if not getlist and not getid: return click.echo(ctx.get_help()) option_list = ['getlist', 'getid'] if validate(ctx.params, option_list): if getlist: page = 1 has_page = True while has_page: result = invoke_list(token, proxy, page) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) has_page = False else: record = 'action' headers = ['Fields', 'Values'] for dic in result['actions']: table = [['Id', dic['id']], ['Status', dic['status']], ['Type', click.style(dic['type'], fg='blue')], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource id', dic['resource_id']], ['Resource type', dic['resource_type']], ['Region', dic['region']['name']], ['Size', dic['region']['sizes'][0]]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total results: %d' % (result['meta']['total']) click.echo() click.echo(total) if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False if getid: method = 'GET' url = ACTION_LIST + str(getid) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' %(result['error_message'])) has_page = False else: record = 'action' headers = ['Fields', 'Values'] dic = result['action'] table = [['Id', dic['id']], ['Status', dic['status']], ['Type', click.style(dic['type'], fg='blue')], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource id', dic['resource_id']], ['Resource type', dic['resource_type']], ['Region', dic['region']['name']], ['Size', dic['region']['sizes'][0]]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record)
def action(ctx, getlist, getid, token, proxy, tablefmt): """ Actions are records of events that have occurred on the resources """ if not getlist and not getid: return click.echo(ctx.get_help()) option_list = ['getlist', 'getid'] if validate(ctx.params, option_list): if getlist: page = 1 has_page = True while has_page: result = invoke_list(token, proxy, page) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) has_page = False else: record = 'action' headers = ['Fields', 'Values'] for dic in result['actions']: table = [['Id', dic['id']], ['Status', dic['status']], ['Type', click.style(dic['type'], fg='blue')], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource id', dic['resource_id']], ['Resource type', dic['resource_type']], ['Region', dic['region']['name']], ['Size', dic['region']['sizes'][0]]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total results: %d' % (result['meta']['total']) click.echo() click.echo(total) if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False if getid: method = 'GET' url = ACTION_LIST + str(getid) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: click.echo() click.echo('Error: %s' % (result['error_message'])) has_page = False else: record = 'action' headers = ['Fields', 'Values'] dic = result['action'] table = [['Id', dic['id']], ['Status', dic['status']], ['Type', click.style(dic['type'], fg='blue')], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource id', dic['resource_id']], ['Resource type', dic['resource_type']], ['Region', dic['region']['name']], ['Size', dic['region']['sizes'][0]]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record)
def images(ctx, getlist, distribution, application, private, id, slug, action, update, delete, name, token, tablefmt, proxy): """ Images in DigitalOcean may refer to one of a few different kinds of objects. """ if (not ctx.params['getlist'] and not ctx.params['distribution'] and not ctx.params['application'] and not ctx.params['private'] and not ctx.params['id'] and not ctx.params['slug'] and not ctx.params['action'] and not ctx.params['update'] and not ctx.params['name'] and not ctx.params['delete']): return click.echo(ctx.get_help()) option_list = [ 'getlist', 'distribution', 'application', 'private', 'id', 'slug', 'action', 'update', 'delete' ] if validate(ctx.params, option_list): if getlist: url = IMAGES record = 'list images' return run_command(token, proxy, record, url, tablefmt) if distribution: url = IMAGES + '?type=distribution' record = 'list distribution images' return run_command(token, proxy, record, url, tablefmt) if application: url = IMAGES + '?type=application' record = 'list application images' return run_command(token, proxy, record, url, tablefmt) if private: url = IMAGES + '?private=true' record = 'list private images' return run_command(token, proxy, record, url, tablefmt) if id: url = IMAGES + str(id) record = 'image by id' return image_by_id_slug(token, proxy, record, url, tablefmt) if slug: url = IMAGES + slug record = 'image by slug' return image_by_id_slug(token, proxy, record, url, tablefmt) if action: page = 1 has_page = True while has_page: url = IMAGES + str(action) + '/actions?page=%d' % (page) result = invoke_list(token, proxy, url) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' % (result['error_message'])) else: headers = ['Fields', 'Values'] for dic in result['actions']: table = [ 'Id', dic['id'], ['Status', dic['status']], ['Type', dic['type']], ['Started at', dic['started_at']], ['Completed at', dic['completed_at']], ['Resource Type', dic['resource_type']], ['Resource id', dic['resource_id']], ['Region', dic['region']] ] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) total = 'Total actions: %d' % (result['meta']['total']) click.echo(total) if result['links'].has_key('pages'): if result['links']['pages'].has_key('next'): page += 1 value = click.prompt('Do you want to continue ?', type=str, default='n') if value.lower() != 'y': has_page = False else: has_page = False else: has_page = False if update: method = 'PUT' url = IMAGES + str(update) params = {"name": name} result = DigitalOcean.do_request(method, url, token=token, proxy=proxy, params=params) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' % (result['error_message'])) else: record = 'image update' headers = ['Fields', 'Values'] table = [['Id', result['image']['id']], ['Name', result['image']['name']], ['Distribution', result['image']['distribution']], ['Slug', result['image']['slug']], ['Public', result['image']['public']], ['Regions', result['image']['regions']], ['Created at', result['image']['created_at']]] data = {'headers': headers, 'table_data': table} print_table(tablefmt, data, record) if delete: method = 'DELETE' url = IMAGES + str(delete) result = DigitalOcean.do_request(method, url, token=token, proxy=proxy) if result['has_error']: has_page = False click.echo() click.echo('Error: %s' % (result['error_message'])) else: click.echo() click.echo("Image id %d deleted" % (delete))