Class: Purest::Volume

Inherits:
Rest show all
Defined in:
lib/purest/volume.rb

Constant Summary collapse

GET_PARAMS =
[:action, :block_size, :connect, :historical, :length, :names,
:pending, :pending_only, :pgrouplist, :private, :protect,
:shared, :snap, :space]

Instance Method Summary collapse

Methods inherited from Rest

access_method?, #authenticated?, #concat_url, #initialize, #logout, method_missing, #use_named_parameter

Constructor Details

This class inherits a constructor from Purest::Rest

Instance Method Details

#create(options = nil) ⇒ Object

Create a volume, POST

Parameters:

  • options (Hash) (defaults to: nil)

    options to pass



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/purest/volume.rb', line 45

def create(options = nil)
  @options = options

  raw_resp = @conn.post do |req|
    url = "/api/#{Purest.configuration.api_version}/volume"
    url += "/#{@options[:name]}" if @options[:name]
    url += "/pgroup/#{@options[:pgroup]}" if @options[:pgroup]
    req.body = @options.to_json

    req.url url
  end

  JSON.parse(raw_resp.body, :symbolize_names => true)
end

#delete(options = nil) ⇒ Object

Delete a volume, DELETE

Parameters:

  • options (Hash) (defaults to: nil)

    options to pass



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/purest/volume.rb', line 86

def delete(options = nil)
  @options = options

  raw_resp = @conn.delete do |req|
    url = "/api/#{Purest.configuration.api_version}/volume/#{@options[:name]}"
    url += "/pgroup/#{@options[:pgroup]}" if @options[:pgroup]

    req.url url
  end

  if @options[:eradicate]
    raw_resp = @conn.delete do |req|
      url = ["/api/#{Purest.configuration.api_version}/volume/#{@options[:name]}"]
      req.body = @options.to_json

      req.url concat_url url
    end
  end

  JSON.parse(raw_resp.body, :symbolize_names => true)
end

#get(options = nil) ⇒ Object

Get a list of volumes, GET

Parameters:

  • options (Hash) (defaults to: nil)

    options to pass



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/purest/volume.rb', line 13

def get(options = nil)
  @options = options
  create_session unless authenticated?

  # Build up a url from parts, allowing for dynamic http calls
  # by simply passing in params accepted by Pure's API
  raw_resp = @conn.get do |req|
    url = ["/api/#{Purest.configuration.api_version}/volume"]

    url.map!{|u| u + "/#{@options[:name]}"} if !@options.nil? && @options[:name]

    # Map /diff, /hgroup, or /host depending on option passed
    [:show_diff, :show_hgroup, :show_host].each do |path|
      new_path = path.to_s.gsub('show_', '')
      url.map!{|u| u + "/#{new_path}"} if !@options.nil? && @options[path]
    end

    # Generate array, consisting of url parts, to be built
    # by concat_url method below
    GET_PARAMS.each do |param|
      url += self.send(:"use_#{param}",@options)
    end

    # Build url from array of parts, send request
    req.url concat_url url
  end

  JSON.parse(raw_resp.body, :symbolize_names => true)
end

#update(options = nil) ⇒ Object

Update a volume, PUT

Parameters:

  • options (Hash) (defaults to: nil)

    options to pass



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/purest/volume.rb', line 62

def update(options = nil)
  @options = options

  raw_resp = @conn.put do |req|
    # Here we construct the url first, because the options hash
    # may have to be manipulated below
    req.url "/api/#{Purest.configuration.api_version}/volume/#{@options[:name]}"

    # Repurpose @options[:name] so that it is now set to the new_name
    # allowing us to rename a volume.
    @options[:name] = @options.delete(:new_name) if @options[:new_name]

    # Remove name from @options which is sent in the body,
    # if size is passed in, because those two cannot be sent together.
    @options.delete(:name) if @options[:size]

    req.body = @options.to_json
  end

  JSON.parse(raw_resp.body, :symbolize_names => true)
end