def _CreateGlobalRequests(self, client, resources, args, forwarding_rule_ref):
    """Create a globally scoped request."""
    port_range = _ResolvePortRange(args.port_range, args.ports)
    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)

    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())
    if args.load_balancing_scheme == 'INTERNAL':
      forwarding_rule.loadBalancingScheme = (
          client.messages.ForwardingRule
          .LoadBalancingSchemeValueValuesEnum.INTERNAL)

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

    return [(client.apitools_client.globalForwardingRules, 'Insert', request)]
Esempio n. 2
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,
            include_alpha=(self.ReleaseTrack() == base.ReleaseTrack.ALPHA))
        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 self._support_network_in_global_request and args.network is not None:
            forwarding_rule.network = flags.NETWORK_ARG_ALPHA.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)]
Esempio n. 3
0
    def CreateGlobalRequests(self, client, resources, forwarding_rule_ref,
                             args):
        """Create a globally scoped request."""
        target_ref = utils.GetGlobalTarget(resources, args)

        request = client.messages.ComputeGlobalForwardingRulesSetTargetRequest(
            forwardingRule=forwarding_rule_ref.Name(),
            project=forwarding_rule_ref.project,
            targetReference=client.messages.TargetReference(
                target=target_ref.SelfLink(), ),
        )

        return [(client.apitools_client.globalForwardingRules, 'SetTarget',
                 request)]
Esempio n. 4
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)
        network_tier = self.ConstructNetworkTier(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=network_tier,
            loadBalancingScheme=_GetLoadBalancingScheme(args, client.messages))

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

        return [(client.apitools_client.globalForwardingRules, 'Insert',
                 request)]
Esempio n. 5
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)]
  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)]