module DTK::CrdParser::BaseClass::Helper::ClassAndInstanceMixin

Public Class Methods

destructureActionComponent(actionComponent) click to toggle source
# File lib/crd_parser/base_class/helper_mixins.rb, line 10
def self.destructureActionComponent(actionComponent)
  # regex to match component: module::name[attrName].action
  regex = /(.*)::(.*)\[(.*)\].?(.*)/
  # if component on node group node, get full name first
  # i.e ec2::node[ng-1]/(node-utility::ssh-access[ubuntu])
  nodeName, actionComponent = actionComponent.match(/(.*)\/(.*)/).captures if isOnSpecificNode(actionComponent)
  compModule, compName, attributeName, action = actionComponent.match(regex).captures if actionComponent.match(regex)
  action = (action && !action.empty?) ? action.to_sym : nil
      
  if(compModule.nil? || compName.nil? || attributeName.nil?)
      raise "Could not resolve component module, name or attribute name for component: #{actionComponent}"
  end
  {
    moduleName: compModule,
    componentName: compName,
    attributeName: attributeName,
    action: action
  }
end
isOnSpecificNode(actionComponent) click to toggle source

Helper functions want to have available in all classes

# File lib/crd_parser/base_class/helper_mixins.rb, line 6
def self.isOnSpecificNode(actionComponent)
  actionComponent.include? "/"
end

Public Instance Methods

decrypt_if_encrypted(data_to_decrypt, encrypted) click to toggle source
# File lib/crd_parser/base_class/helper_mixins.rb, line 53
def decrypt_if_encrypted(data_to_decrypt, encrypted)
  return nil             if data_to_decrypt.nil?
  return data_to_decrypt unless encrypted
  #get secret to get the key
  secret = secret()
  decipher = OpenSSL::Cipher::AES256.new :CBC
  decipher.decrypt
  begin
    decoded = Base64.decode64(data_to_decrypt)
    # iv is 16byte string prepended to the encrypted value
    iv, encrypted = [decoded[0,16], decoded[16..-1]]
    decipher.key = secret[:key]
    decipher.iv  = iv
    plain_text = decipher.update(encrypted) + decipher.final
    plain_text
  rescue => exception
    Logger.new('/proc/1/fd/1').error exception.message
  end
end
fail_if_nil(value, property_name) click to toggle source
# File lib/crd_parser/base_class/helper_mixins.rb, line 30
def fail_if_nil(value, property_name)
  fail Error::Usage, "Property #{property_name} should not be nil" if value.nil?
  value
end
fail_if_not_concrete(parent) click to toggle source
# File lib/crd_parser/base_class/helper_mixins.rb, line 43
def fail_if_not_concrete(parent)
  klass = (parent.kind_of?(::Module) ? parent : parent.class)
  the_method = "The method"
  if caller.first =~ /in `(.+)'/
    method_name = $1
    the_method << " '#{method_name}'"
  end
  fail "#{the_method} is not defined in the concrete class #{klass}"
end
fail_if_not_type(object, type) click to toggle source
# File lib/crd_parser/base_class/helper_mixins.rb, line 35
def fail_if_not_type(object, type)
  if object.kind_of?(type)
    object
  else
    fail "Expected a #{type}, but given a '#{object.class}'"
  end
end
secret() click to toggle source
# File lib/crd_parser/base_class/helper_mixins.rb, line 73
def secret
  encrypt_key = ENV["ENCRYPTION_KEY"]
  iv = ENV["ENCRYPTION_IV"]
  {
    key: encrypt_key,
    iv: iv
  }
end