Metrics:
Enabled: false
-# Use the return of the conditional for variable assignment and comparison
-# (wtf)
-Style/ConditionalAssignment:
- Enabled: false
-
-# Favor format over sprintf
-# (easy to read)
-Style/FormatString:
- Enabled: false
-
# Do not introduce global variables
# (do want)
Style/GlobalVars:
Enabled: false
-# Extra empty line detected at class body end
-# (easy to read)
-Style/EmptyLinesAroundClassBody:
- Enabled: false
-
-# Extra empty line detected at module body beginning
-# Extra empty line detected at module body end
-# (easy to read)
-Style/EmptyLinesAroundModuleBody:
- Enabled: false
-
-# Favor modifier if usage when having a single-line body
-# (easy to read)
-Style/IfUnlessModifier:
- Enabled: false
-
# Missing space after #
# (debugging)
Style/LeadingCommentSpace:
Enabled: false
-# Do not use parentheses for method calls with no arguments
-# (easy to read)
-Style/MethodCallParentheses:
- Enabled: false
-
-# Freeze mutable objects assigned to constants
-# (easy to read)
-Style/MutableConstant:
- Enabled: false
-
-# Favor unless over if for negative conditions
-# (easy to read)
-Style/NegatedIf:
- Enabled: false
-
# Redundant return detected
# (easy to read)
Style/RedundantReturn:
Enabled: false
-# Avoid comma after the last parameter of a method call.
-# (do want)
-Style/TrailingCommaInArguments:
- Enabled: false
-
# Avoid comma after the last item of an array
# (do want)
Style/TrailingCommaInLiteral:
$logger = Logger.new(STDOUT)
$logger.formatter = proc do |severity, datetime, _progname, msg|
date_format = datetime.strftime('%Y-%m-%dT%H:%M:%S%z')
- sprintf "[#{date_format}] %5s: #{msg}\n", severity
+ format("[#{date_format}] %5{severity}: #{msg}\n", severity: severity)
end
-$config = Now::Config.new()
+$config = Now::Config.new
helpers do
def switch_user(user)
if user.nil?
- nebula.switch_server()
+ nebula.switch_server
else
nebula.switch_user(user)
end
halt e.code, e.message
end
end
-
end
end
require 'yaml'
module Now
-
CONFIG_FILES = [
::File.expand_path('~/.config/now.yml'),
'/etc/now.yml',
::File.expand_path('../../etc/now.yml', __FILE__),
- ]
+ ].freeze
# Config class for NOW
class Config < Hash
replace config
end
-
end
end
module Now
-
# The main exception class for NOW.
class NowError < StandardError
attr_accessor :code
def initialize(code)
@code = code
end
-
end
-
end
require 'ipaddress'
module Now
-
EXPIRE_LENGTH = 8 * 60 * 60
# NOW core class for communication with OpenNebula
end
def check(return_code)
- if !OpenNebula.is_error?(return_code)
- return true
- end
+ return true unless OpenNebula.is_error?(return_code)
code = error_one2http(return_code.errno)
raise NowError.new(code), return_code.message
raise NowError.new(422), "Missing 'IP' in the address range #{id} of network #{vn_id}"
end
address = IPAddress ip
- if !ip.include? '/'
- address.prefix = 24
- end
+ address.prefix = 24 unless ip.include? '/'
when 'IP6', 'IP4_6'
ip = ar['GLOBAL_PREFIX'] || ar['ULA_PREFIX']
if ip.nil? || ip.empty?
raise NowError.new(422), "Missing 'GLOBAL_PREFIX' in the address range #{id} of network #{vn_id}"
end
address = IPAddress ip
- if !ip.include? '/'
- address.prefix = 64
- end
+ address.prefix = 64 unless ip.include? '/'
when nil
if ip.nil? || ip.empty?
raise NowError.new(422), "No address range and no NETWORK_ADDRESS in the network #{vn_id}"
end
# get the mask from NETWORK_MASK network parameter, if IP not in CIDR notation already
- if !ip.include? '/'
+ unless ip.include? '/'
if mask && !mask.empty?
- if /\d+\.\d+\.\d+\.\d+/.match(mask)
+ if /\d+\.\d+\.\d+\.\d+/ =~ mask
address.netmask = mask
else
address.prefix = mask.to_i
def parse_ranges(vn_id, vn)
ar = nil
vn.each('AR_POOL/AR') do |a|
- if !ar.nil?
+ unless ar.nil?
raise NowError.new(501), "Multiple address ranges found in network #{vn_id}"
end
ar = a
vn.each('CLUSTERS/ID') do |cluster_xml|
id = cluster_xml.text
logger.debug "[parse_cluster] cluster: #{id}"
- if !cluster.nil?
+ unless cluster.nil?
raise NowError.new(501), "Multiple clusters assigned to network #{vn_id}"
end
cluster = id
id = vn.id
title = vn.name
desc = vn['SUMMARY']
- if desc.nil? || desc.empty?
- desc = nil
- end
+ desc && desc.empty? && desc = nil
vlan = vn['VLAN_ID']
- if vlan.nil? || vlan.empty?
- vlan = nil
- end
+ vlan && vlan.empty? && vlan = nil
range = parse_ranges(id, vn)
zone = parse_cluster(id, vn)
bridge: vn['BRIDGE'],
vlan: vlan,
range: range,
- zone: zone,
+ zone: zone
)
return network
end
-
end
end
###########################################################################
# Constants with paths to relevant files and defaults
###########################################################################
- CIPHER = 'aes-256-cbc'
+ CIPHER = 'aes-256-cbc'.freeze
def initialize(srv_user, srv_passwd)
@srv_user = srv_user
@srv_passwd = srv_passwd
- if srv_passwd.nil? || srv_passwd.empty?
- @key = ''
- else
- @key = ::Digest::SHA1.hexdigest(@srv_passwd)
- end
+ @key = if srv_passwd.nil? || srv_passwd.empty?
+ ''
+ else
+ ::Digest::SHA1.hexdigest(@srv_passwd)
+ end
@cipher = ::OpenSSL::Cipher::Cipher.new(CIPHER)
end
module Now
-
# Generic hash class with custom accessors and helper methods
class NowObject
-
def initialize(parameters = {})
parameters.select { |_k, v| !v.nil? }.each_pair { |k, v| instance_variable_set("@#{k}", v) }
end
value
end
end
-
end
-
end
require 'date'
module Now
-
# Network object
class Network < NowObject
# OpenNebula ID
# IP address range (writer)
def range=(new_value)
- if !valid_range?(new_value)
+ unless valid_range?(new_value)
raise NowError.new(500), 'Invalid range type'
end
@range = new_value
attr_accessor :zone
def initialize(parameters = {})
- if !parameters.key?(:id)
+ unless parameters.key?(:id)
raise NowError.new(500), 'ID required in network object'
end
if parameters.key?(:range) && !valid_range?(parameters[:range])
# @return true if the model is valid
def valid?
return false if id.nil?
- return false if !valid_range?(range)
+ return false unless valid_range?(range)
return true
end
h = {}
[:id, :title, :description, :user, :vlan, :range, :state, :zone].each do |k|
v = instance_variable_get "@#{k}"
- if !v.nil?
- h[k] = _to_hash(v)
- end
+ v.nil? || h[k] = _to_hash(v)
end
return h
value.nil? || value.is_a?(Now::Range)
end
end
-
end
require 'ipaddress'
module Now
-
# Address range
class Range < NowObject
-
# Address range in CIDR notation (reader)
attr_reader :address
# Address range in CIDR notation (writer)
def address=(new_value)
- if !valid_address?(new_value)
+ unless valid_address?(new_value)
raise NowError.new(500), 'Internal error: Invalid IP network address'
end
@address = new_value
attr_accessor :allocation
def initialize(parameters = {})
- if !parameters.key?(:address)
+ unless parameters.key?(:address)
raise NowError.new(500), 'Internal error: IP network address required'
end
- if !valid_address?(parameters[:address])
+ unless valid_address?(parameters[:address])
raise NowError.new(500), 'Internal error: Invalid IP network address'
end
super
# Check to see if the all the properties in the model are valid
# @return true if the model is valid
def valid?
- return false if !valid_address?(address)
+ return false unless valid_address?(address)
return true
end
# @return [Hash] Returns the object in the form of hash
def to_hash
h = {}
- if !address.nil?
- h[:address] = "#{address}/#{address.prefix}"
- end
- if !allocation.nil?
- h[:allocation] = allocation
- end
+ address.nil? || h[:address] = "#{address}/#{address.prefix}"
+ allocation.nil? || h[:allocation] = allocation
return h
end
def valid_address?(value)
!value.nil? && value.is_a?(IPAddress)
end
-
end
-
end
get '/'
assert_equal Now::API_VERSION, last_response.body
end
-
end
module Now
- VERSION = '0.0.1'
- API_VERSION = '0.0.0'
+ VERSION = '0.0.1'.freeze
+ API_VERSION = '0.0.0'.freeze
end