コード例 #1
0
def CreateNetworkInterfaceMessage(
    resources, scope_lister, messages, network, region, subnet, address,
    alias_ip_ranges_string=None):
  """Creates and returns a new NetworkInterface message.

  Args:
    resources: generates resource references,
    scope_lister: function, provides scopes for prompting subnet region,
    messages: GCE API messages,
    network: network,
    region: region for subnetwork,
    subnet: regional subnetwork,
    address: specify static address for instance template
               * None - no address,
               * EPHEMERAL_ADDRESS - ephemeral address,
               * string - address name to be fetched from GCE API.
    alias_ip_ranges_string: command line string specifying a list of alias
        IP ranges.
  Returns:
    network_interface: a NetworkInterface message object
  """
  # By default interface is attached to default network. If network or subnet
  # are specified they're used instead.
  network_interface = messages.NetworkInterface()
  if subnet is not None:
    subnet_ref = subnet_flags.SubnetworkResolver().ResolveResources(
        [subnet], compute_scope.ScopeEnum.REGION, region, resources,
        scope_lister=scope_lister)[0]
    network_interface.subnetwork = subnet_ref.SelfLink()
  if network is not None:
    network_ref = resources.Parse(
        network,
        params={'project': properties.VALUES.core.project.GetOrFail},
        collection='compute.networks')
    network_interface.network = network_ref.SelfLink()
  elif subnet is None:
    network_ref = resources.Parse(
        constants.DEFAULT_NETWORK,
        params={'project': properties.VALUES.core.project.GetOrFail},
        collection='compute.networks')
    network_interface.network = network_ref.SelfLink()

  if address:
    access_config = messages.AccessConfig(
        name=constants.DEFAULT_ACCESS_CONFIG_NAME,
        type=messages.AccessConfig.TypeValueValuesEnum.ONE_TO_ONE_NAT)

    # If the user provided an external IP, populate the access
    # config with it.
    if address != EPHEMERAL_ADDRESS:
      access_config.natIP = address

    network_interface.accessConfigs = [access_config]

  if alias_ip_ranges_string:
    network_interface.aliasIpRanges = (
        alias_ip_range_utils.CreateAliasIpRangeMessagesFromString(
            messages, False, alias_ip_ranges_string))

  return network_interface
コード例 #2
0
def _ParseSubnetFields(args, compute_holder):
    """Parses arguments related to subnets to use for NAT."""
    subnetworks = list()
    messages = compute_holder.client.messages
    if args.subnet_option == nat_flags.SubnetOption.ALL_RANGES:
        ranges_to_nat = (
            messages.RouterNat.SourceSubnetworkIpRangesToNatValueValuesEnum.
            ALL_SUBNETWORKS_ALL_IP_RANGES)
    elif args.subnet_option == nat_flags.SubnetOption.PRIMARY_RANGES:
        ranges_to_nat = (
            messages.RouterNat.SourceSubnetworkIpRangesToNatValueValuesEnum.
            ALL_SUBNETWORKS_ALL_PRIMARY_IP_RANGES)
    else:
        ranges_to_nat = (
            messages.RouterNat.SourceSubnetworkIpRangesToNatValueValuesEnum.
            LIST_OF_SUBNETWORKS)

        # Mapping of subnet names to SubnetUsage.
        subnet_usages = dict()

        for custom_subnet_arg in args.nat_custom_subnet_ip_ranges:
            colons = custom_subnet_arg.count(':')
            secondary_range = None
            if colons > 1:
                raise calliope_exceptions.InvalidArgumentException(
                    '--nat-custom-subnet-ip-ranges',
                    ('Each specified subnet must be of the form SUBNETWORK '
                     'or SUBNETWORK:RANGE_NAME'))
            elif colons == 1:
                subnet_name, secondary_range = custom_subnet_arg.split(':')
            else:
                subnet_name = custom_subnet_arg

            if subnet_name not in subnet_usages:
                subnet_usages[subnet_name] = SubnetUsage()

            if secondary_range is not None:
                subnet_usages[subnet_name].secondary_ranges.append(
                    secondary_range)
            else:
                subnet_usages[subnet_name].using_primary = True

        for subnet_name in subnet_usages:
            subnet_ref = subnet_flags.SubnetworkResolver().ResolveResources(
                [subnet_name],
                compute_scope.ScopeEnum.REGION,
                args.region,
                compute_holder.resources,
                scope_lister=compute_flags.GetDefaultScopeLister(
                    compute_holder.client))

            subnet_usage = subnet_usages[subnet_name]

            options = []
            if subnet_usage.using_primary:
                options.append(messages.RouterNatSubnetworkToNat.
                               SourceIpRangesToNatValueListEntryValuesEnum.
                               PRIMARY_IP_RANGE)
            if subnet_usage.secondary_ranges:
                options.append(messages.RouterNatSubnetworkToNat.
                               SourceIpRangesToNatValueListEntryValuesEnum.
                               LIST_OF_SECONDARY_IP_RANGES)

            subnetworks.append({
                'name':
                six.text_type(subnet_ref[0]),
                'sourceIpRangesToNat':
                options,
                'secondaryIpRangeNames':
                subnet_usage.secondary_ranges
            })
    # Sorted for test stability.
    return (ranges_to_nat,
            sorted(subnetworks, key=lambda subnet: subnet['name']))
