API add_data_disk lun is optional argument.
This commit is contained in:
Родитель
276a267cb4
Коммит
372aa1d186
|
@ -373,13 +373,14 @@ virtual_machine_service.start_virtual_machine('vm_name', 'cloud_service_name')
|
|||
virtual_machine_service.restart_virtual_machine('vm_name', 'cloud_service_name')
|
||||
|
||||
#API for add disk to Virtual Machine
|
||||
lun = 1 #Valid LUN values are 0 through 15.
|
||||
options = {
|
||||
:disk_label => 'disk-label',
|
||||
:disk_size => 100, #In GB
|
||||
:import => false
|
||||
:import => false,
|
||||
:lun => 10, #Valid LUN values are 0 through 15.
|
||||
:disk_name => 'Disk name' #Required when import is true
|
||||
}
|
||||
virtual_machine_service.add_data_disk('vm_name', 'cloud_service_name', lun, options)
|
||||
virtual_machine_service.add_data_disk('vm_name', 'cloud_service_name', options)
|
||||
|
||||
#API to add/update Virtual Machine endpoints
|
||||
endpoint1 = {
|
||||
|
|
|
@ -263,6 +263,7 @@ module Azure
|
|||
if xml_content(role, 'RoleName') == role_name
|
||||
vm.availability_set_name = xml_content(role, 'AvailabilitySetName')
|
||||
endpoints_from_xml(role, vm)
|
||||
vm.data_disks = data_disks_from_xml(role)
|
||||
vm.os_type = xml_content(role, 'OSVirtualHardDisk OS')
|
||||
vm.disk_name = xml_content(role, 'OSVirtualHardDisk DiskName')
|
||||
vm.media_link = xml_content(role, 'OSVirtualHardDisk MediaLink')
|
||||
|
@ -276,6 +277,20 @@ module Azure
|
|||
end
|
||||
end
|
||||
|
||||
def self.data_disks_from_xml(rolesXML)
|
||||
data_disks = []
|
||||
virtual_hard_disks = rolesXML.css('DataVirtualHardDisks DataVirtualHardDisk')
|
||||
virtual_hard_disks.each do |disk|
|
||||
data_disk = {}
|
||||
data_disk[:name] = xml_content(disk, 'DiskName')
|
||||
data_disk[:lun] = xml_content(disk, 'Lun')
|
||||
data_disk[:size_in_gb] = xml_content(disk, 'LogicalDiskSizeInGB')
|
||||
data_disk[:media_link] = xml_content(disk, 'MediaLink')
|
||||
data_disks << data_disk
|
||||
end
|
||||
data_disks
|
||||
end
|
||||
|
||||
def self.endpoints_from_xml(rolesXML, vm)
|
||||
vm.tcp_endpoints = []
|
||||
vm.udp_endpoints = []
|
||||
|
@ -370,10 +385,12 @@ module Azure
|
|||
end
|
||||
end
|
||||
|
||||
def self.add_data_disk_to_xml(lun, media_link, options)
|
||||
def self.add_data_disk_to_xml(vm, options)
|
||||
if options[:import] && options[:disk_name].nil?
|
||||
Loggerx.error_with_exit "The data disk name is not valid."
|
||||
end
|
||||
media_link = vm.media_link
|
||||
lun = options[:lun] || (Array(0..15) - vm.data_disks.map{|x| x[:lun].to_i}).first
|
||||
builder = Nokogiri::XML::Builder.new do |xml|
|
||||
xml.DataVirtualHardDisk(
|
||||
'xmlns' => 'http://schemas.microsoft.com/windowsazure',
|
||||
|
@ -383,7 +400,7 @@ module Azure
|
|||
xml.DiskLabel options[:disk_label]
|
||||
xml.DiskName options[:disk_name] if options[:import]
|
||||
xml.Lun lun
|
||||
xml.LogicalDiskSizeInGB options[:disk_size] || 1
|
||||
xml.LogicalDiskSizeInGB options[:disk_size] || 100
|
||||
unless options[:import]
|
||||
disk_name = media_link[/([^\/]+)$/]
|
||||
media_link = media_link.gsub(/#{disk_name}/, (Time.now.strftime('disk_%Y_%m_%d_%H_%M')) + '.vhd')
|
||||
|
@ -399,7 +416,7 @@ module Azure
|
|||
def self.port_already_opened?(existing_ports, port)
|
||||
return false if existing_ports.nil?
|
||||
raise "Port #{port} conflicts with a port already opened. "\
|
||||
"Please select a different port." if existing_ports.include?(port)
|
||||
"Please select a different port." if existing_ports.include?(port)
|
||||
false
|
||||
end
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ module Azure
|
|||
attr_accessor :virtual_network_name
|
||||
attr_accessor :availability_set_name
|
||||
attr_accessor :media_link
|
||||
attr_accessor :data_disks
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -377,8 +377,6 @@ module Azure
|
|||
#
|
||||
# * +cloud_service_name+ - String. Cloud service name.
|
||||
# * +vm_name+ - String. Virtual machine name.
|
||||
# * +lun+ - String. Specifies the Logical Unit Number
|
||||
# (LUN) for the disk. Valid LUN values are 0 through 15.
|
||||
# * +options+ - Hash. Optional parameters.
|
||||
#
|
||||
# ==== Options
|
||||
|
@ -392,16 +390,18 @@ module Azure
|
|||
# The default is ReadOnly. Possible values are: None, ReadOnly, ReadWrite
|
||||
# * +:disk_label+ - String. Specifies the description of the data disk.
|
||||
# * +:disk_size+ - String. Specifies the size of disk in GB
|
||||
# * +lun+ - String. Specifies the Logical Unit Number
|
||||
# (LUN) for the disk. Valid LUN values are 0 through 15.
|
||||
#
|
||||
# See http://msdn.microsoft.com/en-us/library/windowsazure/jj157199.aspx
|
||||
#
|
||||
# Returns None
|
||||
def add_data_disk(vm_name, cloud_service_name, lun, options = {})
|
||||
def add_data_disk(vm_name, cloud_service_name, options = {})
|
||||
options[:import] ||= false
|
||||
vm = get_virtual_machine(vm_name, cloud_service_name)
|
||||
if vm
|
||||
path = "/services/hostedservices/#{cloud_service_name}/deployments/#{vm.deployment_name}/roles/#{vm_name}/DataDisks"
|
||||
body = Serialization.add_data_disk_to_xml(lun, vm.media_link, options)
|
||||
body = Serialization.add_data_disk_to_xml(vm, options)
|
||||
Loggerx.info "Adding data disk to virtual machine #{vm_name} ..."
|
||||
request = ManagementHttpRequest.new(:post, path, body)
|
||||
request.call
|
||||
|
|
|
@ -107,9 +107,8 @@ describe Azure::VirtualMachineManagementService do
|
|||
|
||||
describe '#add_data_disk' do
|
||||
it 'add data disk to virtual machine' do
|
||||
lun = rand(1..15)
|
||||
others = { disk_size: 100 }
|
||||
subject.add_data_disk(vm_name, csn , lun, others)
|
||||
subject.add_data_disk(vm_name, csn, others)
|
||||
dms = VirtualMachineDiskManagementService.new
|
||||
disks = dms.list_virtual_machine_disks
|
||||
disks = disks.select { |x| (/#{csn}/ =~ x.name) && x.os_type.empty? }
|
||||
|
|
|
@ -169,22 +169,28 @@ describe Azure::VirtualMachineManagement::Serialization do
|
|||
describe '#add_data_disk_to_xml' do
|
||||
|
||||
let(:options) do
|
||||
{disk_size: 100}
|
||||
{
|
||||
disk_size: 100,
|
||||
lun: 5
|
||||
}
|
||||
end
|
||||
let(:media_link) { 'https://sta.blob.managment.core.net/vhds/1234.vhd' }
|
||||
let(:lun) { 5 }
|
||||
|
||||
before do
|
||||
Loggerx.expects(:puts).returns(nil).at_least(0)
|
||||
@vm = Azure::VirtualMachineManagement::VirtualMachine.new
|
||||
@vm.data_disks = []
|
||||
@vm.media_link = media_link
|
||||
end
|
||||
|
||||
it 'returns an xml for newly created data disk' do
|
||||
result = subject.add_data_disk_to_xml(lun, media_link, options)
|
||||
it 'returns an xml for newly created data disk' do
|
||||
result = subject.add_data_disk_to_xml(@vm, options)
|
||||
doc = Nokogiri::XML(result)
|
||||
disk_size = doc.css('DataVirtualHardDisk LogicalDiskSizeInGB').text
|
||||
media_link = doc.css('DataVirtualHardDisk MediaLink').text
|
||||
disk_name = doc.css('DataVirtualHardDisk DiskName').text
|
||||
result.must_be_kind_of String
|
||||
doc.css('DataVirtualHardDisk Lun').text.must_equal lun.to_s
|
||||
doc.css('DataVirtualHardDisk Lun').text.must_equal options[:lun].to_s
|
||||
disk_size.must_equal options[:disk_size].to_s
|
||||
media_link.wont_be_empty
|
||||
disk_name.must_be_empty
|
||||
|
@ -192,13 +198,14 @@ describe Azure::VirtualMachineManagement::Serialization do
|
|||
|
||||
it 'returns an xml for existing data disk' do
|
||||
options[:import] = true
|
||||
options.delete(:lun)
|
||||
options[:disk_name] = 'disk_name'
|
||||
result = subject.add_data_disk_to_xml(lun, media_link, options)
|
||||
result = subject.add_data_disk_to_xml(@vm, options)
|
||||
doc = Nokogiri::XML(result)
|
||||
media_link = doc.css('DataVirtualHardDisk MediaLink').text
|
||||
disk_name = doc.css('DataVirtualHardDisk DiskName').text
|
||||
result.must_be_kind_of String
|
||||
doc.css('DataVirtualHardDisk Lun').text.must_equal lun.to_s
|
||||
doc.css('DataVirtualHardDisk Lun').text.must_equal '0'
|
||||
media_link.must_be_empty
|
||||
disk_name.wont_be_empty
|
||||
end
|
||||
|
@ -206,7 +213,7 @@ describe Azure::VirtualMachineManagement::Serialization do
|
|||
it 'raise error when disk name is empty' do
|
||||
options[:import] = true
|
||||
exception = assert_raises(RuntimeError) do
|
||||
subject.add_data_disk_to_xml(lun, media_link, options)
|
||||
subject.add_data_disk_to_xml(@vm, options)
|
||||
end
|
||||
assert_match(/The data disk name is not valid/i, exception.message)
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче