Exemplo n.º 1
0
 def _SetInstanceMetadata(self, client, instance, new_metadata):
     """Sets the project metadata to the new metadata."""
     errors = []
     # API wants just the zone name, not the full URL
     zone = instance.zone.split('/')[-1]
     client.MakeRequests(requests=[
         (client.apitools_client.instances, 'SetMetadata',
          client.messages.ComputeInstancesSetMetadataRequest(
              instance=instance.name,
              metadata=new_metadata,
              project=properties.VALUES.core.project.Get(required=True),
              zone=zone))
     ],
                         errors_to_collect=errors)
     if errors:
         utils.RaiseToolException(
             errors,
             error_message='Could not add SSH key to instance metadata:')
Exemplo n.º 2
0
 def GetTargetPool(self):
     """Fetches the target pool resource."""
     errors = []
     objects = list(
         request_helper.MakeRequests(requests=[
             (self.service, 'Get',
              self.messages.ComputeTargetPoolsGetRequest(
                  project=self.project,
                  region=self.target_pool_ref.region,
                  targetPool=self.target_pool_ref.Name()))
         ],
                                     http=self.http,
                                     batch_url=self.batch_url,
                                     errors=errors))
     if errors:
         utils.RaiseToolException(
             errors, error_message='Could not fetch target pool:')
     return objects[0]
Exemplo n.º 3
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        ref = self.TARGET_SSL_PROXY_ARG.ResolveAsResource(
            args, holder.resources)

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

        request = messages.ComputeTargetSslProxiesGetRequest(
            project=ref.project, targetSslProxy=ref.Name())

        errors = []
        resources = holder.client.MakeRequests(
            [(client.targetSslProxies, 'Get', request)], errors)

        if errors:
            utils.RaiseToolException(errors)
        return resources[0]
Exemplo n.º 4
0
    def GetInstance(self, instance_ref):
        """Fetch an instance based on the given instance_ref."""
        request = (self.compute.instances, 'Get',
                   self.messages.ComputeInstancesGetRequest(
                       instance=instance_ref.Name(),
                       project=instance_ref.project,
                       zone=instance_ref.zone))

        errors = []
        objects = list(
            request_helper.MakeRequests(requests=[request],
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors))
        if errors:
            utils.RaiseToolException(errors,
                                     error_message='Could not fetch instance:')
        return objects[0]
Exemplo n.º 5
0
 def MakeRequests(self,
                  requests,
                  errors_to_collect=None,
                  progress_tracker=None,
                  followup_overrides=None):
     """Sends given request in batch mode."""
     errors = errors_to_collect if errors_to_collect is not None else []
     objects = list(
         request_helper.MakeRequests(requests=requests,
                                     http=self._client.http,
                                     batch_url=self._batch_url,
                                     errors=errors,
                                     progress_tracker=progress_tracker,
                                     followup_overrides=followup_overrides))
     if errors_to_collect is None and errors:
         utils.RaiseToolException(errors,
                                  error_message='Could not fetch resource:')
     return objects
Exemplo n.º 6
0
 def GetBackendService(self, _):
     """Fetches the backend service resource."""
     errors = []
     objects = list(
         request_helper.MakeRequests(requests=[
             (self.service, 'Get',
              self.messages.ComputeBackendServicesGetRequest(
                  project=self.project,
                  backendService=self.backend_service_ref.Name()))
         ],
                                     http=self.http,
                                     batch_url=self.batch_url,
                                     errors=errors,
                                     custom_get_requests=None))
     if errors:
         utils.RaiseToolException(
             errors, error_message='Could not fetch backend service:')
     return objects[0]
