Exemple #1
0
    def _CreateSubnetwork(self, messages, subnet_ref, network_ref, args):
        subnetwork = super(CreateAlpha,
                           self)._CreateSubnetwork(messages, subnet_ref,
                                                   network_ref, args)

        if (args.enable_flow_logs is not None
                or args.aggregation_interval is not None
                or args.flow_sampling is not None
                or args.metadata is not None):
            log_config = messages.SubnetworkLogConfig(
                enable=args.enable_flow_logs)
            if args.aggregation_interval:
                log_config.aggregationInterval = (
                    flags.GetLoggingAggregationIntervalArgAlpha(
                        messages).GetEnumForChoice(args.aggregation_interval))
            if args.flow_sampling is not None:
                log_config.flowSampling = args.flow_sampling
            if args.metadata:
                log_config.metadata = flags.GetLoggingMetadataArgAlpha(
                    messages).GetEnumForChoice(args.metadata)
            subnetwork.logConfig = log_config

        if getattr(args, 'role', None):
            subnetwork.role = messages.Subnetwork.RoleValueValuesEnum(
                args.role)

        if args.enable_private_ipv6_access is not None:
            subnetwork.enablePrivateV6Access = args.enable_private_ipv6_access
        if args.private_ipv6_google_access_type is not None:
            subnetwork.privateIpv6GoogleAccess = (
                flags.GetPrivateIpv6GoogleAccessTypeFlagMapper(
                    messages).GetEnumForChoice(
                        args.private_ipv6_google_access_type))

        if args.purpose:
            subnetwork.purpose = messages.Subnetwork.PurposeValueValuesEnum(
                args.purpose)
        if (subnetwork.purpose == messages.Subnetwork.PurposeValueValuesEnum.
                INTERNAL_HTTPS_LOAD_BALANCER):
            # Clear unsupported fields in the subnet resource
            subnetwork.privateIpGoogleAccess = None
            subnetwork.enableFlowLogs = None
            subnetwork.logConfig = None

        return subnetwork
