def testGeneration(self):
   global_mock = self.StartObjectPatch(yaml_command_translator.CommandBuilder,
                                       '_ConfigureGlobalAttributes')
   command = yaml_command_translator.Translator().Translate(
       ['foo', 'delete'], self.MakeCommandData())
   self.assertTrue(issubclass(command, calliope_base.Command))
   global_mock.assert_called_once_with(command)
Esempio n. 2
0
 def testGenerationDefaultHelp(self):
   command = yaml_command_translator.Translator().Translate(
       ['foo', 'get_iam_policy'],
       self.MakeIAMCommandData(help_text='to get IAM policy of'))
   self.assertTrue(issubclass(command, calliope_base.ListCommand))
   self.assertEqual('<brief>', command.detailed_help.get('brief'))
   self.assertEqual('<DESCRIPTION>', command.detailed_help.get('DESCRIPTION'))
def CreateCLI(surfaces, translator=None):
    """Generates the gcloud CLI from 'surface' folder with extra surfaces.

  Args:
    surfaces: list(tuple(dot_path, dir_path)), extra commands or subsurfaces to
      add, where dot_path is calliope command path and dir_path path to command
      group or command.
    translator: yaml_command_translator.Translator, an alternative translator.

  Returns:
    calliope cli object.
  """
    def VersionFunc():
        generated_cli.Execute(['version'])

    def HandleKnownErrorFunc():
        crash_handling.ReportError(is_crash=False)

    pkg_root = os.path.dirname(os.path.dirname(surface.__file__))
    loader = cli.CLILoader(
        name='gcloud',
        command_root_directory=os.path.join(pkg_root, 'surface'),
        allow_non_existing_modules=True,
        version_func=VersionFunc,
        known_error_handler=HandleKnownErrorFunc,
        yaml_command_translator=(translator
                                 or yaml_command_translator.Translator()),
    )
    loader.AddReleaseTrack(base.ReleaseTrack.ALPHA,
                           os.path.join(pkg_root, 'surface', 'alpha'),
                           component='alpha')
    loader.AddReleaseTrack(base.ReleaseTrack.BETA,
                           os.path.join(pkg_root, 'surface', 'beta'),
                           component='beta')

    for dot_path, dir_path in surfaces:
        loader.AddModule(dot_path, dir_path, component=None)

    # TODO(b/128465608): Remove cloned ml-engine commands and PreRunHook after a
    # suitable deprecation period.
    # Clone 'ai-platform' surface into 'ml-engine' for backward compatibility.
    loader.AddModule('ml_engine',
                     os.path.join(pkg_root, 'surface', 'ai_platform'))
    loader.RegisterPreRunHook(_IssueAIPlatformAliasWarning,
                              include_commands=r'gcloud\..*ml-engine\..*')

    # Clone 'container/hub' surface into 'container/fleet'
    # for backward compatibility.
    loader.AddModule('container.hub',
                     os.path.join(pkg_root, 'surface', 'container', 'fleet'))

    # Check for updates on shutdown but not for any of the updater commands.
    # Skip update checks for 'gcloud version' command as it does that manually.
    exclude_commands = r'gcloud\.components\..*|gcloud\.version'
    loader.RegisterPostRunHook(UpdateCheck, exclude_commands=exclude_commands)
    loader.RegisterPostRunHook(SurveyPromptCheck)
    generated_cli = loader.Generate()
    return generated_cli
Esempio n. 4
0
 def GetRemoveIamPolicyBindingCommandTranslator(cls,
                                                brief=None,
                                                description=None):
     return yaml_command_translator.Translator().Translate(
         ['foo', 'remove_iam_policy_binding'],
         cls.MakeIAMCommandData(
             help_text='to remove IAM policy binding from',
             brief=brief,
             description=description))
Esempio n. 5
0
 def testGeneration(self):
   global_mock = self.StartObjectPatch(yaml_command_translator.CommandBuilder,
                                       '_ConfigureGlobalAttributes')
   d = self.MakeCommandData(is_create=True)
   d['request']['method'] = 'insert'
   command = yaml_command_translator.Translator().Translate(
       ['foo', 'create'], d)
   self.assertTrue(issubclass(command, calliope_base.CreateCommand))
   global_mock.assert_called_once_with(command)
Esempio n. 6
0
  def SetUp(self):
    self.known_error_handler = mock.MagicMock()
    self._loader = calliope.CLILoader(
        name='gcloud',
        command_root_directory=self.Resource('surface'),
        allow_non_existing_modules=True,
        known_error_handler=self.known_error_handler,
        yaml_command_translator=yaml_command_translator.Translator())

    self.cli = self._loader.Generate()
 def testGenerationExplicitHelp(self):
     brief = 'explicit brief'
     description = 'explicit description'
     command = yaml_command_translator.Translator().Translate(
         ['foo', 'set_iam_policy'],
         self.MakeIAMCommandData(help_text='to set IAM policy to',
                                 brief=brief,
                                 description=description))
     self.assertTrue(issubclass(command, calliope_base.Command))
     self.assertEqual(brief, command.detailed_help.get('brief'))
     self.assertEqual(description, command.detailed_help.get('DESCRIPTION'))
