Skip to content

Common issues about Python SDK

Yuchao Yan edited this page Jul 7, 2023 · 12 revisions

This doc clarify some common misunderstandings about Python SDK.

How to update an existing resource with create_or_update/begin_create_or_update

An operation named create_or_update/begin_create_or_update of Python SDK usually matches PUT. To update a filed of existing resource, users must do like:

    ...
    # get all fileds of existing resource
    agent_pool = client.agent_pools.get(
        resource_group_name="rg1",
        resource_name="clustername1",
        agent_pool_name="agentpool1",
    )
 
    agent_pool.max_count = 10
    agent_pool.min_count = 1
    # change any filed that you want
    #...
 
    response = client.agent_pools.begin_create_or_update(
        resource_group_name="rg1",
        resource_name="clustername1",
        agent_pool_name="agentpool1",
        parameters=agent_pool,
    ).result()

Users may have confusion: why can't I set the filed directly but have to get the existing resource filed? Here is guideline about PUT: image

So the question becomes: Why does PUT has such strange limitations when update existing resource? To explain the question, let us assume special scenarios:

  • User A want to delete a field X, so A set the filed X with None and think: Now that I set X with None, service shall delete X
  • User B want to update a field Y, so B only set the filed Y and think: Now that I don't set X and it shall be None, service shall keep X same as before

Why is there such ambiguity? It is caused by the meaning of None(in other language, it may be named null/undefined/...). None has two kinds of different meaning in nature: delete it or keep it same as before. To eliminate the ambiguity, PUT adopts meaning that delete it so if users want to update existing resource, they have to get all the filed of the resource and update specific feild.

Operation

Usually, operation has a body parameter corresponding to http request body. For Python SDK users, there are 2 ways to set the body parameter.

from azure.identity import DefaultAzureCredential
from azure.mgmt.network import NetworkManagementClient

client = NetworkManagementClient(credential=DefaultAzureCredential(), subscription_id="subid")

# 1: with json-like object
response = client.public_ip_addresses.begin_create_or_update(
    resource_group_name="rg1",
    public_ip_address_name="test-ip",
    parameters={"location": "eastus", "properties": {"dnsSettings": {"domainNameLabel": "dnslbl"}}},
).result()
print(response.as_dict())

# 2: With native model
from azure.mgmt.network.models import PublicIPAddress, PublicIPAddressDnsSettings
parameters = PublicIPAddress(location="eastus", dns_settings=PublicIPAddressDnsSettings(domain_name_label="dnslbl"))
response = client.public_ip_addresses.begin_create_or_update(
    resource_group_name="rg1",
    public_ip_address_name="test-ip",
    parameters=parameters,
).result()
print(response.as_dict())

In model, the signature name is snake-case like domain_name_label; in json-like object, the name is camel-case like domainNameLabel