コード例 #1
0
    def _CreateGlobalRequests(self, client, resources, args,
                              forwarding_rule_ref):
        """Create a globally scoped request."""
        ports_all_specified, range_list = _ExtractPortsAndAll(args.ports)
        port_range = _ResolvePortRange(args.port_range, range_list)
        if ports_all_specified:
            raise exceptions.ToolException(
                '[--ports] can not be specified to all for global forwarding rules.'
            )
        if not port_range:
            raise exceptions.ToolException(
                '[--ports] is required for global forwarding rules.')
        target_ref = utils.GetGlobalTarget(resources, args)
        protocol = self.ConstructProtocol(client.messages, args)

        if args.address is None or args.ip_version:
            ip_version = client.messages.ForwardingRule.IpVersionValueValuesEnum(
                args.ip_version or 'IPV4')
        else:
            ip_version = None

        address = self._ResolveAddress(
            resources, args, compute_flags.compute_scope.ScopeEnum.GLOBAL,
            forwarding_rule_ref)

        forwarding_rule = client.messages.ForwardingRule(
            description=args.description,
            name=forwarding_rule_ref.Name(),
            IPAddress=address,
            IPProtocol=protocol,
            portRange=port_range,
            target=target_ref.SelfLink(),
            ipVersion=ip_version,
            networkTier=_ConstructNetworkTier(client.messages, args),
            loadBalancingScheme=_GetLoadBalancingScheme(args, client.messages))

        if args.IsSpecified('network'):
            forwarding_rule.network = flags.NetworkArg(
                self._support_l7_internal_load_balancing).ResolveAsResource(
                    args, resources).SelfLink()

        if self._support_global_access and args.IsSpecified(
                'allow_global_access'):
            forwarding_rule.allowGlobalAccess = args.allow_global_access

        request = client.messages.ComputeGlobalForwardingRulesInsertRequest(
            forwardingRule=forwarding_rule,
            project=forwarding_rule_ref.project)

        return [(client.apitools_client.globalForwardingRules, 'Insert',
                 request)]
コード例 #2
0
    def _CreateRegionalRequests(self, client, resources, args,
                                forwarding_rule_ref):
        """Create a regionally scoped request."""
        target_ref, region_ref = utils.GetRegionalTarget(
            client,
            resources,
            args,
            forwarding_rule_ref,
            include_l7_internal_load_balancing=self.
            _support_l7_internal_load_balancing)
        if not args.region and region_ref:
            args.region = region_ref
        protocol = self.ConstructProtocol(client.messages, args)

        address = self._ResolveAddress(
            resources, args, compute_flags.compute_scope.ScopeEnum.REGION,
            forwarding_rule_ref)

        forwarding_rule = client.messages.ForwardingRule(
            description=args.description,
            name=forwarding_rule_ref.Name(),
            IPAddress=address,
            IPProtocol=protocol,
            networkTier=_ConstructNetworkTier(client.messages, args),
            loadBalancingScheme=_GetLoadBalancingScheme(args, client.messages))

        ports_all_specified, range_list = _ExtractPortsAndAll(args.ports)
        if (target_ref.Collection() == 'compute.regionBackendServices') or (
                target_ref.Collection() == 'compute.targetInstances'
                and args.load_balancing_scheme == 'INTERNAL'):
            forwarding_rule.portRange = (six.text_type(args.port_range)
                                         if args.port_range else None)
            if target_ref.Collection() == 'compute.regionBackendServices':
                forwarding_rule.backendService = target_ref.SelfLink()
            else:
                forwarding_rule.target = target_ref.SelfLink()
            if ports_all_specified:
                forwarding_rule.allPorts = True
            if range_list:
                forwarding_rule.portRange = None
                forwarding_rule.ports = [
                    six.text_type(p) for p in _GetPortList(range_list)
                ]
            if args.subnet is not None:
                if not args.subnet_region:
                    args.subnet_region = forwarding_rule_ref.region
                forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
                    args, resources).SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
        elif (
            (target_ref.Collection() == 'compute.regionTargetHttpProxies'
             or target_ref.Collection() == 'compute.regionTargetHttpsProxies')
                and args.load_balancing_scheme == 'INTERNAL'):
            forwarding_rule.ports = [
                six.text_type(p) for p in _GetPortList(range_list)
            ]
            if args.subnet is not None:
                if not args.subnet_region:
                    args.subnet_region = forwarding_rule_ref.region
                forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
                    args, resources).SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
            forwarding_rule.target = target_ref.SelfLink()
        elif args.load_balancing_scheme == 'INTERNAL':
            raise exceptions.InvalidArgumentException(
                '--load-balancing-scheme',
                'Only target instances and backend services should be specified as '
                'a target for internal load balancing.')
        elif args.load_balancing_scheme == 'INTERNAL_MANAGED':
            forwarding_rule.portRange = (_ResolvePortRange(
                args.port_range, range_list))
            if args.subnet is not None:
                if not args.subnet_region:
                    args.subnet_region = forwarding_rule_ref.region
                forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
                    args, resources).SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
            forwarding_rule.target = target_ref.SelfLink()
        else:
            forwarding_rule.portRange = (_ResolvePortRange(
                args.port_range, range_list))
            forwarding_rule.target = target_ref.SelfLink()
        if hasattr(args, 'service_label'):
            forwarding_rule.serviceLabel = args.service_label

        if self._support_global_access and args.IsSpecified(
                'allow_global_access'):
            forwarding_rule.allowGlobalAccess = args.allow_global_access

        if hasattr(args, 'is_mirroring_collector'):
            forwarding_rule.isMirroringCollector = args.is_mirroring_collector

        request = client.messages.ComputeForwardingRulesInsertRequest(
            forwardingRule=forwarding_rule,
            project=forwarding_rule_ref.project,
            region=forwarding_rule_ref.region)

        return [(client.apitools_client.forwardingRules, 'Insert', request)]
