fog-azure-rm-temp 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/.arclint +8 -0
  3. data/.travis.yml +28 -14
  4. data/CHANGELOG.md +77 -0
  5. data/CONTRIBUTORS.md +4 -0
  6. data/fog-azure-rm.gemspec +3 -2
  7. data/lib/fog/azurerm.rb +1 -0
  8. data/lib/fog/azurerm/async_response.rb +9 -1
  9. data/lib/fog/azurerm/compute.rb +3 -1
  10. data/lib/fog/azurerm/constants.rb +11 -3
  11. data/lib/fog/azurerm/dns.rb +3 -1
  12. data/lib/fog/azurerm/docs/application_gateway.md +119 -117
  13. data/lib/fog/azurerm/docs/compute.md +299 -202
  14. data/lib/fog/azurerm/docs/dns.md +44 -43
  15. data/lib/fog/azurerm/docs/key_vault.md +31 -28
  16. data/lib/fog/azurerm/docs/network.md +407 -404
  17. data/lib/fog/azurerm/docs/resources.md +52 -49
  18. data/lib/fog/azurerm/docs/sql.md +56 -54
  19. data/lib/fog/azurerm/docs/storage.md +92 -157
  20. data/lib/fog/azurerm/docs/traffic_manager.md +55 -53
  21. data/lib/fog/azurerm/models/application_gateway/gateway.rb +5 -1
  22. data/lib/fog/azurerm/models/compute/availability_set.rb +4 -1
  23. data/lib/fog/azurerm/models/compute/managed_disk.rb +0 -1
  24. data/lib/fog/azurerm/models/compute/server.rb +61 -11
  25. data/lib/fog/azurerm/models/compute/servers.rb +2 -2
  26. data/lib/fog/azurerm/models/dns/a_record.rb +15 -0
  27. data/lib/fog/azurerm/models/dns/cname_record.rb +15 -0
  28. data/lib/fog/azurerm/models/dns/record_set.rb +20 -8
  29. data/lib/fog/azurerm/models/key_vault/vault.rb +3 -1
  30. data/lib/fog/azurerm/models/network/load_balancer.rb +3 -1
  31. data/lib/fog/azurerm/models/network/local_network_gateway.rb +3 -1
  32. data/lib/fog/azurerm/models/network/network_interface.rb +3 -1
  33. data/lib/fog/azurerm/models/network/network_security_group.rb +4 -2
  34. data/lib/fog/azurerm/models/network/public_ip.rb +4 -2
  35. data/lib/fog/azurerm/models/network/virtual_network.rb +4 -2
  36. data/lib/fog/azurerm/models/resources/resource_group.rb +3 -1
  37. data/lib/fog/azurerm/models/sql/sql_database.rb +3 -3
  38. data/lib/fog/azurerm/models/sql/sql_server.rb +3 -2
  39. data/lib/fog/azurerm/models/storage/directories.rb +6 -1
  40. data/lib/fog/azurerm/models/storage/file.rb +2 -0
  41. data/lib/fog/azurerm/models/storage/storage_account.rb +4 -1
  42. data/lib/fog/azurerm/models/storage/storage_accounts.rb +7 -0
  43. data/lib/fog/azurerm/models/traffic_manager/traffic_manager_profile.rb +3 -1
  44. data/lib/fog/azurerm/requests/application_gateway/check_ag_exists.rb +1 -1
  45. data/lib/fog/azurerm/requests/application_gateway/create_or_update_application_gateway.rb +1 -0
  46. data/lib/fog/azurerm/requests/compute/attach_data_disk_to_vm.rb +2 -2
  47. data/lib/fog/azurerm/requests/compute/check_availability_set_exists.rb +1 -1
  48. data/lib/fog/azurerm/requests/compute/check_managed_disk_exists.rb +1 -1
  49. data/lib/fog/azurerm/requests/compute/check_vm_exists.rb +1 -1
  50. data/lib/fog/azurerm/requests/compute/check_vm_extension_exists.rb +2 -2
  51. data/lib/fog/azurerm/requests/compute/create_availability_set.rb +4 -2
  52. data/lib/fog/azurerm/requests/compute/create_image.rb +68 -0
  53. data/lib/fog/azurerm/requests/compute/create_virtual_machine.rb +302 -135
  54. data/lib/fog/azurerm/requests/compute/delete_image.rb +29 -0
  55. data/lib/fog/azurerm/requests/compute/get_image.rb +45 -0
  56. data/lib/fog/azurerm/requests/compute/get_virtual_machine.rb +4 -2
  57. data/lib/fog/azurerm/requests/dns/check_record_set_exists.rb +1 -1
  58. data/lib/fog/azurerm/requests/dns/check_zone_exists.rb +3 -2
  59. data/lib/fog/azurerm/requests/dns/create_or_update_record_set.rb +3 -2
  60. data/lib/fog/azurerm/requests/dns/create_or_update_zone.rb +1 -2
  61. data/lib/fog/azurerm/requests/key_vault/check_vault_exists.rb +1 -1
  62. data/lib/fog/azurerm/requests/key_vault/create_or_update_vault.rb +1 -0
  63. data/lib/fog/azurerm/requests/network/check_express_route_cir_auth_exists.rb +1 -1
  64. data/lib/fog/azurerm/requests/network/check_express_route_circuit_exists.rb +1 -1
  65. data/lib/fog/azurerm/requests/network/check_load_balancer_exists.rb +1 -1
  66. data/lib/fog/azurerm/requests/network/check_local_net_gateway_exists.rb +1 -1
  67. data/lib/fog/azurerm/requests/network/check_net_sec_group_exists.rb +1 -1
  68. data/lib/fog/azurerm/requests/network/check_net_sec_rule_exists.rb +1 -1
  69. data/lib/fog/azurerm/requests/network/check_network_interface_exists.rb +1 -1
  70. data/lib/fog/azurerm/requests/network/check_public_ip_exists.rb +1 -1
  71. data/lib/fog/azurerm/requests/network/check_subnet_exists.rb +1 -1
  72. data/lib/fog/azurerm/requests/network/check_virtual_network_exists.rb +1 -1
  73. data/lib/fog/azurerm/requests/network/check_vnet_gateway_connection_exists.rb +1 -1
  74. data/lib/fog/azurerm/requests/network/check_vnet_gateway_exists.rb +1 -1
  75. data/lib/fog/azurerm/requests/network/create_load_balancer.rb +4 -4
  76. data/lib/fog/azurerm/requests/network/create_or_update_express_route_circuit.rb +1 -1
  77. data/lib/fog/azurerm/requests/network/create_or_update_local_network_gateway.rb +1 -2
  78. data/lib/fog/azurerm/requests/network/create_or_update_network_interface.rb +4 -3
  79. data/lib/fog/azurerm/requests/network/create_or_update_network_security_group.rb +4 -3
  80. data/lib/fog/azurerm/requests/network/create_or_update_public_ip.rb +5 -3
  81. data/lib/fog/azurerm/requests/network/create_or_update_virtual_network.rb +4 -3
  82. data/lib/fog/azurerm/requests/network/create_or_update_virtual_network_gateway_connection.rb +1 -1
  83. data/lib/fog/azurerm/requests/resources/check_azure_resource_exists.rb +4 -4
  84. data/lib/fog/azurerm/requests/resources/create_resource_group.rb +2 -1
  85. data/lib/fog/azurerm/requests/resources/delete_resource_tag.rb +4 -4
  86. data/lib/fog/azurerm/requests/resources/tag_resource.rb +4 -4
  87. data/lib/fog/azurerm/requests/sql/create_or_update_database.rb +13 -10
  88. data/lib/fog/azurerm/requests/sql/create_or_update_sql_server.rb +8 -5
  89. data/lib/fog/azurerm/requests/storage/check_storage_account_exists.rb +1 -1
  90. data/lib/fog/azurerm/requests/storage/create_storage_account.rb +4 -2
  91. data/lib/fog/azurerm/requests/traffic_manager/check_traffic_manager_endpoint_exists.rb +1 -1
  92. data/lib/fog/azurerm/requests/traffic_manager/check_traffic_manager_profile_exists.rb +1 -1
  93. data/lib/fog/azurerm/requests/traffic_manager/create_or_update_traffic_manager_profile.rb +13 -3
  94. data/lib/fog/azurerm/storage.rb +0 -18
  95. data/lib/fog/azurerm/utilities/general.rb +21 -10
  96. data/lib/fog/azurerm/version.rb +1 -1
  97. data/rake-script.sh +7 -1
  98. data/rakefile +1 -1
  99. data/test/api_stub.rb +19 -2
  100. data/test/api_stub/models/compute/server.rb +21 -5
  101. data/test/api_stub/requests/compute/image.rb +41 -0
  102. data/test/api_stub/requests/compute/virtual_machine.rb +60 -62
  103. data/test/api_stub/requests/resources/resource_group.rb +1 -1
  104. data/test/integration/application_gateway.rb +8 -1
  105. data/test/integration/availability_set.rb +9 -2
  106. data/test/integration/external_load_balancer.rb +2 -1
  107. data/test/integration/internal_load_balancer.rb +2 -1
  108. data/test/integration/key_vault.rb +2 -1
  109. data/test/integration/managed_disk.rb +10 -5
  110. data/test/integration/network_interface.rb +2 -1
  111. data/test/integration/network_security_group.rb +2 -1
  112. data/test/integration/public_ip.rb +2 -1
  113. data/test/integration/record_set.rb +5 -5
  114. data/test/integration/resource_group.rb +5 -2
  115. data/test/integration/resource_tag.rb +4 -2
  116. data/test/integration/server.rb +113 -23
  117. data/test/integration/server_custom_image.rb +120 -20
  118. data/test/integration/sql_server.rb +10 -2
  119. data/test/integration/storage_account.rb +32 -20
  120. data/test/integration/traffic_manager.rb +6 -1
  121. data/test/integration/virtual_network.rb +2 -1
  122. data/test/integration/virtual_network_gateway_connection.rb +3 -3
  123. data/test/models/application_gateway/test_gateway.rb +2 -1
  124. data/test/models/compute/test_availability_set.rb +2 -1
  125. data/test/models/compute/test_managed_disk.rb +2 -1
  126. data/test/models/compute/test_server.rb +13 -4
  127. data/test/models/dns/test_zone.rb +2 -1
  128. data/test/models/key_vault/test_vault.rb +2 -1
  129. data/test/models/network/test_load_balancer.rb +2 -1
  130. data/test/models/network/test_network_interface.rb +2 -1
  131. data/test/models/network/test_network_security_group.rb +2 -1
  132. data/test/models/network/test_public_ip.rb +2 -1
  133. data/test/models/network/test_virtual_network.rb +2 -1
  134. data/test/models/resources/test_resource_group.rb +2 -1
  135. data/test/models/sql/test_database.rb +2 -1
  136. data/test/models/sql/test_sql_server.rb +2 -1
  137. data/test/models/storage/test_storage_account.rb +2 -1
  138. data/test/models/traffic_manager/test_traffic_manager_profile.rb +2 -1
  139. data/test/requests/application_gateway/test_check_ag_exists.rb +11 -4
  140. data/test/requests/compute/test_attach_data_disk_to_vm.rb +2 -2
  141. data/test/requests/compute/test_check_availability_set_exists.rb +9 -2
  142. data/test/requests/compute/test_check_managed_disk_exists.rb +9 -2
  143. data/test/requests/compute/test_check_vm_exists.rb +9 -2
  144. data/test/requests/compute/test_check_vm_extension_exists.rb +9 -2
  145. data/test/requests/compute/test_create_image.rb +27 -0
  146. data/test/requests/compute/test_create_virtual_machine.rb +41 -17
  147. data/test/requests/compute/test_delete_image.rb +23 -0
  148. data/test/requests/compute/test_get_image.rb +23 -0
  149. data/test/requests/dns/test_check_record_set_exists.rb +9 -2
  150. data/test/requests/dns/test_check_zone_exists.rb +16 -2
  151. data/test/requests/key_vault/test_check_vault_exists.rb +9 -2
  152. data/test/requests/network/test_check_express_route_cir_auth_exists.rb +9 -2
  153. data/test/requests/network/test_check_express_route_circuit_exists.rb +9 -2
  154. data/test/requests/network/test_check_load_balancer_exists.rb +9 -2
  155. data/test/requests/network/test_check_local_net_gateway_exists.rb +11 -4
  156. data/test/requests/network/test_check_net_sec_group_exists.rb +9 -2
  157. data/test/requests/network/test_check_net_sec_rule_exists.rb +9 -2
  158. data/test/requests/network/test_check_network_interface_exists.rb +9 -2
  159. data/test/requests/network/test_check_public_ip_exists.rb +9 -2
  160. data/test/requests/network/test_check_subnet_exists.rb +9 -2
  161. data/test/requests/network/test_check_virtual_network_exists.rb +10 -4
  162. data/test/requests/network/test_check_vnet_gateway_connection_exists.rb +9 -2
  163. data/test/requests/network/test_check_vnet_gateway_exists.rb +9 -2
  164. data/test/requests/network/test_create_load_balancer.rb +3 -2
  165. data/test/requests/network/test_create_network_interface.rb +4 -3
  166. data/test/requests/network/test_create_or_update_network_security_group.rb +3 -2
  167. data/test/requests/network/test_create_or_update_virtual_network.rb +5 -3
  168. data/test/requests/network/test_create_public_ip.rb +3 -2
  169. data/test/requests/network/test_update_public_ip.rb +3 -2
  170. data/test/requests/resources/test_create_resource_group.rb +3 -2
  171. data/test/requests/resources/test_delete_resource_tag.rb +3 -3
  172. data/test/requests/resources/test_tag_resource.rb +3 -3
  173. data/test/requests/storage/test_check_storage_account_exists.rb +8 -1
  174. data/test/requests/traffic_manager/test_check_traffic_manager_endpoint_exists.rb +9 -2
  175. data/test/requests/traffic_manager/test_check_traffic_manager_profile_exists.rb +9 -2
  176. data/test/test_helper.rb +5 -14
  177. metadata +67 -75
  178. data/lib/fog/azurerm/models/storage/recovery_vault.rb +0 -50
  179. data/lib/fog/azurerm/models/storage/recovery_vaults.rb +0 -27
  180. data/lib/fog/azurerm/requests/storage/create_or_update_recovery_vault.rb +0 -54
  181. data/lib/fog/azurerm/requests/storage/delete_recovery_vault.rb +0 -35
  182. data/lib/fog/azurerm/requests/storage/disable_backup_protection.rb +0 -60
  183. data/lib/fog/azurerm/requests/storage/enable_backup_protection.rb +0 -61
  184. data/lib/fog/azurerm/requests/storage/get_all_backup_jobs.rb +0 -56
  185. data/lib/fog/azurerm/requests/storage/get_backup_container.rb +0 -53
  186. data/lib/fog/azurerm/requests/storage/get_backup_item.rb +0 -58
  187. data/lib/fog/azurerm/requests/storage/get_backup_job_for_vm.rb +0 -53
  188. data/lib/fog/azurerm/requests/storage/get_backup_protection_policy.rb +0 -64
  189. data/lib/fog/azurerm/requests/storage/get_recovery_vault.rb +0 -49
  190. data/lib/fog/azurerm/requests/storage/list_recovery_vaults.rb +0 -48
  191. data/lib/fog/azurerm/requests/storage/set_recovery_vault_context.rb +0 -36
  192. data/lib/fog/azurerm/requests/storage/start_backup.rb +0 -54
  193. data/test/api_stub/models/storage/recovery_vault.rb +0 -23
  194. data/test/api_stub/requests/storage/recovery_vault.rb +0 -189
  195. data/test/models/storage/test_recovery_vault.rb +0 -61
  196. data/test/models/storage/test_recovery_vaults.rb +0 -47
  197. data/test/requests/storage/test_create_recovery_vault.rb +0 -35
  198. data/test/requests/storage/test_delete_recovery_vault.rb +0 -34
  199. data/test/requests/storage/test_disable_backup_protection.rb +0 -52
  200. data/test/requests/storage/test_enable_backup_protection.rb +0 -66
  201. data/test/requests/storage/test_get_all_backup_jobs.rb +0 -35
  202. data/test/requests/storage/test_get_backup_container.rb +0 -35
  203. data/test/requests/storage/test_get_backup_item.rb +0 -35
  204. data/test/requests/storage/test_get_backup_job_for_vm.rb +0 -26
  205. data/test/requests/storage/test_get_backup_protection_policy.rb +0 -35
  206. data/test/requests/storage/test_get_recovery_vault.rb +0 -35
  207. data/test/requests/storage/test_list_recovery_vault.rb +0 -35
  208. data/test/requests/storage/test_set_recovery_vault_context.rb +0 -34
  209. data/test/requests/storage/test_start_backup.rb +0 -55
