Class: Hcloud::VolumeResource

Inherits:
AbstractResource show all
Defined in:
lib/hcloud/volume_resource.rb

Instance Attribute Summary

Attributes inherited from AbstractResource

#client

Instance Method Summary collapse

Methods inherited from AbstractResource

#all, bind_to, #each, filter_attributes, #find, #find_by, #initialize, #limit, #order, #page, #pagination, #per_page, resource, resource_class, resource_path, resource_url, #run, #where

Constructor Details

This class inherits a constructor from Hcloud::AbstractResource

Instance Method Details

#[](arg) ⇒ Object



7
8
9
10
11
12
# File 'lib/hcloud/volume_resource.rb', line 7

def [](arg)
  case arg
  when Integer then find_by(id: arg)
  when String then find_by(name: arg)
  end
end

#create(size:, name:, automount: nil, format: nil, location: nil, server: nil, labels: {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hcloud/volume_resource.rb', line 14

def create(size:, name:, automount: nil, format: nil, location: nil, server: nil, labels: {})
  raise Hcloud::Error::InvalidInput, 'no name given' if name.blank?
  raise Hcloud::Error::InvalidInput, 'invalid size given' unless size.to_i >= 10
  if location.blank? && server.nil?
    raise Hcloud::Error::InvalidInput, 'location or server must be given'
  end

  prepare_request(
    'volumes', j: COLLECT_ARGS.call(__method__, binding),
               expected_code: 201
  ) do |response|
    [
      Action.new(client, response.parsed_json[:action]),
      Volume.new(client, response.parsed_json[:volume]),
      response.parsed_json[:next_actions].map do |action|
        Action.new(client, action)
      end
    ]
  end
end