Class: LMC::Account

Inherits:
Entity show all
Defined in:
lib/lmc/Account.rb

Constant Summary collapse

ROOT_ACCOUNT_UUID =
'9ec458c2-d05f-3004-96f0-ebe73fa20de8'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#[], get_by_uuid_or_name

Constructor Details

#initialize(cloud, data) ⇒ Account

Returns a new instance of Account.



36
37
38
39
# File 'lib/lmc/Account.rb', line 36

def initialize(cloud, data)
  @cloud = cloud
  apply_data(data)
end

Instance Attribute Details

#cloudObject (readonly)

Returns the value of attribute cloud.



9
10
11
# File 'lib/lmc/Account.rb', line 9

def cloud
  @cloud
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/lmc/Account.rb', line 9

def id
  @id
end

#identifierObject (readonly)

Returns the value of attribute identifier.



9
10
11
# File 'lib/lmc/Account.rb', line 9

def identifier
  @identifier
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/lmc/Account.rb', line 8

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/lmc/Account.rb', line 9

def state
  @state
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/lmc/Account.rb', line 9

def type
  @type
end

Class Method Details

.get(id) ⇒ Object



11
12
13
14
15
# File 'lib/lmc/Account.rb', line 11

def self.get(id)
  cloud = Cloud.instance
  result = cloud.get ['cloud-service-auth', 'accounts', id.to_s]
  Account.new(cloud, result.body)
end

.get_by_name(name, type = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lmc/Account.rb', line 22

def self.get_by_name(name, type = nil)
  raise 'Missing argument' if name.nil?
  accounts = Cloud.instance.get_accounts_objects.select do |a|
    (name.nil? || a.name == name) && (type.nil? || a.type == type)
  end
  if accounts.length == 1
    accounts[0]
  elsif accounts.length == 0
    raise 'Did not find account'
  else
    raise 'Account name not unique'
  end
end

.get_by_uuid(uuid) ⇒ Object



17
18
19
20
# File 'lib/lmc/Account.rb', line 17

def self.get_by_uuid(uuid)
  raise 'Missing argument' if uuid.nil?
  get uuid
end

Instance Method Details

#authoritiesObject



119
120
121
122
123
124
125
126
# File 'lib/lmc/Account.rb', line 119

def authorities
  response = @cloud.get ['cloud-service-auth', 'accounts', id, 'authorities']
  raise 'Unable to get authorities' unless response.code == 200
  authorities = response.body.map do |r|
    Authority.new r, self
  end
  authorities
end

#authority(authority_id) ⇒ Object



112
113
114
115
116
117
# File 'lib/lmc/Account.rb', line 112

def authority(authority_id)
  response = @cloud.get(
      ['cloud-service-auth', 'accounts', id, 'authorities', authority_id]
  )
  Authority.new(response, self)
end

#childrenObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/lmc/Account.rb', line 128

def children
  @cloud.auth_for_accounts([ROOT_ACCOUNT_UUID])
  # Projects can not have children, return empty map immediately as optimization
  if type != 'PROJECT'
    response = @cloud.get ['cloud-service-auth', 'accounts', id, 'children']
    response.map { |child| Account.new @cloud, child }
  else
    []
  end
end

#config_updatestatesObject



160
161
162
163
164
# File 'lib/lmc/Account.rb', line 160

def config_updatestates
  @cloud.auth_for_accounts([id])
  response = @cloud.get ['cloud-service-config', 'configdevice', 'accounts', id, 'updatestates']
  LMC::Configstates.new response.body
end

#delete!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lmc/Account.rb', line 53

def delete!
  if @id != nil
    @cloud.auth_for_accounts [@id]
    delete_action = AuthAction.new @cloud
    delete_action.type = AuthAction::ACCOUNT_DELETE
    delete_action.name = Cloud.user
    delete_action.data = { 'password' => Cloud.password,
                          'accountId' => @id }
    delete_action.post
    @id = nil
    true
  end
end

#devicesObject



156
157
158
# File 'lib/lmc/Account.rb', line 156

def devices
  Device. self
end

#exists?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/lmc/Account.rb', line 67

def exists?
  # noch ungelöst: woran mache ich fest, ob das objekt in der DB schon da ist.
  # Bleibt wohl nix außer manuell auf @id != nil zu checken.
  @id != nil
end

#find_member_by_name(name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/lmc/Account.rb', line 86

def find_member_by_name(name)
  if name.nil?
    raise 'No member name given'
  end
  member = members.find { |m| m.name == name }
  unless member
    raise "Member named #{name} not found in account #{self}"
  end
  member
end

#logsObject

This can only show the most recent page of logs so far



140
141
142
143
144
# File 'lib/lmc/Account.rb', line 140

def logs
  cloud = Cloud.instance
  cloud.auth_for_accounts [id]
  cloud.get(['cloud-service-logging', 'accounts', id, 'logs', 'paginated?lang=DE']).body.logs
end

#membersObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lmc/Account.rb', line 73

def members
  ids = Cloud.instance.get ['cloud-service-auth', 'accounts', @id, 'members'], 'select' => 'id'
  puts ids.inspect if Cloud.debug
  # these are not actual membership objects
  memberships = ids.map do |member_id|
    response = Cloud.instance.get ['cloud-service-auth', 'accounts', @id, 'members', member_id]
    memberships = response.body
    puts memberships.inspect if Cloud.debug
    memberships
  end
  memberships
end

#remove_membership(member_id) ⇒ Object

– def update_member(principal_id, data)

response = @cloud.post ["cloud-service-auth", "accounts", id, 'members', principal_id], data
return response

end ++



104
105
106
# File 'lib/lmc/Account.rb', line 104

def remove_membership(member_id)
  @cloud.delete ['cloud-service-auth', 'accounts', id, 'members', member_id]
end

#remove_membership_selfObject



108
109
110
# File 'lib/lmc/Account.rb', line 108

def remove_membership_self
  @cloud.delete ['cloud-service-auth', 'accounts', id, 'members', 'self']
end

#saveObject

returns itself, allows chaining



42
43
44
45
46
47
48
49
50
51
# File 'lib/lmc/Account.rb', line 42

def save
  response = if @id.nil?
               @cloud.auth_for_accounts [@parent]
               @cloud.post ['cloud-service-auth', 'accounts'], self
             else
               @cloud.post path, self
             end
  apply_data(response.body)
  self
end

#sitesObject



146
147
148
149
150
151
152
153
154
# File 'lib/lmc/Account.rb', line 146

def sites
  # private clouds can not have sites
  return [] if @type == 'PRIVATE_CLOUD'
  @cloud.auth_for_accounts([id])
  response = @cloud.get ['cloud-service-devices', 'accounts', id, 'sites'], :select => :id
  response.body.map { |data|
    Site.new(UUID.new(data), self)
  }
end

#summaryObject



179
180
181
# File 'lib/lmc/Account.rb', line 179

def summary
  "\"#{@name}\" (#{@type}) ID: #{@id}"
end

#to_json(*a) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/lmc/Account.rb', line 166

def to_json(*a)
  {
      'name' => @name,
      'state' => @state,
      'type' => @type,
      'parent' => @parent
  }.to_json(*a)
end

#to_sObject



175
176
177
# File 'lib/lmc/Account.rb', line 175

def to_s
  "#{name}"
end