Class: Stoarray

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

Constant Summary collapse

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

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
30
31
32
33
34
35
36
37
38
# File 'lib/arraycalls.rb', line 21

def cookie(testy: false)
  if @url.to_s =~ /auth\/session/
    case testy
    when false
      @response = @request.start { |http| http.request(@call) }
      all_cookies = @response.get_fields('set-cookie')
    when true
      all_cookies = ['cookie time']
    end
    cookies_array = Array.new
    all_cookies.each { | cookie |
      cookies_array.push(cookie.split('; ')[0])
    }
    cookies = cookies_array.join('; ')
  else
    error_text("cookie", @url.to_s, 'auth/session')
  end
end

#error_text(method_name, url, wanted) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/arraycalls.rb', line 40

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" => 400
  }
end

#flippy(temp_hash, testy: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/arraycalls.rb', line 50

def flippy(temp_hash, testy: false)
  flippy = temp_hash['to-snapshot-set-id'] + '_347'
  url = 'https://' + @url.host + '/api/json/v2/types/snapshot-sets'
  case testy
  when false
    x = Stoarray.new(headers: @headers, meth: 'Get', params: {}, url: url).snap
  when true
    x = {
      "response" => {
        "snapshot-sets" => [ { "name" => flippy } ]
      }
    }
  end
  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



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

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/arraycalls.rb', line 82

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

#responder(response) ⇒ Object

combine into one method? with error_text



97
98
99
100
101
102
# File 'lib/arraycalls.rb', line 97

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

#snapObject



104
105
106
107
108
109
110
111
# File 'lib/arraycalls.rb', line 104

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



113
114
115
116
117
118
119
120
# File 'lib/arraycalls.rb', line 113

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