Exemplo n.º 7
0
    def Run(self, args):
        if not (args.proxy_header or args.backend_service):
            raise exceptions.ToolException(
                'You must specify at least one of [--backend-service] or '
                '[--proxy-header].')

        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        requests = []
        target_tcp_proxy_ref = self.TARGET_TCP_PROXY_ARG.ResolveAsResource(
            args, holder.resources)

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

        if args.backend_service:
            backend_service_ref = self.BACKEND_SERVICE_ARG.ResolveAsResource(
                args, holder.resources)
            requests.append(
                (client.targetTcpProxies, 'SetBackendService',
                 messages.ComputeTargetTcpProxiesSetBackendServiceRequest(
                     project=target_tcp_proxy_ref.project,
                     targetTcpProxy=target_tcp_proxy_ref.Name(),
                     targetTcpProxiesSetBackendServiceRequest=(
                         messages.TargetTcpProxiesSetBackendServiceRequest(
                             service=backend_service_ref.SelfLink())))))

        if args.proxy_header:
            proxy_header = (messages.TargetTcpProxiesSetProxyHeaderRequest.
                            ProxyHeaderValueValuesEnum(args.proxy_header))
            requests.append(
                (client.targetTcpProxies, 'SetProxyHeader',
                 messages.ComputeTargetTcpProxiesSetProxyHeaderRequest(
                     project=target_tcp_proxy_ref.project,
                     targetTcpProxy=target_tcp_proxy_ref.Name(),
                     targetTcpProxiesSetProxyHeaderRequest=(
                         messages.TargetTcpProxiesSetProxyHeaderRequest(
                             proxyHeader=proxy_header)))))

        errors = []
        resources = holder.client.MakeRequests(requests, errors)

        if errors:
            utils.RaiseToolException(errors)
        return resources
Exemplo n.º 8
0
  def Run(self, args):
    """Retrieves response with instance in the instance group."""
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    group_ref = (
        instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_ARG.ResolveAsResource(
            args, holder.resources,
            default_scope=compute_scope.ScopeEnum.ZONE,
            scope_lister=flags.GetDefaultScopeLister(client)))

    if args.regexp:
      # Regexp interprested as RE2 by Instance Group API
      filter_expr = 'instance eq {0}'.format(args.regexp)
    else:
      filter_expr = None

    if group_ref.Collection() == 'compute.instanceGroups':
      service = client.apitools_client.instanceGroups
      request = client.messages.ComputeInstanceGroupsListInstancesRequest(
          instanceGroup=group_ref.Name(),
          instanceGroupsListInstancesRequest=(
              client.messages.InstanceGroupsListInstancesRequest()),
          zone=group_ref.zone,
          filter=filter_expr,
          project=group_ref.project)
    else:
      service = client.apitools_client.regionInstanceGroups
      request = client.messages.ComputeRegionInstanceGroupsListInstancesRequest(
          instanceGroup=group_ref.Name(),
          regionInstanceGroupsListInstancesRequest=(
              client.messages.RegionInstanceGroupsListInstancesRequest()),
          region=group_ref.region,
          filter=filter_expr,
          project=group_ref.project)

    errors = []
    results = client.MakeRequests(
        requests=[(service, 'ListInstances', request)],
        errors_to_collect=errors)

    if errors:
      utils.RaiseToolException(errors)
    return instance_groups_utils.UnwrapResponse(results, 'items')
Exemplo n.º 9
0
 def Run(self, args):
     cleared_fields = []
     (service, method, request) = self.CreateRequest(args, cleared_fields)
     errors = []
     with self.compute_client.apitools_client.IncludeFields(cleared_fields):
         resources = list(
             request_helper.MakeRequests(requests=[(service, method,
                                                    request)],
                                         http=self.http,
                                         batch_url=self.batch_url,
                                         errors=errors,
                                         custom_get_requests=None))
     resources = lister.ProcessResults(
         resources=resources,
         field_selector=property_selector.PropertySelector(
             properties=None, transformations=self.transformations))
     if errors:
         utils.RaiseToolException(errors)
     return resources
 def GetAddress(self, address_ref):
     """Returns the address resource corresponding to the given reference."""
     errors = []
     res = list(
         request_helper.MakeRequests(requests=[
             (self.compute.addresses, 'Get',
              self.messages.ComputeAddressesGetRequest(
                  address=address_ref.Name(),
                  project=address_ref.project,
                  region=address_ref.region))
         ],
                                     http=self.http,
                                     batch_url=self.batch_url,
                                     errors=errors,
                                     custom_get_requests=None))
     if errors:
         utils.RaiseToolException(
             errors, error_message='Could not fetch address resource:')
     return res[0]
