module AlertLogic::Resource

Common methods that we’ll mix into our resource classes

Resource Filters

Public Class Methods

filters(filters = nil) click to toggle source

Resource filters accessor

# File lib/alert_logic/resources/filters.rb, line 6
def self.filters(filters = nil)
  defaults = {
    :ok                   =>  { 'status.status'     => 'ok'       },
    :online               =>  { 'status.status'     => 'ok'       },
    :offline              =>  { 'status.status'     => 'offline'  },
    :error                =>  { 'status.status'     => 'error'    },
    :windows              =>  { 'metadata.os_type'  => 'windows'  },
    :linux                =>  { 'metadata.os_type'  => 'unix'     },
    :unix                 =>  { 'metadata.os_type'  => 'unix'     },
    :appliance_assignment =>  { 'type'  => 'appliance_assignment' },
    :all      =>  {}
  }
  filters && @filters = filters
  @filters ||= defaults
end
included(klass) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 6
def self.included(klass)
  klass.extend Resource
end
new(resource_hash) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 32
def initialize(resource_hash)
  @resource_type = self.class.to_s.downcase.split('::').last
  objectify(resource_hash)
end

Public Instance Methods

find(*params) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 21
def find(*params)
  resource_type = name.split('::').last
  options       = params.empty? ? {} : eval_filters(params)
  klass         = AlertLogic.const_get(resource_type.to_sym)
  resources     = AlertLogic \
                    .api_client \
                    .list(resource_type.downcase, options) \
                    .body
  resources.map! { |resource_hash| klass.new(resource_hash) }
end
find_by_id(id) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 10
def find_by_id(id)
  resource_type = name.split('::').last
  klass         = AlertLogic.const_get(resource_type.to_sym)
  resource      = AlertLogic \
                    .api_client \
                    .retrieve(resource_type.downcase, id) \
                    .body \
                    .first
  klass.new(resource)
end
name=(name) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 37
def name=(name)
  payload = { @resource_type => { 'name' => name } }
  AlertLogic.api_client.edit(@resource_type, id, payload)
  reload!
end
reload!() click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 52
def reload!
  objectify(
    AlertLogic \
      .api_client \
      .list(@resource_type, 'id' => id) \
      .body \
      .first
  )
end
tags=(tags) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 43
def tags=(tags)
  msg = 'Tags must be a space separated string'
  fail ClientError, msg unless tags.is_a?(String)
  tags = tags.split.map! { |tag| { 'name' => tag } }
  payload = { @resource_type => { 'tags' => tags } }
  AlertLogic.api_client.edit(@resource_type, id, payload)
  reload!
end

Private Instance Methods

def_method(name, value) click to toggle source

create and set an instance variable and define an accessor method if it’s not already there.

# File lib/alert_logic/resources/base_resource.rb, line 111
def def_method(name, value)
  instance_variable_set("@#{name}", value)
  # Don't overwrite existing methods unless it's id.  id isnt fully
  # deprecatedin 1.8.7
  (return true) if respond_to?(name.to_sym) && name != 'id'
  self.class.send(
    :define_method,
    name,
    proc { instance_variable_get("@#{name}") }
  )
end
eval_filters(params) click to toggle source
# File lib/alert_logic/resources/base_resource.rb, line 64
def eval_filters(params)
  filters = Resource.filters
  unknown_filters = []
  params = params.map do |param|
    if param.is_a?(Symbol)
      if filters.key?(param)
        filters[param]
      else
        unknown_filters << param
        {}
      end
    elsif param.is_a?(Hash)
      param
    else
      {}
    end
  end.reduce(&:merge)
  msg = "Unknown filter(s) passed: #{unknown_filters.inspect}."
  msg << " Valid filters: #{filters.keys.inspect}"
  AlertLogic.logger.warn(msg) unless unknown_filters.empty?
  params
end
objectify(hash) click to toggle source

Recursively traverse the hash and create an instance variable and accessor method for each key/value pair.

# File lib/alert_logic/resources/base_resource.rb, line 89
def objectify(hash)
  hash.each do |name, value|
    if value.is_a?(Hash)
      value = uniquify_keys(name, value)
      objectify(value)
    else
      def_method(name, value)
    end
  end
end
uniquify_keys(name, hash) click to toggle source

Some hash pairs have similar namespaces. We want to rename those to avoid a naming collision.

# File lib/alert_logic/resources/base_resource.rb, line 102
def uniquify_keys(name, hash)
  conflicts = /^config$|^appliance$|^created$|
              ^modified$|^config_policy$|^appliance_policy$/
  return hash unless name =~ conflicts
  hash.map { |k, v| { "#{name}_#{k}" => v } }.reduce(&:merge)
end