Exemple #2
0
def MakeSubnetworkUpdateRequest(
        client,
        subnet_ref,
        include_alpha_logging,
        enable_private_ip_google_access=None,
        add_secondary_ranges=None,
        remove_secondary_ranges=None,
        enable_flow_logs=None,
        aggregation_interval=None,
        flow_sampling=None,
        metadata=None,
        set_role_active=None,
        drain_timeout_seconds=None,
        enable_private_ipv6_access=None,
        private_ipv6_google_access_type=None,
        private_ipv6_google_access_service_accounts=None):
    """Make the appropriate update request for the args.

  Args:
    client: GCE API client
    subnet_ref: Reference to a subnetwork
    include_alpha_logging: Include alpha-specific logging args.
    enable_private_ip_google_access: Enable/disable access to Google Cloud APIs
      from this subnet for instances without a public ip address.
    add_secondary_ranges: List of secondary IP ranges to add to the subnetwork
      for use in IP aliasing.
    remove_secondary_ranges: List of secondary ranges to remove from the
      subnetwork.
    enable_flow_logs: Enable/disable flow logging for this subnet.
    aggregation_interval: The internal at which to aggregate flow logs.
    flow_sampling: The sampling rate for flow logging in this subnet.
    metadata: Whether metadata fields should be added reported flow logs.
    set_role_active: Updates the role of a BACKUP subnet to ACTIVE.
    drain_timeout_seconds: The maximum amount of time to drain connections from
      the active subnet to the backup subnet with set_role_active=True.
    enable_private_ipv6_access: Enable/disable private IPv6 access for the
      subnet.
    private_ipv6_google_access_type: The private IPv6 google access type for the
      VMs in this subnet.
    private_ipv6_google_access_service_accounts: The service accounts can be
      used to selectively turn on Private IPv6 Google Access only on the VMs
      primary service account matching the value.

  Returns:
    response, result of sending the update request for the subnetwork
  """
    convert_to_enum = lambda x: x.replace('-', '_').upper()
    if enable_private_ip_google_access is not None:
        google_access = (
            client.messages.SubnetworksSetPrivateIpGoogleAccessRequest())
        google_access.privateIpGoogleAccess = enable_private_ip_google_access

        google_access_request = (
            client.messages.ComputeSubnetworksSetPrivateIpGoogleAccessRequest(
                project=subnet_ref.project,
                region=subnet_ref.region,
                subnetwork=subnet_ref.Name(),
                subnetworksSetPrivateIpGoogleAccessRequest=google_access))
        return client.MakeRequests([
            (client.apitools_client.subnetworks, 'SetPrivateIpGoogleAccess',
             google_access_request)
        ])
    elif add_secondary_ranges is not None:
        subnetwork = client.MakeRequests([
            (client.apitools_client.subnetworks, 'Get',
             client.messages.ComputeSubnetworksGetRequest(
                 **subnet_ref.AsDict()))
        ])[0]

        for secondary_range in add_secondary_ranges:
            for range_name, ip_cidr_range in sorted(
                    six.iteritems(secondary_range)):
                subnetwork.secondaryIpRanges.append(
                    client.messages.SubnetworkSecondaryRange(
                        rangeName=range_name, ipCidrRange=ip_cidr_range))

        return client.MakeRequests(
            [CreateSubnetworkPatchRequest(client, subnet_ref, subnetwork)])
    elif remove_secondary_ranges is not None:
        subnetwork = client.MakeRequests([
            (client.apitools_client.subnetworks, 'Get',
             client.messages.ComputeSubnetworksGetRequest(
                 **subnet_ref.AsDict()))
        ])[0]

        for name in remove_secondary_ranges[0]:
            if name not in [r.rangeName for r in subnetwork.secondaryIpRanges]:
                raise calliope_exceptions.UnknownArgumentException(
                    'remove-secondary-ranges',
                    'Subnetwork does not have a range {}, '
                    'present ranges are {}.'.format(
                        name,
                        [r.rangeName for r in subnetwork.secondaryIpRanges]))
        subnetwork.secondaryIpRanges = [
            r for r in subnetwork.secondaryIpRanges
            if r.rangeName not in remove_secondary_ranges[0]
        ]

        cleared_fields = []
        if not subnetwork.secondaryIpRanges:
            cleared_fields.append('secondaryIpRanges')
        with client.apitools_client.IncludeFields(cleared_fields):
            return client.MakeRequests(
                [CreateSubnetworkPatchRequest(client, subnet_ref, subnetwork)])
    elif (enable_flow_logs is not None or aggregation_interval is not None
          or flow_sampling is not None or metadata is not None):
        subnetwork = client.messages.Subnetwork()
        original_subnetwork = client.MakeRequests([
            (client.apitools_client.subnetworks, 'Get',
             client.messages.ComputeSubnetworksGetRequest(
                 **subnet_ref.AsDict()))
        ])[0]
        subnetwork.fingerprint = original_subnetwork.fingerprint

        if include_alpha_logging:
            log_config = client.messages.SubnetworkLogConfig(
                enable=enable_flow_logs)
            if aggregation_interval is not None:
                log_config.aggregationInterval = (
                    flags.GetLoggingAggregationIntervalArgAlpha(
                        client.messages).GetEnumForChoice(aggregation_interval)
                )
            if flow_sampling is not None:
                log_config.flowSampling = flow_sampling
            if metadata is not None:
                log_config.metadata = flags.GetLoggingMetadataArgAlpha(
                    client.messages).GetEnumForChoice(metadata)
            subnetwork.logConfig = log_config
        else:
            log_config = client.messages.SubnetworkLogConfig(
                enable=enable_flow_logs)
            if aggregation_interval is not None:
                log_config.aggregationInterval = flags.GetLoggingAggregationIntervalArg(
                    client.messages).GetEnumForChoice(aggregation_interval)
            if flow_sampling is not None:
                log_config.flowSampling = flow_sampling
            if metadata is not None:
                log_config.metadata = flags.GetLoggingMetadataArg(
                    client.messages).GetEnumForChoice(metadata)
            subnetwork.logConfig = log_config

        return client.MakeRequests(
            [CreateSubnetworkPatchRequest(client, subnet_ref, subnetwork)])
    elif (private_ipv6_google_access_type is not None
          or private_ipv6_google_access_service_accounts is not None):
        subnetwork = client.MakeRequests([
            (client.apitools_client.subnetworks, 'Get',
             client.messages.ComputeSubnetworksGetRequest(
                 **subnet_ref.AsDict()))
        ])[0]

        cleared_fields = []
        if private_ipv6_google_access_type is not None:
            subnetwork.privateIpv6GoogleAccess = (
                client.messages.Subnetwork.
                PrivateIpv6GoogleAccessValueValuesEnum(
                    ConvertPrivateIpv6GoogleAccess(
                        convert_to_enum(private_ipv6_google_access_type))))
        if private_ipv6_google_access_service_accounts is not None:
            subnetwork.privateIpv6GoogleAccessServiceAccounts = (
                private_ipv6_google_access_service_accounts)
            if not private_ipv6_google_access_service_accounts:
                cleared_fields.append('privateIpv6GoogleAccessServiceAccounts')
        with client.apitools_client.IncludeFields(cleared_fields):
            return client.MakeRequests(
                [CreateSubnetworkPatchRequest(client, subnet_ref, subnetwork)])
    elif enable_private_ipv6_access is not None:
        subnetwork = client.MakeRequests([
            (client.apitools_client.subnetworks, 'Get',
             client.messages.ComputeSubnetworksGetRequest(
                 **subnet_ref.AsDict()))
        ])[0]

        subnetwork.enablePrivateV6Access = enable_private_ipv6_access
        subnetwork.privateIpv6GoogleAccess = None
        return client.MakeRequests(
            [CreateSubnetworkPatchRequest(client, subnet_ref, subnetwork)])
    elif set_role_active is not None:
        subnetwork = client.MakeRequests([
            (client.apitools_client.subnetworks, 'Get',
             client.messages.ComputeSubnetworksGetRequest(
                 **subnet_ref.AsDict()))
        ])[0]

        subnetwork.role = client.messages.Subnetwork.RoleValueValuesEnum.ACTIVE
        patch_request = client.messages.ComputeSubnetworksPatchRequest(
            project=subnet_ref.project,
            subnetwork=subnet_ref.subnetwork,
            region=subnet_ref.region,
            subnetworkResource=subnetwork,
            drainTimeoutSeconds=drain_timeout_seconds)
        return client.MakeRequests([(client.apitools_client.subnetworks,
                                     'Patch', patch_request)])

    return client.MakeRequests([])
