Module: Reign

Defined in:
lib/common.rb,
lib/reign-client.rb

Defined Under Namespace

Classes: Client

Constant Summary collapse

GET_REGIONS_URI =

Constants for API resources

'/api/v1/regions'
GET_PRESENCE_URI =
'/api/v1/presence/'
HEARTBEAT_URI =
'/api/v1/heartbeat/'
KEY_VALUE_URI =
'/api/v1/kv/'
LEASE_URI =
'/api/v1/lease/'
WATCH_URI =
'/api/v1/watch/'

Class Method Summary collapse

Class Method Details

.generate_acquire_lease_uri(args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/common.rb', line 79

def self.generate_acquire_lease_uri args
  term_s = args[:term_s]
  owner_id = args[:owner_id]
  if term_s == nil
    raise ArgumentError.new("must pass term_s")
  end
  if owner_id == nil
    raise ArgumentError.new("must pass owner_id")
  end
  term_s = term_s.to_s
  query_params = {"op" => "acquire", "term_s" => term_s, "owner_id" => owner_id}

  uri = Reign.generate_lease_uri(args[:namespace], args[:lease_path])
  return uri + Reign.generate_query_string(query_params) 
end

.generate_categories_uri(namespace) ⇒ Object



13
14
15
16
17
18
# File 'lib/common.rb', line 13

def self.generate_categories_uri(namespace)
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end
  return GET_PRESENCE_URI + namespace
end

.generate_delete_kv_uri(args) ⇒ Object



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

def self.generate_delete_kv_uri args
  if args[:sig] == nil
    raise ArgumentError.new("must pass sig parameter in order to delete entry")
  end
  uri = Reign.generate_kv_uri(args[:namespace], args[:kv_path])
  uri = uri + Reign.generate_query_string({"sig" => args[:sig]})
  return uri
end

.generate_get_kv_uri(args) ⇒ Object



63
64
65
# File 'lib/common.rb', line 63

def self.generate_get_kv_uri args
  return Reign.generate_kv_uri(args[:namespace], args[:kv_path])
end

.generate_get_lease_uri(args) ⇒ Object



117
118
119
# File 'lib/common.rb', line 117

def self.generate_get_lease_uri args
  return Reign.generate_lease_uri(args[:namespace], args[:lease_path])
end

.generate_header_for_request(args) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/common.rb', line 223

def self.generate_header_for_request(args)
  #parse arguments
  request_method = args[:method] or raise ArgumentError, 'must pass :method'
  request_uri = args[:request_uri] or raise ArgumentError, 'must pass :request_uri'
  api_key = args[:api_key] or raise ArgumentError, 'must pass :api_key'
  secret_key = args[:secret_key] or raise ArgumentError, 'must pass :secret_key'
  region = args[:region] or raise ArgumentError, 'must pass :region'
  payload = args[:payload]

  now = Time.new
  date_header_value = now.strftime("%a, %d %b %Y %X %z")    

  case request_method
  when :get
    message = date_header_value + "\n" + "GET" + "\n" + request_uri
  when :post
    if payload != nil
      message = date_header_value + "\n" + "POST" + "\n" + request_uri + "\n" + Base64.strict_encode64(payload.strip) 
    else 
      message = date_header_value + "\n" + "POST" + "\n" + request_uri
    end
  when :put
    if payload != nil
      message = date_header_value + "\n" + "PUT" + "\n" + request_uri + "\n" + Base64.strict_encode64(payload.strip)
    else
      message = date_header_value + "\n" + "PUT" + "\n" + request_uri
    end
  when :delete
    message = date_header_value + "\n" + "DELETE" + "\n" + request_uri
  end

  hash  = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', secret_key, message)).strip 
  auth = 'REIGN-HMAC-SHA256 ' + api_key + ":" + hash   
  headers = {:Date => date_header_value, :Authorization => auth, :'Reign-Region' => region}

  return headers
end

.generate_heartbeat_body(args) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/common.rb', line 152

def self.generate_heartbeat_body args
  node_id = args[:node_id]
  host = args[:host]
  port = args[:port]
  status = args[:status]
  hb_to_s = args[:hb_to_s]      
  category = args[:category]
  namespace = args[:namespace]
  if node_id == nil
    raise ArgumentError.new("must pass node_id")
  end
  if not (status == "OK" or status == "NOK")
    raise ArgumentError.new("must pass status = OK / NOK")
  end 
  if hb_to_s == nil
    raise ArgumentError.new("must pass heartbeat timeout secs")
  end
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end
  if category == nil
    raise ArgumentError.new("must pass category")
  end

  request_body = {}
  request_body['presence'] = {}
  request_body['presence']['category'] = category
    
  node = {}
  node['id'] = node_id
  node['host'] = host
  node['port'] = port
  node['status'] = status
  node['hb_to_s'] = hb_to_s

  request_body['presence']['node'] = node
  return request_body
end

.generate_heartbeat_uri(namespace) ⇒ Object



145
146
147
148
149
150
# File 'lib/common.rb', line 145

def self.generate_heartbeat_uri(namespace)
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end
  return Reign::HEARTBEAT_URI + namespace
end

.generate_kv_uri(namespace, kv_path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/common.rb', line 67

def self.generate_kv_uri(namespace, kv_path)
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end
  if kv_path == nil
    raise ArgumentError.new("must pass kv_path")
  end
  kv_path.slice!(0) if kv_path[0] == "/"
  kv_path.chomp!("/")
  return KEY_VALUE_URI + namespace + "/" + kv_path
end

.generate_lease_uri(namespace, lease_path) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/common.rb', line 121

def self.generate_lease_uri(namespace, lease_path)
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end
  if lease_path == nil or lease_path.length == 0
    raise ArgumentError.new("must pass non-trivial lease_path")
  end  
  lease_path.slice!(0) if lease_path[0] == "/"
  lease_path.chomp!("/")
  return LEASE_URI + namespace + "/" + lease_path
end

.generate_nodes_in_category_uri(args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/common.rb', line 20

def self.generate_nodes_in_category_uri args
  category = args[:category]
  namespace = args[:namespace]
  status = args[:status]
  if category == nil
    raise ArgumentError.new("must pass category")
  end
  if status != nil and not ["OK", "NOK"].include?(status) 
    raise ArgumentError.new("status must be OK or NOK if passed")
  end
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end

  uri = Reign::GET_PRESENCE_URI + namespace + "/" + category
  if status != nil
    uri = uri + Reign.generate_query_string({"status" => status})
  end
  return uri
end

.generate_query_string(map) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/common.rb', line 191

def self.generate_query_string(map)
  query = "?"
  map.each {
    |k, v|
    query.concat(k)
    query.concat("=")
    query.concat(v)
    query.concat("&")
  }
  return query.chomp("&")
end

.generate_release_lease_uri(args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/common.rb', line 95

def self.generate_release_lease_uri args
  sig = args[:sig]
  if sig == nil
    raise ArgumentError.new("must pass sig.  This value is returned in the response body when the lease is acquired")
  end

  query_params = {"op" => "release", "sig" => sig}
  uri = Reign.generate_lease_uri(args[:namespace], args[:lease_path])
  return uri + Reign.generate_query_string(query_params)
end

.generate_revoke_lease_uri(args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/common.rb', line 106

def self.generate_revoke_lease_uri args
  sig = args[:sig]
  if sig == nil
    raise ArgumentError.new("must pass sig.  This value is returned in the response body when the lease is acquired")
  end

  query_params = {"op" => "revoke", "sig" => sig}
  uri = Reign.generate_lease_uri(args[:namespace], args[:lease_path])
  return uri + Reign.generate_query_string(query_params)
end

.generate_upsert_kv_uri(args) ⇒ Object



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

def self.generate_upsert_kv_uri args
  uri = Reign.generate_kv_uri(args[:namespace], args[:kv_path])
  query_params = {}
  if args[:owner_id]
    query_params["owner_id"] = args[:owner_id]
  end
  if args[:sig]
    query_params["sig"] = args[:sig]
  end
  uri = uri + Reign.generate_query_string(query_params)
  return uri
end

.generate_watch_body_template(owner_id, email_contacts, webhook_contacts) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/common.rb', line 203

def self.generate_watch_body_template(owner_id, email_contacts, webhook_contacts) 
  if owner_id == nil
    raise ArgumentError.new("must pass owner_id")
  end

  email_contacts = email_contacts ? email_contacts : []
  webhook_contacts = webhook_contacts ? webhook_contacts : []

  body = {}
  body["watch"] = {}
  body["watch"]["webhooks"] = []
  body["watch"]["emails"] = []
  body["watch"]["owner_id"] = owner_id

  email_contacts.each {|email| body["watch"]["emails"].push(email)}
  webhook_contacts.each {|webhook| body["watch"]["webhooks"].push(webhook)}
 
  return body
end

.generate_watch_uri(namespace, watch_entity_path) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/common.rb', line 133

def self.generate_watch_uri(namespace, watch_entity_path)
  if namespace == nil
    raise ArgumentError.new("must pass namespace")
  end
  if watch_entity_path == nil
    raise ArgumentError.new("must pass watch_entity_path")
  end
  watch_entity_path.slice!(0) if watch_entity_path[0] == "/"
  watch_entity_path.chomp!("/")
  return WATCH_URI + namespace + "/" + watch_entity_path
end