Exemple #1
0
def _ApplyEnvVarsArgsToFunction(function, args):
    """Determines if environment variables have to be updated.

  It compares the cli args with the existing environment variables to compute
  the resulting build environment variables.

  Args:
    function: CloudFunction message to be checked and filled with env vars based
      on the flags
    args: all cli args

  Returns:
    updated_fields: update mask containing the list of fields that are
    considered for updating based on the cli args and existing variables
  """

    updated_fields = []
    old_env_vars = env_vars_api_util.GetEnvVarsAsDict(
        function.environmentVariables)
    env_var_flags = map_util.GetMapFlagsFromArgs('env-vars', args)
    new_env_vars = map_util.ApplyMapFlags(old_env_vars, **env_var_flags)
    if old_env_vars != new_env_vars:
        env_vars_type_class = api_util.GetApiMessagesModule(
        ).CloudFunction.EnvironmentVariablesValue
        function.environmentVariables = env_vars_api_util.DictToEnvVarsProperty(
            env_vars_type_class, new_env_vars)
        updated_fields.append('environmentVariables')
    return updated_fields
def ApplyFlags(old_secrets, args, default_project_id, default_project_number):
    """Applies the current flags to existing secrets configuration.

  Args:
    old_secrets: Existing combined secrets configuration dict.
    args: Argparse namespace.
    default_project_id: The project ID of the project to which the function is
      deployed.
    default_project_number: The project number of the project to which the
      function is deployed.

  Returns:
    new_secrets: A new combined secrets configuration dict generated by
      applying the flags to the existing secrets configuration.

  Raises:
    ArgumentTypeError: Generated secrets configuration is invalid.
  """
    secret_flags = map_util.GetMapFlagsFromArgs('secrets', args)
    new_secrets = map_util.ApplyMapFlags(old_secrets, **secret_flags)

    new_secrets = {
        secrets_key:
        _SubstituteDefaultProject(secrets_value, default_project_id,
                                  default_project_number)
        for secrets_key, secrets_value in six.iteritems(new_secrets)
    }
    new_secrets = collections.OrderedDict(sorted(six.iteritems(new_secrets)))

    # Handles the case when the newly configured secrets could conflict with
    # existing secrets.
    _ValidateSecrets(new_secrets)
    return new_secrets
Exemple #3
0
def _ApplyEnvVarsArgsToFunction(function, args):
    updated_fields = []
    old_env_vars = env_vars_api_util.GetFunctionEnvVarsAsDict(function)
    env_var_flags = map_util.GetMapFlagsFromArgs('env-vars', args)
    new_env_vars = map_util.ApplyMapFlags(old_env_vars, **env_var_flags)
    if old_env_vars != new_env_vars:
        function.environmentVariables = env_vars_api_util.DictToEnvVarsProperty(
            new_env_vars)
        updated_fields.append('environmentVariables')
    return updated_fields
Exemple #4
0
    def testClearFlag(self):
        """Check that --clear-* works."""
        args = self.parser.parse_args(['--clear-food-prices'])
        map_flags = map_util.GetMapFlagsFromArgs('food-prices', args)
        expected = {
            'set_flag_value': None,
            'update_flag_value': None,
            'clear_flag_value': True,
            'remove_flag_value': None,
            'file_flag_value': None,
        }
        self.assertEqual(map_flags, expected)

        new_map = map_util.ApplyMapFlags(self.old_map, **map_flags)
        self.assertEqual(new_map, {})
Exemple #5
0
    def testSetFileFlag(self):
        """Check that --*-file works."""
        files.WriteFileContents('daily-prices.yaml', _TEST_MAP_YAML)
        args = self.parser.parse_args(['--food-prices-file=daily-prices.yaml'])
        map_flags = map_util.GetMapFlagsFromArgs('food-prices', args)
        expected = {
            'set_flag_value': None,
            'update_flag_value': None,
            'clear_flag_value': None,
            'remove_flag_value': None,
            'file_flag_value': _TEST_MAP,
        }
        self.assertEqual(map_flags, expected)

        new_map = map_util.ApplyMapFlags(self.old_map, **map_flags)
        self.assertEqual(new_map, {'cod': 12, 'tomato': 2})
