Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloud types to authentication options #13951

Merged
merged 5 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/api/authentications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions app/models/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 3 additions & 15 deletions spec/requests/api/arbitration_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions spec/requests/api/authentications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions spec/support/api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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