diff --git a/app/controllers/api/authentications_controller.rb b/app/controllers/api/authentications_controller.rb index 552f8915d56..2be1ba026bb 100644 --- a/app/controllers/api/authentications_controller.rb +++ b/app/controllers/api/authentications_controller.rb @@ -9,10 +9,20 @@ def delete_resource(type, id, _data = {}) action_result(false, err.to_s) end + def options + render_options(:authentications, build_additional_fields) + end + private def authentication_ident(auth) "Authentication id:#{auth.id} name: '#{auth.name}'" end + + def build_additional_fields + { + :credential_types => ::Authentication.build_credential_options + } + end end end diff --git a/app/models/authentication.rb b/app/models/authentication.rb index 7f812635f86..096f40a45b5 100644 --- a/app/models/authentication.rb +++ b/app/models/authentication.rb @@ -50,6 +50,11 @@ def self.new(*args, &block) RETRYABLE_STATUS = %w(error unreachable).freeze + CREDENTIAL_TYPES = { + :external_credential_types => 'ManageIQ::Providers::ExternalAutomationManager::Authentication', + :embedded_ansible_credential_types => 'ManageIQ::Providers::EmbeddedAutomationManager::Authentication' + }.freeze + # FIXME: To address problem with url resolution when displayed as a quadicon, # but it's not *really* the db_name. Might be more proper to override `to_partial_path` def self.db_name @@ -105,6 +110,14 @@ def assign_values(options) self.attributes = options end + def self.build_credential_options + CREDENTIAL_TYPES.each_with_object({}) do |(k, v), hash| + hash[k] = v.constantize.descendants.each_with_object({}) do |klass, fields| + fields[klass.name] = klass::API_OPTIONS if defined? klass::API_OPTIONS + end + end + end + private def set_credentials_changed_on diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/amazon_credential.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/amazon_credential.rb index 66bf71107e7..b5ab8fbda5f 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/amazon_credential.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/amazon_credential.rb @@ -23,4 +23,10 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::AmazonCrede }.freeze API_ATTRIBUTES = COMMON_ATTRIBUTES.merge(EXTRA_ATTRIBUTES).freeze + + API_OPTIONS = { + :type => 'cloud', + :label => N_('Amazon'), + :attributes => API_ATTRIBUTES + }.freeze end diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/machine_credential.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/machine_credential.rb index 204d079fe68..bae4a799b53 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/machine_credential.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/machine_credential.rb @@ -52,4 +52,10 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::MachineCred }.freeze API_ATTRIBUTES = COMMON_ATTRIBUTES.merge(EXTRA_ATTRIBUTES).freeze + + API_OPTIONS = { + :label => N_('Machine'), + :type => 'machine', + :attributes => API_ATTRIBUTES + }.freeze end diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/scm_credential.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/scm_credential.rb index 0dc17eb3084..55b4f0e9a89 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/scm_credential.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/scm_credential.rb @@ -28,4 +28,10 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::ScmCredenti }.freeze API_ATTRIBUTES = COMMON_ATTRIBUTES.merge(EXTRA_ATTRIBUTES).freeze + + API_OPTIONS = { + :label => N_('Scm'), + :type => 'scm', + :attributes => API_ATTRIBUTES + }.freeze end diff --git a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/vmware_credential.rb b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/vmware_credential.rb index d601f0d3bec..4f61f4f3bcc 100644 --- a/app/models/manageiq/providers/ansible_tower/shared/automation_manager/vmware_credential.rb +++ b/app/models/manageiq/providers/ansible_tower/shared/automation_manager/vmware_credential.rb @@ -23,4 +23,10 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::VmwareCrede }.freeze API_ATTRIBUTES = COMMON_ATTRIBUTES.merge(EXTRA_ATTRIBUTES).freeze + + API_OPTIONS = { + :label => N_('VMware'), + :type => 'cloud', + :attributes => API_ATTRIBUTES + }.freeze end diff --git a/spec/requests/api/arbitration_rule_spec.rb b/spec/requests/api/arbitration_rule_spec.rb index a9738541e00..67aac77e9b0 100644 --- a/spec/requests/api/arbitration_rule_spec.rb +++ b/spec/requests/api/arbitration_rule_spec.rb @@ -183,22 +183,10 @@ it 'returns arbitration rule field_values' do api_basic_authorize - attributes = (ArbitrationRule.attribute_names - ArbitrationRule.virtual_attribute_names).sort.as_json - reflections = (ArbitrationRule.reflections.keys | ArbitrationRule.virtual_reflections.keys.collect(&:to_s)).sort - subcollections = Array(Api::ApiConfig.collections[:arbitration_rules].subcollections).collect(&:to_s).sort - expected = { - 'attributes' => attributes, - 'virtual_attributes' => ArbitrationRule.virtual_attribute_names.sort.as_json, - 'relationships' => reflections, - 'subcollections' => subcollections, - 'data' => { - 'field_values' => ArbitrationRule.field_values - } - } - run_options(arbitration_rules_url) - expect(response.parsed_body).to eq(expected) - expect(response.headers['Access-Control-Allow-Methods']).to include('OPTIONS') + + additional_options = { 'field_values' => ArbitrationRule.field_values } + expect_options_results(:arbitration_rules, additional_options) end end end diff --git a/spec/requests/api/authentications_spec.rb b/spec/requests/api/authentications_spec.rb index 934895fb9e4..e0afcc4cfd9 100644 --- a/spec/requests/api/authentications_spec.rb +++ b/spec/requests/api/authentications_spec.rb @@ -165,4 +165,31 @@ expect(response).to have_http_status(:forbidden) end end + + describe 'OPTIONS /api/authentications' do + it 'returns expected and additional attributes' do + api_basic_authorize + + run_options(authentications_url) + + additional_options = { + 'credential_types' => build_credential_options + } + expect_options_results(:authentications, additional_options) + end + end + + def build_credential_options + Authentication::CREDENTIAL_TYPES.each_with_object({}) do |(description, klass), hash| + hash[description] = klass.constantize.descendants.each_with_object({}) do |subklass, fields| + next unless defined? subklass::API_OPTIONS + subklass::API_OPTIONS.tap do |options| + options[:attributes].each do |_k, val| + val[:type] = val[:type].to_s if val[:type] + end + fields[subklass.name] = options + end + end + end.deep_stringify_keys + end end diff --git a/spec/support/api_helper.rb b/spec/support/api_helper.rb index 4082fa19401..0c96c87cec0 100644 --- a/spec/support/api_helper.rb +++ b/spec/support/api_helper.rb @@ -266,6 +266,30 @@ def expect_tagging_result(tag_results) ) end end + + def expect_options_results(type, data = {}) + klass = Api::ApiConfig.collections[type].klass.constantize + attributes = select_attributes(klass.attribute_names - klass.virtual_attribute_names) + reflections = (klass.reflections.keys | klass.virtual_reflections.keys.collect(&:to_s)).sort + subcollections = Array(Api::ApiConfig.collections[type].subcollections).collect(&:to_s).sort + expected = { + 'attributes' => attributes, + 'virtual_attributes' => select_attributes(klass.virtual_attribute_names), + 'relationships' => reflections, + 'subcollections' => subcollections, + 'data' => data + } + expect(response.parsed_body).to eq(expected) + expect(response.headers['Access-Control-Allow-Methods']).to include('OPTIONS') + end + + def select_attributes(attrlist) + attrlist.sort.select { |attr| !encrypted_attribute?(attr) } + end + + def encrypted_attribute?(attr) + Api::Environment.normalized_attributes[:encrypted].key?(attr.to_s) || attr.to_s.include?('password') + end end end end