@@ -11,8 +11,8 @@ module Fog
11
11
  Fog::Logger.debug "Virtual Machine Extension #{vm_extension_name} exists."
12
12
  true
13
13
  rescue MsRestAzure::AzureOperationError => e
14
- if e.body['error']['code'] == 'ResourceNotFound'
15
- Fog::Logger.debug "Virtual machine #{vm_extension_name} doesn't exist."
14
+ if resource_not_found?(e)
15
+ Fog::Logger.debug "Virtual Machine Extension #{vm_extension_name} doesn't exist."
16
16
  false
17
17
  else
18
18
  raise_azure_exception(e, msg)
@@ -10,10 +10,11 @@ module Fog
10
10
  fault_domain_count = availability_set_params[:platform_fault_domain_count]
11
11
  update_domain_count = availability_set_params[:platform_update_domain_count]
12
12
  use_managed_disk = availability_set_params[:use_managed_disk].nil? ? false : availability_set_params[:use_managed_disk]
13
+ tags = availability_set_params[:tags]
13
14
 
14
15
  msg = "Creating Availability Set '#{name}' in #{location} region."
15
16
  Fog::Logger.debug msg
16
- avail_set_params = get_availability_set_properties(location, fault_domain_count, update_domain_count, use_managed_disk)
17
+ avail_set_params = get_availability_set_properties(location, fault_domain_count, update_domain_count, use_managed_disk, tags)
17
18
 
