Exemple #1
0
    def _change_project(self):
        """Changes the project configuration being managed.

    Returns:
      A new instance of _Manager for the selected project.
    """
        project_key = utils.prompt_string(
            'You are currently managing Google Cloud Project {!r}.\n'
            'This project is currently saved as {!r}.\n'
            'All of the currently configured projects include: {}.\n'
            'Which project would you like to switch to?'.format(
                self._config.project, self._config.key,
                ', '.join(common.get_available_configs(self._config.path))))
        return _Manager.new(self._config.path, project_key)
Exemple #2
0
 def test_get_available_configs(self):
     path = '/this/config/file.yaml'
     self.fs.CreateFile(path, contents=_MULTI_CONFIG)
     self.assertEqual(['default', 'dev'],
                      common.get_available_configs(path))
Exemple #3
0
    def new(cls, config_file_path, project_key=None):
        """Creates a new instance of a Grab n Go Manager.

    Args:
      config_file_path: str, the name or path to the config file.
      project_key: Optional[str], the project friendly name, used as the
          top-level key in the config file.

    Returns:
      A new instance of a Grab n Go Manager.

    Raises:
      _AuthError: when unable to generate valid credentials.
    """
        if project_key is None:
            project_key = utils.prompt_string(
                'A project name was not provided, which project would you like to '
                'configure?\nNOTE: If you are unsure, or if you plan to use only one '
                'Google Cloud Project, you can use the default.\n'
                'The following projects are currently defined in {!r}:\n {}'.
                format(
                    config_file_path,
                    ', '.join(common.get_available_configs(config_file_path))),
                default=common.DEFAULT).lower()
        logging.debug(
            'Project key %r was provided, attempting to load from yaml.',
            project_key)

        try:
            config = common.ProjectConfig.from_yaml(project_key,
                                                    config_file_path)
        except (common.ConfigError, KeyError) as err:
            logging.debug(
                'Failed to initialize project with key %r: %s\n'
                'Attempting to load new config...', project_key, err)
            utils.write(
                'There does not appear to be a saved configuration for this project: '
                '{!r}. Before we can get started, we need to gather some information.'
                '\n'.format(project_key))
            config = common.ProjectConfig.from_prompts(project_key,
                                                       config_file_path)
            config.write()

        try:
            cloud_creds = auth.CloudCredentials(config, _get_oauth_scopes())
        except auth.InvalidCredentials as err:
            utils.write(
                'We were unable to create credentials for this configuration, please '
                'verify the Project ID, OAuth2 Client ID and Client Secret are '
                'correct and then re-enter them in the following prompts.')
            config = common.ProjectConfig.from_prompts(project_key,
                                                       config_file_path)
            config.write()
            return cls.new(config_file_path, project_key)

        gae_admin_api = app_engine.AdminAPI.from_config(config, cloud_creds)
        datastore_api = datastore.DatastoreAPI.from_config(config, cloud_creds)
        directory_api = directory.DirectoryAPI.from_config(config, cloud_creds)
        storage_api = storage.CloudStorageAPI.from_config(config, cloud_creds)

        constants = app_constants.get_constants_from_flags()

        return cls(config, constants, cloud_creds, gae_admin_api,
                   datastore_api, directory_api, storage_api)