コード例 #3
0
    def _CreateRegionalRequests(self, client, resources, args,
                                forwarding_rule_ref):
        """Create a regionally scoped request."""
        is_psc_ilb = False
        if hasattr(args, 'target_service_attachment'
                   ) and args.target_service_attachment:
            if not self._support_target_service_attachment:
                raise exceptions.InvalidArgumentException(
                    '--target-service-attachment',
                    'Private Service Connect for ILB (the target-service-attachment '
                    'option) is not supported in this API version.')
            else:
                is_psc_ilb = True

        target_ref, region_ref = utils.GetRegionalTarget(
            client,
            resources,
            args,
            forwarding_rule_ref,
            include_l7_internal_load_balancing=self.
            _support_l7_internal_load_balancing,
            include_target_service_attachment=self.
            _support_target_service_attachment)
        if not args.region and region_ref:
            args.region = region_ref
        protocol = self.ConstructProtocol(client.messages, args)

        address = self._ResolveAddress(
            resources, args, compute_flags.compute_scope.ScopeEnum.REGION,
            forwarding_rule_ref)
        load_balancing_scheme = _GetLoadBalancingScheme(
            args, client.messages, is_psc_ilb)
        if is_psc_ilb and load_balancing_scheme:
            raise exceptions.InvalidArgumentException(
                '--load-balancing-scheme',
                'The --load-balancing-scheme flag is not allowed for PSC-ILB '
                'forwarding rules.')

        if args.ip_version:
            ip_version = client.messages.ForwardingRule.IpVersionValueValuesEnum(
                args.ip_version)
        else:
            ip_version = None

        forwarding_rule = client.messages.ForwardingRule(
            description=args.description,
            name=forwarding_rule_ref.Name(),
            IPAddress=address,
            IPProtocol=protocol,
            networkTier=_ConstructNetworkTier(client.messages, args),
            loadBalancingScheme=load_balancing_scheme)
        if ip_version:
            forwarding_rule.ipVersion = ip_version

        ports_all_specified, range_list = _ExtractPortsAndAll(args.ports)
        if target_ref.Collection() == 'compute.serviceAttachments':
            forwarding_rule.target = target_ref.SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
        elif (target_ref.Collection() == 'compute.regionBackendServices') or (
                target_ref.Collection() == 'compute.targetInstances'
                and args.load_balancing_scheme == 'INTERNAL'):
            forwarding_rule.portRange = (six.text_type(args.port_range)
                                         if args.port_range else None)
            if target_ref.Collection() == 'compute.regionBackendServices':
                forwarding_rule.backendService = target_ref.SelfLink()
            else:
                forwarding_rule.target = target_ref.SelfLink()
            if ports_all_specified:
                forwarding_rule.allPorts = True
            if range_list:
                forwarding_rule.portRange = None
                forwarding_rule.ports = [
                    six.text_type(p) for p in _GetPortList(range_list)
                ]
            if args.subnet is not None:
                if not args.subnet_region:
                    args.subnet_region = forwarding_rule_ref.region
                forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
                    args, resources).SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
        elif (
            (target_ref.Collection() == 'compute.regionTargetHttpProxies'
             or target_ref.Collection() == 'compute.regionTargetHttpsProxies')
                and args.load_balancing_scheme == 'INTERNAL'):
            forwarding_rule.ports = [
                six.text_type(p) for p in _GetPortList(range_list)
            ]
            if args.subnet is not None:
                if not args.subnet_region:
                    args.subnet_region = forwarding_rule_ref.region
                forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
                    args, resources).SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
            forwarding_rule.target = target_ref.SelfLink()
        elif args.load_balancing_scheme == 'INTERNAL':
            raise exceptions.InvalidArgumentException(
                '--load-balancing-scheme',
                'Only target instances and backend services should be specified as '
                'a target for internal load balancing.')
        elif args.load_balancing_scheme == 'INTERNAL_MANAGED':
            forwarding_rule.portRange = (_ResolvePortRange(
                args.port_range, range_list))
            if args.subnet is not None:
                if not args.subnet_region:
                    args.subnet_region = forwarding_rule_ref.region
                forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
                    args, resources).SelfLink()
            if args.network is not None:
                forwarding_rule.network = flags.NetworkArg(
                    self._support_l7_internal_load_balancing
                ).ResolveAsResource(args, resources).SelfLink()
            forwarding_rule.target = target_ref.SelfLink()
        else:
            forwarding_rule.portRange = (_ResolvePortRange(
                args.port_range, range_list))
            forwarding_rule.target = target_ref.SelfLink()
        if hasattr(args, 'service_label'):
            forwarding_rule.serviceLabel = args.service_label

        if self._support_global_access and args.IsSpecified(
                'allow_global_access'):
            forwarding_rule.allowGlobalAccess = args.allow_global_access

        if hasattr(args, 'is_mirroring_collector'):
            forwarding_rule.isMirroringCollector = args.is_mirroring_collector

        if hasattr(args, 'service_directory_registration'
                   ) and args.service_directory_registration:
            # Parse projects/../locations/../namespaces/../services/..
            match = re.match(
                r'^projects/([^/]+)/locations/([^/]+)/namespaces/([^/]+)/services/([^/]+)$',
                args.service_directory_registration)
            if not match:
                raise exceptions.InvalidArgumentException(
                    '--service-directory-registration',
                    'Must be of the form projects/PROJECT/locations/REGION/namespace/NAMESPACE/services/SERVICE'
                )
            project = match.group(1)
            region = match.group(2)

            if project != forwarding_rule_ref.project or region != forwarding_rule_ref.region:
                raise exceptions.InvalidArgumentException(
                    '--service-directory-registration',
                    'Service Directory registration must be in the same project and region as the forwarding rule.'
                )

            sd_registration = client.messages.ForwardingRuleServiceDirectoryRegistration(
                namespace=match.group(3), service=match.group(4))
            forwarding_rule.serviceDirectoryRegistrations.append(
                sd_registration)

        request = client.messages.ComputeForwardingRulesInsertRequest(
            forwardingRule=forwarding_rule,
            project=forwarding_rule_ref.project,
            region=forwarding_rule_ref.region)

        return [(client.apitools_client.forwardingRules, 'Insert', request)]
