Class: Inspec::Resources::AzureResourceBase
- Inherits:
-
Object
- Object
- Inspec::Resources::AzureResourceBase
- Defined in:
- lib/resources/azure/azure_backend.rb
Direct Known Subclasses
AzureGenericResource, AzureResourceGroup, AzureVirtualMachine, AzureVirtualMachineDataDisk
Instance Attribute Summary collapse
-
#azure ⇒ Object
readonly
Returns the value of attribute azure.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #catch_azure_errors ⇒ Object
-
#create_tag_methods ⇒ Object
It is necessary to be able to test the tags of a resource.
- #failed_resource? ⇒ Boolean
-
#has_tags? ⇒ Boolean
Does the resource have any tags?.
-
#initialize(opts) ⇒ AzureResourceBase
constructor
Constructor that retreives the specified resource.
-
#resource_group ⇒ Object
Return information about the resource group.
- #resources ⇒ Object
-
#tag_count ⇒ Object
Returns how many tags have been set on the resource.
Constructor Details
#initialize(opts) ⇒ AzureResourceBase
Constructor that retreives the specified resource
The opts hash should contain the following
:group_name - name of the resource group in which to look for items
:type - the type of Azure resource to look for
:apiversion - API version to use when looking for a specific resource
:name - name of the resource to find
rubocop:disable Metrics/AbcSize
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/resources/azure/azure_backend.rb', line 21 def initialize(opts) # declare the hashtable of counts @counts = {} @total = 0 @opts = opts # Determine if the environment variables for the options have been set option_var_names = { group_name: "AZURE_RESOURCE_GROUP_NAME", name: "AZURE_RESOURCE_NAME", type: "AZURE_RESOURCE_TYPE", apiversion: "AZURE_RESOURCE_API_VERSION", } option_var_names.each do |option_name, env_var_name| opts[option_name] = ENV[env_var_name] unless ENV[env_var_name].nil? end @azure = inspec.backend @client = azure.azure_client @failed_resource = false end |
Instance Attribute Details
#azure ⇒ Object (readonly)
Returns the value of attribute azure.
7 8 9 |
# File 'lib/resources/azure/azure_backend.rb', line 7 def azure @azure end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/resources/azure/azure_backend.rb', line 7 def client @client end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/resources/azure/azure_backend.rb', line 7 def opts @opts end |
Instance Method Details
#catch_azure_errors ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/resources/azure/azure_backend.rb', line 47 def catch_azure_errors yield rescue MsRestAzure::AzureOperationError => e # e.message is actually a massive stringified JSON, which might be useful in the future. # You want error_message here. fail_resource e. @failed_resource = true nil end |
#create_tag_methods ⇒ Object
It is necessary to be able to test the tags of a resource. It is possible to say of the resource has tags or not, and it is possible to check that the tags include a specific tag However the value is not accessible, this function creates methods for all the tags that are available.
The format of the method name is ‘<TAG_NAME>_tag’ and will return the value of that tag
Disabling rubopcop check. If this is set as a normal if..then..end statement there is a violation stating it should use a guard. When using a guard it throws this error
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/resources/azure/azure_backend.rb', line 154 def create_tag_methods # Iterate around the items of the tags and create the necessary access methods if defined?(.item) .item.each do |name, value| method_name = format("%s_tag", name) define_singleton_method method_name do value end end end end |
#failed_resource? ⇒ Boolean
43 44 45 |
# File 'lib/resources/azure/azure_backend.rb', line 43 def failed_resource? @failed_resource end |
#has_tags? ⇒ Boolean
Does the resource have any tags?
If it is a Hashtable then it does not, because there was nothing to parse so there is not a nested object to work with
132 133 134 |
# File 'lib/resources/azure/azure_backend.rb', line 132 def .is_a?(Hash) ? false : true end |
#resource_group ⇒ Object
Return information about the resource group
58 59 60 61 62 63 64 65 66 |
# File 'lib/resources/azure/azure_backend.rb', line 58 def resource_group catch_azure_errors do resource_group = client.resource_groups.get(opts[:group_name]) # create the methods for the resource group object dm = AzureResourceDynamicMethods.new dm.create_methods(self, resource_group) end end |
#resources ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/resources/azure/azure_backend.rb', line 68 def resources resources = nil catch_azure_errors do resources = client.resources.list_by_resource_group(opts[:group_name]) end return if failed_resource? # filter the resources based on the type, and the name if they been specified resources = filter_resources(resources, opts) # if there is one resource then define methods on this class if resources.count == 1 @total = 1 resource = nil catch_azure_errors do # get the apiversion for the resource, if one has not been specified apiversion = azure.get_api_version(resources[0].type, opts) # get the resource by id so it can be interrogated resource = client.resources.get_by_id(resources[0].id, apiversion) end return if failed_resource? dm = AzureResourceDynamicMethods.new dm.create_methods(self, resource) else # As there are many resources, parse each one so that it can be # interrogated by the FilterTable # @probes = parse_resources(resources, azure) @probes = resources.each.map do |item| # update the total @total += 1 # determine the counts for each type namespace, type_name = item.type.split(/\./) counts.key?(namespace) ? false : counts[namespace] = {} counts[namespace].key?(type_name) ? counts[namespace][type_name] += 1 : counts[namespace][type_name] = 1 # get the detail about the resource apiversion = azure.get_api_version(item.type, opts) resource = client.resources.get_by_id(item.id, apiversion) # parse the resource parse_resource(resource) end.compact # Iterate around the counts and create the necessary classes counts.each do |namespace, ns_counts| define_singleton_method namespace do AzureResourceTypeCounts.new(ns_counts) end end end end |
#tag_count ⇒ Object
Returns how many tags have been set on the resource
139 140 141 |
# File 'lib/resources/azure/azure_backend.rb', line 139 def tag_count .count end |