From c658924b050042cacab6593c871ac8c5ea2e5d61 Mon Sep 17 00:00:00 2001 From: Russell Seymour Date: Mon, 20 Feb 2017 17:02:26 +0000 Subject: [PATCH] Removed need for multiple Azure class helpers Updated the RubyDoc on Classes and Methods Fixes #9 Signed-off-by: Russell Seymour --- libraries/common/compute_management.rb | 21 ++++++++++ libraries/common/helpers.rb | 50 ++++++++++++----------- libraries/common/network_management.rb | 20 ++++++++++ libraries/common/resource_groups.rb | 23 ----------- libraries/common/resource_management.rb | 53 +++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 46 deletions(-) create mode 100644 libraries/common/compute_management.rb create mode 100644 libraries/common/network_management.rb delete mode 100644 libraries/common/resource_groups.rb create mode 100644 libraries/common/resource_management.rb diff --git a/libraries/common/compute_management.rb b/libraries/common/compute_management.rb new file mode 100644 index 000000000..b13ab1ce5 --- /dev/null +++ b/libraries/common/compute_management.rb @@ -0,0 +1,21 @@ + +require 'azure_mgmt_compute' + +# Class to return a ComputeManagement client to get information about VMs +# +# @author Russell Seymour +# +# @attr_reader [Azure::ARM::Compute::ComputeManagementClient] client ComputeManagement client object +class ComputeManagement + attr_reader :client + + # Constructor for the class. Creates the new Network Management client object + # + # @author Russell Seymour + # + # @param [MsRest::TokenCredentials] azure Connection object for Azure + def initialize(azure) + @client = Azure::ARM::Compute::ComputeManagementClient.new(azure.connection) + client.subscription_id = azure.subscription_id + end +end diff --git a/libraries/common/helpers.rb b/libraries/common/helpers.rb index 5b0cb89f8..3a1a3fa4f 100644 --- a/libraries/common/helpers.rb +++ b/libraries/common/helpers.rb @@ -1,49 +1,53 @@ require_relative '../azure_conn' -require 'azure_mgmt_compute' -require_relative 'resource_groups' +require_relative 'resource_management' +require_relative 'compute_management' +require_relative 'network_management' +# Helper class to configure and give access to the various management components of Azure +# Also provides shortcuts for certain components, such as returing the VM object and performing +# all the checks that need to be done before retrieving the VM +# +# @author Russell Seymour +# @attr_reader [MsRest::TokenCredentials] azure Azure connection credentials +# @attr_reader [ComputeManagement] compute_mgmt Compute object for retrieving details about VMs +# @attr_reader [ResourceManagement] resource_mgmt Resource object for accessing specific resources and resoure groups +# @attr_reader [NetworkManagement] network_mgmt Network object for retrieving all information about Network cards and IP configurations class Helpers - attr_reader :azure, :client, :resource_group + attr_reader :azure, :compute_mgmt, :resource_mgmt, :network_mgmt + # Constructor to configure the various objects that are required for Inspec testing + # + # @author Russell Seymour def initialize # Azure connection @azure = AzureConnection.new - @client = Azure::ARM::Compute::ComputeManagementClient.new(azure.connection) - client.subscription_id = azure.subscription_id + # Create the necessary clients + @compute_mgmt = ComputeManagement.new(azure) + @resource_mgmt = ResourceManagement.new(azure) + @network_mgmt = NetworkManagement.new(azure) - @resource_group = ResourceGroups.new(azure) - end - - # Retrive the specified resource group - # - # == Returns: - # Object representing the resource group - # - def get_resource_group(rg_name) - resource_group.get(rg_name) - end - - def get_resources(rg_name) - resource_group.get_resources(rg_name) end # Retrieve the named virtual machine from Azure # - # == Returns: - # Object representing the VM in Azure + # This is specified here as it combines two different resource types, Compute and Resource Groups + # + # @author Russell Seymour + # + # @return [] VM object # def get_vm(name, rg_name) # Ensure that the resource group exists - unless resource_group.exists(rg_name) + unless resource_mgmt.client.resource_groups.check_existence(rg_name) raise "The Resource group cannot be found: #{rg_name}" end # get a vm from the named resource group begin - client.virtual_machines.get(rg_name, name) + compute_mgmt.client.virtual_machines.get(rg_name, name) rescue => e e.error_message end diff --git a/libraries/common/network_management.rb b/libraries/common/network_management.rb new file mode 100644 index 000000000..a12fed784 --- /dev/null +++ b/libraries/common/network_management.rb @@ -0,0 +1,20 @@ + +require 'azure_mgmt_network' + +# Class to return a NetworkManagement client for use with NICs and Public IP Addresses +# +# @author Russell Seymour +# @attr_reader [Azure::ARM::Network::NetworkManagementClient] client Azure Network Management cient +class NetworkManagement + attr_reader :client + + # Constructor for the class. Creates the new Network Management client object + # + # @author Russell Seymour + # + # @param [MsRest::TokenCredentials] azure Connection object for Azure + def initialize(azure) + @client = Azure::ARM::Network::NetworkManagementClient.new(azure.connection) + client.subscription_id = azure.subscription_id + end +end diff --git a/libraries/common/resource_groups.rb b/libraries/common/resource_groups.rb deleted file mode 100644 index dcaeceb0b..000000000 --- a/libraries/common/resource_groups.rb +++ /dev/null @@ -1,23 +0,0 @@ - -require 'azure_mgmt_resources' - -class ResourceGroups - attr_reader :client - - def initialize(azure) - @client = Azure::ARM::Resources::ResourceManagementClient.new(azure.connection) - client.subscription_id = azure.subscription_id - end - - def exists(name) - client.resource_groups.check_existence(name) - end - - def get(name) - client.resource_groups.get(name) if exists(name) - end - - def get_resources(name) - client.resource_groups.list_resources_as_lazy(name) if exists(name) - end -end diff --git a/libraries/common/resource_management.rb b/libraries/common/resource_management.rb new file mode 100644 index 000000000..247e1b7d3 --- /dev/null +++ b/libraries/common/resource_management.rb @@ -0,0 +1,53 @@ + +require 'azure_mgmt_resources' + +# Class to return a NetworkManagement client for use with NICs and Public IP Addresses +# +# @author Russell Seymour +# @attr_reader [Azure::ARM::Network::NetworkManagementClient] client Azure Network Management cient +class ResourceManagement + attr_reader :client + + # Constructor for the class. Creates the new Network Management client object + # + # @author Russell Seymour + # + # @param [MsRest::TokenCredentials] azure Connection object for Azure + def initialize(azure) + @client = Azure::ARM::Resources::ResourceManagementClient.new(azure.connection) + client.subscription_id = azure.subscription_id + end + + # Determine if the specified resource group exists in the subscription_id + # + # @author Russell Seymour + # + # @param [String] name Name of the resource group + # + # @return [Boolean] Whether the resource group exists or not + def exists(name) + client.resource_groups.check_existence(name) + end + + # Retrieve the named resource group if it exists + # + # @author Russell Seymour + # + # @param [String] name Name of the resource group + # + # @return [Azure::ARM::Resources::Models::ResourceGroup] Object containing information about the resource group + def get_resource_group(name) + client.resource_groups.get(name) if exists(name) + end + + # Get all of the resources that are contained within the resource group if it exists + # + # @author Russell Seymour + # + # @param [String] name Name of the resource group + # + # @return [Azure::ARM::Resources::Models::ResourceListResult] Object containing array of all the resources + def get_resources(name) + client.resource_groups.list_resources_as_lazy(name) if exists(name) + end +end