コード例 #4
0
    def _CreateGlobalRequests(self, client, resources, args,
                              forwarding_rule_ref):
        """Create a globally scoped request."""

        if hasattr(args, 'service_directory_registration'
                   ) and args.service_directory_registration:
            raise exceptions.InvalidArgumentException(
                '--service-directory-registration',
                '[--service-directory-registration] can not be specified for global forwarding rules.'
            )

        is_psc_google_apis = False
        if hasattr(args, 'target_google_apis_bundle'
                   ) and args.target_google_apis_bundle:
            if not self._support_psc_google_apis:
                raise exceptions.InvalidArgumentException(
                    '--target-google-apis-bundle',
                    'Private Service Connect for Google APIs (the target-google-apis-bundle option '
                    'for forwarding rules) is not supported in this API version.'
                )
            else:
                is_psc_google_apis = True

        ports_all_specified, range_list = _ExtractPortsAndAll(args.ports)
        port_range = _ResolvePortRange(args.port_range, range_list)
        load_balancing_scheme = _GetLoadBalancingScheme(
            args, client.messages, is_psc_google_apis)

        if ports_all_specified and not is_psc_google_apis:
            raise exceptions.InvalidArgumentException(
                '--ports',
                '[--ports] can not be specified to all for global forwarding rules.'
            )
        if not is_psc_google_apis and not port_range:
            raise exceptions.InvalidArgumentException(
                '--ports',
                '[--ports] is required for global forwarding rules.')

        if is_psc_google_apis:
            rule_name = forwarding_rule_ref.Name()
            if len(rule_name) > 20 or rule_name[0].isdigit(
            ) or not rule_name.isalnum():
                raise exceptions.ToolException(
                    'A forwarding rule to Google APIs must have a name that is between '
                    ' 1-20 characters long, alphanumeric, starting with a letter.'
                )

            if port_range:
                raise exceptions.InvalidArgumentException(
                    '--ports',
                    '[--ports] is not allowed for PSC-GoogleApis forwarding rules.'
                )
            if load_balancing_scheme:
                raise exceptions.InvalidArgumentException(
                    '--load-balancing-scheme',
                    'The --load-balancing-scheme flag is not allowed for PSC-GoogleApis'
                    ' forwarding rules.')

            if args.target_google_apis_bundle in flags.PSC_GOOGLE_APIS_BUNDLES:
                target_as_str = args.target_google_apis_bundle
            else:
                bundles_list = ', '.join(flags.PSC_GOOGLE_APIS_BUNDLES)
                raise exceptions.InvalidArgumentException(
                    '--target-google-apis-bundle',
                    'The valid values for target-google-apis-bundle are: ' +
                    bundles_list)
        else:
            target_ref = utils.GetGlobalTarget(resources, args)
            target_as_str = target_ref.SelfLink()
        protocol = self.ConstructProtocol(client.messages, args)

        if args.address is None or args.ip_version:
            ip_version = client.messages.ForwardingRule.IpVersionValueValuesEnum(
                args.ip_version or 'IPV4')
        else:
            ip_version = None

        address = self._ResolveAddress(
            resources, args, compute_flags.compute_scope.ScopeEnum.GLOBAL,
            forwarding_rule_ref)
        forwarding_rule = client.messages.ForwardingRule(
            description=args.description,
            name=forwarding_rule_ref.Name(),
            IPAddress=address,
            IPProtocol=protocol,
            portRange=port_range,
            target=target_as_str,
            ipVersion=ip_version,
            networkTier=_ConstructNetworkTier(client.messages, args),
            loadBalancingScheme=load_balancing_scheme)

        if args.IsSpecified('network'):
            forwarding_rule.network = flags.NetworkArg(
                self._support_l7_internal_load_balancing).ResolveAsResource(
                    args, resources).SelfLink()

        if self._support_global_access and args.IsSpecified(
                'allow_global_access'):
            forwarding_rule.allowGlobalAccess = args.allow_global_access

        request = client.messages.ComputeGlobalForwardingRulesInsertRequest(
            forwardingRule=forwarding_rule,
            project=forwarding_rule_ref.project)

        return [(client.apitools_client.globalForwardingRules, 'Insert',
                 request)]
