{"id":877,"date":"2021-01-15T11:51:14","date_gmt":"2021-01-15T19:51:14","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sdk\/?p=877"},"modified":"2021-01-15T11:51:14","modified_gmt":"2021-01-15T19:51:14","slug":"migrating-python-management-libraries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sdk\/migrating-python-management-libraries\/","title":{"rendered":"How to migrate to the new Azure Python Management Libraries"},"content":{"rendered":"<h2>Why did we create the new Azure SDK for Python Management Libraries?<\/h2>\n<p>The goal of our management libraries is to enhance the productivity of developers managing Azure resources and provide idiomatic, consistent, approachable, diagnosable, and dependable code to easily integrate with Azure resources. We\u2019ve been listening to customer feedback and we\u2019ve made sure that our new effort has incorporated those suggestions and requests. Finally, we understand that ease of use, service coverage, documentation, and consistency are equally important when it comes to Azure resource management.<\/p>\n<p>We have made improvements in the following areas: * Create easy-to-use APIs with productivity that is on par with the best libraries of the language ecosystems. * Provide APIs that are idiomatic to Python. * Evolve over time in a very compatible fashion. * Focus as much on documentation and samples, as on APIs. * Change how we create the libraries at their core. (See our blog post, <a href=\"https:\/\/devblogs.microsoft.com\/azure-sdk\/code-generation-with-autorest\/\">AutoRest and OpenAPI: The backbone of Azure SDK<\/a>, for more details)<\/p>\n<h2>Which SDKs have been upgraded to the new version?<\/h2>\n<p>We are constantly improving the SDK to provide a better developer experience. You can find the full list of latest versions at <a href=\"https:\/\/azure.github.io\/azure-sdk\/releases\/latest\/mgmt\/python.html\">azure.github.io\/azure-sdk<\/a>.<\/p>\n<p>The versions that start with &#8220;b&#8221; means the packages are still in beta state. The versions without &#8220;b&#8221; are stable versions and ready for production uses. If you intend to use the SDK in a production environment, please use one of the stable versions.<\/p>\n<p>NOTE: Since the package name remains the same between the old and new generation of SDKs, your application may automatically pull the latest new version. If you do not intend to upgrade, you can always pin a specific version of the package.<\/p>\n<p>For example, you can run command <code>pip install azure-mgmt-storage==11.2.0<\/code> to install previous version.<\/p>\n<h2>How to migrate to the new Azure SDK for Python Management libraries?<\/h2>\n<p>Some of the key differences between old and new management libraries are that they depend on different core libraries and code-generators.<\/p>\n<p>The following is the key differences and highlights:<\/p>\n<h2>Azure Identity Support for Authentication<\/h2>\n<p>There are two differences: * <code>ServicePrincipalCredentials<\/code> in <code>azure.common<\/code> for identity verification has been replaced with <code>TokenCredential<\/code> based classes in <code>azure.identity<\/code>. * The keyword <code>credentials<\/code> in Client is replaced by <code>credential<\/code><\/p>\n<p>To use Azure Identity, we need to install the dependecy using <code>pip install azure-identity<\/code>.<\/p>\n<p>After installation of the package, we will need to modify the existing authentication code to use the new Identity features:<\/p>\n<pre><code class=\"python\"># Previous Version\nimport azure.mgmt.compute\nfrom azure.common.credentials import ServicePrincipalCredentials\n\ncredentials = ServicePrincipalCredentials(\n    client_id=client_id,\n    secret=client_secret,\n    tenant=tenant_id\n)\ncompute_client = azure.mgmt.compute.ComputeManagementClient(credentials=credentials, subscription_id=self.subscription_id)\n\n# New Version\nimport azure.mmgt.compute\nfrom azure.identity import ClientSecretCredential\n\ncredential = ClientSecretCredential(\n    tenant_id=tenant_id,\n    client_id=client_id,\n    client_secret=client_secret\n)\ncompute_client = azure.mgmt.compute.ComputeManagementClient(credential=credential, subscription_id=self.subscription_id)\n<\/code><\/pre>\n<h2>Long Running Operations (LRO) Method Names<\/h2>\n<p>In the new version, many operations like <code>ComputeManagementClient.virtual_machines.begin_create_or_update<\/code> return an object of type <code>AzureOperationPoller<\/code>; these methods are asynchronous. Older libraries that aren&#8217;t based on <code>azure.core<\/code> typically use names like <code>create_or_update<\/code>. In the new version, libraries are based on <code>azure.core<\/code> and they add the <code>begin_<\/code> prefix to method names to clearly indicate that they are asynchronous. Migrating old code to a newer version of SDK typically means adding the <code>begin_<\/code> prefix to method names. That being said, most method signatures remain the same.<\/p>\n<p>For example, we want to create a new VM in Azure:<\/p>\n<pre><code class=\"python\"># Previous Version\nresult = compute_client.virtual_machines.create_or_update(\n    group_name,\n    vm_name,\n    parameters\n)\nresult = result.result()\n\n# New Version\nresult = compute_client.virtual_machines.begin_create_or_update(\n    group_name,\n    vm_name,\n    parameters\n)\nvm = result.result()\n<\/code><\/pre>\n<h2>Unified operation parameter style<\/h2>\n<p>In previous library versions, many operations allow you to express object argument either as discrete objects or as inline JSON, but some operations do not.<\/p>\n<p>For example, there is an <a href=\"https:\/\/docs.microsoft.com\/rest\/api\/storagerp\/storageaccounts\/checknameavailability\">check name availability<\/a> operation in storage. The request body is:<\/p>\n<pre><code class=\"json\">{\n  \"name\": \"sto3363\",\n  \"type\": \"Microsoft.Storage\/storageAccounts\"\n}\n<\/code><\/pre>\n<p>In the previous library version, you just need to provide a string which is string type like: <code>StorageClient.storage_accounts.check_name_availability(name=\"sto3363\")<\/code>.<\/p>\n<p>In order to keep the style consistent in the new libraries, all operations that need request body will need to express objects argument. So the operation <code>check_name_availability<\/code> in the new libraries will be:<\/p>\n<pre><code class=\"python\">account_name_model = {\n  \"name\": \"sto3363\",\n  \"type\": \"Microsoft.Storage\/storageAccounts\"\n}\nresult = storage_client.storage_accounts.check_name_availability(account_name=account_name_model)\n<\/code><\/pre>\n<h2>Summary<\/h2>\n<p>In this blog post we have explained the basic information of the new Azure SDK for Python Management Libraries and how to migrate to the new version of the SDK from old versions. For more information on how to use the python SDK, please refer to <a href=\"https:\/\/aka.ms\/azsdk\/python\/mgmt\">Use the Azure libraries (SDK) for Python<\/a>.<\/p>\n<p><!-- FOOTER: DO NOT EDIT OR REMOVE --> <div  class=\"d-flex justify-content-center\"><a class=\"cta_button_link btn-primary mb-24\" href=\"https:\/\/aka.ms\/azsdk\/releases\" target=\"_blank\">Azure SDK Releases<\/a><\/div><\/p>\n<h2>Azure SDK Blog Contributions<\/h2>\n<p>Thank you for reading this Azure SDK blog post! We hope that you learned something new and welcome you to share this post. We are open to Azure SDK blog contributions. Please contact us at <a href=\"&#109;&#x61;&#105;&#x6c;&#116;&#x6f;&#58;&#x61;z&#115;&#x64;&#107;&#x62;&#108;&#x6f;&#103;&#x40;&#109;&#105;&#x63;&#114;&#x6f;&#115;&#x6f;&#102;&#x74;&#46;&#x63;o&#109;\">&#x61;z&#115;&#x64;&#107;&#x62;&#108;&#x6f;&#103;&#x40;&#109;&#105;&#x63;&#114;&#x6f;&#115;&#x6f;&#102;&#x74;&#46;&#x63;o&#109;<\/a> with your topic and we&#8217;ll get you setup as a guest blogger.<\/p>\n<h2>Azure SDK Links<\/h2>\n<ul>\n<li>Azure SDK Website: <a href=\"https:\/\/aka.ms\/azsdk\">aka.ms\/azsdk<\/a><\/li>\n<li>Azure SDK Intro (3 minute video): <a href=\"https:\/\/aka.ms\/azsdk\/intro\">aka.ms\/azsdk\/intro<\/a><\/li>\n<li>Azure SDK Intro Deck (PowerPoint deck): <a href=\"https:\/\/aka.ms\/azsdk\/intro\/deck\">aka.ms\/azsdk\/intro\/deck<\/a><\/li>\n<li>Azure SDK Releases: <a href=\"https:\/\/aka.ms\/azsdk\/releases\">aka.ms\/azsdk\/releases<\/a><\/li>\n<li>Azure SDK Blog: <a href=\"https:\/\/aka.ms\/azsdk\/blog\">aka.ms\/azsdk\/blog<\/a><\/li>\n<li>Azure SDK Twitter: <a href=\"https:\/\/twitter.com\/AzureSDK\">twitter.com\/AzureSDK<\/a><\/li>\n<li>Azure SDK Design Guidelines: <a href=\"https:\/\/aka.ms\/azsdk\/guide\">aka.ms\/azsdk\/guide<\/a><\/li>\n<li>Azure SDKs &amp; Tools: <a href=\"https:\/\/azure.microsoft.com\/downloads\">azure.microsoft.com\/downloads<\/a><\/li>\n<li>Azure SDK Central Repository: <a href=\"https:\/\/github.com\/azure\/azure-sdk#azure-sdk\">github.com\/azure\/azure-sdk<\/a><\/li>\n<li>Azure SDK for .NET: <a href=\"https:\/\/github.com\/azure\/azure-sdk-for-net\">github.com\/azure\/azure-sdk-for-net<\/a><\/li>\n<li>Azure SDK for Java: <a href=\"https:\/\/github.com\/azure\/azure-sdk-for-java\">github.com\/azure\/azure-sdk-for-java<\/a><\/li>\n<li>Azure SDK for Python: <a href=\"https:\/\/github.com\/azure\/azure-sdk-for-python\">github.com\/azure\/azure-sdk-for-python<\/a><\/li>\n<li>Azure SDK for JavaScript\/TypeScript: <a href=\"https:\/\/github.com\/azure\/azure-sdk-for-js\">github.com\/azure\/azure-sdk-for-js<\/a><\/li>\n<li>Azure SDK for Android: <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-android\">github.com\/Azure\/azure-sdk-for-android<\/a><\/li>\n<li>Azure SDK for iOS: <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-ios\">github.com\/Azure\/azure-sdk-for-ios<\/a><\/li>\n<li>Azure SDK for Go: <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-go\">github.com\/Azure\/azure-sdk-for-go<\/a><\/li>\n<li>Azure SDK for C: <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-c\">github.com\/Azure\/azure-sdk-for-c<\/a><\/li>\n<li>Azure SDK for C++: <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-cpp\">github.com\/Azure\/azure-sdk-for-cpp<\/a><\/li>\n<\/ul>\n<p><!-- FOOTER: DO NOT EDIT OR REMOVE --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;ll introduce the new Azure SDK for Python Resource Management Libraries and guide you on how to migrate to the new version<\/p>\n","protected":false},"author":42560,"featured_media":879,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[570,162,705],"class_list":["post-877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sdk","tag-management","tag-python","tag-sdk"],"acf":[],"blog_post_summary":"<p>In this post we&#8217;ll introduce the new Azure SDK for Python Resource Management Libraries and guide you on how to migrate to the new version<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/users\/42560"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/comments?post=877"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/877\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media\/879"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media?parent=877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/categories?post=877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/tags?post=877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}