Class: Inspec::Resources::AzureResourceGroup

Inherits:
AzureResourceBase show all
Defined in:
lib/resources/azure/azure_resource_group.rb

Instance Attribute Summary collapse

Attributes inherited from AzureResourceBase

#azure, #client, #opts

Instance Method Summary collapse

Methods inherited from AzureResourceBase

#catch_azure_errors, #create_tag_methods, #failed_resource?, #has_tags?, #resource_group, #resources, #tag_count

Constructor Details

#initialize(opts) ⇒ AzureResourceGroup

Constructor to get the resource group itself and perform some analysis on the resources that in the resource group.

This analysis is defined by the the mapping hashtable which is used to define the ‘has_xxx?’ methods (see AzureResourceGroup#create_has_methods) and return the counts for each type

Author:

  • Russell Seymour



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/resources/azure/azure_resource_group.rb', line 25

def initialize(opts)
  opts.key?(:name) ? opts[:group_name] = opts[:name] : false
  # Ensure that the opts only have the name of the resource group set
  opts.select! { |k, _v| k == :group_name }
  super(opts)

  # set the mapping for the Azure Resources
  @mapping = {
    nic: 'Microsoft.Network/networkInterfaces',
    vm: 'Microsoft.Compute/virtualMachines',
    extension: 'Microsoft.Compute/virtualMachines/extensions',
    nsg: 'Microsoft.Network/networkSecurityGroups',
    vnet: 'Microsoft.Network/virtualNetworks',
    managed_disk: 'Microsoft.Compute/disks',
    managed_disk_image: 'Microsoft.Compute/images',
    sa: 'Microsoft.Storage/storageAccounts',
    public_ip: 'Microsoft.Network/publicIPAddresses',
  }

  # Get information about the resource group itself
  resource_group

  # Get information about the resources in the resource group
  resources

  # Call method to create the has_xxxx? methods
  create_has_methods

  # Call method to allow access to the tag values
  create_tag_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id) ⇒ Object

This method catches the xxx_count calls that are made on the resource.

The method that is called is stripped of ‘_count’ and then compared with the mappings table. If that type exists then the number of those items is returned. However if that type is not in the Resource Group then the method will return a NoMethodError exception

Parameters:

  • method_id (Symbol)

    The name of the method that was called

Author:

  • Russell Seymour



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/resources/azure/azure_resource_group.rb', line 101

def method_missing(method_id)
  # Determine the mapping_key based on the method_id
  mapping_key = method_id.to_s.chomp('_count').to_sym

  if mapping.key?(mapping_key)
    # based on the method id get the
    namespace, type_name = mapping[mapping_key].split(/\./)

    # check that the type_name is defined, if not return 0
    if send(namespace).methods.include?(type_name.to_sym)
      # return the count for the method id
      send(namespace).send(type_name)
    else
      0
    end
  else
    msg = format('undefined method `%s` for %s', method_id, self.class)
    raise NoMethodError, msg
  end
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



15
16
17
# File 'lib/resources/azure/azure_resource_group.rb', line 15

def counts
  @counts
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/resources/azure/azure_resource_group.rb', line 15

def id
  @id
end

#locationObject (readonly)

Returns the value of attribute location.



15
16
17
# File 'lib/resources/azure/azure_resource_group.rb', line 15

def location
  @location
end

#mappingObject (readonly)

Returns the value of attribute mapping.



15
16
17
# File 'lib/resources/azure/azure_resource_group.rb', line 15

def mapping
  @mapping
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/resources/azure/azure_resource_group.rb', line 15

def name
  @name
end

#totalObject (readonly)

Returns the value of attribute total.



15
16
17
# File 'lib/resources/azure/azure_resource_group.rb', line 15

def total
  @total
end

Instance Method Details

#parse_resource(resource) ⇒ Object

Method to parse the resources that have been returned This allows the calculations of the amount of resources to be determined

Parameters:

  • resource (Hash)

    A hashtable representing the resource group

Author:

  • Russell Seymour



81
82
83
84
85
86
87
88
89
# File 'lib/resources/azure/azure_resource_group.rb', line 81

def parse_resource(resource)
  # return a hash of information
  parsed = {
    'name' => resource.name,
    'type' => resource.type,
  }

  parsed
end

#provisioning_stateObject

Return the provisioning state of the resource group

Author:

  • Russell Seymour



60
61
62
# File 'lib/resources/azure/azure_resource_group.rb', line 60

def provisioning_state
  properties.provisioningState
end

#subscription_idObject

Analyze the fully qualified id of the resource group to return the subscription id that this resource group is part of

The format of the id is

/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>

Author:

  • Russell Seymour



71
72
73
# File 'lib/resources/azure/azure_resource_group.rb', line 71

def subscription_id
  id.split(%r{\/}).reject(&:empty?)[1]
end