コード例 #5
0
  def _CreateRegionalRequests(self, client, resources, args,
                              forwarding_rule_ref):
    """Create a regionally scoped request."""
    is_psc_ilb = False
    if hasattr(args,
               'target_service_attachment') and args.target_service_attachment:
      if not self._support_target_service_attachment:
        raise exceptions.InvalidArgumentException(
            '--target-service-attachment',
            'Private Service Connect for ILB (the target-service-attachment '
            'option) is not supported in this API version.')
      else:
        is_psc_ilb = True

    target_ref, region_ref = utils.GetRegionalTarget(
        client,
        resources,
        args,
        forwarding_rule_ref,
        include_regional_tcp_proxy=self._support_regional_tcp_proxy,
        include_l7_internal_load_balancing=self
        ._support_l7_internal_load_balancing,
        include_target_service_attachment=self
        ._support_target_service_attachment)

    if not args.region and region_ref:
      args.region = region_ref

    protocol = self.ConstructProtocol(client.messages, args)

    address = self._ResolveAddress(resources, args,
                                   compute_flags.compute_scope.ScopeEnum.REGION,
                                   forwarding_rule_ref)

    load_balancing_scheme = _GetLoadBalancingScheme(args, client.messages,
                                                    is_psc_ilb)

    if is_psc_ilb and load_balancing_scheme:
      raise exceptions.InvalidArgumentException(
          '--load-balancing-scheme',
          'The --load-balancing-scheme flag is not allowed for PSC-ILB '
          'forwarding rules.')

    if args.ip_version:
      ip_version = client.messages.ForwardingRule.IpVersionValueValuesEnum(
          args.ip_version)
    else:
      ip_version = None

    forwarding_rule = client.messages.ForwardingRule(
        description=args.description,
        name=forwarding_rule_ref.Name(),
        IPAddress=address,
        IPProtocol=protocol,
        networkTier=_ConstructNetworkTier(client.messages, args),
        loadBalancingScheme=load_balancing_scheme)
    if ip_version:
      forwarding_rule.ipVersion = ip_version

    if self._support_source_ip_range and args.source_ip_ranges:
      forwarding_rule.sourceIpRanges = args.source_ip_ranges

    if args.subnet is not None:
      # Subnet arg needed for:
      # - L4ILB and internal protocol forwarding (scheme INTERNAL, target BES
      # or target instance)
      # - L7ILB (scheme INTERNAL_MANAGED)
      # - NetLB or external protocol forwarding when using IPv6 (scheme
      # EXTERNAL target BES or target instance)
      if not args.subnet_region:
        args.subnet_region = forwarding_rule_ref.region
      forwarding_rule.subnetwork = flags.SUBNET_ARG.ResolveAsResource(
          args, resources).SelfLink()
    if args.network is not None:
      # Network arg needed for:
      # - L4ILB and internal protocol forwarding (scheme INTERNAL, target BES
      # or target instance)
      # - L7ILB (scheme INTERNAL_MANAGED)
      # - PSC forwarding rules (no scheme)
      forwarding_rule.network = flags.NetworkArg(
          self._support_l7_internal_load_balancing).ResolveAsResource(
              args, resources).SelfLink()

    ports_all_specified, range_list = _ExtractPortsAndAll(args.ports)

    if target_ref.Collection() == 'compute.regionBackendServices':
      # A FR pointing to a BES has no target attribute.
      forwarding_rule.backendService = target_ref.SelfLink()
      forwarding_rule.target = None
    else:
      # A FR pointing to anything not a BES has a target attribute.
      forwarding_rule.backendService = None
      forwarding_rule.target = target_ref.SelfLink()

    if ((target_ref.Collection() == 'compute.regionBackendServices' or
         target_ref.Collection() == 'compute.targetInstances') and
        args.load_balancing_scheme == 'INTERNAL'):
      # This is for L4ILB and internal protocol forwarding.
      # API fields allPorts, ports, and portRange are mutually exclusive.
      # API field portRange is not valid for this case.
      # Use of L3_DEFAULT implies all ports even if allPorts is unset.
      if ports_all_specified:
        forwarding_rule.allPorts = True
      elif range_list:
        forwarding_rule.ports = [
            six.text_type(p) for p in _GetPortList(range_list)
        ]
    elif ((target_ref.Collection() == 'compute.regionTargetHttpProxies' or
           target_ref.Collection() == 'compute.regionTargetHttpsProxies') and
          args.load_balancing_scheme == 'INTERNAL'):
      # This is a legacy configuration for L7ILB.
      forwarding_rule.ports = [
          six.text_type(p) for p in _GetPortList(range_list)
      ]
    elif args.load_balancing_scheme == 'INTERNAL':
      # There are currently no other valid combinations of targets with scheme
      # internal. With scheme internal, targets must presently be a regional
      # backend service (L4ILB) or a target instance (protocol forwarding).
      raise exceptions.InvalidArgumentException(
          '--load-balancing-scheme',
          'Only target instances and backend services should be specified as '
          'a target for internal load balancing.')
    elif args.load_balancing_scheme == 'INTERNAL_MANAGED':
      # This is L7ILB.
      forwarding_rule.portRange = _MakeSingleUnifiedPortRange(
          args.port_range, range_list)
    elif args.load_balancing_scheme == 'EXTERNAL_MANAGED':
      # This is regional L7XLB.
      forwarding_rule.portRange = _MakeSingleUnifiedPortRange(
          args.port_range, range_list)
    elif ((target_ref.Collection() == 'compute.regionBackendServices') and
          ((args.load_balancing_scheme == 'EXTERNAL') or
           (not args.load_balancing_scheme))):
      # This is NetLB using a backend service. Scheme is either explicitly
      # EXTERNAL or not supplied (EXTERNAL is the default scheme).
      # API fields allPorts, ports, and portRange are mutually exclusive.
      # All three API fields are valid for this case.
      # Use of L3_DEFAULT implies all ports even if allPorts is unset.
      if ports_all_specified:
        forwarding_rule.allPorts = True
      elif range_list:
        if len(range_list) > 1:
          # More than one port, potentially discontiguous, from --ports= flag.
          forwarding_rule.ports = [
              six.text_type(p) for p in _GetPortList(range_list)
          ]
        else:
          # Exactly one value from --ports= flag. Might be a single port (80);
          # might be a range (80-90). Since it might be a range, the portRange
          # API attribute is more appropriate.
          forwarding_rule.portRange = six.text_type(range_list[0])
      elif args.port_range:
        forwarding_rule.portRange = _MakeSingleUnifiedPortRange(
            args.port_range, range_list)
    elif ((target_ref.Collection() == 'compute.targetPool' or
           target_ref.Collection() == 'compute.targetInstances') and
          ((args.load_balancing_scheme == 'EXTERNAL') or
           (not args.load_balancing_scheme))):
      # This is NetLB using a target pool or external protocol forwarding.
      # Scheme is either explicitly EXTERNAL or not supplied (EXTERNAL is the
      # default scheme).
      # API fields allPorts, ports, and portRange are mutually exclusive.
      # API field ports is not valid for this case.
      # Use of L3_DEFAULT implies all ports by definition.
      if ports_all_specified:
        forwarding_rule.allPorts = True
      else:
        forwarding_rule.portRange = _MakeSingleUnifiedPortRange(
            args.port_range, range_list)
    else:
      # All other regional forwarding rules with load balancing scheme EXTERNAL.
      forwarding_rule.portRange = _MakeSingleUnifiedPortRange(
          args.port_range, range_list)

    if hasattr(args, 'service_label'):
      forwarding_rule.serviceLabel = args.service_label

    if self._support_global_access and args.IsSpecified('allow_global_access'):
      forwarding_rule.allowGlobalAccess = args.allow_global_access

    if self._support_psc_global_access and args.IsSpecified(
        'allow_psc_global_access'):
      forwarding_rule.allowPscGlobalAccess = args.allow_psc_global_access

    if self._support_disable_automate_dns_zone and args.IsSpecified(
        'disable_automate_dns_zone'):
      forwarding_rule.noAutomateDnsZone = args.disable_automate_dns_zone

    if hasattr(args, 'is_mirroring_collector'):
      forwarding_rule.isMirroringCollector = args.is_mirroring_collector

    if hasattr(args, 'service_directory_registration'
              ) and args.service_directory_registration:
      if is_psc_ilb:
        # Parse projects/../locations/../namespaces/..
        match = re.match(
            r'^projects/([^/]+)/locations/([^/]+)/namespaces/([^/]+)$',
            args.service_directory_registration)
        if not match:
          raise exceptions.InvalidArgumentException(
              '--service-directory-registration',
              'If set, must be of the form projects/PROJECT/locations/REGION/namespaces/NAMESPACE'
          )
        project = match.group(1)
        region = match.group(2)

        if project != forwarding_rule_ref.project or region != forwarding_rule_ref.region:
          raise exceptions.InvalidArgumentException(
              '--service-directory-registration',
              'Service Directory registration must be in the same project and region as the forwarding rule.'
          )

        sd_registration = client.messages.ForwardingRuleServiceDirectoryRegistration(
            namespace=match.group(3))
        forwarding_rule.serviceDirectoryRegistrations.append(sd_registration)
      else:
        if not self._support_sd_registration_for_regional:
          raise exceptions.InvalidArgumentException(
              '--service-directory-registration',
              """flag is available in one or more alternate release tracks. Try:

  gcloud alpha compute forwarding-rules create --service-directory-registration
  gcloud beta compute forwarding-rules create --service-directory-registration"""
          )
        # Parse projects/../locations/../namespaces/../services/..
        match = re.match(
            r'^projects/([^/]+)/locations/([^/]+)/namespaces/([^/]+)/services/([^/]+)$',
            args.service_directory_registration)
        if not match:
          raise exceptions.InvalidArgumentException(
              '--service-directory-registration',
              'Must be of the form projects/PROJECT/locations/REGION/namespaces/NAMESPACE/services/SERVICE'
          )
        project = match.group(1)
        region = match.group(2)

        if project != forwarding_rule_ref.project or region != forwarding_rule_ref.region:
          raise exceptions.InvalidArgumentException(
              '--service-directory-registration',
              'Service Directory registration must be in the same project and region as the forwarding rule.'
          )

        sd_registration = client.messages.ForwardingRuleServiceDirectoryRegistration(
            namespace=match.group(3), service=match.group(4))
        forwarding_rule.serviceDirectoryRegistrations.append(sd_registration)

    request = client.messages.ComputeForwardingRulesInsertRequest(
        forwardingRule=forwarding_rule,
        project=forwarding_rule_ref.project,
        region=forwarding_rule_ref.region)

    return [(client.apitools_client.forwardingRules, 'Insert', request)]
