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.2.0"

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

#pgroupObject



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

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

#refreshObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/arraycalls.rb', line 91

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 /1.4/ # Pure, handle the interim snap automagically
    error_state = false
    url     = 'https://' + @url.host + '/api/1.4/pgroup'
    source  = (JSON.parse(@call.body))['source']
    suffix  = Time.new.strftime("%A") # Day of the week.
    pam     = { :snap => true, :source => source, :suffix => suffix }
    snap    = Stoarray.new(headers: @headers, meth: 'Post', params: pam, url: url).pgroup
    respond = {
      "response" => {
        "snap" => {
          "response" => snap['response']
        }
      },
      "status" => {
        "snap" => {
          "status" => snap['status']
        }
      }
    }
    if snap['status'] >= 200 && snap['status'] <= 299
      pairs   = (JSON.parse(@call.body))['snap_pairs']
      pairs.each do |key, val|
        tgt = 'clone_' + val
        src = source[0] + '.' + suffix + '.' + key
        pam = { :overwrite => true, :source => src }
        url = 'https://' + @url.host + '/api/1.4/volume/' + val
        clone = Stoarray.new(headers: @headers, meth: 'Post', params: pam, url: url).volume
        respond['response'][tgt] = {
          "response" => clone['response']
        }
        respond['status'][tgt] = {
          "status" => clone['status']
        }
      end
      url   = 'https://' + @url.host + '/api/1.4/pgroup/' + source[0] + '.' + suffix
      zappy = Stoarray.new(headers: @headers, meth: 'Delete', params: {}, url: url).pgroup
      respond['response']['destroy'] = {
        "response" => zappy['response']
      }
      respond['status']['destroy'] = {
        "status" => zappy['status']
      }
      pam   = { :eradicate => true }
      disintegrate = Stoarray.new(headers: @headers, meth: 'Delete', params: pam, url: url).pgroup
      respond['response']['eradicate'] = {
        "response" => disintegrate['response']
      }
      respond['status']['eradicate'] = {
        "status" => disintegrate['status']
      }
    else
      respond
    end
    respond['status'].each do |key, val|
      error_state = true if val.any? { |status, code| code.to_i < 200 || code.to_i > 299 }
    end
    if error_state == true
      respond
    else
      response = {
        "response" =>
          "SUCCESS: Refresh completed for #{source[0]} protection group.\n",
        "status" => 201
      }
    end
  else
    error_text("refresh", @url.to_s, "snapshot or 1.4")
  end
end

#responder(response) ⇒ Object

combine into one method? with error_text



167
168
169
170
171
172
# File 'lib/arraycalls.rb', line 167

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

#snapObject



174
175
176
177
178
179
180
181
# File 'lib/arraycalls.rb', line 174

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



183
184
185
186
187
188
189
190
# File 'lib/arraycalls.rb', line 183

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