Module: Morpheus::Cli::AccountsHelper
- Included in:
- AccountGroupsCommand, Accounts, Apps, ChangePasswordCommand, Clusters, Hosts, Instances, KeyPairs, PoliciesCommand, RecentActivityCommand, Roles, TenantsCommand, UserGroupsCommand, UserSourcesCommand, Users, Whoami
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #accounts_interface ⇒ Object
- #find_account_by_id(id) ⇒ Object
- #find_account_by_name(name) ⇒ Object
- #find_account_by_name_or_id(val) ⇒ Object
- #find_account_from_options(options) ⇒ Object
- #find_all_user_ids(account_id, usernames) ⇒ Object
- #find_role_by_id(account_id, id) ⇒ Object
- #find_role_by_name(account_id, name) ⇒ Object (also: #find_role_by_authority)
- #find_role_by_name_or_id(account_id, val) ⇒ Object
- #find_user_by_id(account_id, id) ⇒ Object
- #find_user_by_username(account_id, username) ⇒ Object
- #find_user_by_username_or_id(account_id, val) ⇒ Object
- #find_user_group_by_id(account_id, id) ⇒ Object
- #find_user_group_by_name(account_id, name) ⇒ Object
- #find_user_group_by_name_or_id(account_id, val) ⇒ Object
- #format_role_type(role) ⇒ Object
- #format_user_role_names(user) ⇒ Object
- #get_access_string(val) ⇒ Object
- #print_accounts_table(accounts, options = {}) ⇒ Object
- #print_roles_table(roles, options = {}) ⇒ Object
- #print_users_table(users, options = {}) ⇒ Object
- #roles_interface ⇒ Object
- #user_groups_interface ⇒ Object
- #users_interface ⇒ Object
Class Method Details
.included(klass) ⇒ Object
8 9 10 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 8 def self.included(klass) klass.send :include, Morpheus::Cli::PrintHelper end |
Instance Method Details
#accounts_interface ⇒ Object
12 13 14 15 16 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 12 def accounts_interface # @api_client.accounts raise "#{self.class} has not defined @accounts_interface" if @accounts_interface.nil? @accounts_interface end |
#find_account_by_id(id) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 44 def find_account_by_id(id) begin json_response = accounts_interface.get(id.to_i) return json_response['account'] rescue RestClient::Exception => e if e.response && 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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 57 def find_account_by_name(name) 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_by_name_or_id(val) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 36 def find_account_by_name_or_id(val) if val.to_s =~ /\A\d{1,}\Z/ return find_account_by_id(val) else return find_account_by_name(val) end end |
#find_account_from_options(options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 73 def () account = nil if [:account] account = find_account_by_name_or_id([:account]) exit 1 if account.nil? elsif [:account_name] account = find_account_by_name([:account_name]) exit 1 if account.nil? elsif [:account_id] account = find_account_by_id([:account_id]) exit 1 if account.nil? else account = nil # use current account end return account end |
#find_all_user_ids(account_id, usernames) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 164 def find_all_user_ids(account_id, usernames) user_ids = [] if usernames.is_a?(String) usernames = usernames.split(",").collect {|it| it.to_s.strip }.select {|it| it }.uniq else usernames = usernames.collect {|it| it.to_s.strip }.select {|it| it }.uniq end usernames.each do |username| # save a query #user = find_user_by_username_or_id(nil, username) if username.to_s =~ /\A\d{1,}\Z/ user_ids << username.to_i else user = find_user_by_username(account_id, username) if user.nil? return nil else user_ids << user['id'] end end end user_ids end |
#find_role_by_id(account_id, id) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 98 def find_role_by_id(account_id, id) begin json_response = roles_interface.get(account_id, id.to_i) return json_response['role'] rescue RestClient::Exception => e if e.response && 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:
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 111 def find_role_by_name(account_id, name) roles = roles_interface.list(account_id, {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, thin: true}) print reset,"\n\n" return nil else return roles[0] end end |
#find_role_by_name_or_id(account_id, val) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 90 def find_role_by_name_or_id(account_id, val) if val.to_s =~ /\A\d{1,}\Z/ return find_role_by_id(account_id, val) else return find_role_by_name(account_id, val) end end |
#find_user_by_id(account_id, id) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 136 def find_user_by_id(account_id, id) begin json_response = users_interface.get(account_id, id.to_i) return json_response['user'] rescue RestClient::Exception => e if e.response && 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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 149 def find_user_by_username(account_id, username) users = users_interface.list(account_id, {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, thin: true}) print reset,"\n\n" return nil else return users[0] end end |
#find_user_by_username_or_id(account_id, val) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 128 def find_user_by_username_or_id(account_id, val) if val.to_s =~ /\A\d{1,}\Z/ return find_user_by_id(account_id, val) else return find_user_by_username(account_id, val) end end |
#find_user_group_by_id(account_id, id) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 196 def find_user_group_by_id(account_id, id) begin json_response = @user_groups_interface.get(account_id, id.to_i) return json_response['userGroup'] rescue RestClient::Exception => e if e.response && e.response.code == 404 print_red_alert "User Group not found by id #{id}" else raise e end end end |
#find_user_group_by_name(account_id, name) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 209 def find_user_group_by_name(account_id, name) user_groups = @user_groups_interface.list(account_id, {name: name.to_s})['userGroups'] if user_groups.empty? print_red_alert "User Group not found by name #{name}" return nil elsif user_groups.size > 1 print_red_alert "#{user_groups.size} user groups found by name #{name}" print_user_groups_table(user_groups, {color: red}) print_red_alert "Try using ID instead" print reset,"\n" return nil else return user_groups[0] end end |
#find_user_group_by_name_or_id(account_id, val) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 188 def find_user_group_by_name_or_id(account_id, val) if val.to_s =~ /\A\d{1,}\Z/ return find_user_group_by_id(account_id, val) else return find_user_group_by_name(account_id, val) end end |
#format_role_type(role) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 255 def format_role_type(role) str = "" if role['roleType'] == "account" str = "Account" elsif role['roleType'] == "user" str = "User" else if role['scope'] == 'Account' str = "Legacy" else str = "Admin" # System Admin end end # if role['scope'] && role['filterType'] != 'Account' # str = "(System) #{str}" # end return str end |
#format_user_role_names(user) ⇒ Object
320 321 322 323 324 325 326 327 328 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 320 def format_user_role_names(user) role_names = "" if user && user['roles'] roles = user['roles'] roles = roles.sort {|a,b| a['authority'].to_s.downcase <=> b['authority'].to_s.downcase } role_names = roles.collect {|r| r['authority'] }.join(', ') end role_names end |
#get_access_string(val) ⇒ Object
330 331 332 333 334 335 336 337 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 330 def get_access_string(val) val ||= 'none' if val == 'none' "#{white}#{val.to_s.capitalize}#{cyan}" else "#{green}#{val.to_s.capitalize}#{cyan}" end end |
#print_accounts_table(accounts, options = {}) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 225 def print_accounts_table(accounts, ={}) table_color = .key?(:color) ? [:color] : cyan rows = accounts.collect do |account| status_state = nil if account['active'] status_state = "#{green}ACTIVE#{table_color}" else status_state = "#{red}INACTIVE#{table_color}" end { id: account['id'], name: account['name'], description: account['description'], role: account['role'] ? account['role']['authority'] : nil, status: status_state, dateCreated: format_local_dt(account['dateCreated']) } end print table_color if table_color print as_pretty_table(rows, [ :id, :name, :description, :role, {:dateCreated => {:display_name => "Date Created"} }, :status ], .merge({color:table_color})) print reset if table_color end |
#print_roles_table(roles, options = {}) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 274 def print_roles_table(roles, ={}) table_color = .key?(:color) ? [:color] : cyan rows = roles.collect do |role| { id: role['id'], name: role['authority'], description: role['description'], scope: role['scope'], multitenant: role['multitenant'] ? 'Yes' : 'No', type: format_role_type(role), owner: role['owner'] ? role['owner']['name'] : "System", dateCreated: format_local_dt(role['dateCreated']) } end columns = [ :id, :name, :description, # options[:is_master_account] ? :scope : nil, [:is_master_account] ? :type : nil, [:is_master_account] ? :multitenant : nil, [:is_master_account] ? :owner : nil, {:dateCreated => {:display_name => "Date Created"} } ].compact if [:include_fields] columns = [:include_fields] end # print table_color if table_color print as_pretty_table(rows, columns, ) # print reset if table_color end |
#print_users_table(users, options = {}) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 306 def print_users_table(users, ={}) table_color = [:color] || cyan rows = users.collect do |user| {id: user['id'], username: user['username'], name: user['displayName'], first: user['firstName'], last: user['lastName'], email: user['email'], role: format_user_role_names(user), account: user['account'] ? user['account']['name'] : nil} end columns = [:id, :account, :first, :last, :username, :email, :role] if [:include_fields] columns = [:include_fields] end #print table_color if table_color print as_pretty_table(rows, columns, ) #print reset if table_color end |
#roles_interface ⇒ Object
30 31 32 33 34 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 30 def roles_interface # @api_client.roles raise "#{self.class} has not defined @roles_interface" if @roles_interface.nil? @roles_interface end |
#user_groups_interface ⇒ Object
24 25 26 27 28 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 24 def user_groups_interface # @api_client.users raise "#{self.class} has not defined @user_groups_interface" if @user_groups_interface.nil? @user_groups_interface end |
#users_interface ⇒ Object
18 19 20 21 22 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 18 def users_interface # @api_client.users raise "#{self.class} has not defined @users_interface" if @users_interface.nil? @users_interface end |