コード例 #6
0
  def _CreateGlobalRequests(self, client, resources, args, forwarding_rule_ref):
    """Create a globally scoped request."""

    is_psc_google_apis = False
    if hasattr(args,
               'target_google_apis_bundle') and args.target_google_apis_bundle:
      if not self._support_psc_google_apis:
        raise exceptions.InvalidArgumentException(
            '--target-google-apis-bundle',
            'Private Service Connect for Google APIs (the target-google-apis-bundle option '
            'for forwarding rules) is not supported in this API version.')
      else:
        is_psc_google_apis = True

    sd_registration = None
    if hasattr(args, 'service_directory_registration'
              ) and args.service_directory_registration:
      if not is_psc_google_apis:
        raise exceptions.InvalidArgumentException(
            '--service-directory-registration',
            'Can only be specified for regional forwarding rules or Private Service Connect forwarding rules targeting a Google APIs bundle.'
        )
      # Parse projects/../locations/..
      match = re.match(
          r'^projects/([^/]+)/locations/([^/]+)(?:/namespaces/([^/]+))?$',
          args.service_directory_registration)
      if not match:
        raise exceptions.InvalidArgumentException(
            '--service-directory-registration',
            'Must be of the form projects/PROJECT/locations/REGION or projects/PROJECT/locations/REGION/namespaces/NAMESPACE'
        )
      project = match.group(1)
      region = match.group(2)
      namespace = match.group(3)

      if project != forwarding_rule_ref.project:
        raise exceptions.InvalidArgumentException(
            '--service-directory-registration',
            'Must be in the same project as the forwarding rule.')

      sd_registration = client.messages.ForwardingRuleServiceDirectoryRegistration(
          serviceDirectoryRegion=region, namespace=namespace)

    ports_all_specified, range_list = _ExtractPortsAndAll(args.ports)
    port_range = _MakeSingleUnifiedPortRange(args.port_range, range_list)
    # All global forwarding rules must use EXTERNAL or INTERNAL_SELF_MANAGED
    # schemes presently.
    load_balancing_scheme = _GetLoadBalancingScheme(args, client.messages,
                                                    is_psc_google_apis)

    if is_psc_google_apis:
      rule_name = forwarding_rule_ref.Name()
      if len(rule_name) > 20 or rule_name[0].isdigit(
      ) or not rule_name.isalnum():
        raise fw_exceptions.ArgumentError(
            'A forwarding rule to Google APIs must have a name that is between '
            ' 1-20 characters long, alphanumeric, starting with a letter.')

      if port_range:
        raise exceptions.InvalidArgumentException(
            '--ports',
            '[--ports] is not allowed for PSC-GoogleApis forwarding rules.')
      if load_balancing_scheme:
        raise exceptions.InvalidArgumentException(
            '--load-balancing-scheme',
            'The --load-balancing-scheme flag is not allowed for PSC-GoogleApis'
            ' forwarding rules.')

      if args.target_google_apis_bundle in flags.PSC_GOOGLE_APIS_BUNDLES:
        target_as_str = args.target_google_apis_bundle
      else:
        bundles_list = ', '.join(flags.PSC_GOOGLE_APIS_BUNDLES)
        raise exceptions.InvalidArgumentException(
            '--target-google-apis-bundle',
            'The valid values for target-google-apis-bundle are: ' +
            bundles_list)
    else:
      # L7XLB in Premium Tier.
      target_ref = utils.GetGlobalTarget(resources, args)
      target_as_str = target_ref.SelfLink()

      if ports_all_specified:
        raise exceptions.InvalidArgumentException(
            '--ports',
            '[--ports] cannot be set to ALL for global forwarding rules.')
      if not port_range:
        raise exceptions.InvalidArgumentException(
            '--ports', '[--ports] is required for global forwarding rules.')

    protocol = self.ConstructProtocol(client.messages, args)

    if args.address is None or args.ip_version:
      ip_version = client.messages.ForwardingRule.IpVersionValueValuesEnum(
          args.ip_version or 'IPV4')
    else:
      ip_version = None

    address = self._ResolveAddress(resources, args,
                                   compute_flags.compute_scope.ScopeEnum.GLOBAL,
                                   forwarding_rule_ref)
    forwarding_rule = client.messages.ForwardingRule(
        description=args.description,
        name=forwarding_rule_ref.Name(),
        IPAddress=address,
        IPProtocol=protocol,
        portRange=port_range,
        target=target_as_str,
        ipVersion=ip_version,
        networkTier=_ConstructNetworkTier(client.messages, args),
        loadBalancingScheme=load_balancing_scheme)

    if sd_registration:
      forwarding_rule.serviceDirectoryRegistrations.append(sd_registration)
    if args.IsSpecified('network'):
      forwarding_rule.network = flags.NetworkArg(
          self._support_l7_internal_load_balancing).ResolveAsResource(
              args, resources).SelfLink()

    if self._support_global_access and args.IsSpecified('allow_global_access'):
      forwarding_rule.allowGlobalAccess = args.allow_global_access

    request = client.messages.ComputeGlobalForwardingRulesInsertRequest(
        forwardingRule=forwarding_rule, project=forwarding_rule_ref.project)

    return [(client.apitools_client.globalForwardingRules, 'Insert', request)]