lib/fog/azurerm/requests/storage/create_disk.rb in fog-azure-rm-0.1.0 vs lib/fog/azurerm/requests/storage/create_disk.rb in fog-azure-rm-0.1.1

- old
+ new

@@ -1,35 +1,61 @@ module Fog module Storage class AzureRM - # This class provides the actual implemention for service calls. + # This class provides the actual implementation for service calls. class Real - def create_disk(blob_name, options = {}) - msg = "Creating disk(blob) #{blob_name}" + # Create a disk in Azure storage. + # + # @param disk_name [String] Name of disk + # @param disk_size_in_gb [Integer] Disk size in GiB. Value range: [1, 1023] + # @param options [Hash] + # @option options [String] container_name Sets name of the container which contains the disk. Default is 'vhds'. + # + # @return [Boolean] + # + def create_disk(disk_name, disk_size_in_gb, options = {}) + msg = "Creating disk(#{disk_name}, #{disk_size_in_gb}). options: #{options}" Fog::Logger.debug msg - ::File.open('out.txt', 'w') { |f| f.write('Sample File') } - path = ::File.expand_path(::File.dirname('out.txt')) + '/' + 'out.txt' + + raise ArgumentError, "disk_size_in_gb #{disk_size_in_gb} must be an integer" unless disk_size_in_gb.is_a?(Integer) + raise ArgumentError, "Azure minimum disk size is 1 GiB: #{disk_size_in_gb}" if disk_size_in_gb < 1 + raise ArgumentError, "Azure maximum disk size is 1023 GiB: #{disk_size_in_gb}" if disk_size_in_gb > 1023 + + container_name = options.delete(:container_name) + container_name = 'vhds' if container_name.nil? + blob_name = "#{disk_name}.vhd" + vhd_size = disk_size_in_gb * 1024 * 1024 * 1024 + blob_size = vhd_size + 512 + + opts = { + type: :fixed, + name: '/tmp/footer.vhd', # Only used to initialize vhd, no local file is generated. + size: disk_size_in_gb + } + vhd_footer = Vhd::Library.new(opts).footer.values.join + begin - disk = upload_block_blob_from_file('vhds', "#{blob_name}.vhd", path.to_s, options) - rescue Azure::Core::Http::HTTPError => e - raise_azure_exception(e, msg) + create_page_blob(container_name, blob_name, blob_size, options) + put_blob_pages(container_name, blob_name, vhd_size, blob_size - 1, vhd_footer, options) + rescue + begin + delete_blob(container_name, blob_name) + rescue => ex + Fog::Logger.debug "Cannot delete the blob: #{container_name}/#{blob_name} after create_disk failed. #{ex.inspect}" + end + raise end - disk + + Fog::Logger.debug "Created a disk #{disk_name} successfully." + true end end + # This class provides the mock implementation for unit tests. class Mock def create_disk(*) - Fog::Logger.debug 'Disk(Blob) created successfully.' - { - 'name' => 'test_blob', - 'properties' => - { - 'last_modified' => 'Thu, 28 Jul 2016 06:53:05 GMT', - 'etag' => '0x8D3B6B3D353FFCA', - 'content_md5' => 'tXAohIyxuu/t94Lp/ujeRw==' - } - } + Fog::Logger.debug 'Disk created successfully.' + true end end end end end