Exemple #3
0
def _CreateSubnetwork(messages, subnet_ref, network_ref, args,
                      include_alpha_logging, include_beta_logging,
                      include_l7_internal_load_balancing,
                      include_private_ipv6_access):
    """Create the subnet resource."""
    subnetwork = messages.Subnetwork(
        name=subnet_ref.Name(),
        description=args.description,
        network=network_ref.SelfLink(),
        ipCidrRange=args.range,
        privateIpGoogleAccess=args.enable_private_ip_google_access,
        enableFlowLogs=args.enable_flow_logs)

    if include_alpha_logging:
        if (args.enable_flow_logs is not None
                or args.aggregation_interval is not None
                or args.flow_sampling is not None
                or args.metadata is not None):
            log_config = messages.SubnetworkLogConfig(
                enable=args.enable_flow_logs)
            if args.aggregation_interval:
                log_config.aggregationInterval = (
                    flags.GetLoggingAggregationIntervalArgAlpha(
                        messages).GetEnumForChoice(args.aggregation_interval))
            if args.flow_sampling is not None:
                log_config.flowSampling = args.flow_sampling
            if args.metadata:
                log_config.metadata = flags.GetLoggingMetadataArgAlpha(
                    messages).GetEnumForChoice(args.metadata)
            subnetwork.logConfig = log_config
        if getattr(args, 'role', None):
            subnetwork.role = messages.Subnetwork.RoleValueValuesEnum(
                args.role)
    elif include_beta_logging:
        if (args.enable_flow_logs is not None
                or args.logging_aggregation_interval is not None
                or args.logging_flow_sampling is not None
                or args.logging_metadata is not None):
            log_config = messages.SubnetworkLogConfig(
                enable=args.enable_flow_logs)
            if args.logging_aggregation_interval:
                log_config.aggregationInterval = flags.GetLoggingAggregationIntervalArg(
                    messages).GetEnumForChoice(
                        args.logging_aggregation_interval)
            if args.logging_flow_sampling is not None:
                log_config.flowSampling = args.logging_flow_sampling
            if args.logging_metadata:
                log_config.metadata = flags.GetLoggingMetadataArg(
                    messages).GetEnumForChoice(args.logging_metadata)
            subnetwork.logConfig = log_config

    if include_l7_internal_load_balancing:
        if args.purpose:
            subnetwork.purpose = messages.Subnetwork.PurposeValueValuesEnum(
                args.purpose)
        if (subnetwork.purpose == messages.Subnetwork.PurposeValueValuesEnum.
                INTERNAL_HTTPS_LOAD_BALANCER):
            # Clear unsupported fields in the subnet resource
            subnetwork.privateIpGoogleAccess = None
            subnetwork.enableFlowLogs = None
            subnetwork.logConfig = None

    if include_private_ipv6_access:
        if args.enable_private_ipv6_access is not None:
            subnetwork.enablePrivateV6Access = args.enable_private_ipv6_access
        if args.private_ipv6_google_access_type is not None:
            subnetwork.privateIpv6GoogleAccess = (
                flags.GetPrivateIpv6GoogleAccessTypeFlagMapper(
                    messages).GetEnumForChoice(
                        args.private_ipv6_google_access_type))

    return subnetwork