Exemplo n.º 11
0
    def Run(self, args):
        start = time_utils.CurrentTimeSec()
        group_ref = self.CreateGroupReference(args)

        while True:
            responses, errors = self._GetResources(group_ref)
            if errors:
                utils.RaiseToolException(errors)
            if wait_info.IsGroupStable(responses[0]):
                break
            log.out.Print(wait_info.CreateWaitText(responses[0]))
            time_utils.Sleep(WaitUntilStable._TIME_BETWEEN_POLLS_SEC)

            if args.timeout and time_utils.CurrentTimeSec(
            ) - start > args.timeout:
                raise utils.TimeoutError(
                    'Timeout while waiting for group to become '
                    'stable.')
        log.out.Print('Group is stable')
Exemplo n.º 12
0
    def _CreateResource(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

        backend_service_ref = self.BACKEND_SERVICE_ARG.ResolveAsResource(
            args, holder.resources)

        target_ssl_proxy_ref = self.TARGET_SSL_PROXY_ARG.ResolveAsResource(
            args, holder.resources)

        ssl_cert_refs = self.SSL_CERTIFICATES_ARG.ResolveAsResource(
            args, holder.resources)

        client = holder.client.apitools_client
        messages = holder.client.messages
        if args.proxy_header:
            proxy_header = messages.TargetSslProxy.ProxyHeaderValueValuesEnum(
                args.proxy_header)
        else:
            proxy_header = (
                messages.TargetSslProxy.ProxyHeaderValueValuesEnum.NONE)

        target_ssl_proxy = messages.TargetSslProxy(
            description=args.description,
            name=target_ssl_proxy_ref.Name(),
            proxyHeader=proxy_header,
            service=backend_service_ref.SelfLink(),
            sslCertificates=[ref.SelfLink() for ref in ssl_cert_refs])

        if args.ssl_policy:
            target_ssl_proxy.sslPolicy = self.SSL_POLICY_ARG.ResolveAsResource(
                args, holder.resources).SelfLink()

        request = messages.ComputeTargetSslProxiesInsertRequest(
            project=target_ssl_proxy_ref.project,
            targetSslProxy=target_ssl_proxy)

        errors = []
        resources = holder.client.MakeRequests(
            [(client.targetSslProxies, 'Insert', request)], errors)

        if errors:
            utils.RaiseToolException(errors)
        return resources
Exemplo n.º 13
0
 def _GetSerialPortOutput(self, port=4):
     """Returns the serial port output for self.instance_ref."""
     request = (self.compute.instances, 'GetSerialPortOutput',
                self.messages.ComputeInstancesGetSerialPortOutputRequest(
                    instance=self.ref.Name(),
                    project=self.project,
                    port=port,
                    zone=self.ref.zone))
     errors = []
     objects = list(
         request_helper.MakeRequests(requests=[request],
                                     http=self.http,
                                     batch_url=self.batch_url,
                                     errors=errors,
                                     custom_get_requests=None))
     if errors:
         utils.RaiseToolException(
             errors, error_message='Could not fetch serial port output:')
     return objects[0].contents
Exemplo n.º 14
0
 def GetProject(self):
   """Returns the project object."""
   errors = []
   objects = list(request_helper.MakeRequests(
       requests=[(self.compute.projects,
                  'Get',
                  self.messages.ComputeProjectsGetRequest(
                      project=properties.VALUES.core.project.Get(
                          required=True),
                  ))],
       http=self.http,
       batch_url=self.batch_url,
       errors=errors,
       custom_get_requests=None))
   if errors:
     utils.RaiseToolException(
         errors,
         error_message='Could not fetch project resource:')
   return objects[0]
Exemplo n.º 15
0
    def GetInstances(self, refs):
        """Fetches instance objects corresponding to the given references."""
        instance_get_requests = []
        for ref in refs:
            request_protobuf = self.messages.ComputeInstancesGetRequest(
                instance=ref.Name(), zone=ref.zone, project=ref.project)
            instance_get_requests.append(
                (self.service, 'Get', request_protobuf))

        errors = []
        instances = list(
            request_helper.MakeRequests(requests=instance_get_requests,
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors))
        if errors:
            utils.RaiseToolException(
                errors, error_message='Failed to fetch some instances:')
        return instances
Exemplo n.º 16
0
  def Run(self, args):
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client
    resources = holder.resources

    igm_ref = (instance_groups_flags.MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG.
               ResolveAsResource)(
                   args,
                   resources,
                   scope_lister=compute_flags.GetDefaultScopeLister(client),
               )

    if igm_ref.Collection() == 'compute.instanceGroupManagers':
      service = client.apitools_client.instanceGroupManagers
      request = (client.messages.
                 ComputeInstanceGroupManagersListPerInstanceConfigsRequest)(
                     instanceGroupManager=igm_ref.Name(),
                     project=igm_ref.project,
                     zone=igm_ref.zone,
                 )
    elif igm_ref.Collection() == 'compute.regionInstanceGroupManagers':
      service = client.apitools_client.regionInstanceGroupManagers
      request = (
          client.messages.
          ComputeRegionInstanceGroupManagersListPerInstanceConfigsRequest)(
              instanceGroupManager=igm_ref.Name(),
              project=igm_ref.project,
              region=igm_ref.region,
          )

    errors = []
    results = list(
        request_helper.MakeRequests(
            requests=[(service, 'ListPerInstanceConfigs', request)],
            http=client.apitools_client.http,
            batch_url=client.batch_url,
            errors=errors))

    if errors:
      utils.RaiseToolException(errors)

    return instance_groups_utils.UnwrapResponse(results, 'items')
Exemplo n.º 17
0
    def Run(self, args):
        holder = base_classes.ComputeUserAccountsApiHolder(self.ReleaseTrack())
        client = holder.client

        user_refs = [
            holder.resources.Parse(
                user,
                params={'project': properties.VALUES.core.project.GetOrFail},
                collection='clouduseraccounts.users') for user in args.members
        ]

        group_refs = [
            holder.resources.Parse(
                group,
                params={'project': properties.VALUES.core.project.GetOrFail},
                collection='clouduseraccounts.groups') for group in args.names
        ]

        requests = []
        for group_ref in group_refs:
            for user_ref in user_refs:
                remove_member = client.MESSAGES_MODULE.GroupsRemoveMemberRequest(
                    users=[user_ref.SelfLink()])

                request = (client.MESSAGES_MODULE.
                           ClouduseraccountsGroupsRemoveMemberRequest(
                               project=group_ref.project,
                               groupsRemoveMemberRequest=remove_member,
                               groupName=group_ref.Name()))
                requests.append((client.groups, 'RemoveMember', request))

        errors = []
        responses = list(
            request_helper.MakeRequests(
                requests=requests,
                http=client.http,
                batch_url='https://www.googleapis.com/batch/',
                errors=errors))
        if errors:
            utils.RaiseToolException(errors,
                                     error_message='Could not fetch resource:')
        return responses
Exemplo n.º 18
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        refs = self._BACKEND_SERVICE_ARG.ResolveAsResource(
            args,
            holder.resources,
            default_scope=backend_services_utils.GetDefaultScope())
        utils.PromptForDeletion(refs)

        requests = []
        for ref in refs:
            backend_service = client.BackendService(
                ref, compute_client=holder.client)
            requests.extend(backend_service.Delete(only_generate_request=True))

        errors = []
        resources = holder.client.MakeRequests(requests, errors)

        if errors:
            utils.RaiseToolException(errors)
        return resources
  def Run(self, args):
    """Issues requests necessary to wait until stable on a MIG."""
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client
    start = time_util.CurrentTimeSec()
    group_ref = self.CreateGroupReference(client, holder.resources, args)

    while True:
      responses, errors = self._GetResources(client, group_ref)
      if errors:
        utils.RaiseToolException(errors)
      if wait_info.IsGroupStable(responses[0]):
        break
      log.out.Print(wait_info.CreateWaitText(responses[0]))
      time_util.Sleep(WaitUntilStable._TIME_BETWEEN_POLLS_SEC)

      if args.timeout and time_util.CurrentTimeSec() - start > args.timeout:
        raise utils.TimeoutError('Timeout while waiting for group to become '
                                 'stable.')
    log.out.Print('Group is stable')
Exemplo n.º 20
0
    def Run(self, args):
        """Retrieves response with instance in the instance group."""
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        group_ref = (instance_groups_flags.
                     MULTISCOPE_INSTANCE_GROUP_MANAGER_ARG.ResolveAsResource(
                         args,
                         holder.resources,
                         default_scope=compute_scope.ScopeEnum.ZONE,
                         scope_lister=flags.GetDefaultScopeLister(client)))

        if hasattr(group_ref, 'zone'):
            service = client.apitools_client.instanceGroupManagers
            request = (client.messages.
                       ComputeInstanceGroupManagersListManagedInstancesRequest(
                           instanceGroupManager=group_ref.Name(),
                           zone=group_ref.zone,
                           project=group_ref.project))
        elif hasattr(group_ref, 'region'):
            service = client.apitools_client.regionInstanceGroupManagers
            request = (
                client.messages.
                ComputeRegionInstanceGroupManagersListManagedInstancesRequest(
                    instanceGroupManager=group_ref.Name(),
                    region=group_ref.region,
                    project=group_ref.project))

        errors = []
        results = list(
            request_helper.MakeRequests(requests=[
                (service, 'ListManagedInstances', request)
            ],
                                        http=client.apitools_client.http,
                                        batch_url=client.batch_url,
                                        errors=errors))

        if errors:
            utils.RaiseToolException(errors)
        return instance_groups_utils.UnwrapResponse(results,
                                                    'managedInstances')
Exemplo n.º 21
0
def _MakeRequests(client, requests, is_async):
    """Helper for making asynchronous or synchronous peering creation requests."""
    if is_async:
        responses, errors = batch_helper.MakeRequests(
            requests=requests,
            http=client.apitools_client.http,
            batch_url=client.batch_url)
        if not errors:
            for operation in responses:
                log.status.write('Creating network peering for [{0}]\n'.format(
                    operation.targetLink))
                log.status.write('Monitor its progress at [{0}]\n'.format(
                    operation.selfLink))
        else:
            utils.RaiseToolException(errors)
    else:
        # We want to run through the generator that MakeRequests returns in order
        # to actually make the requests.
        responses = client.MakeRequests(requests)

    return responses
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        cua_holder = base_classes.ComputeUserAccountsApiHolder(
            self.ReleaseTrack())
        client = holder.client

        request_data = lister.ParseNamesAndRegexpFlags(args, holder.resources)

        errors = []

        for item in lister.GetGlobalResourcesDicts(
                service=cua_holder.client.users,
                project=list(request_data.scope_set)[0].project,
                filter_expr=request_data.filter,
                http=client.apitools_client.http,
                batch_url='https://www.googleapis.com/batch/',
                errors=errors):
            yield item

        if errors:
            utils.RaiseToolException(errors)
Exemplo n.º 23
0
  def Run(self, args):
    """Returns a list of TargetPoolInstanceHealth objects."""
    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
    client = holder.client

    target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
        args,
        holder.resources,
        scope_lister=compute_flags.GetDefaultScopeLister(client))
    target_pool = self.GetTargetPool(client, target_pool_ref)
    instances = target_pool.instances

    # If the target pool has no instances, we should return an empty
    # list.
    if not instances:
      return

    requests = []
    for instance in instances:
      request_message = client.messages.ComputeTargetPoolsGetHealthRequest(
          instanceReference=client.messages.InstanceReference(
              instance=instance),
          project=target_pool_ref.project,
          region=target_pool_ref.region,
          targetPool=target_pool_ref.Name())
      requests.append((client.apitools_client.targetPools, 'GetHealth',
                       request_message))

    errors = []
    resources = client.MakeRequests(
        requests=requests,
        errors_to_collect=errors)

    for resource in resources:
      yield resource

    if errors:
      utils.RaiseToolException(
          errors,
          error_message='Could not get health for some targets:')
