Class: Fog::Google::Compute::Disk
- Inherits:
-
Model
- Object
- Model
- Fog::Google::Compute::Disk
- Defined in:
- lib/fog/google/compute/models/disk.rb
Constant Summary collapse
- RUNNING_STATE =
"READY".freeze
Instance Method Summary collapse
-
#attached_disk_obj(opts = {}) ⇒ Hash
Returns an attached disk configuration hash.
- #create_snapshot(snapshot_name, snapshot = {}) ⇒ Object
- #default_description ⇒ Object
- #destroy(async = true) ⇒ Object
-
#get_as_boot_disk(writable = true, auto_delete = false) ⇒ Hash
A legacy shorthand for attached_disk_obj.
- #ready? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #zone_name ⇒ Object
Instance Method Details
#attached_disk_obj(opts = {}) ⇒ Hash
Returns an attached disk configuration hash.
Compute API needs attached disks to be specified in a custom format. This provides a handy shortcut for generating a preformatted config.
Example output:
:boot=>true,
:mode=>"READ_WRITE",
:source=>"https://www.googleapis.com/compute/v1/projects/myproj/zones/us-central1-f/disks/mydisk",
:type=>"PERSISTENT"
See Instances.insert API docs for more info: cloud.google.com/compute/docs/reference/rest/v1/instances/insert
115 116 117 118 |
# File 'lib/fog/google/compute/models/disk.rb', line 115 def attached_disk_obj(opts = {}) requires :self_link collection.attached_disk_obj(self_link, **opts) end |
#create_snapshot(snapshot_name, snapshot = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/fog/google/compute/models/disk.rb', line 151 def create_snapshot(snapshot_name, snapshot = {}) requires :name, :zone raise ArgumentError, "Invalid snapshot name" unless snapshot_name data = service.create_disk_snapshot(snapshot_name, name, zone_name, snapshot) operation = Fog::Google::Compute::Operations.new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } service.snapshots.get(snapshot_name) end |
#default_description ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/fog/google/compute/models/disk.rb', line 24 def default_description if !source_image.nil? "created from image: #{source_image}" elsif !source_snapshot.nil? "created from snapshot: #{source_snapshot}" else "created with fog" end end |
#destroy(async = true) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/fog/google/compute/models/disk.rb', line 72 def destroy(async = true) requires :name, :zone data = service.delete_disk(name, zone_name) operation = Fog::Google::Compute::Operations.new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async operation end |
#get_as_boot_disk(writable = true, auto_delete = false) ⇒ Hash
A legacy shorthand for attached_disk_obj
127 128 129 130 131 |
# File 'lib/fog/google/compute/models/disk.rb', line 127 def get_as_boot_disk(writable = true, auto_delete = false) attached_disk_obj(boot: true, writable: writable, auto_delete: auto_delete) end |
#ready? ⇒ Boolean
133 134 135 |
# File 'lib/fog/google/compute/models/disk.rb', line 133 def ready? status == RUNNING_STATE end |
#reload ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/fog/google/compute/models/disk.rb', line 137 def reload requires :identity, :zone return unless data = begin collection.get(identity, zone_name) rescue Google::Apis::TransmissionError nil end new_attributes = data.attributes merge_attributes(new_attributes) self end |
#save ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fog/google/compute/models/disk.rb', line 34 def save requires :name, :zone, :size_gb = { :description => description || default_description, :type => type, :size_gb => size_gb, :source_image => source_image, :source_snapshot => source_snapshot, :labels => labels }.reject { |_, v| v.nil? } if [:source_image] unless source_image.include?("projects/") [:source_image] = service.images.get(source_image).self_link end end # Request needs backward compatibility so source image is specified in # method arguments data = service.insert_disk(name, zone, [:source_image], **) operation = Fog::Google::Compute::Operations.new(service: service) .get(data.name, data.zone) operation.wait_for { ready? } # Handle errors if operation.error? msg = "Error creating disk #{name} size #{size_gb}." err = operation.primary_error msg = "#{msg} #{err.}" unless err.nil? raise Fog::Errors::Error.new(msg) end reload end |
#zone_name ⇒ Object
82 83 84 |
# File 'lib/fog/google/compute/models/disk.rb', line 82 def zone_name zone.nil? ? nil : zone.split("/")[-1] end |