예제 #1
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    forwarding_rule_ref = self.FORWARDING_RULE_ARG.ResolveAsResource(
        args,
        holder.resources,
        scope_lister=compute_flags.GetDefaultScopeLister(holder.client))

    data = console_io.ReadFromFileOrStdin(args.source or '-', binary=False)

    try:
      forwarding_rule = export_util.Import(
          message_type=client.messages.ForwardingRule,
          stream=data,
          schema_path=self.GetSchemaPath())
    except yaml_validator.ValidationError as e:
      raise exceptions.ValidationError(str(e))

    # Get existing forwarding rule.
    try:
      forwarding_rule_old = utils.SendGetRequest(client, forwarding_rule_ref)
    except apitools_exceptions.HttpError as error:
      if error.status_code != 404:
        raise error
      # Forwarding rule does not exist, create a new one.
      return self.SendInsertRequest(client, forwarding_rule_ref,
                                    forwarding_rule)

    # No change, do not send requests to server.
    if forwarding_rule_old == forwarding_rule:
      return

    console_io.PromptContinue(
        message=('Forwarding Rule [{0}] will be overwritten.').format(
            forwarding_rule_ref.Name()),
        cancel_on_no=True)

    # Populate id and fingerprint fields. These two fields are manually
    # removed from the schema files.
    forwarding_rule.id = forwarding_rule_old.id
    forwarding_rule.fingerprint = forwarding_rule_old.fingerprint

    # Unspecified fields are assumed to be cleared.
    cleared_fields = []
    if not forwarding_rule.networkTier:
      cleared_fields.append('networkTier')
    if not forwarding_rule.allowGlobalAccess:
      cleared_fields.append('allowGlobalAccess')
    if self._support_source_ip_range and not forwarding_rule.sourceIpRanges:
      cleared_fields.append('sourceIpRanges')
    if not forwarding_rule.metadataFilters:
      cleared_fields.append('metadataFilters')

    with client.apitools_client.IncludeFields(cleared_fields):
      return self.SendPatchRequest(client, forwarding_rule_ref, forwarding_rule)
예제 #2
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        forwarding_rule_ref = self.FORWARDING_RULE_ARG.ResolveAsResource(
            args,
            holder.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(client))

        forwarding_rule = utils.SendGetRequest(client, forwarding_rule_ref)

        if args.destination:
            with files.FileWriter(args.destination) as stream:
                export_util.Export(message=forwarding_rule,
                                   stream=stream,
                                   schema_path=self.GetSchemaPath())
        else:
            export_util.Export(message=forwarding_rule,
                               stream=sys.stdout,
                               schema_path=self.GetSchemaPath())