Class: EnfApi::API

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/enfapi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#apiObject

Returns the value of attribute api.



290
291
292
# File 'lib/enfapi.rb', line 290

def api
  @api
end

#headersObject

Returns the value of attribute headers.



290
291
292
# File 'lib/enfapi.rb', line 290

def headers
  @headers
end

Instance Method Details

#activate_enfnw(subnet) ⇒ Object



491
492
493
494
495
# File 'lib/enfapi.rb', line 491

def activate_enfnw(subnet)
  @api["/api/xcr/v3/enfnws/#{subnet}"].put("", @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#authenticate(host, user, password) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/enfapi.rb', line 292

def authenticate(host, user, password)
  # format url
  host = "https://#{host}" unless host =~ /^(http|https):\/\//

  # chomp trailing '/'
  host = host.chomp("/")

  # Initalize rest client
  @api = RestClient::Resource.new(host)

  # Create request json
  hash = { username: user, password: password }
  json = to_json(hash)

  # call api
  @api["/api/xcr/v3/xauth"].post(json, { content_type: :json, accept: :json }) { |response, request, result|
    case response.code
    when 200
      # get resp from json
      resp = from_json(response.body)

      # set request headers for subsequent api calls
      token = resp[:data][0][:token]
      @headers = { content_type: :json, accept: :json, "Authorization" => "Bearer #{token}" }

      # return resp
      resp
    else
      raise EnfApi::ERROR, "Login Failed! Invalid user or password!"
    end
  }
end

#create_domain(domain_hash) ⇒ Object



376
377
378
379
380
381
# File 'lib/enfapi.rb', line 376

def create_domain(domain_hash)
  json = to_json(domain_hash)
  @api["/api/xcr/v3/domains"].post(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#create_nw(domain, nw_hash) ⇒ Object



438
439
440
441
442
443
# File 'lib/enfapi.rb', line 438

def create_nw(domain, nw_hash)
  json = to_json(nw_hash)
  @api["/api/xcr/v3/domains/#{domain}/nws"].post(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#delete(request_uri) ⇒ Object



343
344
345
346
347
# File 'lib/enfapi.rb', line 343

def delete(request_uri)
  @api[request_uri].delete(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get(request_uri) ⇒ Object



325
326
327
328
329
# File 'lib/enfapi.rb', line 325

def get(request_uri)
  @api[request_uri].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get_domain(domain_id) ⇒ Object



370
371
372
373
374
# File 'lib/enfapi.rb', line 370

def get_domain(domain_id)
  @api["/api/xcr/v3/domains/#{domain_id}"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get_domain_rate_limits(domain_network, filter = nil) ⇒ Object



404
405
406
407
408
409
410
411
# File 'lib/enfapi.rb', line 404

def get_domain_rate_limits(domain_network, filter = nil)
  api_url = "/api/xcr/v3/domains/#{domain_network}/ep_rate_limits"
  api_url = "#{api_url}/#{filter}" if filter

  @api[api_url].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get_endpoint(ipv6) ⇒ Object



476
477
478
479
480
# File 'lib/enfapi.rb', line 476

def get_endpoint(ipv6)
  @api["/api/xcr/v3/cxns/#{ipv6}"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get_ep_rate_limits(ipv6, filter = nil) ⇒ Object



422
423
424
425
426
427
428
429
# File 'lib/enfapi.rb', line 422

def get_ep_rate_limits(ipv6, filter = nil)
  api_url = "/api/xcr/v3/cxns/#{ipv6}/ep_rate_limits"
  api_url = "#{api_url}/#{filter}" if filter

  @api[api_url].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get_network_rate_limits(network, filter = nil) ⇒ Object



413
414
415
416
417
418
419
420
# File 'lib/enfapi.rb', line 413

def get_network_rate_limits(network, filter = nil)
  api_url = "/api/xcr/v3/nws/#{network}/ep_rate_limits"
  api_url = "#{api_url}/#{filter}" if filter

  @api[api_url].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#get_nw(network_cidr) ⇒ Object



452
453
454
455
456
# File 'lib/enfapi.rb', line 452

def get_nw(network_cidr)
  @api["/api/xcr/v3/nws/#{network_cidr}"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#list_domain_nws(domain_id) ⇒ Object

NOT READY API. MOVE ABOVE THIS LINE AFTER RSPEC OR INTO OWN CLASS



352
353
354
355
356
# File 'lib/enfapi.rb', line 352

def list_domain_nws(domain_id)
  @api["/api/xcr/v3/domains/#{domain_id}/nws"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#list_domainsObject



364
365
366
367
368
# File 'lib/enfapi.rb', line 364

def list_domains
  @api["/api/xcr/v3/domains"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#list_endpoint_events(ipv6) ⇒ Object



464
465
466
467
468
# File 'lib/enfapi.rb', line 464

def list_endpoint_events(ipv6)
  @api["/api/xcr/v3/cxns/#{ipv6}/events"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#list_enfnwsObject



358
359
360
361
362
# File 'lib/enfapi.rb', line 358

def list_enfnws
  @api["/api/xcr/v3/enfnws"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#list_network_events(network_cidr) ⇒ Object



470
471
472
473
474
# File 'lib/enfapi.rb', line 470

def list_network_events(network_cidr)
  @api["/api/xcr/v3/nws/#{network_cidr}/events"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#list_nw_connections(network_cidr, file = nil) ⇒ Object



458
459
460
461
462
# File 'lib/enfapi.rb', line 458

def list_nw_connections(network_cidr, file = nil)
  @api["/api/xcr/v3/nws/#{network_cidr}/cxns"].get(@headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#post(request_uri, request_body = "") ⇒ Object



331
332
333
334
335
# File 'lib/enfapi.rb', line 331

def post(request_uri, request_body = "")
  @api[request_uri].post(request_body, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#put(request_uri, request_body = "") ⇒ Object



337
338
339
340
341
# File 'lib/enfapi.rb', line 337

def put(request_uri, request_body = "")
  @api[request_uri].put(request_body, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#update_domain_rate_limits(domain_network, limit, limits_hash) ⇒ Object



383
384
385
386
387
388
# File 'lib/enfapi.rb', line 383

def update_domain_rate_limits(domain_network, limit, limits_hash)
  json = to_json(limits_hash)
  @api["/api/xcr/v3/domains/#{domain_network}/ep_rate_limits/#{limit}"].put(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#update_domain_status(domain_network, status) ⇒ Object



431
432
433
434
435
436
# File 'lib/enfapi.rb', line 431

def update_domain_status(domain_network, status)
  json = to_json(status)
  @api["/api/xcr/v3/domains/#{domain_network}/status"].put(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#update_endpoint(ipv6, name) ⇒ Object



482
483
484
485
486
487
488
489
# File 'lib/enfapi.rb', line 482

def update_endpoint(ipv6, name)
  hash = { ipv6: ipv6, name: name }
  json = to_json(hash)

  @api["/api/xcr/v3/cxns/#{ipv6}"].put(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#update_ep_rate_limits(ipv6, limit, limits_hash) ⇒ Object



397
398
399
400
401
402
# File 'lib/enfapi.rb', line 397

def update_ep_rate_limits(ipv6, limit, limits_hash)
  json = to_json(limits_hash)
  @api["/api/xcr/v3/cxns/#{ipv6}/ep_rate_limits/#{limit}"].put(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#update_network_rate_limits(network, limit, limits_hash) ⇒ Object



390
391
392
393
394
395
# File 'lib/enfapi.rb', line 390

def update_network_rate_limits(network, limit, limits_hash)
  json = to_json(limits_hash)
  @api["/api/xcr/v3/nws/#{network}/ep_rate_limits/#{limit}"].put(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end

#update_nw(network_cidr, nw_hash) ⇒ Object



445
446
447
448
449
450
# File 'lib/enfapi.rb', line 445

def update_nw(network_cidr, nw_hash)
  json = to_json(nw_hash)
  @api["/api/xcr/v3/nws/#{network_cidr}"].put(json, @headers) { |response, request, result|
    process_api_response response, request, result
  }
end