Skip to content

Instantly share code, notes, and snippets.

@lmazuel
Last active December 29, 2017 00:57
Show Gist options
  • Save lmazuel/f253918eb33db20a7d9f9006984da608 to your computer and use it in GitHub Desktop.
Save lmazuel/f253918eb33db20a7d9f9006984da608 to your computer and use it in GitHub Desktop.
LRO sandbox
# This starts a poller, and you can call "result" on the poller to get the resource
poller = client.storage_accounts.create(parameters)
assert isinstance(poller, AzureOperationPoller)
account = poller.result()
assert isinstance(account, StorageAccount)
# This starts a poller, and you can call "result" on the poller to get the ClientRawResponse
# THIS IS A BREAKING CHANGE, but diferentiate the resource envelope (raw or not) from the polling
# This makes the return type consistent: whatever the value of "raw", return type is an AzureOperationPoller
poller = client.storage_accounts.create(parameters, raw=True)
assert isinstance(poller, AzureOperationPoller)
raw_account = poller.result()
assert isinstance(raw_account, ClientRawResponse)
# MyPollingAlgorithm is an instance of LROPollingAlgorithm.
# Default is "ARMPollingAlgorithm". There is also "MicrosoftRestPollingAlgorithm".
# Return type is still the "engine" type AzureOperationPoller (should be renamed to something like "LROPoller")
poller = client.storage_account.create(parameters, polling=MyPollingAlgorithm)
assert isinstance(poller, AzureOperationPoller)
account = poller.result()
assert isinstance(account, StorageAccount) # Should not change, but probably we can allow the algo to change it.
# Allow the "fire-and-forget" or "nowait"
# polling=False is actually an alias to polling=NoPolling algorithm
poller = client.storage_account.create(parameters, polling=False)
assert isinstance(poller, AzureOperationPoller)
assert poller.result() is None # Storage does not answer initially, you shouldn't expect something unless you know the service is returning something
# "nowait" is not incompatible with "raw", this is two different purposes
poller = client.storage_account.create(parameters, raw=True, polling=False)
assert isinstance(poller.result(), ClientRawResponse) # Difference here is you get the full answer (Headers, etc.)
# Now we understands the difference between raw and polling, we can do that:
raw_result = client.storage_account.create(parameters, raw=True, polling=False).result()
poller = client.create_poller(raw_result, StorageAccount, polling=ARMPollingAlgorithm)
storage_account = poller.result()
# We created a poller:
# - With client authentication (hence the "create_poller" on the client)
# - With the initial raw result. We need everything (headers, status code, initial body, etc.)
# - The actual type we expect in return
# - This method accepts "raw" to return a ClientRawResponse<StorageAccount>
# - With a polling algorithm. By default it's ARM for "azure" Autorest, "MicrosoftRestPollingAlgorithm" for basic Autorest.
# Indeed, "create_poller" is created by the default Autorest too (we just can't generate the operation methods,
# because there is no way to write that in the Swagger, but the generic one is possible)
# ClientRawResponse is pickable, allowing this to be used with multi-processing if necessary. Only requirement is to create
# An authenticated client per process by yourself (not the complicated part).
# Alternate syntax
from msrest.poller import PollerFactory
poller = PollerFactory.create_poller(client, raw_result, StorageAccount, polling=ARMPollingAlgorithm)
storage_account = poller.result()
# Allows this mix resource scenario too
raw_result = resource_client.resources.create_or_update(nic_parameters, raw=True, polling=False).result()
poller = network_client.create_poller(raw_result, NetworkInterface, polling=ARMPollingAlgorithm)
nic_instance = poller.result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment