def Wait(self, **kwargs): args = dict( warnings=self.warnings, errors=self.errors, http=self.mock_http, batch_url=self.batch_url, progress_tracker=None, ) args.update(kwargs) return list(waiters.WaitForOperations(**args))
def Run(self, args): self._ValidateArgs(args) holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) resources = holder.resources commitment_ref = flags.MakeCommitmentArg(False).ResolveAsResource( args, resources, scope_lister=compute_flags.GetDefaultScopeLister(holder.client)) messages = holder.client.messages region = properties.VALUES.compute.region.Get() project = properties.VALUES.core.project.Get() create_request = self._MakeCreateRequest(args, messages, project, region, commitment_ref) service = holder.client.apitools_client.regionCommitments batch_url = holder.client.batch_url http = holder.client.apitools_client.http errors = [] operations = list( request_helper.MakeRequests(requests=[(service, 'Insert', create_request)], http=http, batch_url=batch_url, errors=errors)) operation_service = holder.client.apitools_client.regionOperations return waiters.WaitForOperations(operations=operations, project=project, operation_service=operation_service, resource_service=service, http=http, batch_url=batch_url, warnings=[], errors=errors)
def MakeRequests(requests, http, batch_url, errors, progress_tracker=None): """Makes one or more requests to the API. Each request can be either a synchronous API call or an asynchronous one. For synchronous calls (e.g., get and list), the result from the server is yielded immediately. For asynchronous calls (e.g., calls that return operations like insert), this function waits until the operation reaches the DONE state and fetches the corresponding object and yields that object (nothing is yielded for deletions). Currently, a heterogenous set of synchronous calls can be made (e.g., get request to fetch a disk and instance), however, the asynchronous requests must be homogenous (e.g., they must all be the same verb on the same collection). In the future, heterogenous asynchronous requests will be supported. For now, it is up to the client to ensure that the asynchronous requests are homogenous. Synchronous and asynchronous requests can be mixed. Args: requests: A list of requests to make. Each element must be a 3-element tuple where the first element is the service, the second element is the string name of the method on the service, and the last element is a protocol buffer representing the request. http: An httplib2.Http-like object. batch_url: The handler for making batch requests. errors: A list for capturing errors. If any response contains an error, it is added to this list. progress_tracker: progress tracker to be ticked while waiting for operations to finish. Yields: A response for each request. For deletion requests, no corresponding responses are returned. """ if _RequestsAreListRequests(requests): for item in _List(requests=requests, http=http, batch_url=batch_url, errors=errors): yield item return responses, new_errors = batch_helper.MakeRequests(requests=requests, http=http, batch_url=batch_url) errors.extend(new_errors) operation_service = None resource_service = None project = None # Collects all operation objects in a list so they can be waited on # and yields all non-operation objects since non-operation responses # cannot be waited on. operations_data = [] for request, response in zip(requests, responses): if response is None: continue service, _, request_body = request if (isinstance(response, service.client.MESSAGES_MODULE.Operation) and service.__class__.__name__ not in ('GlobalOperationsService', 'RegionOperationsService', 'ZoneOperationsService', 'GlobalAccountsOperationsService')): resource_service = service project = request_body.project if response.zone: operation_service = service.client.zoneOperations elif response.region: operation_service = service.client.regionOperations else: operation_service = service.client.globalOperations operations_data.append( waiters.OperationData(response, project, operation_service, resource_service)) else: yield response if operations_data: warnings = [] for response in waiters.WaitForOperations( operations_data=operations_data, http=http, batch_url=batch_url, warnings=warnings, progress_tracker=progress_tracker, errors=errors): yield response if warnings: log.warning( utils.ConstructList('Some requests generated warnings:', warnings))
def MakeRequests(requests, http, batch_url, errors): """Makes one or more requests to the API. Each request can be either a synchronous API call or an asynchronous one. For synchronous calls (e.g., get and list), the result from the server is yielded immediately. For asynchronous calls (e.g., calls that return operations like insert), this function waits until the operation reaches the DONE state and fetches the corresponding object and yields that object (nothing is yielded for deletions). Currently, a heterogenous set of synchronous calls can be made (e.g., get request to fetch a disk and instance), however, the asynchronous requests must be homogenous (e.g., they must all be the same verb on the same collection). In the future, heterogenous asynchronous requests will be supported. For now, it is up to the client to ensure that the asynchronous requests are homogenous. Synchronous and asynchronous requests can be mixed. Args: requests: A list of requests to make. Each element must be a 3-element tuple where the first element is the service, the second element is the string name of the method on the service, and the last element is a protocol buffer representing the request. http: An httplib2.Http-like object. batch_url: The handler for making batch requests. errors: A list for capturing errors. If any response contains an error, it is added to this list. Yields: A response for each request. For deletion requests, no corresponding responses are returned. """ if _RequestsAreListRequests(requests): for item in _List(requests=requests, http=http, batch_url=batch_url, errors=errors): yield item return # TODO(b/36057059): Delete the batch_helper module and move its logic # here. To do this, we also have to edit the lister module to depend # on this module instead of batch_helper. responses, new_errors = batch_helper.MakeRequests(requests=requests, http=http, batch_url=batch_url) errors.extend(new_errors) operation_service = None resource_service = None project = None # Collects all operation objects in a list so they can be waited on # and yields all non-operation objects since non-operation responses # cannot be waited on. operations = [] for request, response in zip(requests, responses): if response is None: continue service, _, request_body = request if (isinstance(response, service.client.MESSAGES_MODULE.Operation) and service.__class__.__name__ not in ('GlobalOperationsService', 'RegionOperationsService', 'ZoneOperationsService', 'GlobalAccountsOperationsService')): operations.append(response) # This logic assumes that all requests are homogenous, i.e., # they all make calls to the same service and verb. This is # temporary. In the future, we will push this logic into the # function that does the actual waiting. For now, we have to # deal with existing interfaces to keep the scopes of the # refactoring CLs small. # TODO(b/32276307) if not operation_service: resource_service = service project = request_body.project if response.kind == 'clouduseraccounts#operation': operation_service = service.client.globalAccountsOperations elif response.zone: operation_service = service.client.zoneOperations elif response.region: operation_service = service.client.regionOperations else: operation_service = service.client.globalOperations else: yield response if operations: warnings = [] for response in waiters.WaitForOperations( operations=operations, project=project, operation_service=operation_service, resource_service=resource_service, http=http, batch_url=batch_url, warnings=warnings, errors=errors): yield response if warnings: log.warn( utils.ConstructList('Some requests generated warnings:', warnings))
def MakeRequests(requests, http, batch_url, errors, progress_tracker=None, no_followup=False, always_return_operation=False, followup_overrides=None, log_result=True, log_warnings=True, timeout=None): """Makes one or more requests to the API. Each request can be either a synchronous API call or an asynchronous one. For synchronous calls (e.g., get and list), the result from the server is yielded immediately. For asynchronous calls (e.g., calls that return operations like insert), this function waits until the operation reaches the DONE state and fetches the corresponding object and yields that object (nothing is yielded for deletions). Currently, a heterogenous set of synchronous calls can be made (e.g., get request to fetch a disk and instance), however, the asynchronous requests must be homogenous (e.g., they must all be the same verb on the same collection). In the future, heterogenous asynchronous requests will be supported. For now, it is up to the client to ensure that the asynchronous requests are homogenous. Synchronous and asynchronous requests can be mixed. Args: requests: A list of requests to make. Each element must be a 3-element tuple where the first element is the service, the second element is the string name of the method on the service, and the last element is a protocol buffer representing the request. http: An httplib2.Http-like object. batch_url: The handler for making batch requests. errors: A list for capturing errors. If any response contains an error, it is added to this list. progress_tracker: progress tracker to be ticked while waiting for operations to finish. no_followup: If True, do not followup operation with a GET request. always_return_operation: If True, return operation object even if operation fails. followup_overrides: A list of new resource names to GET once the operation finishes. Generally used in renaming calls. log_result: Whether the Operation Waiter should print the result in past tense of each request. log_warnings: Whether warnings for completed operation should be printed. timeout: The maximum amount of time, in seconds, to wait for the operations to reach the DONE state. Yields: A response for each request. For deletion requests, no corresponding responses are returned. """ if _RequestsAreListRequests(requests): for item in _List(requests=requests, http=http, batch_url=batch_url, errors=errors): yield item return responses, new_errors = batch_helper.MakeRequests(requests=requests, http=http, batch_url=batch_url) errors.extend(new_errors) operation_service = None resource_service = None # Collects all operation objects in a list so they can be waited on # and yields all non-operation objects since non-operation responses # cannot be waited on. operations_data = [] if not followup_overrides: followup_overrides = [None for _ in requests] for request, response, followup_override in zip(requests, responses, followup_overrides): if response is None: continue service, _, request_body = request if (isinstance(response, service.client.MESSAGES_MODULE.Operation) and not _IsEmptyOperation(response, service) and service.__class__.__name__ not in ('GlobalOperationsService', 'RegionOperationsService', 'ZoneOperationsService', 'GlobalOrganizationOperationsService', 'GlobalAccountsOperationsService')): resource_service = service project = None if hasattr(request_body, 'project'): project = request_body.project if response.zone: operation_service = service.client.zoneOperations elif response.region: operation_service = service.client.regionOperations else: operation_service = service.client.globalOperations else: operation_service = service.client.globalOrganizationOperations operations_data.append( waiters.OperationData( response, operation_service, resource_service, project=project, no_followup=no_followup, followup_override=followup_override, always_return_operation=always_return_operation)) else: yield response if operations_data: warnings = [] for response in waiters.WaitForOperations( operations_data=operations_data, http=http, batch_url=batch_url, warnings=warnings, progress_tracker=progress_tracker, errors=errors, log_result=log_result, timeout=timeout): yield response if warnings and log_warnings: log.warning( utils.ConstructList('Some requests generated warnings:', warnings))