Guest User

Untitled

a guest
Oct 29th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. ```
  2. require 'azure_mgmt_compute'
  3. require 'azure_mgmt_network'
  4.  
  5. include Azure::ARM::Compute
  6. include Azure::ARM::Compute::Models
  7.  
  8. include Azure::ARM::Network
  9. include Azure::ARM::Network::Models
  10.  
  11. def self.subscription_id
  12. ENV['AZURE_SUBSCRIPTION_ID']
  13. end
  14.  
  15. def self.vm_name
  16. 'testvm1'
  17. end
  18.  
  19. def self.resource_group
  20. 'rjanjyatagstest'
  21. end
  22.  
  23. def self.create_os_profile
  24. os_profile = OSProfile.new
  25. os_profile.admin_username = 'testvm1'
  26. os_profile.admin_password = 'P@ssword1'
  27. os_profile.computer_name = vm_name
  28.  
  29. linux_configuration = LinuxConfiguration.new
  30. linux_configuration.disable_password_authentication = false
  31. os_profile.linux_configuration = linux_configuration
  32.  
  33. os_profile
  34. end
  35.  
  36. def self.create_hardware_profile
  37. hardware_profile = HardwareProfile.new
  38. hardware_profile.vm_size = 'Standard_A0'
  39.  
  40. hardware_profile
  41. end
  42.  
  43. def self.create_network_profile
  44. network_interface = NetworkInterface.new
  45. network_interface.id = "/subscriptions/#{subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/networkInterfaces/MyNic"
  46.  
  47. profile = NetworkProfile.new
  48. profile.network_interfaces = [network_interface]
  49.  
  50. profile
  51. end
  52.  
  53. def self.get_image_reference
  54. ref = ImageReference.new
  55. ref.publisher = 'OpenLogic'
  56. ref.offer = 'CentOS'
  57. ref.sku = '7.2'
  58. ref.version = 'latest'
  59. ref
  60. end
  61.  
  62. def self.create_storage_profile
  63. storage_profile = StorageProfile.new
  64. storage_profile.image_reference = get_image_reference
  65.  
  66. os_disk = OSDisk.new
  67. os_disk.caching = 'None'
  68. os_disk.create_option = 'fromImage'
  69. os_disk.name = 'Test'
  70.  
  71. md_params = ManagedDiskParameters.new
  72. md_params.storage_account_type = 'Standard_LRS'
  73. os_disk.managed_disk = md_params
  74.  
  75. storage_profile.os_disk = os_disk
  76. storage_profile
  77. end
  78.  
  79. def self.build_virtual_machine_parameters
  80. params = VirtualMachine.new
  81. params.type = 'Microsoft.Compute/virtualMachines'
  82. params.network_profile = create_network_profile
  83. params.storage_profile = create_storage_profile
  84. params.hardware_profile = create_hardware_profile
  85. params.os_profile = create_os_profile
  86.  
  87. params.location = "southcentralus"
  88. params.tags = {"env" => "using_azure_ruby_sdk", "test_case" => "propagate tags from vm to osDisk"}
  89. params
  90. end
  91.  
  92. provider = MsRestAzure::ApplicationTokenProvider.new(
  93. ENV['AZURE_TENANT_ID'],
  94. ENV['AZURE_CLIENT_ID'],
  95. ENV['AZURE_CLIENT_SECRET']
  96. )
  97.  
  98. credentials = MsRest::TokenCredentials.new(provider)
  99. compute_client = Azure::ARM::Compute::ComputeManagementClient.new(credentials)
  100. compute_client.subscription_id = subscription_id
  101.  
  102. params = self.build_virtual_machine_parameters
  103. result = compute_client.virtual_machines.create_or_update_async("#{resource_group}", vm_name, params).value!
  104.  
  105. vm = result.body
  106. p vm.name
  107. p vm.vm_id
  108. ```
Add Comment
Please, Sign In to add comment