From d3041f19dd68e4c3ea00631e9fa5d3a63ea92a76 Mon Sep 17 00:00:00 2001 From: Ashique P S Date: Mon, 8 May 2023 23:24:08 +0530 Subject: [PATCH] Feat: Azure sdk namespace updates (#258) * Namespace updates for the azure sdk Signed-off-by: Ashique P S * Updated the dependencies Signed-off-by: Ashique P S * chefstyle issue fix Signed-off-by: Ashique P S --------- Signed-off-by: Ashique P S --- kitchen-azurerm.gemspec | 4 +- lib/kitchen/driver/azure_credentials.rb | 40 ++++++------ lib/kitchen/driver/azurerm.rb | 64 +++++++++---------- spec/spec_helper.rb | 6 +- .../kitchen/driver/azure_credentials_spec.rb | 16 ++--- spec/unit/kitchen/driver/azurerm_spec.rb | 8 +-- 6 files changed, 70 insertions(+), 68 deletions(-) diff --git a/kitchen-azurerm.gemspec b/kitchen-azurerm.gemspec index 3f7c148..7ad8562 100644 --- a/kitchen-azurerm.gemspec +++ b/kitchen-azurerm.gemspec @@ -18,8 +18,8 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.6" - spec.add_dependency "azure_mgmt_network2", "~> 0.27" - spec.add_dependency "azure_mgmt_resources2", "~> 0.19" + spec.add_dependency "azure_mgmt_network2", "~> 1.0.1", ">= 1.0.1" + spec.add_dependency "azure_mgmt_resources2", "~> 1.0.1", ">= 1.0.1" spec.add_dependency "inifile", "~> 3.0", ">= 3.0.0" spec.add_dependency "sshkey", ">= 1.0.0", "< 3" spec.add_dependency "test-kitchen", ">= 1.20", "< 4.0" diff --git a/lib/kitchen/driver/azure_credentials.rb b/lib/kitchen/driver/azure_credentials.rb index 94309ac..efbb2b8 100644 --- a/lib/kitchen/driver/azure_credentials.rb +++ b/lib/kitchen/driver/azure_credentials.rb @@ -1,6 +1,8 @@ require "inifile" + require "kitchen/logging" -autoload :MsRest, "ms_rest" +autoload :MsRest2, "ms_rest2" +autoload :MsRestAzure2, "ms_rest_azure2" module Kitchen module Driver @@ -38,7 +40,7 @@ def initialize(subscription_id:, environment: "Azure") def azure_options options = { tenant_id: tenant_id!, subscription_id: subscription_id, - credentials: ::MsRest::TokenCredentials.new(token_provider), + credentials: ::MsRest2::TokenCredentials.new(token_provider), active_directory_settings: ad_settings, base_url: endpoint_settings.resource_manager_endpoint_url } options[:client_id] = client_id if client_id @@ -87,7 +89,7 @@ def client_secret # Retrieve a token based upon the preferred authentication method. # - # @return [::MsRest::TokenProvider] A new token provider object. + # @return [::MsRest2::TokenProvider] A new token provider object. def token_provider # Login with a credentials file or setting the environment variables # @@ -95,63 +97,63 @@ def token_provider # # SPN with client_id, client_secret and tenant_id if client_id && client_secret && tenant_id - ::MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, client_secret, ad_settings) + ::MsRestAzure2::ApplicationTokenProvider.new(tenant_id, client_id, client_secret, ad_settings) # Login with a Managed Service Identity. # # Typically used with a Managed Service Identity when you have a particular object registered in a tenant. # # MSI with client_id and tenant_id (aka User Assigned Identity). elsif client_id && tenant_id - ::MsRestAzure::MSITokenProvider.new(50342, ad_settings, { client_id: client_id }) + ::MsRestAzure2::MSITokenProvider.new(50342, ad_settings, { client_id: client_id }) # Default approach to inheriting existing object permissions (application or device this code is running on). # # Typically used when you want to inherit the permissions of the system you're running on that are in a tenant. # # MSI with just tenant_id (aka System Assigned Identity). elsif tenant_id - ::MsRestAzure::MSITokenProvider.new(50342, ad_settings) + ::MsRestAzure2::MSITokenProvider.new(50342, ad_settings) # Login using the Azure CLI # # Typically used when you want to rely upon `az login` as your preferred authentication method. else warn("Using tenant id set through `az login`.") - ::MsRestAzure::AzureCliTokenProvider.new(ad_settings) + ::MsRestAzure2::AzureCliTokenProvider.new(ad_settings) end end # - # Retrieves a [MsRestAzure::ActiveDirectoryServiceSettings] object representing the AD settings for the given cloud. + # Retrieves a [MsRestAzure2::ActiveDirectoryServiceSettings] object representing the AD settings for the given cloud. # - # @return [MsRestAzure::ActiveDirectoryServiceSettings] Settings to be used for subsequent requests + # @return [MsRestAzure2::ActiveDirectoryServiceSettings] Settings to be used for subsequent requests # def ad_settings case environment.downcase when "azureusgovernment" - ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_us_government_settings + ::MsRestAzure2::ActiveDirectoryServiceSettings.get_azure_us_government_settings when "azurechina" - ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_china_settings + ::MsRestAzure2::ActiveDirectoryServiceSettings.get_azure_china_settings when "azuregermancloud" - ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_german_settings + ::MsRestAzure2::ActiveDirectoryServiceSettings.get_azure_german_settings when "azure" - ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_settings + ::MsRestAzure2::ActiveDirectoryServiceSettings.get_azure_settings end end # - # Retrieves a [MsRestAzure::AzureEnvironment] object representing endpoint settings for the given cloud. + # Retrieves a [MsRestAzure2::AzureEnvironment] object representing endpoint settings for the given cloud. # - # @return [MsRestAzure::AzureEnvironment] Settings to be used for subsequent requests + # @return [MsRestAzure2::AzureEnvironment] Settings to be used for subsequent requests # def endpoint_settings case environment.downcase when "azureusgovernment" - ::MsRestAzure::AzureEnvironments::AzureUSGovernment + ::MsRestAzure2::AzureEnvironments::AzureUSGovernment when "azurechina" - ::MsRestAzure::AzureEnvironments::AzureChinaCloud + ::MsRestAzure2::AzureEnvironments::AzureChinaCloud when "azuregermancloud" - ::MsRestAzure::AzureEnvironments::AzureGermanCloud + ::MsRestAzure2::AzureEnvironments::AzureGermanCloud when "azure" - ::MsRestAzure::AzureEnvironments::AzureCloud + ::MsRestAzure2::AzureEnvironments::AzureCloud end end end diff --git a/lib/kitchen/driver/azurerm.rb b/lib/kitchen/driver/azurerm.rb index 7355eac..5d5ab6d 100644 --- a/lib/kitchen/driver/azurerm.rb +++ b/lib/kitchen/driver/azurerm.rb @@ -1,11 +1,11 @@ require "kitchen" -autoload :MsRestAzure, "ms_rest_azure" +autoload :MsRestAzure2, "ms_rest_azure2" require_relative "azure_credentials" require "securerandom" unless defined?(SecureRandom) module Azure - autoload :Resources, "azure_mgmt_resources" - autoload :Network, "azure_mgmt_network" + autoload :Resources2, "azure_mgmt_resources2" + autoload :Network2, "azure_mgmt_network2" end require "base64" unless defined?(Base64) autoload :SSHKey, "sshkey" @@ -21,7 +21,7 @@ module Driver # Azurerm # Create a new resource group object and set the location and tags attributes then return it. # - # @return [::Azure::Resources::Profiles::Latest::Mgmt::Models::ResourceGroup] A new resource group object. + # @return [::Azure::Resources2::Profiles::Latest::Mgmt::Models::ResourceGroup] A new resource group object. class Azurerm < Kitchen::Driver::Base attr_accessor :resource_management_client attr_accessor :network_management_client @@ -298,13 +298,13 @@ def create(state) environment: config[:azure_environment]).azure_options debug "Azure environment: #{config[:azure_environment]}" - @resource_management_client = ::Azure::Resources::Profiles::Latest::Mgmt::Client.new(options) + @resource_management_client = ::Azure::Resources2::Profiles::Latest::Mgmt::Client.new(options) # Create Resource Group begin info "Creating Resource Group: #{state[:azure_resource_group_name]}" create_resource_group(state[:azure_resource_group_name], get_resource_group) - rescue ::MsRestAzure::AzureOperationError => operation_error + rescue ::MsRestAzure2::AzureOperationError => operation_error error operation_error.body raise operation_error end @@ -333,7 +333,7 @@ def create(state) create_deployment_async(state[:azure_resource_group_name], post_deployment_name, post_deployment(config[:post_deployment_template], config[:post_deployment_parameters])).value! follow_deployment_until_end_state(state[:azure_resource_group_name], post_deployment_name) end - rescue ::MsRestAzure::AzureOperationError => operation_error + rescue ::MsRestAzure2::AzureOperationError => operation_error rest_error = operation_error.body["error"] deployment_active = rest_error["code"] == "DeploymentActive" if deployment_active @@ -345,7 +345,7 @@ def create(state) end end - @network_management_client = ::Azure::Network::Profiles::Latest::Mgmt::Client.new(options) + @network_management_client = ::Azure::Network2::Profiles::Latest::Mgmt::Client.new(options) if config[:vnet_id] == "" || config[:public_ip] # Retrieve the public IP from the resource group: @@ -464,9 +464,9 @@ def public_key_for_deployment(private_key_filename) def pre_deployment(pre_deployment_template_filename, pre_deployment_parameters) pre_deployment_template = ::File.read(pre_deployment_template_filename) - pre_deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new - pre_deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new - pre_deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental + pre_deployment = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::Deployment.new + pre_deployment.properties = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentProperties.new + pre_deployment.properties.mode = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental pre_deployment.properties.template = JSON.parse(pre_deployment_template) pre_deployment.properties.parameters = parameters_in_values_format(pre_deployment_parameters) debug(pre_deployment.properties.template) @@ -475,9 +475,9 @@ def pre_deployment(pre_deployment_template_filename, pre_deployment_parameters) def deployment(parameters) template = template_for_transport_name - deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new - deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new - deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental + deployment = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::Deployment.new + deployment.properties = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentProperties.new + deployment.properties.mode = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental deployment.properties.template = JSON.parse(template) deployment.properties.parameters = parameters_in_values_format(parameters) debug(JSON.pretty_generate(deployment.properties.template)) @@ -486,9 +486,9 @@ def deployment(parameters) def post_deployment(post_deployment_template_filename, post_deployment_parameters) post_deployment_template = ::File.read(post_deployment_template_filename) - post_deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new - post_deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new - post_deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental + post_deployment = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::Deployment.new + post_deployment.properties = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentProperties.new + post_deployment.properties.mode = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentMode::Incremental post_deployment.properties.template = JSON.parse(post_deployment_template) post_deployment.properties.parameters = parameters_in_values_format(post_deployment_parameters) debug(post_deployment.properties.template) @@ -497,9 +497,9 @@ def post_deployment(post_deployment_template_filename, post_deployment_parameter def empty_deployment template = virtual_machine_deployment_template_file("empty.erb", nil) - empty_deployment = ::Azure::Resources::Profiles::Latest::Mgmt::Models::Deployment.new - empty_deployment.properties = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentProperties.new - empty_deployment.properties.mode = ::Azure::Resources::Profiles::Latest::Mgmt::Models::DeploymentMode::Complete + empty_deployment = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::Deployment.new + empty_deployment.properties = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentProperties.new + empty_deployment.properties.mode = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::DeploymentMode::Complete empty_deployment.properties.template = JSON.parse(template) debug(JSON.pretty_generate(empty_deployment.properties.template)) empty_deployment @@ -570,7 +570,7 @@ def destroy(state) # Setup our authentication components for the SDK options = Kitchen::Driver::AzureCredentials.new(subscription_id: state[:subscription_id], environment: state[:azure_environment]).azure_options - @resource_management_client = ::Azure::Resources::Profiles::Latest::Mgmt::Client.new(options) + @resource_management_client = ::Azure::Resources2::Profiles::Latest::Mgmt::Client.new(options) # If we don't have any instances, let's check to see if the user wants to delete a resource group and if so let's delete! if state[:server_id].nil? && state[:azure_resource_group_name].nil? && !config[:explicit_resource_group_name].nil? && config[:destroy_explicit_resource_group] @@ -581,7 +581,7 @@ def destroy(state) delete_resource_group_async(config[:explicit_resource_group_name]) info "Destroy operation accepted and will continue in the background." return - rescue ::MsRestAzure::AzureOperationError => operation_error + rescue ::MsRestAzure2::AzureOperationError => operation_error error operation_error.body raise operation_error end @@ -614,12 +614,12 @@ def destroy(state) resource_group.tags = {} create_resource_group(state[:azure_resource_group_name], resource_group) unless config[:destroy_explicit_resource_group_tags] == false warn 'The "destroy_explicit_resource_group_tags" setting value is set to "true". The tags on the resource group will be removed.' unless config[:destroy_explicit_resource_group_tags] == false - rescue ::MsRestAzure::AzureOperationError => operation_error + rescue ::MsRestAzure2::AzureOperationError => operation_error error operation_error.body raise operation_error end - rescue ::MsRestAzure::AzureOperationError => operation_error + rescue ::MsRestAzure2::AzureOperationError => operation_error error operation_error.body raise operation_error end @@ -639,7 +639,7 @@ def destroy(state) info "Destroy operation accepted and will continue in the background." # Remove resource group name from driver state state.delete(:azure_resource_group_name) - rescue ::MsRestAzure::AzureOperationError => operation_error + rescue ::MsRestAzure2::AzureOperationError => operation_error error operation_error.body raise operation_error end @@ -769,13 +769,13 @@ def virtual_machine_deployment_template_file(template_file, data = {}) def resource_manager_endpoint_url(azure_environment) case azure_environment.downcase when "azureusgovernment" - MsRestAzure::AzureEnvironments::AzureUSGovernment.resource_manager_endpoint_url + MsRestAzure2::AzureEnvironments::AzureUSGovernment.resource_manager_endpoint_url when "azurechina" - MsRestAzure::AzureEnvironments::AzureChinaCloud.resource_manager_endpoint_url + MsRestAzure2::AzureEnvironments::AzureChinaCloud.resource_manager_endpoint_url when "azuregermancloud" - MsRestAzure::AzureEnvironments::AzureGermanCloud.resource_manager_endpoint_url + MsRestAzure2::AzureEnvironments::AzureGermanCloud.resource_manager_endpoint_url when "azure" - MsRestAzure::AzureEnvironments::AzureCloud.resource_manager_endpoint_url + MsRestAzure2::AzureEnvironments::AzureCloud.resource_manager_endpoint_url end end @@ -798,9 +798,9 @@ def prepared_custom_data # Create a new resource group object and set the location and tags attributes then return it. # - # @return [::Azure::Resources::Profiles::Latest::Mgmt::Models::ResourceGroup] A new resource group object. + # @return [::Azure::Resources2::Profiles::Latest::Mgmt::Models::ResourceGroup] A new resource group object. def get_resource_group - resource_group = ::Azure::Resources::Profiles::Latest::Mgmt::Models::ResourceGroup.new + resource_group = ::Azure::Resources2::Profiles::Latest::Mgmt::Models::ResourceGroup.new resource_group.location = config[:location] resource_group.tags = config[:resource_group_tags] resource_group @@ -868,7 +868,7 @@ def get_public_ip(resource_group_name, public_ip_name) def get_network_interface(resource_group_name, network_interface_name) retries = config[:azure_api_retries] begin - network_interfaces = ::Azure::Network::Profiles::Latest::Mgmt::NetworkInterfaces.new(network_management_client) + network_interfaces = ::Azure::Network2::Profiles::Latest::Mgmt::NetworkInterfaces.new(network_management_client) network_interfaces.get(resource_group_name, network_interface_name) rescue Faraday::TimeoutError, Faraday::ClientError => exception send_exception_message(exception, "while fetching network interface '#{network_interface_name}' for resource group '#{resource_group_name}'. #{retries} retries left.") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3f20301..e1412c2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,7 +12,7 @@ # limitations under the License. require "rspec/its" -require "ms_rest" -require "ms_rest_azure" -require "azure_mgmt_resources" +require "ms_rest2" +require "ms_rest_azure2" +require "azure_mgmt_resources2" require_relative "../lib/kitchen/driver/azurerm" diff --git a/spec/unit/kitchen/driver/azure_credentials_spec.rb b/spec/unit/kitchen/driver/azure_credentials_spec.rb index adeb4b7..cd4e857 100644 --- a/spec/unit/kitchen/driver/azure_credentials_spec.rb +++ b/spec/unit/kitchen/driver/azure_credentials_spec.rb @@ -1,5 +1,5 @@ require "spec_helper" -require "ms_rest_azure" +require "ms_rest_azure2" describe Kitchen::Driver::AzureCredentials do CLIENT_ID_AND_SECRET_SUB = 0 @@ -198,7 +198,7 @@ it { is_expected.to be_instance_of(Hash) } its([:tenant_id]) { is_expected.to eq(tenant_id) } its([:subscription_id]) { is_expected.to eq(subscription_id) } - its([:credentials]) { is_expected.to be_instance_of(MsRest::TokenCredentials) } + its([:credentials]) { is_expected.to be_instance_of(MsRest2::TokenCredentials) } its([:client_id]) { is_expected.to eq(client_id) } its([:client_secret]) { is_expected.to eq(client_secret) } its([:base_url]) { is_expected.to eq("https://management.azure.com/") } @@ -209,8 +209,8 @@ include_examples "common option specs" - it "uses token provider: MsRestAzure::ApplicationTokenProvider" do - expect(token_provider).to be_instance_of(MsRestAzure::ApplicationTokenProvider) + it "uses token provider: MsRestAzure2::ApplicationTokenProvider" do + expect(token_provider).to be_instance_of(MsRestAzure2::ApplicationTokenProvider) end it "sets the client_id" do @@ -229,8 +229,8 @@ include_examples "common option specs" - it "uses token provider: MsRestAzure::MSITokenProvider" do - expect(token_provider).to be_instance_of(MsRestAzure::MSITokenProvider) + it "uses token provider: MsRestAzure2::MSITokenProvider" do + expect(token_provider).to be_instance_of(MsRestAzure2::MSITokenProvider) end it "sets the client_id" do @@ -248,8 +248,8 @@ include_examples "common option specs" - it "uses token provider: MsRestAzure::MSITokenProvider" do - expect(token_provider).to be_instance_of(MsRestAzure::MSITokenProvider) + it "uses token provider: MsRestAzure2::MSITokenProvider" do + expect(token_provider).to be_instance_of(MsRestAzure2::MSITokenProvider) end it "does not set the client_id" do diff --git a/spec/unit/kitchen/driver/azurerm_spec.rb b/spec/unit/kitchen/driver/azurerm_spec.rb index 0bac2a1..a6938db 100644 --- a/spec/unit/kitchen/driver/azurerm_spec.rb +++ b/spec/unit/kitchen/driver/azurerm_spec.rb @@ -46,7 +46,7 @@ end let(:client) do - Azure::Resources::Profiles::Latest::Mgmt::Client.new(options) + Azure::Resources2::Profiles::Latest::Mgmt::Client.new(options) end let(:instance) do @@ -59,7 +59,7 @@ end let(:resource_group) do - Azure::Resources::Profiles::Latest::Mgmt::Models::ResourceGroup.new + Azure::Resources2::Profiles::Latest::Mgmt::Models::ResourceGroup.new end let(:resource_groups) do @@ -204,8 +204,8 @@ rg.location = location rg.tags = vm_tags - # https://github.com/Azure/azure-sdk-for-ruby/blob/master/runtime/ms_rest_azure/spec/azure_operation_error_spec.rb - expect { resource_groups.create_or_update(rgn, rg) }.to raise_error( an_instance_of(MsRestAzure::AzureOperationError) ) + # https://github.com/Azure/azure-sdk-for-ruby/blob/master/runtime/ms_rest_azure2/spec/azure_operation_error_spec.rb + expect { resource_groups.create_or_update(rgn, rg) }.to raise_error( an_instance_of(MsRestAzure2::AzureOperationError) ) end it "saves deployment credentials to state, when store_deployment_credentials_in_state is true" do