mirror of
https://github.com/inspec/inspec
synced 2024-11-28 07:30:50 +00:00
Removed need for multiple Azure class helpers
Updated the RubyDoc on Classes and Methods Fixes #9 Signed-off-by: Russell Seymour <russell.seymour@turtlesystems.co.uk>
This commit is contained in:
parent
24e6153c4e
commit
c658924b05
5 changed files with 121 additions and 46 deletions
21
libraries/common/compute_management.rb
Normal file
21
libraries/common/compute_management.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
20
libraries/common/network_management.rb
Normal file
20
libraries/common/network_management.rb
Normal file
|
@ -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
|
|
@ -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
|
53
libraries/common/resource_management.rb
Normal file
53
libraries/common/resource_management.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue