コード例 #1
0
  def _SetProjectMetadata(self, new_metadata):
    """Sets the project metadata to the new metadata."""
    compute = self.compute

    errors = []
    list(request_helper.MakeRequests(
        requests=[
            (compute.projects,
             'SetCommonInstanceMetadata',
             self.messages.ComputeProjectsSetCommonInstanceMetadataRequest(
                 metadata=new_metadata,
                 project=properties.VALUES.core.project.Get(
                     required=True),
             ))],
        http=self.http,
        batch_url=self.batch_url,
        errors=errors))
    if errors:
      utils.RaiseException(
          errors,
          SetProjectMetadataError,
          error_message='Could not add SSH key to project metadata:')
コード例 #2
0
    def LookupUsers(self, users):
        """Makes a get request for each user in users and returns results."""
        requests = []
        for user in users:
            request = (self.clouduseraccounts.users, 'Get',
                       self.clouduseraccounts.MESSAGES_MODULE.
                       ClouduseraccountsUsersGetRequest(project=self.project,
                                                        user=user))
            requests.append(request)

        errors = []
        res = list(
            request_helper.MakeRequests(requests=requests,
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors,
                                        custom_get_requests=None))
        if errors:
            utils.RaiseException(errors,
                                 UserException,
                                 error_message='Could not fetch some users:')
        return res
コード例 #3
0
ファイル: client.py プロジェクト: eroberts20/bias_crawler
    def RemovePublicKey(self, user, fingerprint):
        """Removes a public key from a user."""
        request = (self.clouduseraccounts.users, 'RemovePublicKey',
                   self.clouduseraccounts.MESSAGES_MODULE.
                   ClouduseraccountsUsersRemovePublicKeyRequest(
                       project=self.project,
                       fingerprint=fingerprint,
                       user=user))

        requests = [request]

        errors = []
        res = list(
            request_helper.MakeRequests(requests=requests,
                                        http=self.http,
                                        batch_url=self.batch_url,
                                        errors=errors))
        if errors:
            utils.RaiseException(errors,
                                 UserException,
                                 error_message='Could not remove public key:')
        return res
コード例 #4
0
def GetInstanceGroupManagerOrThrow(igm_ref, project, compute, http, batch_url):
    """Retrieves the given Instance Group Manager if possible.

  Args:
    igm_ref: reference to the Instance Group Manager.
    project: project owning resources.
    compute: module representing compute api.
    http: communication channel.
    batch_url: batch url.
  Returns:
    Instance Group Manager object.
  """
    if hasattr(igm_ref, 'region'):
        service = compute.regionInstanceGroupManagers
        request = service.GetRequestType('Get')(project=project)
        request.region = igm_ref.region
    if hasattr(igm_ref, 'zone'):
        service = compute.instanceGroupManagers
        request = service.GetRequestType('Get')(project=project)
        request.zone = igm_ref.zone
    request.instanceGroupManager = igm_ref.Name()

    errors = []
    # Run throught the generator to actually make the requests and get potential
    # errors.
    igm_details = list(
        request_helper.MakeRequests(
            requests=[(service, 'Get', request)],
            http=http,
            batch_url=batch_url,
            errors=errors,
            custom_get_requests=None,
        ))

    if errors or len(igm_details) != 1:
        utils.RaiseException(errors,
                             ResourceNotFoundException,
                             error_message='Could not fetch resource:')
    return igm_details[0]
コード例 #5
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,
        custom_get_requests=None))
    if errors:
      utils.RaiseException(
          errors,
          FailedToFetchInstancesError,
          error_message='Failed to fetch some instances:')
    return instances
コード例 #6
0
  def GetGroupFingerprint(self, group):
    """Gets fingerprint of given instance group."""
    get_request = self.messages.ComputeInstanceGroupsGetRequest(
        instanceGroup=group.Name(),
        zone=group.zone,
        project=self.project)

    errors = []
    resources = list(request_helper.MakeRequests(
        requests=[(
            self.compute.instanceGroups,
            'Get',
            get_request)],
        http=self.http,
        batch_url=self.batch_url,
        errors=errors,
        custom_get_requests=None))

    if errors:
      utils.RaiseException(
          errors,
          FingerprintFetchException,
          error_message='Could not set named ports for resource:')
    return resources[0].fingerprint
コード例 #7
0
    def __call__(self, frontend):
        scope_set = frontend.scope_set
        requests = []
        if isinstance(scope_set, ZoneSet):
            for project, zones in six.iteritems(
                    _GroupByProject(sorted(list(scope_set)))):
                for zone_ref in zones:
                    requests.append((self.zonal_service, 'List',
                                     self.zonal_service.GetRequestType('List')(
                                         filter=frontend.filter,
                                         maxResults=frontend.max_results,
                                         project=project,
                                         zone=zone_ref.zone)))
        elif isinstance(scope_set, RegionSet):
            for project, regions in six.iteritems(
                    _GroupByProject(sorted(list(scope_set)))):
                for region_ref in regions:
                    requests.append(
                        (self.regional_service, 'List',
                         self.regional_service.GetRequestType('List')(
                             filter=frontend.filter,
                             maxResults=frontend.max_results,
                             project=project,
                             region=region_ref.region)))
        elif isinstance(scope_set, GlobalScope):
            for project_ref in sorted(list(scope_set)):
                requests.append((self.global_service, 'List',
                                 self.global_service.GetRequestType('List')(
                                     filter=frontend.filter,
                                     maxResults=frontend.max_results,
                                     project=project_ref.project)))
        else:
            # scopeSet is AllScopes
            # generate AggregatedList
            request_message = self.aggregation_service.GetRequestType(
                'AggregatedList')
            for project_ref in sorted(list(scope_set.projects)):
                input_params = {}

                if hasattr(request_message, 'includeAllScopes'):
                    input_params['includeAllScopes'] = True
                if hasattr(request_message, 'returnPartialSuccess'
                           ) and self.return_partial_success:
                    input_params['returnPartialSuccess'] = True

                requests.append(
                    (self.aggregation_service, 'AggregatedList',
                     request_message(filter=frontend.filter,
                                     maxResults=frontend.max_results,
                                     project=project_ref.project,
                                     **input_params)))

        errors = []
        response_count = 0
        for item in request_helper.ListJson(
                requests=requests,
                http=self.client.apitools_client.http,
                batch_url=self.client.batch_url,
                errors=errors):
            response_count += 1

            yield item

        if errors:
            # If the command allows partial server errors, instead of raising an
            # exception to show something went wrong, we show a warning message that
            # contains the error messages instead.
            if self.allow_partial_server_failure and response_count > 0:
                utils.WarnIfPartialRequestFail(errors)
            else:
                utils.RaiseException(errors, ListException)