Exemplo n.º 24
0
    def Run(self, args):
        """Retrieves response with instance in the instance group."""
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        # Note: only zonal resources parsed here.
        group_ref = holder.resources.Parse(
            args.name,
            params={
                'project': properties.VALUES.core.project.GetOrFail,
                'zone': args.zone
            },
            collection='compute.instanceGroups')
        if args.regexp:
            filter_expr = 'instance eq {0}'.format(args.regexp)
        else:
            filter_expr = None

        request = client.messages.ComputeInstanceGroupsListInstancesRequest(
            instanceGroup=group_ref.Name(),
            instanceGroupsListInstancesRequest=(
                client.messages.InstanceGroupsListInstancesRequest()),
            zone=group_ref.zone,
            filter=filter_expr,
            project=group_ref.project)

        errors = []
        results = list(
            request_helper.MakeRequests(requests=[
                (client.apitools_client.instanceGroups, 'ListInstances',
                 request)
            ],
                                        http=client.apitools_client.http,
                                        batch_url=client.batch_url,
                                        errors=errors))

        if errors:
            utils.RaiseToolException(errors)

        return instance_groups_utils.UnwrapResponse(results, 'items')
Exemplo n.º 25
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        refs = self.TARGET_TCP_PROXY_ARG.ResolveAsResource(
            args, holder.resources)
        utils.PromptForDeletion(refs)

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

        requests = []
        for ref in refs:
            requests.append((client.targetTcpProxies, 'Delete',
                             messages.ComputeTargetTcpProxiesDeleteRequest(
                                 project=ref.project,
                                 targetTcpProxy=ref.Name())))

        errors = []
        resources = holder.client.MakeRequests(requests, errors)

        if errors:
            utils.RaiseToolException(errors)
        return resources
    def Run(self, args):
        instance_ref = self.CreateZonalReference(args.name, args.zone)

        request = (self.compute.instances, 'GetSerialPortOutput',
                   self.messages.ComputeInstancesGetSerialPortOutputRequest(
                       instance=instance_ref.Name(),
                       project=self.project,
                       port=args.port,
                       zone=instance_ref.zone))

        errors = []
        objects = list(
            request_helper.MakeRequests(requests=[request],
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))

        if errors:
            utils.RaiseToolException(
                errors, error_message='Could not fetch serial port output:')
        return objects[0].contents
  def Run(self, args):
    request_protobufs = self.CreateRequests(args)
    requests = []
    for request in request_protobufs:
      requests.append((self.service, self.method, request))

    errors = []
    if args.async:
      resources, new_errors = batch_helper.MakeRequests(
          requests=requests,
          http=self.http,
          batch_url=self.batch_url)
      if not new_errors:
        for invalidation_operation in resources:
          log.status.write('Invalidation pending for [{0}]\n'.format(
              invalidation_operation.targetLink))
          log.status.write('Monitor its progress at [{0}]\n'.format(
              invalidation_operation.selfLink))
      errors.extend(new_errors)
    else:
      # We want to run through the generator that MakeRequests returns in order
      # to actually make the requests.
      resources = list(request_helper.MakeRequests(
          requests=requests,
          http=self.http,
          batch_url=self.batch_url,
          errors=errors,
          custom_get_requests=None))

    resources = lister.ProcessResults(
        resources=resources,
        field_selector=property_selector.PropertySelector(
            properties=None,
            transformations=self.transformations))

    if errors:
      utils.RaiseToolException(errors)

    return resources
