def app_routes(self): def execute(client, arguments): resource_id = self.resolve_id(arguments.id[0], lambda x: self._get_client_domain(client).get_first(name=x)) for entity in getattr(self._get_client_domain(client), 'list_routes')(resource_id): print('%s - %s' % (entity['metadata']['guid'], entity['entity']['host'])) return Command('app_routes', self._generate_id_command_parser('app_routes'), execute)
def recent_logs(self): def execute(client, arguments): resource_id = self.resolve_id(arguments.id[0], lambda x: self._get_client_domain(client).get_first(name=x)) for envelope in client.doppler.recent_logs(resource_id): print(envelope) return Command('recent_logs', self._generate_id_command_parser('recent_logs'), execute)
def stream_logs(self): def execute(client, arguments): resource_id = self.resolve_id(arguments.id[0], lambda x: self._get_client_domain(client).get_first(name=x)) try: for envelope in client.doppler.stream_logs(resource_id): print(envelope) except KeyboardInterrupt: pass return Command('stream_logs', self._generate_id_command_parser('stream_logs'), execute)
def simple_extra_command(self, entry) -> Command: def execute(client, arguments): resource_id = self.resolve_id( arguments.id[0], lambda x: self._get_client_domain(client).get_first(name=x)) print( getattr(self._get_client_domain(client), entry)(resource_id).json(indent=1)) return Command(entry, self._generate_id_command_parser(entry), execute)
def generate_oauth_token_command() -> Tuple[Command, str]: entry = 'oauth-token' def generate_parser(parser: argparse._SubParsersAction): parser.add_parser(entry) def execute(client: CloudFoundryClient, arguments: argparse.Namespace): token = client._access_token print(token if token is not None else 'No token') return Command(entry, generate_parser, execute), 'Display oauth token'
def generate_oauth_token_command(): entry = 'oauth-token' def generate_parser(parser): parser.add_parser(entry) def execute(client, arguments): token = client._access_token print(token if token is not None else 'No token') return Command(entry, generate_parser, execute), 'Display oauth token'
def app_routes(self) -> Command: def execute(client: CloudFoundryClient, arguments: Namespace): resource_id = self.resolve_id( arguments.id[0], lambda x: self._get_client_domain(client).get_first(name=x)) for entity in getattr(self._get_client_domain(client), "list_routes")(resource_id): print("%s - %s" % (entity["metadata"]["guid"], entity["entity"]["host"])) return Command("app_routes", self._generate_id_command_parser("app_routes"), execute)
def restart_instance(self) -> Command: def generate_parser(parser: _SubParsersAction): command_parser = parser.add_parser('restart_instance') command_parser.add_argument('id', metavar='ids', type=str, nargs=1, help='The id. Can be UUID or name (first found then)') command_parser.add_argument('instance_id', metavar='instance_ids', type=int, nargs=1, help='The instance id') def execute(client: CloudFoundryClient, arguments: Namespace): app_domain = self._get_client_domain(client) resource_id = self.resolve_id(arguments.id[0], lambda x: app_domain.get_first(name=x)) getattr(app_domain, 'restart_instance')(resource_id, int(arguments.instance_id[0])) return Command('restart_instance', generate_parser, execute)
def cancel(self): entry = 'cancel_task' def execute(client, arguments): print( self._get_client_domain(client).cancel( arguments.id[0]).json(indent=1)) def generate_parser(parser): command_parser = parser.add_parser(entry) command_parser.add_argument('id', metavar='ids', type=str, nargs=1, help='The task UUID') return Command(entry, generate_parser, execute)
def cancel(self) -> Command: entry = 'cancel_task' def execute(client: CloudFoundryClient, arguments: Namespace): print( self._get_client_domain(client).cancel( arguments.id[0]).json(indent=1)) def generate_parser(parser: _SubParsersAction): command_parser = parser.add_parser(entry) command_parser.add_argument('id', metavar='ids', type=str, nargs=1, help='The task UUID') return Command(entry, generate_parser, execute)
def create(self) -> Command: entry = self._create_entry() def execute(client: CloudFoundryClient, arguments: Namespace): data = None if os.path.isfile(arguments.entity[0]): with open(arguments.entity[0], "r") as f: try: data = json.load(f) except ValueError: raise ValueError( "entity: file %s does not contain valid json data" % arguments.entity[0]) else: try: data = json.loads(arguments.entity[0]) except ValueError: raise ValueError( "entity: must be either a valid json file path or a json object" ) print( self._get_client_domain(client).create(arguments.app_id[0], **data).json()) def generate_parser(parser: _SubParsersAction): create_parser = parser.add_parser(entry) create_parser.add_argument("app_id", metavar="ids", type=str, nargs=1, help="The application UUID.") create_parser.add_argument( "entity", metavar="entities", type=str, nargs=1, help= "Either a path of the json file containing the %s or a json object or the json %s object" % (self.client_domain, self.client_domain), ) return Command(entry, generate_parser, execute)
def create(self): entry = self._create_entry() def execute(client, arguments): data = None if os.path.isfile(arguments.entity[0]): with open(arguments.entity[0], 'r') as f: try: data = json.load(f) except ValueError: raise ValueError( 'entity: file %s does not contain valid json data' % arguments.entity[0]) else: try: data = json.loads(arguments.entity[0]) except ValueError: raise ValueError( 'entity: must be either a valid json file path or a json object' ) print( self._get_client_domain(client).create(arguments.app_id[0], **data).json()) def generate_parser(parser): create_parser = parser.add_parser(entry) create_parser.add_argument('app_id', metavar='ids', type=str, nargs=1, help='The application UUID.') create_parser.add_argument( 'entity', metavar='entities', type=str, nargs=1, help= 'Either a path of the json file containing the %s or a json object or the json %s object' % (self.client_domain, self.client_domain)) return Command(entry, generate_parser, execute)
def generate_push_command() -> Tuple[Command, str]: entry = "push_app" def generate_parser(parser: _SubParsersAction): command_parser = parser.add_parser(entry) command_parser.add_argument("manifest_path", metavar="manifest_paths", type=str, nargs=1, help="The manifest path") command_parser.add_argument("-space_guid", action="store", dest="space_guid", type=str, help="Space guid") def execute(client: CloudFoundryClient, arguments: Namespace): manifest_path = arguments.manifest_path[0] PushOperation(client).push(arguments.space_guid, manifest_path) return Command(entry, generate_parser, execute), "Push an application by its manifest"
def generate_push_command(): entry = 'push_app' def generate_parser(parser): command_parser = parser.add_parser(entry) command_parser.add_argument('manifest_path', metavar='manifest_paths', type=str, nargs=1, help='The manifest path') command_parser.add_argument('-space_guid', action='store', dest='space_guid', type=str, help='Space guid') def execute(client, arguments): manifest_path = arguments.manifest_path[0] PushOperation(client).push(arguments.space_guid, manifest_path) return Command(entry, generate_parser, execute), 'Push an application by its manifest'