18
19
  begin
19
20
  availability_set = @compute_mgmt_client.availability_sets.create_or_update(resource_group, name, avail_set_params)
@@ -25,12 +26,13 @@ module Fog
25
26
  end
26
27
 
27
28
  # create the properties object for creating availability sets
28
- def get_availability_set_properties(location, fault_domain_count, update_domain_count, use_managed_disk)
29
+ def get_availability_set_properties(location, fault_domain_count, update_domain_count, use_managed_disk, tags)
29
30
  avail_set = Azure::ARM::Compute::Models::AvailabilitySet.new
30
31
  avail_set.location = location
31
32
  avail_set.sku = create_availability_set_sku(use_managed_disk)
32
33
  avail_set.platform_fault_domain_count = fault_domain_count.nil? ? FAULT_DOMAIN_COUNT : fault_domain_count
33
34
  avail_set.platform_update_domain_count = update_domain_count.nil? ? UPDATE_DOMAIN_COUNT : update_domain_count
35
+ avail_set.tags = tags
34
36
  avail_set
35
37
  end
36
38
 
@@ -0,0 +1,68 @@
1
+ module Fog
2
+ module Compute
3
+ class AzureRM
4
+ # This class provides the actual implementation for service calls.
5
+ class Real
6
+ def create_image(image_config)
7
+ msg = "Creating/Updating Image: #{image_config[:vm_name]}-osImage"
8
+ Fog::Logger.debug msg
9
+ image_name = "#{image_config[:vm_name]}-osImage"
10
+ image = setup_params(image_config)
11
+ begin
12
+ image_obj = @compute_mgmt_client.images.create_or_update(image_config[:resource_group], image_name, image)
13
+ rescue MsRestAzure::AzureOperationError => e
14
+ raise_azure_exception(e, msg)
15
+ end
16
+ Fog::Logger.debug "Image #{image_name} created/updated successfully."
17
+ image_obj
18
+ end
19
+
20
+ private
21
+
22
+ def setup_params(image_config)
23
+ storage_profile_image = Azure::ARM::Compute::Models::ImageStorageProfile.new
24
+ storage_profile_image.os_disk = create_generalized_os_disk_image(image_config)
25
+ image = Azure::ARM::Compute::Models::Image.new
26
+ image.storage_profile = storage_profile_image
27
+ image.location = image_config[:location]
28
+ image
29
+ end
30
+
31
+ def create_generalized_os_disk_image(image_config)
32
+ os_disk_image = Azure::ARM::Compute::Models::ImageOSDisk.new
33
+ os_disk_image.os_type = image_config[:platform]
34
+ os_disk_image.os_state = 'Generalized'
35
+ os_disk_image.blob_uri = image_config[:new_vhd_path]
36
+ os_disk_image.caching = Azure::ARM::Compute::Models::CachingTypes::ReadWrite
37
+ os_disk_image
38
+ end
39
+ end
40
+
41
+ # This class provides the mock implementation for unit tests.
42
+ class Mock
43
+ def create_image(*)
44
+ image_obj = {
45
+ 'location' => 'West US',
46
+ 'tags' => {
47
+ 'key' => 'value'
48
+ },
49
+ 'properties' => {
50
+ 'storageProfile' => {
51
+ 'osDisk' => {
52
+ 'osType' => 'Linux',
53
+ 'blobUri' => 'https://mystorageaccount.blob.core.windows.net/osimages/osimage.vhd',
54
+ 'osState' => 'generalized',
55
+ 'hostCaching' => 'readwrite',
56
+ 'storageAccountType' => 'Standard_LRS'
57
+ },
58
+ 'provisioningState' => 'Succeeded'
59
+ }
60
+ }
61
+ }
62
+ image_mapper = Azure::ARM::Compute::Models::Image.mapper
63
+ @compute_mgmt_client.deserialize(image_mapper, image_obj, 'result.body')
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,4 +1,5 @@
1
1
  require 'base64'
