module ProfitBricks # Volume class class Volume < ProfitBricks::Model # Delete the volume. def delete response = ProfitBricks.request( method: :delete, path: "/datacenters/#{datacenterId}/volumes/#{id}", expects: 202 ) self.requestId = response[:requestId] self end # Update the volume. def update(options = {}) response = ProfitBricks.request( method: :patch, path: "/datacenters/#{datacenterId}/volumes/#{id}", expects: 202, body: options.to_json ) @properties = @properties.merge(response['properties']) self end # Attach volume to server. def attach(server_id) ProfitBricks.request( method: :post, path: "/datacenters/#{datacenterId}/servers/#{server_id}/volumes", expects: 202, body: { id: id }.to_json ) self end # Detach volume from server. def detach(server_id) ProfitBricks.request( method: :delete, path: "/datacenters/#{datacenterId}/servers/#{server_id}/volumes/#{id}", expects: 202 ) end # Create volume snapshot. # # ==== Parameters # * +options+: # - +name+ - *Optional*, name of the snapshot # - +description+ - *Optional*, description of the snapshot # # ==== Returns # * +id+ - Universally unique identifer of resource # * +type+ - Resource type # * +href+ - Resource URL representation # * +metadata+: # - +lastModifiedDate+ # - +lastModifiedBy+ # - +createdDate+ # - +createdBy+ # - +state+ # - +etag+ # * +properties+: # - +name+ # - +description+ # - +location+ # - +cpuHotPlug+ # - +cpuHotUnPlug+ # - +ramHotPlug+ # - +ramHotUnPlug+ # - +nicHotPlug+ # - +nicHotUnPlug+ # - +discVirtioHotPlug+ # - +discVirtioHotUnPlug+ # - +discScsiHotPlug+ # - +discScsiHotUnPlug+ # - +licenceType+ # def create_snapshot(options = {}) response = ProfitBricks.request( method: :post, path: "/datacenters/#{datacenterId}/volumes/#{id}/create-snapshot", headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, expects: 202, body: URI.encode_www_form(options) ) ProfitBricks::Snapshot.new(response) end # Restore snapshot to volume. # # ==== Parameters # * +snapshot_id+: Universally unique identifer of snapshot resource # # ==== Returns # * +true+ # def restore_snapshot(snapshot_id) ProfitBricks.request( method: :post, path: "/datacenters/#{datacenterId}/volumes/#{id}/restore-snapshot", headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, expects: 202, body: URI.encode_www_form(snapshotId: snapshot_id) ) end class << self # Create a new volume. # # ==== Parameters # * +options+: # - +name+ - *Optional*, name of the volume # - +size+ - *Required*, size of the volume in GB # - +type+ - *Required*, the volume type (HDD or SSD) # - +bus+ - *Optional*, the bus type of the volume # * +VIRTIO+ - *Default* # * +IDE+ # - +image+ - *Optional*, image or snapshot ID # - +sshKeys+ - *Optional*, a list of public SSH keys # # ==== Returns # * +id+ - Universally unique identifer of resource # * +type+ - Resource type # * +href+ - Resource URL representation # * +metadata+: # - +lastModifiedDate+ # - +lastModifiedBy+ # - +createdDate+ # - +createdBy+ # - +state+ # - +etag+ # * +properties+: # - +name+ # - +size+ # - +bus+ # - +image+ # - +imagePassword+ # - +type+ # - +licenseType+ # - +cpuHotPlug+ # - +cpuHotUnPlug+ # - +ramHotPlug+ # - +ramHotUnPlug+ # - +nicHotPlug+ # - +nicHotUnPlug+ # - +discVirtioHotPlug+ # - +discVirtioHotUnPlug+ # - +discScsiHotPlug+ # - +discScsiHotUnPlug+ # - +sshKeys+ # def create(datacenter_id, options = {}) response = ProfitBricks.request( method: :post, path: "/datacenters/#{datacenter_id}/volumes", expects: 202, body: { properties: options }.to_json ) add_parent_identities(response) instantiate_objects(response) end # List all datacenter volumes. def list(datacenter_id, server_id = nil) path = if server_id.nil? "/datacenters/#{datacenter_id}/volumes" else "/datacenters/#{datacenter_id}/servers/#{server_id}/volumes" end response = ProfitBricks.request( method: :get, path: path, expects: 200 ) add_parent_identities(response) instantiate_objects(response) end # Retrieve a datacenter volume. def get(datacenter_id, server_id = nil, volume_id) path = if server_id.nil? "/datacenters/#{datacenter_id}/volumes/#{volume_id}" else "/datacenters/#{datacenter_id}/servers/#{server_id}/volumes/#{volume_id}" end response = ProfitBricks.request( method: :get, path: path, expects: 200 ) add_parent_identities(response) instantiate_objects(response) end end end end