Skip to content

Commit

Permalink
Updated the validate_vms action so we properly normalize the result a…
Browse files Browse the repository at this point in the history
…s vm references

instead of the previously returned transformation_mappings hrefs.
  • Loading branch information
abellotti committed Apr 5, 2018
1 parent 720d126 commit 431ba75
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/automate_workspaces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def decrypt(obj, data)
'value' => obj.decrypt(data['object'], data['attribute'])}
end

def normalize_attr(attr, value)
def normalize_attr(attr, value, type = nil)
return "password::********" if value.kind_of?(String) && value.start_with?("password::")
super
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ def normalize_hash(type, obj, opts = {})
attrs.each do |k|
next if Api.encrypted_attribute?(k)
next if is_ar ? !obj.respond_to?(k) : !obj.key?(k)
result[k] = normalize_attr(k, is_ar ? obj.try(k) : obj[k])
result[k] = normalize_attr(k, is_ar ? obj.try(k) : obj[k], type)
end
result
end

private

def normalize_attr(attr, value)
def normalize_attr(attr, value, type = nil)
return if value.nil?
if value.kind_of?(Array) || value.kind_of?(ActiveRecord::Relation)
normalize_array(value)
normalize_array(value, type)
elsif value.respond_to?(:attributes) || value.respond_to?(:keys)
normalize_hash(attr, value)
elsif attr == "id" || attr.to_s.ends_with?("_id")
Expand Down Expand Up @@ -128,7 +128,7 @@ def normalize_select_attributes(obj, opts)

def normalize_array(obj, type = nil)
type ||= @req.subject
obj.collect { |item| normalize_attr(get_reftype(type, type, item), item) }
obj.collect { |item| normalize_attr(get_reftype(type, type, item), item, type) }
end

def create_resource_attributes_hash(attributes, resource)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/transformation_mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create_resource(_type, _id, data = {})

def validate_vms_resource(type, id, data = {})
transformation_mapping = resource_search(id, type, collection_class(type))
transformation_mapping.validate_vms(data["import"]) || {}
normalize_hash(:vms, transformation_mapping.validate_vms(data["import"]) || {})
rescue StandardError => err
raise BadRequestError, "Could not validate vms - #{err}"
end
Expand Down
8 changes: 5 additions & 3 deletions spec/requests/transformation_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@
"action" => "validate_vms",
"import" => [
{"name" => vm.name, "uid" => vm.uid_ems},
{"name" => "bad name", "uid" => "bad uid"}
{"name" => "bad_name", "uid" => "99999"}
]
}
post(api_transformation_mapping_url(nil, transformation_mapping), :params => request)

expected = {
"valid_vms" => [a_hash_including("name" => "foo")],
"invalid_vms" => [a_hash_including("name" => "bad name")],
"valid_vms" => [a_hash_including("href" => a_string_including(api_vm_url(nil, vm)),
"name" => vm.name)],
"invalid_vms" => [a_hash_including("href" => a_string_including(api_vm_url(nil, "99999")),
"name" => "bad_name")],
"conflict_vms" => []
}
expect(response).to have_http_status(:ok)
Expand Down

0 comments on commit 431ba75

Please sign in to comment.