def validate_api_params(self, method, **params): if method.lower() not in self.api_dict: log.error("Invalid ECS API method: %s", method) raise exp.InvalidParameterValue(opt_name='method', opt_value=method) valid_params = self.api_dict[method.lower()]['parameters'] for (k, v) in valid_params.iteritems(): if k == 'Action': continue if valid_params[k] is True and k not in params: log.error("Invalid ECS API parameter: missing parameter %s", k) raise exp.InvalidParameterValue(opt_name=k, opt_value="missing") params['Action'] = self.get_api_action(method) return params
def main(): (args, params) = parseArguments() opts = {'verbose': args.verbose, 'console': True, } log.init_logging(opts) ecs_api = api.EcsApi(args.access_key, args.access_key_secret) if args.info and args.api: api_info = ecs_api.get_api_info(args.api) print_api_info(api_info) return 0 if args.info: api_list = ecs_api.get_available_apis() print_api_list(api_list) return 0 method = getattr(ecs_api, args.api) if not method: log.error("API %s not implemented", args.api) raise exp.InvalidParameterValue(opt_name="api", opt_value=method) resp = method(**params) print "Aliyun ECS API %s results:\n%s" % (args.api, json.dumps(resp, indent=2, sort_keys=True)) return 0
def parseArguments(): params = {} parser = argparse.ArgumentParser(description="Shucaibao aliyun ecs api tool") parser.add_argument('--verbose', action="store_true", required=False, help="print the debug information") parser.add_argument('--access_key', required=False, type=str, help="Aliyun ECS access key") parser.add_argument('--access_key_secret', required=False, type=str, help="Aliyun ECS access key secret") parser.add_argument('--api', required=False, type=str, help="Aliyun ECS api name") parser.add_argument('--params', required=False, nargs='+', type=key_equal_value, help="ECS API request parameters in key=value") parser.add_argument('--info', required=False, action='store_true', help="Print out the ECS API details") args = parser.parse_args() if args.api and args.params: if args.access_key is None or args.access_key_secret is None: log.error("Please specify the access key and secret") raise exp.InvalidParameterValue(opt_name="access_key/secret", opt_value="missing") if args.params is not None: for param in args.params: (k, v) = param.split('=') params[k] = v return (args, params)
def get_entries(self, xmt_type, filter=None): self.validate_type(xmt_type) if self.xmt_tables[xmt_type]['table'] is None: raise exp.InvalidParameterValue(key=xmt_type + "_table", value=None) if filter is None: return self.xmt_tables[xmt_type]['table'].values() xmt_values = self.xmt_tables[xmt_type]['table'].values() return [x for x in xmt_values if XmtManager.filter_entry(x, filter)]
def get_api_action(self, method): if method.lower() not in self.api_dict: log.error("Invalid ECS API method: %s", method) raise exp.InvalidParameterValue(opt_name='method', opt_value=method) return self.api_dict[method.lower()]['parameters']['Action']
def validate_type(self, xmt_type): if xmt_type not in self.xmt_tables.keys(): raise exp.InvalidParameterValue(key='xmt_type', value=xmt_type)
def get_entry(self, xmt_type, key_value): self.validate_type(xmt_type) if self.xmt_tables[xmt_type]['table'] is None: raise exp.InvalidParameterValue(key=xmt_type + "_table", value=None) return self.xmt_tables[xmt_type]['table'][key_value]