Ejemplo n.º 1
0
def ParseMultiScopeFlags(args, resources):
    """Make Frontend suitable for MultiScopeLister argument namespace.

  Generated client-side filter is stored to args.filter.

  Args:
    args: The argument namespace of MultiScopeLister.
    resources: resources.Registry, The resource registry

  Returns:
    Frontend initialized with information from MultiScopeLister argument
    namespace.
  """
    frontend = _GetBaseListerFrontendPrototype(args)
    filter_expr = frontend.filter
    if getattr(args, 'zones', None):
        filter_expr, scope_set = _TranslateZonesFlag(args, resources)
    elif getattr(args, 'regions', None):
        filter_expr, scope_set = _TranslateRegionsFlag(args, resources)
    elif getattr(args, 'global', None):
        scope_set = GlobalScope([
            resources.Parse(properties.VALUES.core.project.GetOrFail(),
                            collection='compute.projects')
        ])
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter)
    else:
        scope_set = AllScopes([
            resources.Parse(properties.VALUES.core.project.GetOrFail(),
                            collection='compute.projects')
        ],
                              zonal='zones' in args,
                              regional='regions' in args)
    return _Frontend(filter_expr, frontend.max_results, scope_set)
Ejemplo n.º 2
0
    def Run(self, args):
        client = base_classes.ComputeApiHolder(
            self.ReleaseTrack()).client.apitools_client

        compute_interconnect_attachments = client.interconnectAttachments

        messages = client.MESSAGES_MODULE
        project = properties.VALUES.core.project.GetOrFail()

        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter)

        request = messages.ComputeInterconnectAttachmentsAggregatedListRequest(
            project=project, filter=filter_expr)

        # TODO(b/34871930): Write and use helper for handling listing.
        interconnect_attachments_lists, next_page_token = self._GetListPage(
            compute_interconnect_attachments, request)
        while next_page_token:
            request.pageToken = next_page_token
            interconnect_attachments_list_page, next_page_token = self._GetListPage(
                compute_interconnect_attachments, request)
            interconnect_attachments_lists += interconnect_attachments_list_page

        return interconnect_attachments_lists
Ejemplo n.º 3
0
  def _Run(self, args, support_global_scope=False):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client
    messages = client.messages

    neg_ref = flags.MakeNetworkEndpointGroupsArg(
        support_global_scope=support_global_scope).ResolveAsResource(
            args,
            holder.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(client))

    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)

    if hasattr(neg_ref, 'zone'):
      request = messages.ComputeNetworkEndpointGroupsListNetworkEndpointsRequest(
          networkEndpointGroup=neg_ref.Name(),
          project=neg_ref.project,
          zone=neg_ref.zone,
          filter=filter_expr)
      service = client.apitools_client.networkEndpointGroups
    else:
      request = messages.ComputeGlobalNetworkEndpointGroupsListNetworkEndpointsRequest(
          networkEndpointGroup=neg_ref.Name(),
          project=neg_ref.project,
          filter=filter_expr)
      service = client.apitools_client.globalNetworkEndpointGroups

    return list_pager.YieldFromList(
        service,
        request,
        method='ListNetworkEndpoints',
        field='items',
        limit=args.limit,
        batch_size=None)