Exemple #6
0
def _GetServiceConfig(args, messages):
    """Construct a ServiceConfig message from the command-line arguments."""
    env_var_flags = map_util.GetMapFlagsFromArgs('env-vars', args)
    env_vars = map_util.ApplyMapFlags({}, **env_var_flags)

    return messages.ServiceConfig(
        availableMemoryMb=utils.BytesToMb(args.memory)
        if args.memory else None,
        maxInstanceCount=args.max_instances,
        serviceAccountEmail=args.run_service_account or args.service_account,
        timeoutSeconds=args.timeout,
        environmentVariables=messages.ServiceConfig.EnvironmentVariablesValue(
            additionalProperties=[
                messages.ServiceConfig.EnvironmentVariablesValue.
                AdditionalProperty(key=key, value=value)
                for key, value in sorted(env_vars.items())
            ]))
Exemple #7
0
    def testSetFlag(self):
        """Check that --set-* works."""
        args = self.parser.parse_args(['--set-food-prices=B=22,E=5'])
        map_flags = map_util.GetMapFlagsFromArgs('food-prices', args)
        expected = {
            'set_flag_value': {
                'B': 22,
                'E': 5
            },
            'update_flag_value': None,
            'clear_flag_value': None,
            'remove_flag_value': None,
            'file_flag_value': None,
        }
        self.assertEqual(map_flags, expected)

        new_map = map_util.ApplyMapFlags(self.old_map, **map_flags)
        self.assertEqual(new_map, {'B': 22, 'E': 5})
Exemple #8
0
    def testUpdateAndRemoveFlags(self):
        """Check that --update-* and --remove-* works in conjunction."""
        args = self.parser.parse_args(
            ['--update-food-prices=A=1,B=22', '--remove-food-prices=C'])
        map_flags = map_util.GetMapFlagsFromArgs('food-prices', args)
        expected = {
            'set_flag_value': None,
            'update_flag_value': {
                'A': 1,
                'B': 22
            },
            'clear_flag_value': None,
            'remove_flag_value': ['C'],
            'file_flag_value': None,
        }
        self.assertEqual(map_flags, expected)

        new_map = map_util.ApplyMapFlags(self.old_map, **map_flags)
        self.assertEqual(new_map, {'A': 1, 'B': 22, 'D': 4})
Exemple #9
0
def _GetBuildConfig(args, messages):
    """Construct a BuildConfig message from the command-line arguments."""
    source_bucket, source_object = _GetSource(args.source)

    build_env_var_flags = map_util.GetMapFlagsFromArgs('build-env-vars', args)
    build_env_vars = map_util.ApplyMapFlags({}, **build_env_var_flags)

    return messages.BuildConfig(
        entryPoint=args.entry_point,
        runtime=args.runtime,
        source=messages.Source(storageSource=messages.StorageSource(
            bucket=source_bucket, object=source_object)),
        workerPool=args.build_worker_pool,
        environmentVariables=messages.BuildConfig.EnvironmentVariablesValue(
            additionalProperties=[
                messages.BuildConfig.EnvironmentVariablesValue.
                AdditionalProperty(key=key, value=value)
                for key, value in sorted(build_env_vars.items())
            ]))
Exemple #10
0
def _GetBuildConfig(args, client, messages, region, function_name,
                    existing_function):
    """Constructs a BuildConfig message from the command-line arguments.

  Args:
    args: argparse.Namespace, arguments that this command was invoked with
    client: The GCFv2 API client
    messages: messages module, the GCFv2 message stubs
    region: str, the region to deploy the function to
    function_name: str, the name of the function
    existing_function: cloudfunctions_v2alpha_messages.Function | None

  Returns:
    build_config: cloudfunctions_v2alpha_messages.BuildConfig, describes the
      build step for the function
    updated_fields_set: frozenset[str], set of update mask fields
  """
    function_source, source_updated_fields = _GetSource(
        client, messages, region, function_name, args.source,
        args.stage_bucket, args.ignore_file, existing_function)

    old_build_env_vars = {}
    if (existing_function and existing_function.buildConfig
            and existing_function.buildConfig.environmentVariables
            and existing_function.buildConfig.environmentVariables.
            additionalProperties):
        for additional_property in (existing_function.buildConfig.
                                    environmentVariables.additionalProperties):
            old_build_env_vars[
                additional_property.key] = additional_property.value

    build_env_var_flags = map_util.GetMapFlagsFromArgs('build-env-vars', args)
    # Dict
    build_env_vars = map_util.ApplyMapFlags(old_build_env_vars,
                                            **build_env_var_flags)

    updated_fields = set()

    if build_env_vars != old_build_env_vars:
        updated_fields.add('build_config.environment_variables')

    if args.entry_point is not None:
        updated_fields.add('build_config.entry_point')
    if args.runtime is not None:
        updated_fields.add('build_config.runtime')

    worker_pool = (None
                   if args.clear_build_worker_pool else args.build_worker_pool)

    if args.build_worker_pool is not None or args.clear_build_worker_pool:
        updated_fields.add('build_config.worker_pool')

    build_updated_fields = frozenset.union(source_updated_fields,
                                           updated_fields)
    return messages.BuildConfig(
        entryPoint=args.entry_point,
        runtime=args.runtime,
        source=function_source,
        workerPool=worker_pool,
        environmentVariables=messages.BuildConfig.EnvironmentVariablesValue(
            additionalProperties=[
                messages.BuildConfig.EnvironmentVariablesValue.
                AdditionalProperty(key=key, value=value)
                for key, value in sorted(build_env_vars.items())
            ])), build_updated_fields