Exemplo n.º 28
0
  def Run(self, args):
    """Issues requests necessary for adding users."""
    holder = base_classes.ComputeUserAccountsApiHolder(self.ReleaseTrack())
    client = holder.client

    owner = args.owner
    if not owner:
      owner = gaia.GetAuthenticatedGaiaEmail(client.http)

    name = args.name
    if not name:
      name = gaia.MapGaiaEmailToDefaultAccountName(owner)

    user_ref = holder.resources.Parse(
        name,
        params={'project': properties.VALUES.core.project.GetOrFail},
        collection='clouduseraccounts.users')

    user = client.MESSAGES_MODULE.User(
        name=user_ref.Name(),
        description=args.description,
        owner=owner,
    )

    request = client.MESSAGES_MODULE.ClouduseraccountsUsersInsertRequest(
        project=user_ref.project,
        user=user)

    errors = []
    responses = list(
        request_helper.MakeRequests(
            requests=[(client.users, 'Insert', request)],
            http=client.http,
            batch_url='https://www.googleapis.com/batch/',
            errors=errors))
    if errors:
      utils.RaiseToolException(
          errors, error_message='Could not fetch resource:')
    return responses
Exemplo n.º 29
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())

        backend_service_ref = self.BACKEND_SERVICE_ARG.ResolveAsResource(
            args, holder.resources)

        target_tcp_proxy_ref = self.TARGET_TCP_PROXY_ARG.ResolveAsResource(
            args, holder.resources)

        client = holder.client.apitools_client
        messages = holder.client.messages
        if args.proxy_header:
            proxy_header = messages.TargetTcpProxy.ProxyHeaderValueValuesEnum(
                args.proxy_header)
        else:
            proxy_header = (
                messages.TargetTcpProxy.ProxyHeaderValueValuesEnum.NONE)

        target_tcp_proxy = messages.TargetTcpProxy(
            description=args.description,
            name=target_tcp_proxy_ref.Name(),
            proxyHeader=proxy_header,
            service=backend_service_ref.SelfLink())

        if args.proxy_bind is not None:
            target_tcp_proxy.proxyBind = args.proxy_bind

        request = messages.ComputeTargetTcpProxiesInsertRequest(
            project=target_tcp_proxy_ref.project,
            targetTcpProxy=target_tcp_proxy)

        errors = []
        resources = holder.client.MakeRequests(
            [(client.targetTcpProxies, 'Insert', request)], errors)

        if errors:
            utils.RaiseToolException(errors)
        return resources
Exemplo n.º 30
0
    def Run(self, args):
        """Returns a list of TargetPoolInstanceHealth objects."""
        self.target_pool_ref = self.TARGET_POOL_ARG.ResolveAsResource(
            args,
            self.resources,
            scope_lister=compute_flags.GetDefaultScopeLister(
                self.compute_client, self.project))
        target_pool = self.GetTargetPool()
        instances = target_pool.instances

        # If the target pool has no instances, we should return an empty
        # list.
        if not instances:
            return

        requests = []
        for instance in instances:
            request_message = self.messages.ComputeTargetPoolsGetHealthRequest(
                instanceReference=self.messages.InstanceReference(
                    instance=instance),
                project=self.project,
                region=self.target_pool_ref.region,
                targetPool=self.target_pool_ref.Name())
            requests.append((self.service, 'GetHealth', request_message))

        errors = []
        resources = request_helper.MakeRequests(requests=requests,
                                                http=self.http,
                                                batch_url=self.batch_url,
                                                errors=errors,
                                                custom_get_requests=None)

        for resource in resources:
            yield resource

        if errors:
            utils.RaiseToolException(
                errors, error_message='Could not get health for some targets:')