def _CreateSubnetwork(messages, subnet_ref, network_ref, args,
                      include_alpha_logging,
                      include_l7_internal_load_balancing,
                      include_private_ipv6_access_alpha,
                      include_private_ipv6_access_beta,
                      include_aggregate_purpose):
    """Create the subnet resource."""
    subnetwork = messages.Subnetwork(
        name=subnet_ref.Name(),
        description=args.description,
        network=network_ref.SelfLink(),
        ipCidrRange=args.range,
        privateIpGoogleAccess=args.enable_private_ip_google_access)

    if (args.enable_flow_logs is not None
            or args.logging_aggregation_interval is not None
            or args.logging_flow_sampling is not None
            or args.logging_metadata is not None
            or (include_alpha_logging and args.logging_filter_expr is not None)
            or
        (include_alpha_logging and args.logging_metadata_fields is not None)):
        log_config = messages.SubnetworkLogConfig(enable=args.enable_flow_logs)
        if args.logging_aggregation_interval:
            log_config.aggregationInterval = flags.GetLoggingAggregationIntervalArg(
                messages).GetEnumForChoice(args.logging_aggregation_interval)
        if args.logging_flow_sampling is not None:
            log_config.flowSampling = args.logging_flow_sampling
        if args.logging_metadata:
            if include_alpha_logging:
                log_config.metadata = flags.GetLoggingMetadataArgAlpha(
                    messages).GetEnumForChoice(args.logging_metadata)
            else:
                log_config.metadata = flags.GetLoggingMetadataArg(
                    messages).GetEnumForChoice(args.logging_metadata)
        if include_alpha_logging and args.logging_filter_expr is not None:
            log_config.filterExpr = args.logging_filter_expr
        if include_alpha_logging and args.logging_metadata_fields is not None:
            log_config.metadataFields = args.logging_metadata_fields
        subnetwork.logConfig = log_config

    if include_alpha_logging:
        if (args.enable_flow_logs is not None
                or args.aggregation_interval is not None
                or args.flow_sampling is not None
                or args.metadata is not None):
            log_config = (subnetwork.logConfig if subnetwork.logConfig
                          is not None else messages.SubnetworkLogConfig(
                              enable=args.enable_flow_logs))
            if args.aggregation_interval:
                log_config.aggregationInterval = (
                    flags.GetLoggingAggregationIntervalArgDeprecated(
                        messages).GetEnumForChoice(args.aggregation_interval))
            if args.flow_sampling is not None:
                log_config.flowSampling = args.flow_sampling
            if args.metadata:
                log_config.metadata = flags.GetLoggingMetadataArgDeprecated(
                    messages).GetEnumForChoice(args.metadata)
            if args.logging_filter_expr is not None:
                log_config.filterExpr = args.logging_filter_expr
            if args.logging_metadata_fields is not None:
                log_config.metadataFields = args.logging_metadata_fields
            subnetwork.logConfig = log_config

    if include_l7_internal_load_balancing:
        if args.purpose:
            subnetwork.purpose = messages.Subnetwork.PurposeValueValuesEnum(
                args.purpose)
        if (subnetwork.purpose == messages.Subnetwork.PurposeValueValuesEnum.
                INTERNAL_HTTPS_LOAD_BALANCER):
            # Clear unsupported fields in the subnet resource
            subnetwork.privateIpGoogleAccess = None
            subnetwork.enableFlowLogs = None
            subnetwork.logConfig = None
        if getattr(args, 'role', None):
            subnetwork.role = messages.Subnetwork.RoleValueValuesEnum(
                args.role)

    # At present aggregate purpose is available only in alpha whereas
    # https_load_balancer is available in Beta. Given Aggregate Purpose Enum
    # is not available in Beta, the code duplication below is necessary.
    if include_aggregate_purpose:
        if args.purpose:
            subnetwork.purpose = messages.Subnetwork.PurposeValueValuesEnum(
                args.purpose)
            if (subnetwork.purpose ==
                    messages.Subnetwork.PurposeValueValuesEnum.AGGREGATE):
                # Clear unsupported fields in the subnet resource
                subnetwork.privateIpGoogleAccess = None
                subnetwork.enableFlowLogs = None
                subnetwork.logConfig = None

    if include_private_ipv6_access_alpha:
        if args.enable_private_ipv6_access is not None:
            subnetwork.enablePrivateV6Access = args.enable_private_ipv6_access
        if args.private_ipv6_google_access_type is not None:
            subnetwork.privateIpv6GoogleAccess = (
                flags.GetPrivateIpv6GoogleAccessTypeFlagMapperAlpha(
                    messages).GetEnumForChoice(
                        args.private_ipv6_google_access_type))
        if args.private_ipv6_google_access_service_accounts is not None:
            subnetwork.privateIpv6GoogleAccessServiceAccounts = (
                args.private_ipv6_google_access_service_accounts)
    elif include_private_ipv6_access_beta:
        if args.private_ipv6_google_access_type is not None:
            subnetwork.privateIpv6GoogleAccess = (
                flags.GetPrivateIpv6GoogleAccessTypeFlagMapperBeta(
                    messages).GetEnumForChoice(
                        args.private_ipv6_google_access_type))

    return subnetwork