Class: Fog::Storage::AzureRM::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/azurerm/storage.rb,
lib/fog/azurerm/requests/storage/delete_disk.rb,
lib/fog/azurerm/requests/storage/list_containers.rb,
lib/fog/azurerm/requests/storage/create_container.rb,
lib/fog/azurerm/requests/storage/delete_container.rb,
lib/fog/azurerm/requests/storage/get_blob_metadata.rb,
lib/fog/azurerm/requests/storage/set_blob_metadata.rb,
lib/fog/azurerm/requests/storage/get_storage_account.rb,
lib/fog/azurerm/requests/storage/list_storage_accounts.rb,
lib/fog/azurerm/requests/storage/create_storage_account.rb,
lib/fog/azurerm/requests/storage/delete_storage_account.rb,
lib/fog/azurerm/requests/storage/get_container_metadata.rb,
lib/fog/azurerm/requests/storage/set_container_metadata.rb,
lib/fog/azurerm/requests/storage/get_storage_access_keys.rb,
lib/fog/azurerm/requests/storage/get_container_properties.rb,
lib/fog/azurerm/requests/storage/list_storage_account_for_rg.rb,
lib/fog/azurerm/requests/storage/get_container_access_control_list.rb,
lib/fog/azurerm/requests/storage/check_storage_account_name_availability.rb

Overview

This class provides the actual implementation for service calls.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real

Returns a new instance of Real.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fog/azurerm/storage.rb', line 59

def initialize(options)
  begin
    require 'azure_mgmt_storage'
    require 'azure/storage'
    @debug = ENV['DEBUG'] || options[:debug]
    require 'azure/core/http/debug_filter' if @debug
  rescue LoadError => e
    retry if require('rubygems')
    raise e.message
  end

  credentials = Fog::Credentials::AzureRM.get_credentials(options[:tenant_id], options[:client_id], options[:client_secret])
  unless credentials.nil?
    @storage_mgmt_client = ::Azure::ARM::Storage::StorageManagementClient.new(credentials)
    @storage_mgmt_client.subscription_id = options[:subscription_id]
  end

  if Fog::Credentials::AzureRM. options
    Azure::Storage.setup(storage_account_name: options[:azure_storage_account_name],
                         storage_access_key: options[:azure_storage_access_key],
                         storage_connection_string: options[:azure_storage_connection_string])

    @blob_client = Azure::Storage::Blob::BlobService.new
    @blob_client.with_filter(Azure::Storage::Core::Filter::ExponentialRetryPolicyFilter.new)
    @blob_client.with_filter(Azure::Core::Http::DebugFilter.new) if @debug
  end
end

Instance Method Details

#check_storage_account_name_availability(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/azurerm/requests/storage/check_storage_account_name_availability.rb', line 6

def (params)
  msg = "Checking Name availability: #{params.name}."
  Fog::Logger.debug msg
  begin
    name_available_obj = @storage_mgmt_client.storage_accounts.check_name_availability(params)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  if name_available_obj.name_available
    Fog::Logger.debug "Name: #{params.name} is available."
    true
  else
    Fog::Logger.debug "Name: #{params.name} is not available."
    Fog::Logger.debug "Reason: #{name_available_obj.reason}."
    false
  end
end

#create_container(name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/create_container.rb', line 6

def create_container(name, options = {})
  msg = "Creating container: #{name}."
  Fog::Logger.debug msg
  begin
    container = @blob_client.create_container(name, options)
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Container #{name} created successfully."
  container
end

#create_storage_account(storage_account_hash) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/azurerm/requests/storage/create_storage_account.rb', line 6

def ()
  msg = "Creating Storage Account: #{[:name]} in Resource Group #{[:resource_group]}."
  Fog::Logger.debug msg
   = ([:sku_name],
                                                      [:location],
                                                      [:replication])
  begin
     = @storage_mgmt_client.storage_accounts.create([:resource_group],
                                                                   [:name],
                                                                   )
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug 'Storage Account created successfully.'
  
end

#delete_container(name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/delete_container.rb', line 6

def delete_container(name, options = {})
  msg = "Deleting container: #{name}."
  Fog::Logger.debug msg
  begin
    @blob_client.delete_container(name, options)
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Container #{name} deleted successfully."
  true
end

#delete_disk(resource_group, storage_account_name, blob_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/azurerm/requests/storage/delete_disk.rb', line 6