2
+
2
3
  WHITE_SPACE = ' '.freeze
3
4
 
4
5
  module Fog
@@ -6,163 +7,106 @@ module Fog
6
7
  class AzureRM
7
8
  # This class provides the actual implementation for service calls.
8
9
  class Real
9
- def create_virtual_machine(vm_hash, async = false)
10
- msg = "Creating Virtual Machine #{vm_hash[:name]} in Resource Group #{vm_hash[:resource_group]}."
10
+ def create_virtual_machine(vm_config, async = false)
11
+ vm_name = vm_config[:name]
12
+ rg_name = vm_config[:resource_group]
13
+
14
+ # In case of updating the VM, we check if the user has passed any value for os_disk_name
15
+ # If the user has not passed any value, we try to retrieve the value of os_disk_name from the VM
16
+ # If the VM exists then the os_disk_name is retrieved; else it remains nil
17
+ os_disk_parameters = get_os_disk_parameters(rg_name, vm_name) if vm_config[:os_disk_name].nil? || vm_config[:os_disk_vhd_uri].nil?
18
+ vm_config[:os_disk_name] = os_disk_parameters[:os_disk_name] if vm_config[:os_disk_name].nil?
19
+ vm_config[:os_disk_vhd_uri] = os_disk_parameters[:os_disk_vhd_uri] if vm_config[:os_disk_vhd_uri].nil?
20
+
21
+ msg = "Creating Virtual Machine '#{vm_name}' in Resource Group '#{rg_name}'..."
11
22
  Fog::Logger.debug msg
