コード例 #1
0
  def _BuildFullMapUpdateTestHelper(self,
                                    expected_patch,
                                    clear,
                                    remove_keys=None,
                                    set_entries=None,
                                    initial_entries=None):
    """Helper for testing BuildFullMapUpdate.

    Using this helper, one can test that BuildFullMapUpdate combines
    `clear`, `remove_keys`, `set_entries`, and `initial_entries` (in that order)
    to build a patch object.

    Args:
      expected_patch: dict, a dictionary of the key-value pairs expected to
          be in the patch
      clear: bool, the value of the clear parameter to pass through
      remove_keys: [str], the value of the remove_keys parameter to pass through
      set_entries: {str: str}, the value of the set_entries parameter to pass
          through
      initial_entries: dict, a dictionary of the key-value pairs in the initial
        entry
    """
    actual_patch = command_util.BuildFullMapUpdate(
        clear=clear,
        remove_keys=remove_keys,
        set_entries=set_entries,
        initial_entries=[
            UtilGATest._FakeEntry(key=key, value=value)
            for key, value in six.iteritems(initial_entries or {})
        ],
        entry_cls=UtilGATest._FakeEntry,
        env_builder=UtilGATest._FakePatchBuilder)
    self.assertEqual(expected_patch, actual_patch)
コード例 #2
0
def _ConstructEnvVariablesPatch(env_ref,
                                clear_env_variables,
                                remove_env_variables,
                                update_env_variables,
                                release_track=base.ReleaseTrack.GA):
    """Constructs an environment patch for updating environment variables.

  Note that environment variable updates do not support partial update masks
  unlike other map updates due to comments in (b/78298321). For this reason, we
  need to retrieve the Environment, apply an update on EnvVariable dictionary,
  and patch the entire dictionary. The potential race condition here
  (environment variables being updated between when we retrieve them and when we
  send patch request)is not a concern since environment variable updates take
  5 mins to complete, and environments cannot be updated while already in the
  updating state.

  Args:
    env_ref: resource argument, Environment resource argument for environment
      being updated.
    clear_env_variables: bool, whether to clear the environment variables
      dictionary.
    remove_env_variables: iterable(string), Iterable of environment variable
      names to remove.
    update_env_variables: {string: string}, dict of environment variable names
      and values to set.
    release_track: base.ReleaseTrack, the release track of command. Will dictate
        which Composer client library will be used.

  Returns:
    (str, Environment), the field mask and environment to use for update.
  """
    env_obj = environments_api_util.Get(env_ref, release_track=release_track)
    initial_env_var_value = env_obj.config.softwareConfig.envVariables
    initial_env_var_list = (initial_env_var_value.additionalProperties
                            if initial_env_var_value else [])

    messages = api_util.GetMessagesModule(release_track=release_track)
    env_cls = messages.Environment
    env_variables_cls = messages.SoftwareConfig.EnvVariablesValue
    entry_cls = env_variables_cls.AdditionalProperty

    def _BuildEnv(entries):
        software_config = messages.SoftwareConfig(
            envVariables=env_variables_cls(additionalProperties=entries))
        config = messages.EnvironmentConfig(softwareConfig=software_config)
        return env_cls(config=config)

    return ('config.software_config.env_variables',
            command_util.BuildFullMapUpdate(clear_env_variables,
                                            remove_env_variables,
                                            update_env_variables,
                                            initial_env_var_list, entry_cls,
                                            _BuildEnv))