def delete_disk(resource_group, , blob_name)
  msg = "Deleting Disk: #{blob_name}."
  Fog::Logger.debug msg
  keys = get_storage_access_keys(resource_group, )
  access_key = keys[1].value
  client = Azure::Storage::Client.new(storage_account_name: , storage_access_key: access_key)
  blob_service = Azure::Storage::Blob::BlobService.new(client: client)
  begin
    result = blob_service.delete_blob('vhds', "#{blob_name}.vhd")
  rescue Azure::Core::Http::HTTPError => e
    raise_azure_exception(e, msg)
  end
  if result.nil?
    Fog::Logger.debug "Successfully deleted Disk: #{blob_name}."
    true
  else
    Fog::Logger.debug 'Error deleting Disk.'
    false
  end
end

#delete_storage_account(resource_group, name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/delete_storage_account.rb', line 6

def (resource_group, name)
  msg = "Deleting Storage Account: #{name} in Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    @storage_mgmt_client.storage_accounts.delete(resource_group, name)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Storage Account #{name} deleted successfully."
  true
end

#get_blob_metadata(container_name, name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/get_blob_metadata.rb', line 6

def (container_name, name)
  msg = "Getting Blob #{name} metadata in container #{container_name}."
  Fog::Logger.debug msg
  begin
      blob = @blob_client.(container_name, name)
    rescue Azure::Core::Http::HTTPError => ex
      raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Getting metadata of blob #{name} successfully."
  blob.
end

#get_container_access_control_list(name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/get_container_access_control_list.rb', line 6

def get_container_access_control_list(name, options = {})
  msg = "Get container ACL: #{name}."
  Fog::Logger.debug msg
  begin
    container_acl = @blob_client.get_container_acl(name, options)
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Getting ACL of container #{name} successfully."
  container_acl
end

#get_container_metadata(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/get_container_metadata.rb', line 6

def (name)
  msg = "Getting Container #{name} metadata."
  Fog::Logger.debug msg
  begin
    container = @blob_client.(name)
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Getting metadata of container #{name} successfully."
  container.
end

#get_container_properties(name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/get_container_properties.rb', line 6

def get_container_properties(name, options = {})
  msg = "Getting container properties: #{name}."
  Fog::Logger.debug msg
  begin
    container_properties = @blob_client.get_container_properties(name, options)
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Getting properties of container #{name} successfully."
  container_properties
end

#get_storage_access_keys(resource_group, storage_account_name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/get_storage_access_keys.rb', line 6

def get_storage_access_keys(resource_group, , options = {})
  msg = "Getting storage access keys for storage account: #{}."
  Fog::Logger.debug msg
  begin
     = @storage_mgmt_client.storage_accounts.list_keys(resource_group, , options)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Storage access keys for storage account: #{} listed successfully."
  .keys
end

#get_storage_account(resource_group_name, storage_account_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/get_storage_account.rb', line 6

def (resource_group_name, )
  msg = "Getting storage account: #{}."
  Fog::Logger.debug msg
  begin
     = @storage_mgmt_client.storage_accounts.get_properties(resource_group_name, )
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  Fog::Logger.debug "Getting storage account: #{} successfully."
  
end

#list_containers(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/list_containers.rb', line 6

def list_containers(options = {})
  msg = "Listing containers."
  Fog::Logger.debug msg
  begin
    containers = @blob_client.list_containers(options)
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug 'Listing containers successfully.'
  containers
end

#list_storage_account_for_rg(resource_group) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/azurerm/requests/storage/list_storage_account_for_rg.rb', line 6

def (resource_group)
  msg = "Listing Storage Accounts in Resource Group #{resource_group}."
  Fog::Logger.debug msg
  begin
    result = @storage_mgmt_client.storage_accounts.list_by_resource_group(resource_group)
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  result.value
end

#list_storage_accountsObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/azurerm/requests/storage/list_storage_accounts.rb', line 6

def list_storage_accounts
  msg = 'Listing Storage Accounts.'
  Fog::Logger.debug msg
  begin
    result = @storage_mgmt_client.storage_accounts.list
  rescue MsRestAzure::AzureOperationError => e
    raise_azure_exception(e, msg)
  end
  result.value
end

#set_blob_metadata(container_name, name, metadata) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/set_blob_metadata.rb', line 6

def (container_name, name, )
  msg = "Setting Blob #{name} metadata in a container #{container_name}."
  Fog::Logger.debug msg
  begin
    @blob_client.(container_name, name, )
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Setting metadata of blob #{name} successfully."
  true
end

#set_container_metadata(name, metadata) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fog/azurerm/requests/storage/set_container_metadata.rb', line 6

def (name, )
  msg = "Setting Container #{name} metadata."
  Fog::Logger.debug msg
  begin
    @blob_client.(name, )
  rescue Azure::Core::Http::HTTPError => ex
    raise_azure_exception(ex, msg)
  end
  Fog::Logger.debug "Setting metadata of container #{name} successfully."
  true
end