Module: AlertLogic::Resource

Includes:
Utils
Included in:
Appliance, Policy, ProtectedHost
Defined in:
lib/alert_logic/resources/filters.rb,
lib/alert_logic/resources/base_resource.rb

Overview

Common methods that we’ll mix into our resource classes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filters(filters = nil) ⇒ Object

Resource filters accessor



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



6
7
8
# File 'lib/alert_logic/resources/base_resource.rb', line 6

def self.included(klass)
  klass.extend Resource
end

Instance Method Details

#find(*params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# 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

#initialize(resource_hash) ⇒ Object



32
33
34
35
# 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

#name=(name) ⇒ Object



37
38
39
40
41
# 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!Object



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



43
44
45
46
47
48
49
50
# 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