Class: Houcho::Role

Inherits:
Object
  • Object
show all
Includes:
Attribute
Defined in:
lib/houcho/role.rb

Instance Method Summary collapse

Methods included from Attribute

#attr_id, #del_attr, #get_attr, #get_attr_json, #set_attr, #set_attr!

Constructor Details

#initializeRole

Returns a new instance of Role.



13
14
15
16
17
# File 'lib/houcho/role.rb', line 13

def initialize
  @db = Houcho::Database.new.handle
  @type = "role"
  @type_id = 0
end

Instance Method Details

#create(role) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/houcho/role.rb', line 41

def create(role)
  role = [role] unless role.is_a?(Array)

  @db.transaction do
    role.each do |r|
      begin
        @db.execute("INSERT INTO role(name) VALUES(?)", r)
      rescue SQLite3::ConstraintException, "column name is not unique"
        raise RoleExistenceException, "role already exist - #{r}"
      end
    end
  end
end

#delete(role) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/houcho/role.rb', line 56

def delete(role)
  role = [role] unless role.is_a?(Array)

  @db.transaction do
    role.each do |r|
      raise RoleExistenceException, "role does not exist - #{r}" unless exist?(r)
      @db.execute("DELETE FROM role WHERE name = ?", r)
    end
  end
end

#details(role) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/houcho/role.rb', line 80

def details(role)
  role = role.is_a?(Array) ? role : [role]
  result = {}
  hostobj = Host.new
  specobj = Spec.new
  orobj = OuterRole.new

  role.each do |r|
    id = id(r)
    next if ! id
    id = id.is_a?(Array) ? id : [id]

    id.each do |i|
      hosts = hostobj.list(i)
      specs = specobj.list(i)
      outerroles = orobj.list(role_id: i)
      outerhosts = orobj.details(outerroles)

      tmp = {}
      tmp["host"] = hosts unless hosts.empty?
      tmp["spec"] = specs unless specs.empty?
      tmp["outer role"] = outerhosts unless outerhosts.empty?

      result[r] = tmp
    end
  end

  result
end

#exist?(role) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/houcho/role.rb', line 36

def exist?(role)
  !id(role).nil?
end

#id(role) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/houcho/role.rb', line 20

def id(role)
  if role.is_a?(Regexp)
    @db.execute("SELECT id, name FROM role").map do |record|
      record[0] if record[1] =~ role
    end
  else
    @db.execute("SELECT id FROM role WHERE name = ?", role).flatten.first
  end
end

#listObject



75
76
77
# File 'lib/houcho/role.rb', line 75

def list
  @db.execute("SELECT name FROM role").flatten
end

#name(id) ⇒ Object



31
32
33
# File 'lib/houcho/role.rb', line 31

def name(id)
  @db.execute("SELECT name FROM role WHERE id = ?", id).flatten.first
end

#rename(exist_role, name) ⇒ Object



68
69
70
71
72
# File 'lib/houcho/role.rb', line 68

def rename(exist_role, name)
  raise RoleExistenceException, "role does not exist - #{exist_role}" unless exist?(exist_role)
  raise RoleExistenceException, "role already exist - #{name}" if exist?(name)
  @db.execute("UPDATE role SET name = '#{name}' WHERE name = '#{exist_role}'")
end