Esempio n. 8
0
def CreateCLI(surfaces, translator=None):
    """Generates the gcloud CLI from 'surface' folder with extra surfaces.

  Args:
    surfaces: list(tuple(dot_path, dir_path)), extra commands or subsurfaces
              to add, where dot_path is calliope command path and dir_path
              path to command group or command.
    translator: yaml_command_translator.Translator, an alternative translator.
  Returns:
    calliope cli object.
  """
    def VersionFunc():
        generated_cli.Execute(['version'])

    def HandleKnownErrorFunc(exc):
        crash_handling.ReportError(exc, is_crash=False)

    pkg_root = os.path.dirname(os.path.dirname(surface.__file__))
    loader = cli.CLILoader(
        name='gcloud',
        command_root_directory=os.path.join(pkg_root, 'surface'),
        allow_non_existing_modules=True,
        version_func=VersionFunc,
        known_error_handler=HandleKnownErrorFunc,
        yaml_command_translator=(translator
                                 or yaml_command_translator.Translator()),
    )
    loader.AddReleaseTrack(base.ReleaseTrack.ALPHA,
                           os.path.join(pkg_root, 'surface', 'alpha'),
                           component='alpha')
    loader.AddReleaseTrack(base.ReleaseTrack.BETA,
                           os.path.join(pkg_root, 'surface', 'beta'),
                           component='beta')

    for dot_path, dir_path in surfaces:
        loader.AddModule(dot_path, dir_path, component=None)

    # TODO(b/63771276): Remove cloned xpn commands and PreRunHook after a
    # suitable deprecation period.
    # Clone 'compute shared-vpc' surface into 'compute xpn' for backward
    # compatibility.
    loader.AddModule(
        'compute.xpn',
        os.path.join(pkg_root, 'surface', 'compute', 'shared_vpc'))
    loader.RegisterPreRunHook(_IssueTestWarning,
                              include_commands=r'gcloud\.compute\.xpn\..*')

    # Check for updates on shutdown but not for any of the updater commands.
    loader.RegisterPostRunHook(UpdateCheck,
                               exclude_commands=r'gcloud\.components\..*')
    generated_cli = loader.Generate()
    return generated_cli
Esempio n. 9
0
def CreateCLI(surfaces, translator=None):
    """Generates the gcloud CLI from 'surface' folder with extra surfaces.

  Args:
    surfaces: list(tuple(dot_path, dir_path)), extra commands or subsurfaces
              to add, where dot_path is calliope command path and dir_path
              path to command group or command.
    translator: yaml_command_translator.Translator, an alternative translator.
  Returns:
    calliope cli object.
  """
    def VersionFunc():
        generated_cli.Execute(['version'])

    def HandleKnownErrorFunc():
        crash_handling.ReportError(is_crash=False)

    pkg_root = os.path.dirname(os.path.dirname(surface.__file__))
    loader = cli.CLILoader(
        name='gcloud',
        command_root_directory=os.path.join(pkg_root, 'surface'),
        allow_non_existing_modules=True,
        version_func=VersionFunc,
        known_error_handler=HandleKnownErrorFunc,
        yaml_command_translator=(translator
                                 or yaml_command_translator.Translator()),
    )
    loader.AddReleaseTrack(base.ReleaseTrack.ALPHA,
                           os.path.join(pkg_root, 'surface', 'alpha'),
                           component='alpha')
    loader.AddReleaseTrack(base.ReleaseTrack.BETA,
                           os.path.join(pkg_root, 'surface', 'beta'),
                           component='beta')

    for dot_path, dir_path in surfaces:
        loader.AddModule(dot_path, dir_path, component=None)

    # Check for updates on shutdown but not for any of the updater commands.
    # Skip update checks for 'gcloud version' command as it does that manually.
    exclude_commands = r'gcloud\.components\..*|gcloud\.version'
    loader.RegisterPostRunHook(UpdateCheck, exclude_commands=exclude_commands)
    generated_cli = loader.Generate()
    return generated_cli
 def SetIamPolicyTranslator(cls, brief=None, description=None):
     return yaml_command_translator.Translator().Translate(
         ['foo', 'set_iam_policy'],
         cls.MakeIAMCommandData(help_text='to set IAM policy to',
                                brief=brief,
                                description=description))