12
- virtual_machine = Azure::ARM::Compute::Models::VirtualMachine.new
13
23
 
14
- unless vm_hash[:availability_set_id].nil?
15
- sub_resource = MsRestAzure::SubResource.new
16
- sub_resource.id = vm_hash[:availability_set_id]
17
- virtual_machine.availability_set = sub_resource
18
- end
24
+ vm = Azure::ARM::Compute::Models::VirtualMachine.new
25
+ vm.location = vm_config[:location]
26
+ vm.tags = vm_config[:tags]
27
+ vm.availability_set = get_vm_availability_set(vm_config[:availability_set_id])
28
+ vm.hardware_profile = get_hardware_profile(vm_config[:vm_size])
29
+ vm.os_profile = get_os_profile(vm_config)
30
+ vm.network_profile = get_network_profile(vm_config[:network_interface_card_ids])
31
+ vm.storage_profile = get_storage_profile(vm_config)
19
32
 
20
- string_data = vm_hash[:custom_data]
21
- string_data = WHITE_SPACE if string_data.nil?
22
- encoded_data = Base64.strict_encode64(string_data)
23
- virtual_machine.hardware_profile = define_hardware_profile(vm_hash[:vm_size])
24
- virtual_machine.storage_profile = define_storage_profile(vm_hash[:name],
25
- vm_hash[:storage_account_name],
26
- vm_hash[:publisher],
27
- vm_hash[:offer],
28
- vm_hash[:sku],
29
- vm_hash[:version],
30
- vm_hash[:vhd_path],
31
- vm_hash[:os_disk_caching],
32
- vm_hash[:platform],
33
- vm_hash[:resource_group],
34
- vm_hash[:managed_disk_storage_type])
35
-
36
- virtual_machine.os_profile = if vm_hash[:platform].casecmp(WINDOWS).zero?
37
- define_windows_os_profile(vm_hash[:name],
38
- vm_hash[:username],
39
- vm_hash[:password],
40
- vm_hash[:provision_vm_agent],
41
- vm_hash[:enable_automatic_updates],
42
- encoded_data)
43
- else
44
- define_linux_os_profile(vm_hash[:name],
45
- vm_hash[:username],
46
- vm_hash[:password],
47
- vm_hash[:disable_password_authentication],
48
- vm_hash[:ssh_key_path],
49
- vm_hash[:ssh_key_data],
50
- encoded_data)
51
- end
52
- virtual_machine.network_profile = define_network_profile(vm_hash[:network_interface_card_ids])
53
- virtual_machine.location = vm_hash[:location]
54
33
  begin
55
34
  response = if async
56
- @compute_mgmt_client.virtual_machines.create_or_update_async(vm_hash[:resource_group], vm_hash[:name], virtual_machine)
35
+ @compute_mgmt_client.virtual_machines.create_or_update_async(rg_name, vm_name, vm)
57
36
  else
58
- @compute_mgmt_client.virtual_machines.create_or_update(vm_hash[:resource_group], vm_hash[:name], virtual_machine)
37
+ @compute_mgmt_client.virtual_machines.create_or_update(rg_name, vm_name, vm)
59
38
  end
60
39
  rescue MsRestAzure::AzureOperationError => e
61
40
  raise_azure_exception(e, msg)
62
41
  end
63
- Fog::Logger.debug "Virtual Machine #{vm_hash[:name]} Created Successfully." unless async
42
+
43
+ unless async
44
+ unless vm_config[:vhd_path].nil? || vm_config[:managed_disk_storage_type].nil?
45
+ delete_image(rg_name, vm_name)
46
+ delete_storage_account_or_container(rg_name, vm_config[:storage_account_name], vm_name)
47
+ end
48
+ end
49
+
50
+ Fog::Logger.debug "Virtual Machine #{vm_name} created successfully!" unless async
64
51
  response
65
52
  end
66
53
 
67
54
  private
68
55
 
69
- def define_hardware_profile(vm_size)
56
+ def get_vm_availability_set(availability_set_id)
57
+ sub_resource = nil
58
+ unless availability_set_id.nil?
59
+ sub_resource = MsRestAzure::SubResource.new
60
+ sub_resource.id = availability_set_id
61
+ end
62
+ sub_resource
63
+ end
64
+
65
+ def get_hardware_profile(vm_size)
70
66
  hw_profile = Azure::ARM::Compute::Models::HardwareProfile.new
71
67
  hw_profile.vm_size = vm_size
72
68
  hw_profile
73
69
  end
74
70
 
75
- def image_reference(publisher, offer, sku, version)
76
- image_reference = Azure::ARM::Compute::Models::ImageReference.new
77
- image_reference.publisher = publisher
78
- image_reference.offer = offer
79
- image_reference.sku = sku
80
- image_reference.version = version
81
- image_reference
82
- end
71
+ def get_os_profile(vm_config)
72
+ # Arguments unpacking
73
+ platform = vm_config[:platform]
74
+ vm_name = vm_config[:name]
75
+ username = vm_config[:username]
76
+ password = vm_config[:password]
77
+ custom_data = vm_config[:custom_data] unless vm_config[:custom_data].nil?
78
+ provision_vm_agent = vm_config[:provision_vm_agent]
79
+ enable_automatic_updates = vm_config[:enable_automatic_updates]
80
+ disable_password_auth = vm_config[:disable_password_authentication]
81
+ ssh_key_path = vm_config[:ssh_key_path]
82
+ ssh_key_data = vm_config[:ssh_key_data]
83
83
 