Ejemplo n.º 4
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

    client = holder.client.apitools_client
    messages = client.MESSAGES_MODULE

    project = properties.VALUES.core.project.GetOrFail()

    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)
    request = messages.ComputeNetworksListRequest(
        project=project, filter=filter_expr)

    for network in [network for network in list_pager.YieldFromList(
        client.networks,
        request,
        field='items',
        limit=args.limit,
        batch_size=None) if network.peerings and (
            args.network is None or
            args.network == network.name)]:
      # Network is synthesized for legacy reasons to maintain prior format.
      # In general, synthesized output should not be done.
      synthesized_network = resource_projector.MakeSerializable(network)
      for peering in synthesized_network['peerings']:
        peering['source_network'] = network.selfLink
      yield synthesized_network
    def Run(self, args):
        client = base_classes.ComputeApiHolder(
            self.ReleaseTrack()).client.apitools_client

        network_edge_security_services = client.networkEdgeSecurityServices

        messages = client.MESSAGES_MODULE
        project = properties.VALUES.core.project.GetOrFail()

        display_info = args.GetDisplayInfo()
        defaults = resource_projection_spec.ProjectionSpec(
            symbols=display_info.transforms, aliases=display_info.aliases)
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter, defaults=defaults)

        request = messages.ComputeNetworkEdgeSecurityServicesAggregatedListRequest(
            project=project, filter=filter_expr)

        # TODO(b/34871930): Write and use helper for handling listing.
        resource_lists, next_page_token = (self._GetListPage(
            network_edge_security_services, request))
        while next_page_token:
            request.pageToken = next_page_token
            resource_list_page, next_page_token = (self._GetListPage(
                network_edge_security_services, request))
            resource_lists += resource_list_page

        return resource_lists
Ejemplo n.º 6
0
def RewriteFilter(args, message=None, frontend_fields=None):
    """Rewrites args.filter into client and server filter expression strings.

  Usage:

    args.filter, request_filter = flags.RewriteFilter(args)

  Args:
    args: The parsed args namespace containing the filter expression args.filter
      and display_info.
    message: The response resource message proto for the request.
    frontend_fields: A set of dotted key names supported client side only.

  Returns:
    A (client_filter, server_filter) tuple of filter expression strings.
    None means the filter does not need to applied on the respective
    client/server side.
  """
    if not args.filter:
        return None, None
    display_info = args.GetDisplayInfo()
    defaults = resource_projection_spec.ProjectionSpec(
        symbols=display_info.transforms, aliases=display_info.aliases)
    client_filter, server_filter = filter_rewrite.Rewriter(
        message=message,
        frontend_fields=frontend_fields).Rewrite(args.filter,
                                                 defaults=defaults)
    log.info('client_filter=%r server_filter=%r', client_filter, server_filter)
    return client_filter, server_filter
Ejemplo n.º 7
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        helper = vpn_tunnels_utils.VpnTunnelHelper(holder)

        project = properties.VALUES.core.project.GetOrFail()
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter)
        return helper.List(project=project, filter_expr=filter_expr)
Ejemplo n.º 8
0
  def Run(self, args):
    """Issues the request to list all VPN Gateways."""
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    helper = vpn_gateways_utils.VpnGatewayHelper(holder)

    project = properties.VALUES.core.project.GetOrFail()
    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)
    return helper.List(project=project, filter_expr=filter_expr)
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        helper = vpn_tunnels_utils.VpnTunnelHelper(holder)

        project = properties.VALUES.core.project.GetOrFail()
        display_info = args.GetDisplayInfo()
        defaults = resource_projection_spec.ProjectionSpec(
            symbols=display_info.transforms, aliases=display_info.aliases)
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter, defaults=defaults)
        return helper.List(project=project, filter_expr=filter_expr)
Ejemplo n.º 10
0
def _GetListCommandFrontendPrototype(args):
    """Make Frontend suitable for ListCommand argument namespace.

  Generated filter is a pair (client-side filter, server-side filter).

  Args:
    args: The argument namespace of ListCommand.

  Returns:
    Frontend initialized with information from ListCommand argument namespace.
    Both client-side and server-side filter is returned.
  """
    filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)
    max_results = int(args.page_size) if args.page_size else None
    if args.limit and (max_results is None or max_results > args.limit):
        max_results = args.limit
    return _Frontend(filter_expr=filter_expr, maxResults=max_results)
Ejemplo n.º 11
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

    client = holder.client.apitools_client
    messages = client.MESSAGES_MODULE

    project = properties.VALUES.core.project.Get(required=True)

    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)

    request = messages.ComputeTargetTcpProxiesListRequest(
        project=project,
        filter=filter_expr
    )

    return list_pager.YieldFromList(
        client.targetTcpProxies, request, field='items',
        limit=args.limit, batch_size=None)
