Example #1
0
 def cancel_operation(self, operation_id: str) -> Operation:
     url = f'{self.operation_url}/operations/{operation_id}:cancel'
     try:
         response = self._request.post(url)
         return Operation.de_json(response, self)
     except YandexCloudError:
         raise BadRequest("Operation can't be canceled.")
Example #2
0
 def _delete_resource(self, url, await_complete=None) -> Operation:
     """Wrapper for delete resources."""
     response = self._request.delete(url)
     operation = Operation.de_json(response, self)
     if not await_complete:
         return operation
     return OperationWait(operation,
                          timeout=self.operation_timeout).completed
Example #3
0
    async def _async_resource_create(self, url, data=None) -> Operation:
        """Half async func for create resource.
        Returns coroutine.
        """
        response = self._request.post(url, json=data)
        operation = Operation.de_json(response, self)

        await OperationWait(
            operation, timeout=self.operation_timeout).await_complete_async()
Example #4
0
    async def _async_delete_resource(self, url) -> CoroutineType:
        """Half async func for delete resource.
        Returns coroutine.
        """
        response = self._request.delete(url)
        operation = Operation.de_json(response, self)

        await OperationWait(
            operation, timeout=self.operation_timeout).await_complete_async()
Example #5
0
    def _resource_create(self,
                         url,
                         data=None,
                         await_complete=True) -> Operation:
        response = self._request.post(url, json=data)
        operation = Operation.de_json(response, self)

        if not await_complete:
            return operation
        return OperationWait(operation).completed
Example #6
0
    def _resource_create(self,
                         url,
                         data=None,
                         await_complete=True) -> Operation:
        """Wrapper for create resource."""
        response = self._request.post(url, json=data)
        operation = Operation.de_json(response, self)

        if not await_complete:
            return operation
        return OperationWait(operation,
                             timeout=self.operation_timeout).completed
Example #7
0
    async def _async_instance_disk_management(self,
                                              instance_id: str,
                                              action=None,
                                              data=None) -> Operation:
        """Supported actions: detachDisk, attachDisk."""

        ACTIONS = ('detachDisk', 'attachDisk')
        if action not in ACTIONS:
            raise TypeError(f'Action {action} not supported')

        url = f'{self.compute_url}/compute/v1/instances/{instance_id}:{action}'
        response = self._request.post(url, json=data)
        operation = Operation.de_json(response, self)

        await OperationWait(operation).await_complete_async()
Example #8
0
    async def _async_instance_state_management(self,
                                               action=None,
                                               instance_id=None
                                               ) -> CoroutineType:
        """Supported actions: start, restart, stop."""

        ACTIONS = ('start', 'restart', 'stop')
        if action not in ACTIONS:
            raise TypeError(f'Action {action} not supported')

        url = f'{self.compute_url}/compute/v1/instances/{instance_id}:{action}'
        response = self._request.post(url)
        operation = Operation.de_json(response, self)

        await OperationWait(operation).await_complete_async()
Example #9
0
    async def _async_instance_disk_management(self,
                                              instance_id: str,
                                              action=None,
                                              data=None) -> Operation:
        """Half async helper func for managing the state of the disk.
        Supported actions: detachDisk, attachDisk.
        Returns coroutine.
        """

        ACTIONS = ('detachDisk', 'attachDisk')
        if action not in ACTIONS:
            raise TypeError(f'Action {action} not supported')

        url = f'{self.compute_url}/compute/v1/instances/{instance_id}:{action}'
        response = self._request.post(url, json=data)
        operation = Operation.de_json(response, self)

        await OperationWait(
            operation, timeout=self.operation_timeout).await_complete_async()
Example #10
0
    def _instance_state_management(self,
                                   action=None,
                                   instance_id=None,
                                   await_complete=None) -> Operation:
        """Helper func for managing the state of the instance.
        Supported actions: start, restart, stop.
        """

        ACTIONS = ('start', 'restart', 'stop')
        if action not in ACTIONS:
            raise TypeError(f'Action {action} not supported')

        url = f'{self.compute_url}/compute/v1/instances/{instance_id}:{action}'
        response = self._request.post(url)
        operation = Operation.de_json(response, self)
        if not await_complete:
            return operation
        return OperationWait(operation,
                             timeout=self.operation_timeout).completed
Example #11
0
 def operation(self, operation_id: str) -> Operation:
     """Returns operation as object."""
     url = f'{self.operation_url}/operations/{operation_id}'
     response = self._request.get(url)
     return Operation.de_json(response, self)
Example #12
0
    async def _async_resource_create(self, url, data=None) -> Operation:
        response = self._request.post(url, json=data)
        operation = Operation.de_json(response, self)

        await OperationWait(operation).await_complete_async()
Example #13
0
    async def _async_delete_resource(self, url) -> CoroutineType:
        response = self._request.delete(url)
        operation = Operation.de_json(response, self)

        await OperationWait(operation).await_complete_async()
Example #14
0
 def _delete_resource(self, url, await_complete=None) -> Operation:
     response = self._request.delete(url)
     operation = Operation.de_json(response, self)
     if not await_complete:
         return operation
     return OperationWait(operation).completed