Module: Morpheus::Cli::OptionSourceHelper
- Included in:
- ActivityCommand, Apps, Audit, BackupsCommand, BlueprintsCommand, CatalogCommand, CatalogItemTypesCommand, CloudResourcePoolsCommand, Groups, Instances, InvoicesCommand, Processes, Projects, UsageCommand, VdiAllocationsCommand, VdiAppsCommand, VdiCommand, VdiGatewaysCommand, VdiPoolsCommand
- Defined in:
- lib/morpheus/cli/mixins/option_source_helper.rb
Overview
Mixin for Morpheus::Cli command classes Provides common methods for fetching objects via /api/options/:optionSource The including class must establish @options_interface or @api_client This is useful for when the user does not need to have permission to other endpoints
Class Method Summary collapse
Instance Method Summary collapse
- #find_available_user_option(name) ⇒ Object
- #find_cloud_option(cloud_id, refresh = false, api_params = {}) ⇒ Object
- #find_group_option(group_id, refresh = false, api_params = {}) ⇒ Object
- #find_tenant_option(tenant_id, refresh = false, api_params = {}) ⇒ Object
-
#get_available_user_options(refresh = false) ⇒ Object
todo: rewrite these specific methods to use the generic one.
- #get_cloud_options(refresh = false, api_params = {}) ⇒ Object
- #get_group_options(refresh = false, api_params = {}) ⇒ Object
- #get_tenant_options(refresh = false, api_params = {}) ⇒ Object
-
#load_option_source_data(option_source, api_params = {}, refresh = false, &block) ⇒ Object
a generic set of parse and find methods for any option source data.
- #options_interface ⇒ Object
- #parse_cloud_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
- #parse_group_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
-
#parse_option_source_id_list(option_source, id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
todo: some other common ones, accounts (tenants), etc.
-
#parse_project_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
def parse_blueprints_id_list(id_list, api_params={}, refresh=false, allow_any_id=false) parse_option_source_id_list(‘blueprints’, id_list, api_params, refresh, allow_any_id) end.
- #parse_tenant_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
- #parse_user_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
9 10 11 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 9 def self.included(klass) klass.send :include, Morpheus::Cli::PrintHelper end |
Instance Method Details
#find_available_user_option(name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 34 def find_available_user_option(name) users = ().select {|it| name && (it['name'].to_s.downcase == name.to_s.downcase || it['value'].to_s == name.to_s) } if users.empty? print_red_alert "User not found by username or id '#{name}'" return nil elsif users.size > 1 print_red_alert "#{users.size} users found by username or id '#{name}'" return nil else return users[0] end end |
#find_cloud_option(cloud_id, refresh = false, api_params = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 86 def find_cloud_option(cloud_id, refresh=false, api_params={}) if cloud_id.to_s.strip == "" print_red_alert "Cloud not found by for blank id" return nil end clouds = (refresh, api_params).select {|it| (it['name'].to_s == cloud_id.to_s || it['id'].to_s == cloud_id.to_s) } if clouds.empty? print_red_alert "Cloud not found by '#{cloud_id}'" return nil elsif clouds.size > 1 print_red_alert "#{clouds.size} clouds found by '#{cloud_id}'" return nil else return clouds[0] end end |
#find_group_option(group_id, refresh = false, api_params = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 59 def find_group_option(group_id, refresh=false, api_params={}) if group_id.to_s.strip == "" print_red_alert "Group not found by for blank id" return nil end groups = (refresh, api_params).select {|it| (it['name'].to_s == group_id.to_s || it['id'].to_s == group_id.to_s) } if groups.empty? print_red_alert "Group not found by '#{group_id}'" return nil elsif groups.size > 1 print_red_alert "#{groups.size} groups found by '#{group_id}'" return nil else return groups[0] end end |
#find_tenant_option(tenant_id, refresh = false, api_params = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 117 def find_tenant_option(tenant_id, refresh=false, api_params={}) tenant_id = tenant_id.to_s.strip tenant_id_downcase = tenant_id.to_s.downcase if tenant_id == "" print_red_alert "Tenant not found by for blank id" return nil end tenants = (refresh, api_params).select {|it| (it['name'].to_s.downcase == tenant_id_downcase || it['id'].to_s == tenant_id.to_s) } if tenants.empty? print_red_alert "Tenant not found by '#{tenant_id}'" return nil elsif tenants.size > 1 print_red_alert "#{tenants.size} tenants found by '#{tenant_id}'" return nil else return tenants[0] end end |
#get_available_user_options(refresh = false) ⇒ Object
todo: rewrite these specific methods to use the generic one.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 23 def (refresh=false) if !@available_user_options || refresh option_results = .('users',{}) @available_user_options = option_results['data'].collect {|it| {"name" => it["name"], "value" => it["value"], "username" => it["name"], "id" => it["value"]} } end return @available_user_options end |
#get_cloud_options(refresh = false, api_params = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 76 def (refresh=false, api_params={}) if !@available_cloud_options || refresh option_results = .('clouds', api_params.merge({'default' => 'false'})) @available_cloud_options = option_results['data'].collect {|it| {"name" => it["name"], "value" => it["value"], "id" => it["value"]} } end return @available_cloud_options end |
#get_group_options(refresh = false, api_params = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 49 def (refresh=false, api_params={}) if !@available_group_options || refresh option_results = .('groups', api_params) @available_group_options = option_results['data'].collect {|it| {"name" => it["name"], "value" => it["value"], "id" => it["value"]} } end return @available_group_options end |
#get_tenant_options(refresh = false, api_params = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 103 def (refresh=false, api_params={}) if !@available_tenant_options || refresh # source should be 'tenants' or 'allTenants' # allTenants includes the current tenant # I think we should always use that so you can use your own id option_source = 'allTenants' option_results = .(option_source, api_params) @available_tenant_options = option_results['data'].collect {|it| {"name" => it["name"], "value" => it["value"], "id" => it["value"]} } end return @available_tenant_options end |
#load_option_source_data(option_source, api_params = {}, refresh = false, &block) ⇒ Object
a generic set of parse and find methods for any option source data
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 159 def load_option_source_data(option_source, api_params={}, refresh=false, &block) @_option_source_cache ||= {} option_source_hash = "#{option_source}#{api_params.empty? ? '' : api_params.to_s}" data = @_option_source_cache[option_source_hash] if data.nil? || refresh json_response = .(option_source, api_params) data = json_response['data'].collect {|it| { "name" => it["name"], "value" => it["value"], "id" => (it["id"] || it["value"]), "code" => it["code"] } } @_option_source_cache[option_source_hash] = data end return data end |
#options_interface ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 13 def api_interface = @options_interface api_interface = @api_client. if api_interface.nil? && @api_client # @api_client.options raise "#{self.class} has not defined @options_interface or @api_client" if api_interface.nil? api_interface end |
#parse_cloud_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
230 231 232 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 230 def parse_cloud_id_list(id_list, api_params={}, refresh=false, allow_any_id=false) parse_option_source_id_list('clouds', id_list, api_params, refresh, allow_any_id) end |
#parse_group_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
234 235 236 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 234 def parse_group_id_list(id_list, api_params={}, refresh=false, allow_any_id=false) parse_option_source_id_list('groups', id_list, api_params, refresh, allow_any_id) end |
#parse_option_source_id_list(option_source, id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
todo: some other common ones, accounts (tenants), etc. todo: a generic set of parse and find methods like like this:
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 181 def parse_option_source_id_list(option_source, id_list, api_params={}, refresh=false, allow_any_id=false) option_source_label = option_source.to_s # .capitalize option_data = load_option_source_data(option_source, api_params, refresh) found_ids = [] parse_id_list(id_list).each {|record_id| lowercase_id = record_id.to_s.downcase # need to parameterize this behavior if record_id.to_s.empty? # never match blank nil or empty strings print_red_alert "#{option_source_label} cannot be not found by with a blank id!" return nil elsif allow_any_id && record_id.to_s =~ /\A\d{1,}\Z/ found_ids << record_id.to_i else # search with in a presedence by value, then name, then id (usually same as value) # exact match on value first. matching_results = [] matching_results = option_data.select {|it| it['value'] && it['value'] == record_id } # match on value case /i if matching_results.empty? matching_results = option_data.select {|it| it['value'] && it['value'].to_s.downcase == lowercase_id } end # match on name case /i if matching_results.empty? matching_results = option_data.select {|it| it['name'] && it['name'].to_s.downcase == lowercase_id } end # match on id too, in case it is returned and different from value? if matching_results.empty? matching_results = option_data.select {|it| it['id'] && it['id'].to_s.downcase == lowercase_id } end if matching_results.empty? print_red_alert "No #{option_source_label} found matching name or id '#{record_id}'" return nil elsif matching_results.size > 1 print_red_alert "#{matching_results.size} #{option_source_label} found matching name '#{record_id}'. Try specifying the id instead." return nil else matching_result = matching_results[0] if matching_result['value'] found_ids << matching_result['value'] else found_ids << matching_result['id'] end end end } return found_ids end |
#parse_project_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
def parse_blueprints_id_list(id_list, api_params={}, refresh=false, allow_any_id=false)
parse_option_source_id_list('blueprints', id_list, api_params, refresh, allow_any_id)
end
250 251 252 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 250 def parse_project_id_list(id_list, api_params={}, refresh=false, allow_any_id=false) parse_option_source_id_list('projects', id_list, api_params, refresh, allow_any_id) end |
#parse_tenant_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
242 243 244 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 242 def parse_tenant_id_list(id_list, api_params={}, refresh=false, allow_any_id=false) parse_option_source_id_list('allTenants', id_list, api_params, refresh, allow_any_id) end |
#parse_user_id_list(id_list, api_params = {}, refresh = false, allow_any_id = false) ⇒ Object
238 239 240 |
# File 'lib/morpheus/cli/mixins/option_source_helper.rb', line 238 def parse_user_id_list(id_list, api_params={}, refresh=false, allow_any_id=false) parse_option_source_id_list('users', id_list, api_params, refresh, allow_any_id) end |