Module: Morpheus::Cli::AccountsHelper

Included in:
Accounts, KeyPairs, Roles, Users
Defined in:
lib/morpheus/cli/mixins/accounts_helper.rb

Overview

Mixin for Morpheus::Cli command classes Provides common methods for fetching and printing accounts, roles, and users. The including class must establish @accounts_interface, @roles_interface, @users_interface

Instance Method Summary collapse

Instance Method Details

#find_account_by_id(id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 6

def (id)
  raise "#{self.class} has not defined @accounts_interface" if @accounts_interface.nil?
  begin
    json_response = @accounts_interface.get(id.to_i)
    return json_response['account']
  rescue RestClient::Exception => e
    if e.response.code == 404
      print_red_alert "Account not found by id #{id}"
    else
      raise e
    end
  end
end

#find_account_by_name(name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 20

def (name)
  raise "#{self.class} has not defined @accounts_interface" if @accounts_interface.nil?
  accounts = @accounts_interface.list({name: name.to_s})['accounts']
  if accounts.empty?
    print_red_alert "Account not found by name #{name}"
    return nil
  elsif accounts.size > 1
    print_red_alert "#{accounts.size} accounts found by name #{name}"
    print_accounts_table(accounts, {color: red})
    print_red_alert "Try using -A ID instead"
    print reset,"\n"
    return nil
  else
    return accounts[0]
  end
end

#find_account_from_options(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 37

def (options)
   = nil
  if options[:account_name]
     = (options[:account_name])
    exit 1 if .nil?
  elsif options[:account_id]
     = (options[:account_id])
    exit 1 if .nil?
  else
     = nil # use current account
  end
  return 
end

#find_role_by_id(account_id, id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 51

def find_role_by_id(, id)
  raise "#{self.class} has not defined @roles_interface" if @roles_interface.nil?
  begin
    json_response = @roles_interface.get(, id.to_i)
    return json_response['role']
  rescue RestClient::Exception => e
    if e.response.code == 404
      print_red_alert "Role not found by id #{id}"
    else
      raise e
    end
  end
end

#find_role_by_name(account_id, name) ⇒ Object Also known as: find_role_by_authority



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 65

def find_role_by_name(, name)
  raise "#{self.class} has not defined @roles_interface" if @roles_interface.nil?
  roles = @roles_interface.list(, {authority: name.to_s})['roles']
  if roles.empty?
    print_red_alert "Role not found by name #{name}"
    return nil
  elsif roles.size > 1
    print_red_alert "#{roles.size} roles by name #{name}"
    print_roles_table(roles, {color: red})
    print reset,"\n\n"
    return nil
  else
    return roles[0]
  end
end

#find_user_by_id(account_id, id) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 83

def find_user_by_id(, id)
  raise "#{self.class} has not defined @users_interface" if @users_interface.nil?
  begin
    json_response = @users_interface.get(, id.to_i)
    return json_response['user']
  rescue RestClient::Exception => e
    if e.response.code == 404
      print_red_alert "User not found by id #{id}"
    else
      raise e
    end
  end
end

#find_user_by_username(account_id, username) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 97

def find_user_by_username(, username)
  raise "#{self.class} has not defined @users_interface" if @users_interface.nil?
  users = @users_interface.list(, {username: username.to_s})['users']
  if users.empty?
    print_red_alert "User not found by username #{username}"
    return nil
  elsif users.size > 1
    print_red_alert "#{users.size} users by username #{username}"
    print_users_table(users, {color: red})
    print reset,"\n\n"
    return nil
  else
    return users[0]
  end
end


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 113

def print_accounts_table(accounts, opts={})
  table_color = opts[:color] || cyan
  rows = accounts.collect do ||
    status_state = nil
    if ['active']
      status_state = "#{green}ACTIVE#{table_color}"
    else
      status_state = "#{red}INACTIVE#{table_color}"
    end
    {
      id: ['id'], 
      name: ['name'], 
      description: ['description'], 
      role: ['role'] ? ['role']['authority'] : nil, 
      status: status_state,
      dateCreated: format_local_dt(['dateCreated']) 
    }
  end
  
  print table_color
  tp rows, [
    :id, 
    :name, 
    :description, 
    :role, 
    {:dateCreated => {:display_name => "Date Created"} },
    :status
  ]
  print reset
end


189
190
191
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 189

def print_green_success(msg)
  print green, bold, "\n#{msg}\n\n", reset
end


185
186
187
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 185

def print_red_alert(msg)
  print red, bold, "\n#{msg}\n\n", reset
end


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 144

def print_roles_table(roles, opts={})
  table_color = opts[:color] || cyan
  # tp roles, [
  #   'id',
  #   'name',
  #   'description',
  #   'scope',
  #   {'dateCreated' => {:display_name => "Date Created", :display_method => lambda{|it| format_local_dt(it['dateCreated']) } } }
  # ]
  rows = roles.collect do |role|
    {
      id: role['id'], 
      name: role['authority'], 
      description: role['description'], 
      scope: role['scope'], 
      owner: role['owner'] ? role['owner']['name'] : nil, 
      dateCreated: format_local_dt(role['dateCreated']) 
    }
  end
  print table_color
  tp rows, [
    :id, 
    :name, 
    :description, 
    :scope, 
    :owner, 
    {:dateCreated => {:display_name => "Date Created"} }
  ]
  print reset
end


175
176
177
178
179
180
181
182
183
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 175

def print_users_table(users, opts={})
  table_color = opts[:color] || cyan
  rows = users.collect do |user|
    {id: user['id'], username: user['username'], first: user['firstName'], last: user['lastName'], email: user['email'], role: user['role'] ? user['role']['authority'] : nil, account: user['account'] ? user['account']['name'] : nil}
  end
  print table_color
  tp rows, :id, :account, :first, :last, :username, :email, :role
  print reset
end