Module: Fog::AzureRM::Utilities::General

Included in:
Storage::Directories, Storage::Directory, Storage::File, Storage::Files, Storage::Real
Defined in:
lib/fog/azurerm/utilities/general.rb

Overview

General utilities mixin.

Instance Method Summary collapse

Instance Method Details

#get_blob_endpoint(storage_account_name, enable_https = false, environment = ENVIRONMENT_AZURE_CLOUD) ⇒ Object



92
93
94
95
# File 'lib/fog/azurerm/utilities/general.rb', line 92

def get_blob_endpoint(, enable_https = false, environment = ENVIRONMENT_AZURE_CLOUD)
  protocol = enable_https ? 'https' : 'http'
  "#{protocol}://#{}.blob#{storage_endpoint_suffix(environment)}"
end

#get_blob_endpoint_with_domain(storage_account_name, enable_https = false, domain = 'blob.core.windows.net') ⇒ Object



97
98
99
100
# File 'lib/fog/azurerm/utilities/general.rb', line 97

def get_blob_endpoint_with_domain(, enable_https = false, domain = 'blob.core.windows.net')
  protocol = enable_https ? 'https' : 'http'
  "#{protocol}://#{}.#{domain}"
end

#get_circuit_name_from_id(circuit_id) ⇒ Object

Pick Express Route Circuit name from Id(String)



48
49
50
# File 'lib/fog/azurerm/utilities/general.rb', line 48

def get_circuit_name_from_id(circuit_id)
  circuit_id.split('/')[8]
end

#get_end_point_type(endpoint_type) ⇒ Object

Extract Endpoint type from (String)



24
25
26
# File 'lib/fog/azurerm/utilities/general.rb', line 24

def get_end_point_type(endpoint_type)
  endpoint_type.split('/')[2]
end

#get_hash_from_object(object) ⇒ Object



36
37
38
39
40
# File 'lib/fog/azurerm/utilities/general.rb', line 36

def get_hash_from_object(object)
  hash = {}
  object.instance_variables.each { |attr| hash[attr.to_s.delete('@')] = object.instance_variable_get(attr) }
  hash
end

#get_image_name(id) ⇒ Object



134
135
136
# File 'lib/fog/azurerm/utilities/general.rb', line 134

def get_image_name(id)
  id.split('/').last
end

#get_record_set_from_id(id) ⇒ Object



28
29
30
# File 'lib/fog/azurerm/utilities/general.rb', line 28

def get_record_set_from_id(id)
  id.split('/')[8]
end

#get_record_type(type) ⇒ Object



52
53
54
# File 'lib/fog/azurerm/utilities/general.rb', line 52

def get_record_type(type)
  type.split('/').last
end

#get_resource_from_resource_id(resource_id, position) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/fog/azurerm/utilities/general.rb', line 67

def get_resource_from_resource_id(resource_id, position)
  data = resource_id.split('/') unless resource_id.nil?

  raise 'Invalid Resource ID' if data.count < 9 && data.count != 5

  data[position]
end

#get_resource_group_from_id(id) ⇒ Object

Pick Resource Group name from Azure Resource Id(String)



9
10
11
# File 'lib/fog/azurerm/utilities/general.rb', line 9

def get_resource_group_from_id(id)
  id.split('/')[4]
end

#get_subscription_id(id) ⇒ Object



138
139
140
# File 'lib/fog/azurerm/utilities/general.rb', line 138

def get_subscription_id(id)
  id.split('/')[2]
end

#get_traffic_manager_profile_name_from_endpoint_id(endpoint_id) ⇒ Object

Extract Traffic Manager Profile Name from Endpoint id(String)



43
44
45
# File 'lib/fog/azurerm/utilities/general.rb', line 43

def get_traffic_manager_profile_name_from_endpoint_id(endpoint_id)
  endpoint_id.split('/')[8]
end

#get_type_from_recordset_type(type) ⇒ Object



32
33
34
# File 'lib/fog/azurerm/utilities/general.rb', line 32

def get_type_from_recordset_type(type)
  type.split('/')[2]
end

#get_virtual_machine_from_id(vme_id) ⇒ Object

Pick Virtual Machine name from Virtual Machine Extension Id(String)



19
20
21
# File 'lib/fog/azurerm/utilities/general.rb', line 19

def get_virtual_machine_from_id(vme_id)
  vme_id.split('/')[VM_NAME_POSITION]
end

#get_virtual_network_from_id(subnet_id) ⇒ Object

Pick Virtual Network name from Subnet Resource Id(String)



14
15
16
# File 'lib/fog/azurerm/utilities/general.rb', line 14

def get_virtual_network_from_id(subnet_id)
  subnet_id.split('/')[8]
end

#parse_storage_object(object) ⇒ Object

Parse storage blob/container to a hash



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fog/azurerm/utilities/general.rb', line 103

def parse_storage_object(object)
  data = {}
  if object.is_a? Hash
    object.each do |k, v|
      if k == 'properties'
        v.each do |j, l|
          data[j] = l
        end
      else
        data[k] = v
      end
    end
  else
    object.instance_variables.each do |p|
      kname = p.to_s.delete('@')
      if kname == 'properties'
        properties = object.instance_variable_get(p)
        properties.each do |k, v|
          data[k.to_s] = v
        end
      else
        data[kname] = object.instance_variable_get(p)
      end
    end
  end

  data['last_modified'] = Time.parse(data['last_modified'])
  data['etag'].delete!('"')
  data
end

#raise_azure_exception(exception, _msg) ⇒ Object



56
57
58
59
# File 'lib/fog/azurerm/utilities/general.rb', line 56

def raise_azure_exception(exception, _msg)
  raise Fog::AzureRM::CustomAzureCoreHttpError.new(exception) if exception.is_a?(Azure::Core::Http::HTTPError)
  raise exception
end

#random_string(length) ⇒ Object



75
76
77
# File 'lib/fog/azurerm/utilities/general.rb', line 75

def random_string(length)
  (0...length).map { ('a'..'z').to_a[rand(26)] }.join
end

#remove_trailing_periods_from_path_segments(path) ⇒ Object



142
143
144
# File 'lib/fog/azurerm/utilities/general.rb', line 142

def remove_trailing_periods_from_path_segments(path)
  path.split('/').map { |segment| segment.gsub(/\.*$/, '') }.join('/')
end

#storage_endpoint_suffix(environment = ENVIRONMENT_AZURE_CLOUD) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fog/azurerm/utilities/general.rb', line 79

def storage_endpoint_suffix(environment = ENVIRONMENT_AZURE_CLOUD)
  case environment
  when ENVIRONMENT_AZURE_CHINA_CLOUD
    '.core.chinacloudapi.cn'
  when ENVIRONMENT_AZURE_US_GOVERNMENT
    '.core.usgovcloudapi.net'
  when ENVIRONMENT_AZURE_GERMAN_CLOUD
    '.core.cloudapi.de'
  else
    '.core.windows.net'
  end
end

#validate_params(required_params, input_params) ⇒ Object

Make sure if input_params(Hash) contains all keys present in required_params(Array)

Raises:

  • (ArgumentError)


62
63
64
65
# File 'lib/fog/azurerm/utilities/general.rb', line 62

def validate_params(required_params, input_params)
  missing_params = required_params.select { |param| param unless input_params.key?(param) }
  raise(ArgumentError, "Missing Parameters: #{missing_params.join(', ')} required for this operation") if missing_params.any?
end