Module: Morpheus::Cli::AccountsHelper
- Included in:
- AccountGroupsCommand, ApplianceSettingsCommand, ApprovalsCommand, Apps, BackupSettingsCommand, BlueprintsCommand, ChangePasswordCommand, Clusters, Hosts, Instances, IntegrationsCommand, JobsCommand, KeyPairs, LogSettingsCommand, PoliciesCommand, PriceSetsCommand, PricesCommand, ProvisioningSettingsCommand, RecentActivityCommand, Roles, ServicePlanCommand, TenantsCommand, UserGroupsCommand, UserSettingsCommand, UserSourcesCommand, Users, WhitelabelSettingsCommand, 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
-
#account_column_definitions ⇒ Object
Tenants (Accounts).
- #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, params = {}) ⇒ 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, params = {}) ⇒ Object
- #find_user_by_username(account_id, username, params = {}) ⇒ Object
- #find_user_by_username_or_id(account_id, val, params = {}) ⇒ 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_access_string(access, access_levels = nil, return_color = cyan) ⇒ Object
this outputs a string that matches the length of all available access levels for outputting in a grid that looks like this:.
- #format_role_type(role) ⇒ Object
-
#format_user_role_names(user) ⇒ Object
These user access formatted methods should probably move up to PrintHelper to be more ubiquitous.
- #format_user_status(user, return_color = cyan) ⇒ Object
- #get_access_color(access) ⇒ Object
- #get_access_string(access, return_color = cyan) ⇒ Object
- #list_account_column_definitions ⇒ Object
- #list_user_column_definitions ⇒ Object
- #list_user_group_column_definitions ⇒ Object
-
#role_column_definitions(options = {}) ⇒ Object
Roles.
- #roles_interface ⇒ Object
- #subtenant_role_column_definitions(options = {}) ⇒ Object
-
#user_column_definitions ⇒ Object
Users.
-
#user_group_column_definitions ⇒ Object
User Groups.
- #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
#account_column_definitions ⇒ Object
Tenants (Accounts)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 38 def account_column_definitions() { "ID" => 'id', "Name" => 'name', # "Name" => lambda {|it| it['name'].to_s + (it['master'] ? " (Master Tenant)" : '') }, "Description" => 'description', "Subdomain" => 'subdomain', "# Instances" => 'stats.instanceCount', "# Users" => 'stats.userCount', "Role" => lambda {|it| it['role']['authority'] rescue nil }, "Master" => lambda {|it| format_boolean(it['master']) }, "Currency" => 'currency', "Status" => lambda {|it| status_state = nil if it['active'] status_state = "#{green}ACTIVE#{cyan}" else status_state = "#{red}INACTIVE#{cyan}" end status_state }, "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }, } end |
#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
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 81 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 "Tenant not found by id #{id}" else raise e end end end |
#find_account_by_name(name) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 94 def find_account_by_name(name) accounts = accounts_interface.list({name: name.to_s})['accounts'] if accounts.empty? print_red_alert "Tenant not found by name #{name}" return nil elsif accounts.size > 1 print_red_alert "Found #{accounts.size} tenants by name '#{name}'. Try using ID instead: #{format_list(accounts.collect {|it| it['id']}, 'or', 3)}" print "\n" print as_pretty_table(accounts, [:id, :name, :description], {color: red, thin: true}) print reset,"\n" return nil else return accounts[0] end end |
#find_account_by_name_or_id(val) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 73 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 110 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, params = {}) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 273 def find_all_user_ids(account_id, usernames, params={}) 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, params) if username.to_s =~ /\A\d{1,}\Z/ user_ids << username.to_i else user = find_user_by_username(account_id, username, params) if user.nil? return nil else user_ids << user['id'] end end end user_ids end |
#find_role_by_id(account_id, id) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 164 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:
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 177 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 "Found #{roles.size} roles by name '#{name}'. Try using ID instead: #{format_list(roles.collect {|it| it['id']}, 'or', 3)}" print "\n" # print as_pretty_table(accounts, [:id, :name, :description], {color: red, thin: true}) print as_pretty_table(roles, {"ID" => 'id', "Name" => 'authority',"Description" => 'description'}.upcase_keys!, {color: red, thin: true}) print reset,"\n" return nil else return roles[0] end end |
#find_role_by_name_or_id(account_id, val) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 156 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, params = {}) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 244 def find_user_by_id(account_id, id, params={}) begin json_response = users_interface.get(account_id, id.to_i, params) 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, params = {}) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 257 def find_user_by_username(account_id, username, params={}) users = users_interface.list(account_id, params.merge({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 "Found #{users.size} users by username '#{username}'. Try using ID instead: #{format_list(users.collect {|it| it['id']}, 'or', 3)}" print "\n" print as_pretty_table(users, list_user_column_definitions, {color: red, thin: true}) print reset,"\n" return nil else return users[0] end end |
#find_user_by_username_or_id(account_id, val, params = {}) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 236 def find_user_by_username_or_id(account_id, val, params={}) if val.to_s =~ /\A\d{1,}\Z/ return find_user_by_id(account_id, val, params) else return find_user_by_username(account_id, val, params) end end |
#find_user_group_by_id(account_id, id) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 331 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
344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 344 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 "Found #{user_groups.size} user groups by name '#{name}'. Try using ID instead: #{format_list(user_groups.collect {|it| it['id']}, 'or', 3)}" print as_pretty_table(user_groups, [:id, :name, :description], {color: red, thin: true}) print reset,"\n" return nil else return user_groups[0] end end |
#find_user_group_by_name_or_id(account_id, val) ⇒ Object
323 324 325 326 327 328 329 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 323 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_access_string(access, access_levels = nil, return_color = cyan) ⇒ Object
this outputs a string that matches the length of all available access levels for outputting in a grid that looks like this:
none
full
full
user
read
full
none
Examples: format_permission_access(“read”)
("custom", "full,custom,none")
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 427 def format_access_string(access, access_levels=nil, return_color=cyan) access = access.to_s.downcase.strip if access.empty? access = "none" end if access_levels.nil? access_levels = ["none","read","user","full"] elsif access_levels.is_a?(Array) # access_levels = access_levels else # parse values like "full,custom,none" access_levels = [access_levels].flatten.collect {|it| it.strip.split(",") }.flatten.collect {|it| it.strip }.compact end # build padded string that contains access eg. 'full' or ' read' access_levels_string = access_levels.join(",") padded_value = "" access_levels.each do |a| # handle some unusual access values # print custom, and provision where 'user' normally is at index 1 if (access == "custom" || access == "provision") && a == "user" padded_value << access else if access == a padded_value << access else padded_value << " " * a.size end end end # no matching access was found, so just print it in one slot if padded_value == "" padded_value = " " * access_levels[0].to_s.size padded_value << access end # strip any extra whitespace off the end if padded_value.size > access_levels_string.size padded_value = padded_value.rstrip end # ok build out string out = "" access_color = get_access_color(access) out << access_color if access_color out << padded_value out << reset if access_color out << return_color if return_color return out end |
#format_role_type(role) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 359 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
These user access formatted methods should probably move up to PrintHelper to be more ubiquitous.
380 381 382 383 384 385 386 387 388 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 380 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 |
#format_user_status(user, return_color = cyan) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 222 def format_user_status(user, return_color=cyan) if user["enabled"] != true red + "DISABLED" + return_color elsif user["accountLocked"] red + "ACCOUNT LOCKED" + return_color elsif user["accountExpired"] yellow + "ACCOUNT EXPIRED" + return_color elsif user["passwordExpired"] yellow + "PASSWORD EXPIRED" + return_color else green + "ACTIVE" + return_color end end |
#get_access_color(access) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 390 def get_access_color(access) access ||= 'none' if access == 'none' # maybe reset instead of white? white elsif access == 'read' cyan else green end end |
#get_access_string(access, return_color = cyan) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 402 def get_access_string(access, return_color=cyan) get_access_color(access) + access + return_color # access ||= 'none' # if access == 'none' # "#{white}#{access.to_s}#{return_color}" # elsif access == 'read' # "#{cyan}#{access.to_s.capitalize}#{return_color}" # else # "#{green}#{access.to_s}#{return_color}" # end end |
#list_account_column_definitions ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 64 def list_account_column_definitions() columns = account_column_definitions columns.delete("Subdomain") columns.delete("Master") columns.delete("Currency") return columns.upcase_keys! end |
#list_user_column_definitions ⇒ Object
216 217 218 219 220 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 216 def list_user_column_definitions() columns = user_column_definitions columns.delete("Notifications") return columns.upcase_keys! end |
#list_user_group_column_definitions ⇒ Object
315 316 317 318 319 320 321 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 315 def list_user_group_column_definitions() columns = user_group_column_definitions columns.delete("Sudo Access") columns.delete("Server Group") columns.delete("Updated") return columns.upcase_keys! end |
#role_column_definitions(options = {}) ⇒ Object
Roles
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 129 def role_column_definitions(={}) { "ID" => 'id', "Name" => 'authority', "Description" => 'description', #"Scope" => lambda {|it| it['scope'] }, "Type" => lambda {|it| format_role_type(it) }, "Multitenant" => lambda {|it| format_boolean(it['multitenant']).to_s + (it['multitenantLocked'] ? " (LOCKED)" : "") }, "Owner" => lambda {|it| it['owner'] ? it['owner']['name'] : '' }, #"Tenant" => lambda {|it| it['account'] ? it['account']['name'] : '' }, "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) } } 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 |
#subtenant_role_column_definitions(options = {}) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 146 def subtenant_role_column_definitions(={}) { "ID" => 'id', "Name" => 'authority', "Description" => 'description', "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) } } end |
#user_column_definitions ⇒ Object
Users
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 199 def user_column_definitions() { "ID" => 'id', "Tenant" => lambda {|it| it['account'] ? it['account']['name'] : '' }, "First Name" => 'firstName', "Last Name" => 'lastName', "Username" => 'username', "Email" => 'email', "Role" => lambda {|it| format_user_role_names(it) }, "Notifications" => lambda {|it| it['receiveNotifications'].nil? ? '' : format_boolean(it['receiveNotifications']) }, "Status" => lambda {|it| format_user_status(it) }, "Last Login" => lambda {|it| format_duration_ago(it['lastLoginDate']) }, "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) }, } end |
#user_group_column_definitions ⇒ Object
User Groups
300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/morpheus/cli/mixins/accounts_helper.rb', line 300 def user_group_column_definitions() { "ID" => lambda {|it| it['id'] }, #"Account" => lambda {|it| it['account'] ? it['account']['name'] : '' }, "Name" => lambda {|it| it['name'] }, "Description" => lambda {|it| it['description'] }, "Server Group" => lambda {|it| it['serverGroup'] }, "Sudo Access" => lambda {|it| format_boolean it['sudoAccess'] }, # "Shared User" => lambda {|it| format_boolean it['sharedUser'] }, "# Users" => lambda {|it| it['users'].size rescue nil }, "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) } } 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 |