Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- require 'azure_mgmt_compute'
- require 'azure_mgmt_network'
- include Azure::ARM::Compute
- include Azure::ARM::Compute::Models
- include Azure::ARM::Network
- include Azure::ARM::Network::Models
- def self.subscription_id
- ENV['AZURE_SUBSCRIPTION_ID']
- end
- def self.vm_name
- 'testvm1'
- end
- def self.resource_group
- 'rjanjyatagstest'
- end
- def self.create_os_profile
- os_profile = OSProfile.new
- os_profile.admin_username = 'testvm1'
- os_profile.admin_password = 'P@ssword1'
- os_profile.computer_name = vm_name
- linux_configuration = LinuxConfiguration.new
- linux_configuration.disable_password_authentication = false
- os_profile.linux_configuration = linux_configuration
- os_profile
- end
- def self.create_hardware_profile
- hardware_profile = HardwareProfile.new
- hardware_profile.vm_size = 'Standard_A0'
- hardware_profile
- end
- def self.create_network_profile
- network_interface = NetworkInterface.new
- network_interface.id = "/subscriptions/#{subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/networkInterfaces/MyNic"
- profile = NetworkProfile.new
- profile.network_interfaces = [network_interface]
- profile
- end
- def self.get_image_reference
- ref = ImageReference.new
- ref.publisher = 'OpenLogic'
- ref.offer = 'CentOS'
- ref.sku = '7.2'
- ref.version = 'latest'
- ref
- end
- def self.create_storage_profile
- storage_profile = StorageProfile.new
- storage_profile.image_reference = get_image_reference
- os_disk = OSDisk.new
- os_disk.caching = 'None'
- os_disk.create_option = 'fromImage'
- os_disk.name = 'Test'
- md_params = ManagedDiskParameters.new
- md_params.storage_account_type = 'Standard_LRS'
- os_disk.managed_disk = md_params
- storage_profile.os_disk = os_disk
- storage_profile
- end
- def self.build_virtual_machine_parameters
- params = VirtualMachine.new
- params.type = 'Microsoft.Compute/virtualMachines'
- params.network_profile = create_network_profile
- params.storage_profile = create_storage_profile
- params.hardware_profile = create_hardware_profile
- params.os_profile = create_os_profile
- params.location = "southcentralus"
- params.tags = {"env" => "using_azure_ruby_sdk", "test_case" => "propagate tags from vm to osDisk"}
- params
- end
- provider = MsRestAzure::ApplicationTokenProvider.new(
- ENV['AZURE_TENANT_ID'],
- ENV['AZURE_CLIENT_ID'],
- ENV['AZURE_CLIENT_SECRET']
- )
- credentials = MsRest::TokenCredentials.new(provider)
- compute_client = Azure::ARM::Compute::ComputeManagementClient.new(credentials)
- compute_client.subscription_id = subscription_id
- params = self.build_virtual_machine_parameters
- result = compute_client.virtual_machines.create_or_update_async("#{resource_group}", vm_name, params).value!
- vm = result.body
- p vm.name
- p vm.vm_id
- ```
Add Comment
Please, Sign In to add comment