Beispiel #1
0
 def _select_app(self) -> CFApplication:
     """Select a single PCF app from the available list"""
     if not self.apps:
         self.apps = self._get_apps()
     return mselect(
         self.apps,
         title='Please select the application you want to manage')
Beispiel #2
0
    def _loop_actions(self) -> None:
        """Wait for the user interactions"""
        while not self.done:
            if self.org and self.space and not self.cf.is_targeted():
                self._target()
            else:
                self._set_org()
                self._set_space()

            if not self.org or not self.space or not self.cf.is_targeted():
                raise CFExecutionError(
                    f"Unable to target ORG={self.org}  SPACE={self.space} => {self.cf.last_result}"
                )

            action = mselect(CFManager.CF_ACTIONS,
                             'Please select an action to perform')

            if not action:
                self.done = True
            else:
                if self._is_callable(action):
                    if self._allow_multiple(action.lower()):
                        apps = self._choose_apps()
                    else:
                        app = self._select_app()
                        apps = [app] if app else None
                    if apps:
                        for app in apps:
                            self._perform(action,
                                          app=app.name,
                                          org=self.org,
                                          space=self.space)
                else:
                    if action.lower() == 'status':
                        apps = self._get_apps(refresh=True)
                        if len(apps) > 0:
                            sysout("{}  {}  {}  {}  {}  {}".format(
                                'Name'.ljust(CFApplication.max_name_length),
                                'State'.ljust(7),
                                'Inst'.ljust(5),
                                'Mem'.ljust(4),
                                'Disk'.ljust(4),
                                'URLs',
                            ))
                            for app in apps:
                                app.print_status()
                    elif action.lower() == 'target':
                        self.space = None
                        self.org = None
                        self.cf.targeted = {
                            'org': None,
                            'space': None,
                            'targeted': False
                        }
                        continue

                    MenuUtils.wait_enter()
Beispiel #3
0
 def _set_space(self) -> None:
     """Set the active space"""
     if not self.space:
         sysout('%YELLOW%Checking space...')
         spaces = self.cf.spaces()
         if not spaces:
             raise CFExecutionError(
                 f'Unable to retrieve spaces: => {self.cf.last_result}')
         self.space = mselect(spaces, title='Please select a space')
         if not self.space:
             sys.exit(1)
         else:
             self._target()
Beispiel #4
0
 def _set_org(self) -> None:
     """Set the active organization"""
     if not self.org:
         sysout('%YELLOW%Checking organization...')
         orgs = self.cf.orgs()
         if not orgs:
             raise CFExecutionError(
                 f'Unable to retrieve organizations: => {self.cf.last_result}'
             )
         self.org = mselect(orgs, title='Please select the organization')
         if not self.org:
             sys.exit(1)
         else:
             self._target()
Beispiel #5
0
 def _select_endpoint(self) -> None:
     """Select the PCF endpoint to connect to"""
     with open(f'{self.configs.resource_dir()}/api_endpoints.txt',
               'r+') as f_hosts:
         endpoints = list(
             map(lambda x: CFEndpoint(x.split(',')), f_hosts.readlines()))
         selected = mselect(endpoints, title='Please select an endpoint')
         if not selected:
             sys.exit(0)
         sysout(f'%GREEN%Connecting to endpoint: {selected}...')
         try:
             response = head(selected.host)
             if response.status_code and HttpCode.OK:
                 self.api = selected.host
             else:
                 syserr(
                     f'Failed to connect to API ({response.status_code}): {selected}'
                 )
                 sys.exit(0)
         except Exception as err:
             raise CFConnectionError(
                 f'Failed to connect to API => {selected.host}') from err
Beispiel #6
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
   TODO Purpose of the file
   @project: HSPyLib
   hspylib.demo.cli.tui.extra
      @file: menu_select.py
   @created: Tue, 4 May 2021
    @author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior"
      @site: https://github.com/yorevs/hspylib
   @license: MIT - Please refer to <https://opensource.org/licenses/MIT>

   Copyright 2021, HSPyLib team
"""
from hspylib.modules.cli.tui.extra.mselect import mselect

if __name__ == '__main__':
    it = [f"Item-{n}" for n in range(1, 21)]
    sel = mselect(it, max_rows=10)
    print(str(sel))