Skip to content

Commit

Permalink
Feat: Azure sdk namespace updates (#258)
Browse files Browse the repository at this point in the history
* Namespace updates for the azure sdk

Signed-off-by: Ashique P S <[email protected]>

* Updated the dependencies

Signed-off-by: Ashique P S <[email protected]>

* chefstyle issue fix

Signed-off-by: Ashique P S <[email protected]>

---------

Signed-off-by: Ashique P S <[email protected]>
  • Loading branch information
ashiqueps authored May 8, 2023
1 parent 12a6dc2 commit d3041f1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 68 deletions.
4 changes: 2 additions & 2 deletions kitchen-azurerm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 21 additions & 19 deletions lib/kitchen/driver/azure_credentials.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -87,71 +89,71 @@ 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
#
# Typically used with a service principal.
#
# 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
Expand Down
64 changes: 32 additions & 32 deletions lib/kitchen/driver/azurerm.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading

0 comments on commit d3041f1

Please sign in to comment.