コード例 #3
0
def CreateNetworkInterfaceMessage(resources,
                                  scope_lister,
                                  messages,
                                  network,
                                  private_ip,
                                  region,
                                  subnet,
                                  address,
                                  alias_ip_ranges_string=None,
                                  network_tier=None,
                                  stack_type=None,
                                  ipv6_network_tier=None):
    """Creates and returns a new NetworkInterface message.

  Args:
    resources: generates resource references,
    scope_lister: function, provides scopes for prompting subnet region,
    messages: GCE API messages,
    network: network,
    private_ip: IPv4 internal IP address to assign to the instance.
    region: region for subnetwork,
    subnet: regional subnetwork,
    address: specify static address for instance template
               * None - no address,
               * EPHEMERAL_ADDRESS - ephemeral address,
               * string - address name to be fetched from GCE API.
    alias_ip_ranges_string: command line string specifying a list of alias
        IP ranges.
    network_tier: specify network tier for instance template
               * None - no network tier
               * PREMIUM - network tier being PREMIUM
               * SELECT - network tier being SELECT
               * STANDARD - network tier being STANDARD
    stack_type: identify whether IPv6 features are enabled
               * IPV4_ONLY - can only have IPv4 address
               * IPV4_IPV6 - can have both IPv4 and IPv6 address
    ipv6_network_tier: specify network tier for IPv6 access config
               * PREMIUM - network tier being PREMIUM
               * STANDARD - network tier being STANDARD
  Returns:
    network_interface: a NetworkInterface message object
  """
    # By default interface is attached to default network. If network or subnet
    # are specified they're used instead.
    network_interface = messages.NetworkInterface()
    if subnet is not None:
        subnet_ref = subnet_flags.SubnetworkResolver().ResolveResources(
            [subnet],
            compute_scope.ScopeEnum.REGION,
            region,
            resources,
            scope_lister=scope_lister)[0]
        network_interface.subnetwork = subnet_ref.SelfLink()
    if network is not None:
        network_ref = resources.Parse(
            network,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='compute.networks')
        network_interface.network = network_ref.SelfLink()
    elif subnet is None:
        network_ref = resources.Parse(
            constants.DEFAULT_NETWORK,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='compute.networks')
        network_interface.network = network_ref.SelfLink()

    if private_ip is not None:
        network_interface.networkIP = private_ip

    if stack_type is not None:
        network_interface.stackType = (
            messages.NetworkInterface.StackTypeValueValuesEnum(stack_type))

    if address:
        access_config = messages.AccessConfig(
            name=constants.DEFAULT_ACCESS_CONFIG_NAME,
            type=messages.AccessConfig.TypeValueValuesEnum.ONE_TO_ONE_NAT)

        # If the user provided an external IP, populate the access
        # config with it.
        if address != EPHEMERAL_ADDRESS:
            access_config.natIP = address

        if network_tier is not None:
            access_config.networkTier = (
                messages.AccessConfig.NetworkTierValueValuesEnum(network_tier))

        network_interface.accessConfigs = [access_config]

    if ipv6_network_tier is not None:
        ipv6_access_config = messages.AccessConfig(
            name=constants.DEFAULT_IPV6_ACCESS_CONFIG_NAME,
            type=messages.AccessConfig.TypeValueValuesEnum.DIRECT_IPV6)
        ipv6_access_config.networkTier = (
            messages.AccessConfig.NetworkTierValueValuesEnum(ipv6_network_tier)
        )

        network_interface.ipv6AccessConfigs = [ipv6_access_config]

    if alias_ip_ranges_string:
        network_interface.aliasIpRanges = (
            alias_ip_range_utils.CreateAliasIpRangeMessagesFromString(
                messages, False, alias_ip_ranges_string))

    return network_interface