Class: Stoarray

Inherits:
Object
  • Object
show all
Defined in:
lib/arraycalls.rb

Constant Summary collapse

VERBS =
{
  'get'    => Net::HTTP::Get,
  'post'   => Net::HTTP::Post,
  'put'    => Net::HTTP::Put,
  'delete' => Net::HTTP::Delete
}

Instance Method Summary collapse

Constructor Details

#initialize(headers: {}, meth: 'Get', params: {}, url: 'https://array/') ⇒ Stoarray

Returns a new instance of Stoarray.



10
11
12
13
14
15
16
17
18
19
# File 'lib/arraycalls.rb', line 10

def initialize(headers: {}, meth: 'Get', params: {}, url: 'https://array/')
  @url = URI.parse(url) # URL of the call
  @headers = headers
  @call = VERBS[meth.downcase].new(@url.path, initheader = @headers)
  @call.body = params.to_json # Pure and Xtremio expect json parameters
  @request = Net::HTTP.new(@url.host, @url.port)
  @request.read_timeout = 30
  @request.use_ssl = true if @url.to_s =~ /https/
  @request.verify_mode = OpenSSL::SSL::VERIFY_NONE # parameterize?
end

Instance Method Details



21
22
23
24
25
26
27
28
29
# File 'lib/arraycalls.rb', line 21

def cookie
  @response = @request.start { |http| http.request(@call) }
  all_cookies = @response.get_fields('set-cookie')
  cookies_array = Array.new
  all_cookies.each { | cookie |
    cookies_array.push(cookie.split('; ')[0])
  }
  cookies = cookies_array.join('; ')
end

#error_text(method_name, url, wanted) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/arraycalls.rb', line 31

def error_text(method_name, url, wanted)
  response = {
    "response" =>
      "ERROR: Wrong url for the #{method_name} method.\n"\
      "Sent: #{url}\n"\
      "Expected: \"#{wanted}\" as part of the url.",
    "status" => "DORKED!"
  }
end

#flippy(temp_hash) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/arraycalls.rb', line 41

def flippy(temp_hash)
  flippy = temp_hash['to-snapshot-set-id'] + '_347'
  url = 'https://sa0319xms01/api/json/v2/types/snapshot-sets'
  x = Stoarray.new(headers: @headers, meth: 'Get', params: {}, url: url).snap
  if x['response']['snapshot-sets'].any? { |y| y['name'].include?(flippy) }
    temp_hash['snapshot-set-name']  = temp_hash['to-snapshot-set-id']
    temp_hash['to-snapshot-set-id'] = flippy
  else
    temp_hash['snapshot-set-name']  = flippy
  end
  temp_hash['no-backup'] = true
  temp_hash
end

#hostObject



55
56
57
58
59
60
61
62
# File 'lib/arraycalls.rb', line 55

def host
  if @url.to_s =~ /host/
    response = @request.start { |http| http.request(@call) }
    responder(response)
  else
    error_text("host", @url.to_s, "host")
  end
end

#refreshObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/arraycalls.rb', line 64

def refresh
  case @url.to_s
  when /snapshot/ # Xtremio
    @call.body = flippy(JSON.parse(@call.body)).to_json
  when /volume/ # Pure, handle the interim snap automagically
    # obviously not implemented yet :)
  end
  refreshy = @request.start { |http| http.request(@call) }
  responder(refreshy)
end

#responder(response) ⇒ Object

combine into one method? with error_text



75
76
77
78
79
80
# File 'lib/arraycalls.rb', line 75

def responder(response) # combine into one method? with error_text
  response = {
    "response" => JSON.parse(response.body),
    "status" => response.code
  }
end

#snapObject



82
83
84
85
86
87
88
89
# File 'lib/arraycalls.rb', line 82

def snap
  if @url.to_s =~ /snapshot/
    response = @request.start { |http| http.request(@call) }
    responder(response)
  else
    error_text("snap", @url.to_s, "snapshot")
  end
end

#volumeObject



91
92
93
94
95
96
97
98
# File 'lib/arraycalls.rb', line 91

def volume
  if @url.to_s =~ /volume/
    response = @request.start { |http| http.request(@call) }
    responder(response)
  else
    error_text("volume", @url.to_s, "volume")
  end
end