Exemple #11
0
def _GetServiceConfig(args, messages, existing_function):
    """Constructs a ServiceConfig message from the command-line arguments.

  Args:
    args: argparse.Namespace, arguments that this command was invoked with
    messages: messages module, the GCFv2 message stubs
    existing_function: cloudfunctions_v2alpha_messages.Function | None

  Returns:
    vpc_connector: str, the vpc connector name
    vpc_egress_settings: VpcConnectorEgressSettingsValueValuesEnum, the vpc
      enum value
    updated_fields_set: frozenset, set of update mask fields
  """

    old_env_vars = {}
    if (existing_function and existing_function.serviceConfig
            and existing_function.serviceConfig.environmentVariables
            and existing_function.serviceConfig.environmentVariables.
            additionalProperties):
        for additional_property in (existing_function.serviceConfig.
                                    environmentVariables.additionalProperties):
            old_env_vars[additional_property.key] = additional_property.value

    env_var_flags = map_util.GetMapFlagsFromArgs('env-vars', args)
    env_vars = map_util.ApplyMapFlags(old_env_vars, **env_var_flags)

    vpc_connector, vpc_egress_settings, vpc_updated_fields = (
        _GetVpcAndVpcEgressSettings(args, messages, existing_function))

    ingress_settings, ingress_updated_fields = _GetIngressSettings(
        args, messages)

    updated_fields = set()

    if args.serve_all_traffic_latest_revision:
        # only set field if flag is specified, never explicitly set to false.
        updated_fields.add('service_config.all_traffic_on_latest_revision')
    if args.memory is not None:
        updated_fields.add('service_config.available_memory')
    if args.max_instances is not None or args.clear_max_instances:
        updated_fields.add('service_config.max_instance_count')
    if args.min_instances is not None or args.clear_min_instances:
        updated_fields.add('service_config.min_instance_count')
    if args.run_service_account is not None or args.service_account is not None:
        updated_fields.add('service_config.service_account_email')
    if args.timeout is not None:
        updated_fields.add('service_config.timeout_seconds')
    if env_vars != old_env_vars:
        updated_fields.add('service_config.environment_variables')

    service_updated_fields = frozenset.union(vpc_updated_fields,
                                             ingress_updated_fields,
                                             updated_fields)

    return messages.ServiceConfig(
        availableMemory=_ParseMemoryStrToK8sMemory(args.memory),
        maxInstanceCount=None
        if args.clear_max_instances else args.max_instances,
        minInstanceCount=None
        if args.clear_min_instances else args.min_instances,
        serviceAccountEmail=args.run_service_account or args.service_account,
        timeoutSeconds=args.timeout,
        ingressSettings=ingress_settings,
        vpcConnector=vpc_connector,
        vpcConnectorEgressSettings=vpc_egress_settings,
        allTrafficOnLatestRevision=(args.serve_all_traffic_latest_revision
                                    or None),
        environmentVariables=messages.ServiceConfig.EnvironmentVariablesValue(
            additionalProperties=[
                messages.ServiceConfig.EnvironmentVariablesValue.
                AdditionalProperty(key=key, value=value)
                for key, value in sorted(env_vars.items())
            ])), service_updated_fields