Ejemplo n.º 12
0
def _TranslateRegionsFlag(args, resources):
    """Translates --regions flag into filter expression and scope set."""
    scope_set = RegionSet([
        resources.Parse(
            region,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='compute.regions') for region in args.regions
    ])
    # Refine args.filter specification to reuse gcloud filtering logic
    # for filtering based on regions
    filter_arg = '({}) AND '.format(args.filter) if args.filter else ''
    # How to escape '*' in region and what are special characters for
    # simple pattern?
    region_regexp = ' '.join([region for region in args.regions])
    region_arg = '(region :({}))'.format(region_regexp)
    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(filter_arg +
                                                                 region_arg)
    return filter_expr, scope_set
Ejemplo n.º 13
0
def _TranslateZonesFlag(args, resources):
    """Translates --zones flag into filter expression and scope set."""
    scope_set = ZoneSet([
        resources.Parse(
            z,
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='compute.zones') for z in args.zones
    ])
    # Refine args.filter specification to reuse gcloud filtering logic
    # for filtering based on zones
    filter_arg = '({}) AND '.format(args.filter) if args.filter else ''
    # How to escape '*' in zone and what are special characters for
    # simple pattern?
    zone_regexp = ' '.join([zone for zone in args.zones])
    zone_arg = '(zone :({}))'.format(zone_regexp)
    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(filter_arg +
                                                                 zone_arg)
    return filter_expr, scope_set
def _GetOperations(compute_client, project, operation_group_id):
    """Requests operations with group id matching the given one."""

    errors_to_collect = []

    _, operation_filter = filter_rewrite.Rewriter().Rewrite(
        expression='operationGroupId=' + operation_group_id)

    operations_response = compute_client.MakeRequests(
        [(compute_client.apitools_client.globalOperations, 'AggregatedList',
          compute_client.apitools_client.globalOperations.GetRequestType(
              'AggregatedList')(filter=operation_filter, project=project))],
        errors_to_collect=errors_to_collect,
        log_result=False,
        always_return_operation=True,
        no_followup=True)

    return operations_response, errors_to_collect
Ejemplo n.º 15
0
def ParseNamesAndRegexpFlags(args, resources):
    """Makes Frontend suitable for GlobalLister argument namespace.

  Stores generated client-side filter in args.filter.

  Args:
    args: The argument namespace of BaseLister.
    resources: resources.Registry, The resource registry

  Returns:
    Frontend initialized with information from BaseLister argument namespace.
  """
    frontend = _GetBaseListerFrontendPrototype(args)
    scope_set = GlobalScope([
        resources.Parse(properties.VALUES.core.project.GetOrFail(),
                        collection='compute.projects')
    ])
    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)
    return _Frontend(filter_expr, frontend.max_results, scope_set)
Ejemplo n.º 16
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

        client = holder.client.apitools_client
        messages = client.MESSAGES_MODULE

        project = properties.VALUES.core.project.GetOrFail()

        if args.filter:
            filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)
        else:
            filter_expr = None
        request = messages.ComputeInterconnectsListRequest(project=project,
                                                           filter=filter_expr)

        return list_pager.YieldFromList(client.interconnects,
                                        request,
                                        field='items',
                                        limit=args.limit,
                                        batch_size=None)
Ejemplo n.º 17
0
def RewriteFilter(args):
    """Rewrites args.filter into client and server filter expression strings.

  Usage:

    args.filter, request_filter = flags.RewriteFilter(args)

  Args:
    args: The parsed args namespace containing the filter expression args.filter
      and display_info.

  Returns:
    A (client_expression, server_expression) tuple of expression strings. None
    means the expression does not need to applied on the respective
    client/server side.
  """
    display_info = args.GetDisplayInfo()
    defaults = resource_projection_spec.ProjectionSpec(
        symbols=display_info.transforms, aliases=display_info.aliases)
    return filter_rewrite.Rewriter().Rewrite(args.filter, defaults=defaults)
