Skip to content

Commit

Permalink
Merge pull request #212 from inspec/sa_queue
Browse files Browse the repository at this point in the history
Adds Queue functionality to azurerm_storage_account
  • Loading branch information
rmoles authored Oct 22, 2019
2 parents c42029f + 6fde532 commit 622dfb8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 10 deletions.
9 changes: 5 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

source 'https://rubygems.org'

gem 'faraday', '~> 0.15.0'
gem 'faraday_middleware', '~> 0.12.2'
gem 'inspec', '~> 3.0', '>= 3.0.9'
gem 'rake', '~> 12.3', '>= 12.3.1'
gem 'activesupport', '~> 5.2.3'
gem 'faraday', '~> 0.15.0'
gem 'faraday_middleware', '~> 0.12.2'
gem 'inspec', '~> 3.0', '>= 3.0.9'
gem 'rake', '~> 12.3', '>= 12.3.1'

group :development do
gem 'pry', '~> 0.11.3'
Expand Down
4 changes: 4 additions & 0 deletions libraries/azurerm_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def vault(vault_name)
Azure::Vault.new(vault_name, inspec.backend)
end

def queue(queue_name)
Azure::Queue.new(queue_name, inspec.backend)
end

private

def has_error?(struct)
Expand Down
8 changes: 8 additions & 0 deletions libraries/azurerm_storage_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def has_recently_generated_access_key?
log_events.any?
end

def queues
@queues ||= queue(name).queues
end

def queue_properties
@queue_properties ||= queue(name).queue_properties
end

def to_s
"#{name} Storage Account"
end
Expand Down
1 change: 1 addition & 0 deletions libraries/support/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
require 'support/azure/management'
require 'support/azure/vault'
require 'support/azure/graph'
require 'support/azure/queue'
require 'support/azure/deprecations/strings_in_where_clause'
95 changes: 95 additions & 0 deletions libraries/support/azure/queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# frozen_string_literal: true

require 'ostruct'
require 'json'
require 'active_support/core_ext/hash'

module Azure
class Queue
include Service

def initialize(queue_name, backend)
@required_attrs = []
@page_link_name = 'nextMarker'
@rest_client = Azure::Rest.new(client(queue_name, backend))
end

def queues
catch_404 do
get(
url: '?comp=list',
headers: { 'x-ms-version' => '2017-11-09' },
api_version: nil,
unwrap: from_xml,
)
end
end

def queue_properties
catch_404 do
get(
url: '/?restype=service&comp=properties',
headers: { 'x-ms-version' => '2017-11-09' },
api_version: nil,
unwrap: from_xml,
).storage_service_properties
end
end

private

attr_reader :rest_client

def client(queue, backend)
OpenStruct.new(
{
base_url: "https://#{queue}.queue.core.windows.net",
credentials: auth_token(backend),
},
)
end

def auth_token(backend)
begin
credentials = backend.instance_variable_get('@credentials')
tenant = credentials[:tenant_id]
client = credentials[:client_id]
secret = credentials[:client_secret]
rescue StandardError => e
raise "Unable to load Azure Configuration from backend.\n #{e}"
end

settings = MsRestAzure::ActiveDirectoryServiceSettings.get_azure_settings
settings.authentication_endpoint = 'https://login.microsoftonline.com/'
settings.token_audience = 'https://storage.azure.com/'

::MsRest::TokenCredentials.new(::MsRestAzure::ApplicationTokenProvider.new(tenant, client, secret, settings))
end

def from_xml
result = {}
lambda do |body, _api_version|
# API returns XML.
body = Hash.from_xml(body) unless body.is_a?(Hash)

# Snake case recursively.
body.each do |k, v|
if v.is_a?(Hash)
result[k.underscore] = from_xml.call(v, nil)
else
result[k.underscore] = v
end
end
result
end
end

def catch_404
yield # yield
rescue ::Faraday::ConnectionFailed
# No such Queue.
# Not all Storage Accounts have a Queue.
nil
end
end
end
2 changes: 2 additions & 0 deletions libraries/support/azure/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def post(path, params: {}, headers: {})
end
end

private

def connection
@connection ||= Faraday.new(url: host) do |conn|
conn.request :multipart
Expand Down
16 changes: 10 additions & 6 deletions libraries/support/azure/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ def set_reader(name, value, override)
self
end

def get(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, params: {}) # rubocop:disable Metrics/ParameterLists
def get(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, params: {}, headers: {}) # rubocop:disable Metrics/ParameterLists
confirm_configured!

body = cache.fetch(url) if use_cache

params = { 'api-version' => api_version }.merge(params) if api_version

body ||= rest_client.get(url,
params: { 'api-version' => api_version }.merge(params),
headers: { Accept: 'application/json' }).body
params: params,
headers: { Accept: 'application/json' }.merge(headers)).body

error_handler&.(body)

Expand All @@ -102,14 +104,16 @@ def get(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, pa
end
end

def post(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, params: {}) # rubocop:disable Metrics/ParameterLists
def post(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, params: {}, headers: {}) # rubocop:disable Metrics/ParameterLists
confirm_configured!

body = cache.fetch(url) if use_cache

params = { 'api-version' => api_version }.merge(params) if api_version

body ||= rest_client.post(url,
params: { 'api-version' => api_version }.merge(params),
headers: { Accept: 'application/json' }).body
params: params,
headers: { Accept: 'application/json' }.merge(headers)).body

error_handler&.(body)

Expand Down

0 comments on commit 622dfb8

Please sign in to comment.