84
- def define_storage_profile(vm_name, storage_account_name, publisher, offer, sku, version, vhd_path, os_disk_caching, platform, resource_group, managed_disk_storage_type)
85
- storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
86
- storage_profile.image_reference = image_reference(publisher, offer, sku, version)
87
- os_disk = Azure::ARM::Compute::Models::OSDisk.new
88
-
89
- if managed_disk_storage_type.nil?
90
- vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
91
- vhd.uri = get_blob_endpoint(storage_account_name) + "/vhds/#{vm_name}_os_disk.vhd"
92
-
93
- unless vhd_path.nil?
94
- # Copy if VHD does not exist belongs to same storage account.
95
- vhd_storage_account = (vhd_path.split('/')[2]).split('.')[0]
96
- if storage_account_name != vhd_storage_account
97
- storage_account = @storage_service.storage_accounts.get(resource_group, storage_account_name)
98
- access_key = storage_account.get_access_keys.first.value
99
- storage_data = Fog::Storage::AzureRM.new(provider: 'AzureRM', azure_storage_account_name: storage_account_name, azure_storage_access_key: access_key)
100
- new_time = current_time
101
- container_name = "customvhd#{new_time}"
102
- blob_name = "vhd_image#{new_time}.vhd"
103
- storage_data.directories.create(
104
- key: container_name
105
- )
106
-
107
- storage_data.copy_blob_from_uri(container_name, blob_name, vhd_path)
108
- until storage_data.get_blob_properties(container_name, blob_name).properties[:copy_status] == 'success'
109
- Fog::Logger.debug 'Waiting disk to ready'
110
- sleep(10)
111
- end
112
- new_vhd_path = get_blob_endpoint(storage_account_name) + "/#{container_name}/#{blob_name}"
113
- Fog::Logger.debug "Path:#{new_vhd_path}. | Copy done"
114
- else
115
- new_vhd_path = vhd_path
116
- end
117
-
118
- img_vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
119
- img_vhd.uri = new_vhd_path
120
- os_disk.image = img_vhd
121
- end
84
+ # Build and return os profile object
85
+ os_profile = Azure::ARM::Compute::Models::OSProfile.new
86
+ os_profile.computer_name = vm_name
87
+ os_profile.admin_username = username
88
+ os_profile.admin_password = password
89
+ os_profile.custom_data = Base64.strict_encode64(custom_data) unless vm_config[:custom_data].nil?
122
90
 
123
- os_disk.vhd = vhd
91
+ if platform.casecmp(WINDOWS).zero?
92
+ os_profile.windows_configuration = get_windows_config(provision_vm_agent, enable_automatic_updates)
124
93
  else
125
- managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new
126
- managed_disk.storage_account_type = managed_disk_storage_type
127
- os_disk.managed_disk = managed_disk
94
+ os_profile.linux_configuration = get_linux_config(disable_password_auth, ssh_key_path, ssh_key_data)
128
95
  end
129
-
130
- os_disk.name = "#{vm_name}_os_disk"
131
- os_disk.os_type = platform
132
-
133
- os_disk.create_option = Azure::ARM::Compute::Models::DiskCreateOptionTypes::FromImage
134
- os_disk.caching = unless os_disk_caching.nil?
135
- case os_disk_caching
136
- when 'None'
137
- Azure::ARM::Compute::Models::CachingTypes::None
138
- when 'ReadOnly'
139
- Azure::ARM::Compute::Models::CachingTypes::ReadOnly
140
- when 'ReadWrite'
141
- Azure::ARM::Compute::Models::CachingTypes::ReadWrite
142
- end
143
- end
144
-
145
- storage_profile.os_disk = os_disk
146
- storage_profile
96
+
97
+ os_profile
147
98
  end
148
99
 
149
- def define_windows_os_profile(vm_name, username, password, provision_vm_agent, enable_automatic_updates, encoded_data)
150
- os_profile = Azure::ARM::Compute::Models::OSProfile.new
100
+ def get_windows_config(provision_vm_agent, enable_automatic_updates)
151
101
  windows_config = Azure::ARM::Compute::Models::WindowsConfiguration.new
152
102
  windows_config.provision_vmagent = provision_vm_agent
153
103
  windows_config.enable_automatic_updates = enable_automatic_updates
154
-
155
- os_profile.windows_configuration = windows_config
156
- os_profile.computer_name = vm_name
157
- os_profile.admin_username = username
158
- os_profile.admin_password = password
159
- os_profile.custom_data = encoded_data
160
- os_profile
104
+ windows_config
161
105
  end
162
106
 
163
- def define_linux_os_profile(vm_name, username, password, disable_password_authentication, ssh_key_path, ssh_key_data, encoded_data)
164
- os_profile = Azure::ARM::Compute::Models::OSProfile.new
107
+ def get_linux_config(disable_password_auth, ssh_key_path, ssh_key_data)
165
108
  linux_config = Azure::ARM::Compute::Models::LinuxConfiguration.new
109
+ linux_config.disable_password_authentication = disable_password_auth
166
110
 
167
111
  unless ssh_key_path.nil? || ssh_key_data.nil?
168
112
  ssh_config = Azure::ARM::Compute::Models::SshConfiguration.new
@@ -173,21 +117,15 @@ module Fog
173
117
  linux_config.ssh = ssh_config
174
118
  end
175
119
 
176
- linux_config.disable_password_authentication = disable_password_authentication
177
- os_profile.linux_configuration = linux_config
178
- os_profile.computer_name = vm_name
179
- os_profile.admin_username = username
180
- os_profile.admin_password = password
181
- os_profile.custom_data = encoded_data
182
- os_profile
120
+ linux_config
183
121
  end
184
122
 
185
- def define_network_profile(network_interface_card_ids)
123
+ def get_network_profile(network_interface_card_ids)
186
124
  network_interface_cards = []
187
125
  network_interface_card_ids.each_with_index do |id, index|