Ejemplo n.º 18
0
def ParseZonalFlags(args, resources):
    """Make Frontend suitable for ZonalLister argument namespace.

  Generated client-side filter is stored to args.filter.

  Args:
    args: The argument namespace of BaseLister.
    resources: resources.Registry, The resource registry

  Returns:
    Frontend initialized with information from BaseLister argument namespace.
    Server-side filter is None.
  """
    frontend = _GetBaseListerFrontendPrototype(args)
    filter_expr = frontend.filter
    if args.zones:
        scope_set = ZoneSet([
            resources.Parse(
                z,
                params={'project': properties.VALUES.core.project.GetOrFail},
                collection='compute.zones') for z in args.zones
        ])
        # Refine args.filter specification to reuse gcloud filtering logic
        # for filtering based on zones
        filter_arg = '({}) AND '.format(args.filter) if args.filter else ''
        # How to escape '*' in zone and what are special characters for
        # simple pattern?
        zone_regexp = ' '.join(['*' + zone for zone in args.zones])
        zone_arg = '(zone :({}))'.format(zone_regexp)
        args.filter = filter_arg + zone_arg
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter)
    else:
        scope_set = AllScopes([
            resources.Parse(properties.VALUES.core.project.GetOrFail(),
                            collection='compute.projects')
        ],
                              zonal=True,
                              regional=False)
    frontend = _Frontend(filter_expr, frontend.max_results, scope_set)
    return frontend
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

        client = holder.client.apitools_client
        messages = client.MESSAGES_MODULE

        project = properties.VALUES.core.project.Get(required=True)

        display_info = args.GetDisplayInfo()
        defaults = resource_projection_spec.ProjectionSpec(
            symbols=display_info.transforms, aliases=display_info.aliases)
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter, defaults=defaults)
        request = messages.ComputeSecurityPoliciesListRequest(
            project=project, filter=filter_expr)

        return list_pager.YieldFromList(client.securityPolicies,
                                        request,
                                        field='items',
                                        limit=args.limit,
                                        batch_size=None)
Ejemplo n.º 20
0
  def Run(self, args):
    compute_interconnect_attachments = apis.GetClientInstance(
        'compute', 'alpha').interconnectAttachments

    messages = apis.GetMessagesModule('compute', 'alpha')
    project = properties.VALUES.core.project.GetOrFail()

    args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(args.filter)

    request = messages.ComputeInterconnectAttachmentsAggregatedListRequest(
        project=project, filter=filter_expr)

    # TODO(b/34871930): Write and use helper for handling listing.
    interconnect_attachments_lists, next_page_token = self._GetListPage(
        compute_interconnect_attachments, request)
    while next_page_token:
      request.pageToken = next_page_token
      interconnect_attachments_list_page, next_page_token = self._GetListPage(
          compute_interconnect_attachments, request)
      interconnect_attachments_lists += interconnect_attachments_list_page

    return interconnect_attachments_lists
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client.apitools_client

        project = properties.VALUES.core.project.Get(required=True)

        display_info = args.GetDisplayInfo()
        defaults = resource_projection_spec.ProjectionSpec(
            symbols=display_info.transforms, aliases=display_info.aliases)
        args.filter, filter_expr = filter_rewrite.Rewriter().Rewrite(
            args.filter, defaults=defaults)

        if not self._enable_region_target_tcp_proxy:
            return self._ListGlobal(client, project, args, filter_expr)

        # TODO(b/229835962): switch to lister.MultiScopeLister
        #   until then emulate AggregatedList behaviour similar to
        #   third_party/py/googlecloudsdk/surface/compute/\
        #     network_firewall_policies/list.py
        global_items = []
        regional_items = []

        if args.regions:
            regional_items = (self._ListRegional(client, project, args,
                                                 filter_expr, args.regions))
        elif getattr(args, 'global', None):
            global_items = self._ListGlobal(client, project, args, filter_expr)
        else:
            global_items = self._ListGlobal(client, project, args, filter_expr)
            all_regions = self._GetRegions(client, project)
            regional_items = (self._ListRegional(client, project, args,
                                                 filter_expr, all_regions))

        all_items = itertools.chain(global_items, regional_items)
        if args.limit:
            all_items = itertools.islice(all_items, args.limit)
        return all_items