188
- nic = Azure::ARM::Compute::Models::NetworkInterfaceReference .new
126
+ nic = Azure::ARM::Compute::Models::NetworkInterfaceReference.new
189
127
  nic.id = id
190
- nic.primary = index == PRIMARY_NIC_INDEX ? true : false
128
+ nic.primary = index == PRIMARY_NIC_INDEX
191
129
  network_interface_cards << nic
192
130
  end
193
131
 
@@ -195,7 +133,235 @@ module Fog
195
133
  network_profile.network_interfaces = network_interface_cards
196
134
  network_profile
197
135
  end
136
+
137
+ def get_storage_profile(vm_config)
138
+ # Arguments unpacking
139
+ managed_disk_storage_type = vm_config[:managed_disk_storage_type]
140
+
141
+ storage_profile = if managed_disk_storage_type.nil?
142
+ get_unmanaged_vm_storage_profile(vm_config)
143
+ else
144
+ get_managed_vm_storage_profile(vm_config)
145
+ end
146
+ storage_profile
147
+ end
148
+
149
+ def get_unmanaged_vm_storage_profile(vm_config)
150
+ # Arguments unpacking
151
+ vm_name = vm_config[:name]
152
+ storage_account_name = vm_config[:storage_account_name]
153
+ publisher = vm_config[:publisher]
154
+ offer = vm_config[:offer]
155
+ sku = vm_config[:sku]
156
+ version = vm_config[:version]
157
+ vhd_path = vm_config[:vhd_path]
158
+ os_disk_caching = vm_config[:os_disk_caching]
159
+ platform = vm_config[:platform]
160
+ resource_group = vm_config[:resource_group]
161
+ os_disk_size = vm_config[:os_disk_size]
162
+ location = vm_config[:location]
163
+ image_ref = vm_config[:image_ref]
164
+ os_disk_name = vm_config[:os_disk_name]
165
+ os_disk_vhd_uri = vm_config[:os_disk_vhd_uri]
166
+
167
+ storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
168
+ # Set OS disk VHD path
169
+ os_disk = Azure::ARM::Compute::Models::OSDisk.new
170
+ vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
171
+ vhd.uri = os_disk_vhd_uri.nil? ? get_blob_endpoint(storage_account_name) + "/vhds/#{vm_name}_os_disk.vhd" : os_disk_vhd_uri
172
+ os_disk.vhd = vhd
173
+
174
+ if vhd_path.nil? && image_ref.nil?
175
+ # Marketplace
176
+ storage_profile.image_reference = image_reference(publisher, offer, sku, version)
177
+ elsif !vhd_path.nil? && image_ref.nil?
178
+ # VHD
179
+ new_vhd_path = copy_vhd_to_storage_account(resource_group, storage_account_name, vhd_path, location, vm_name)
180
+ img_vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
181
+ img_vhd.uri = new_vhd_path
182
+ os_disk.image = img_vhd
183
+ else
184
+ # Image
185
+ image_resource_group = get_resource_group_from_id(image_ref)
186
+ image_name = get_image_name(image_ref)
187
+ image = get_image(image_resource_group, image_name)
188
+ storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
189
+ storage_profile.image_reference.id = image.id
190
+ end
191
+
192
+ storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_name, os_disk_caching, os_disk_size, platform, vm_name)
193
+ storage_profile
194
+ end
195
+
196
+ def get_managed_vm_storage_profile(vm_config)
197
+ # Argument unpacking
198
+ managed_disk_storage_type = vm_config[:managed_disk_storage_type]
199
+ vhd_path = vm_config[:vhd_path]
200
+ resource_group = vm_config[:resource_group]
201
+ storage_account_name = vm_config[:storage_account_name]
202
+ location = vm_config[:location]
203
+ publisher = vm_config[:publisher]
204
+ offer = vm_config[:offer]
205
+ sku = vm_config[:sku]
206
+ version = vm_config[:version]
207
+ os_disk_caching = vm_config[:os_disk_caching]
208
+ os_disk_size = vm_config[:os_disk_size]
209
+ platform = vm_config[:platform]
210
+ vm_name = vm_config[:name]
211
+ image_ref = vm_config[:image_ref]
212
+ os_disk_name = vm_config[:os_disk_name]
213
+
214
+ # Build storage profile
215
+ storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
216
+ os_disk = Azure::ARM::Compute::Models::OSDisk.new
217
+ managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new
218
+ managed_disk.storage_account_type = managed_disk_storage_type
219
+ os_disk.managed_disk = managed_disk
220
+
221
+ if vhd_path.nil? && image_ref.nil?
222
+ # Marketplace
223
+ storage_profile.image_reference = image_reference(publisher, offer, sku, version)
224
+ elsif !vhd_path.nil? && image_ref.nil?
225
+ # VHD
226
+ new_vhd_path = copy_vhd_to_storage_account(resource_group, storage_account_name, vhd_path, location, vm_name)
227
+ image = create_image(image_config_params(location, new_vhd_path, platform, resource_group, vm_name))
228
+ storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
229
+ storage_profile.image_reference.id = image.id
230
+ else
231
+ # Image
232
+ image_resource_group = get_resource_group_from_id(image_ref)
233
+ image_name = get_image_name(image_ref)
234
+ image = get_image(image_resource_group, image_name)
235
+ storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
236
+ storage_profile.image_reference.id = image.id
237
+ end
238
+
239
+ storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_name, os_disk_caching, os_disk_size, platform, vm_name)
240
+ storage_profile
241
+ end
242
+
243
+ def image_reference(publisher, offer, sku, version)
244
+ image_reference = Azure::ARM::Compute::Models::ImageReference.new
245
+ image_reference.publisher = publisher
246
+ image_reference.offer = offer
247
+ image_reference.sku = sku
248
+ image_reference.version = version
249
+ image_reference
250
+ end
251
+
252
+ def image_config_params(location, new_vhd_path, platform, resource_group, vm_name)
253
+ {
254
+ location: location,
255
+ new_vhd_path: new_vhd_path,
256
+ platform: platform,
257
+ resource_group: resource_group,
258
+ vm_name: vm_name
259
+ }
260
+ end
261
+
262
+ def configure_os_disk_object(os_disk, os_disk_name, os_disk_caching, os_disk_size, platform, vm_name)
263
+ # It will use the os_disk_name provided or it will generate a name for itself if it is nil
264
+ os_disk.name = os_disk_name.nil? ? "#{vm_name}_os_disk" : os_disk_name
265
+ os_disk.os_type = platform
266
+ os_disk.disk_size_gb = os_disk_size unless os_disk_size.nil?
267
+ os_disk.create_option = Azure::ARM::Compute::Models::DiskCreateOptionTypes::FromImage
268
+ os_disk.caching = unless os_disk_caching.nil?
269
+ case os_disk_caching
270
+ when 'None'
271
+ Azure::ARM::Compute::Models::CachingTypes::None
272
+ when 'ReadOnly'
273
+ Azure::ARM::Compute::Models::CachingTypes::ReadOnly
274
+ when 'ReadWrite'
275
+ Azure::ARM::Compute::Models::CachingTypes::ReadWrite
276
+ end
277
+ end
278
+ os_disk
279
+ end
280
+
281
+ def copy_vhd_to_storage_account(resource_group, storage_account_name, vhd_path, location, vm_name)
282
+ # Copy if VHD does not exist belongs to same storage account.
283
+ vhd_storage_account_name = (vhd_path.split('/')[2]).split('.')[0]
284
+ if storage_account_name != vhd_storage_account_name
285
+ if storage_account_name.nil?
286
+ new_time = current_time
287
+ storage_account_name = "sa#{new_time}"
288
+ storage_account_config = storage_account_config_params(location, resource_group, storage_account_name)
289
+ storage_account = @storage_service.storage_accounts.create(storage_account_config)
290
+ else
291
+ storage_account = @storage_service.storage_accounts.get(resource_group, storage_account_name)
292
+ end
293
+
294
+ access_key = storage_account.get_access_keys.first.value
295
+ storage_data = Fog::Storage::AzureRM.new(azure_storage_account_name: storage_account_name, azure_storage_access_key: access_key)
296
+ new_time = current_time
297
+ container_name = "customvhd-#{vm_name.downcase}-os-image"
298
+ blob_name = "vhd_image#{new_time}.vhd"
299
+ storage_data.directories.create(key: container_name)
300
+ storage_data.copy_blob_from_uri(container_name, blob_name, vhd_path)
301
+
302
+ until storage_data.get_blob_properties(container_name, blob_name).properties[:copy_status] == 'success'
303
+ Fog::Logger.debug 'Waiting disk to ready'
304
+ sleep(10)
305
+ end
306
+
307
+ new_vhd_path = get_blob_endpoint(storage_account_name) + "/#{container_name}/#{blob_name}"
308
+ Fog::Logger.debug "Path:#{new_vhd_path}. | Copy done"
309
+ else
310
+ new_vhd_path = vhd_path
311
+ end
312
+
313
+ new_vhd_path
314
+ end
315
+
316
+ def storage_account_config_params(location, resource_group, storage_account_name)
317
+ {
318
+ name: storage_account_name,
319
+ location: location,
320
+ resource_group: resource_group,
321
+ account_type: 'Standard',
322
+ replication: 'LRS',
323
+ tags:
324
+ {
325
+ TEMPORARY_STORAGE_ACCOUNT_TAG_KEY => TEMPORARY_STORAGE_ACCOUNT_TAG_VALUE
326
+ }
327
+ }
328
+ end
329
+
330
+ def delete_storage_account_or_container(resource_group, storage_account_name, vm_name)
331
+ delete_storage_account(resource_group) if storage_account_name.nil?
332
+ delete_storage_container(resource_group, storage_account_name, vm_name) unless storage_account_name.nil?
333
+ end
334
+
335
+ def delete_storage_container(resource_group, storage_account_name, vm_name)
336
+ access_key = @storage_service.get_storage_access_keys(resource_group, storage_account_name).first.value
337
+ container_name = "customvhd-#{vm_name.downcase}-os-image"
338
+ @storage_service.directories.delete_temporary_container(storage_account_name, access_key, container_name)
339
+ end
340
+
341
+ def delete_storage_account(resource_group)
342
+ @storage_service.storage_accounts.delete_storage_account_from_tag(resource_group,
343
+ TEMPORARY_STORAGE_ACCOUNT_TAG_KEY,
344
+ TEMPORARY_STORAGE_ACCOUNT_TAG_VALUE)
345
+ end
346
+
347
+ def get_os_disk_parameters(resource_group, virtual_machine_name)
348
+ os_disk_parameters = {}
349
+
350
+ begin
351
+ vm = get_virtual_machine(resource_group, virtual_machine_name, false)
352
+ rescue
353
+ return os_disk_parameters
354
+ end
355
+
356
+ unless vm.storage_profile.nil?
357
+ os_disk_parameters[:os_disk_name] = vm.storage_profile.os_disk.name
358
+ os_disk_parameters[:os_disk_vhd_uri] = vm.storage_profile.os_disk.vhd.uri unless vm.storage_profile.os_disk.vhd.nil?
359
+ end
360
+
361
+ os_disk_parameters
362
+ end
198
363
  end
364
+
199
365
  # This class provides the mock implementation for unit tests.
200
366
  class Mock
201
367
  def create_virtual_machine(*)
@@ -255,6 +421,7 @@ module Fog
255
421
  'provisioningState' => 'Succeeded'
256
422
  }
257
423
  }
424
+
258
425
  vm_mapper = Azure::ARM::Compute::Models::VirtualMachine.mapper
259
426
  @compute_mgmt_client.deserialize(vm_mapper, vm, 'result.body')
260
427
  end