Skip to content

Commit

Permalink
repo querying - hack to enable queries with multiple groupids when us…
Browse files Browse the repository at this point in the history
…ing oauth

temporary solution until it gets fixed in pulp
  • Loading branch information
Tomas Strachota committed Oct 27, 2011
1 parent 1801569 commit b8b660a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
40 changes: 39 additions & 1 deletion src/lib/http_resource.rb
Expand Up @@ -161,8 +161,15 @@ def rest_client(http_type, method, path)
consumer = OAuth::Consumer.new(self.consumer_key,
self.consumer_secret,
params)

# TODO: this is a temporary hack!
# Python implementation of OAuth in Pulp don't support multiple query parameters with the same name.
# It uses only the first of the parameters and ignores the rest for basestring construction.
# Workaround: The basestring we sign is different from the url that we actually request from candlepin.
aouth_url = get_oauth_url(url)
# The type is passed in, GET/POST/PUT/DELETE
request = http_type.new(url)
request = http_type.new(aouth_url)

# Sign the request with OAuth
consumer.sign!(request)
# Extract the header and add it to the RestClient
Expand All @@ -188,5 +195,36 @@ def hash_to_query(query_parameters)
so_far << "#{current[0].to_s}=#{url_encode(current[1])}"
end
end

private

def get_oauth_query_hash(query)
query_hash = {}

query_parts = query.split("&")
query_parts.each do |qp|
qp_parts = qp.split("=")
key = qp_parts[0]
value = qp_parts[1]

query_hash[key] = value if not query_hash.has_key?(key)
end

query_hash
end

def get_oauth_url(url)
parts = url.split("?")
if parts.length > 1

base = parts[0]
query = parts[1]

return base +"?"+ get_oauth_query_hash(query).to_query
else
return url
end
end

end
end
33 changes: 25 additions & 8 deletions src/lib/resources/pulp.rb
Expand Up @@ -164,15 +164,11 @@ def find repo_id, yell_on_404 = false
# Get all the Repositories known by Pulp
# currently filtering against only one groupid is supported in PULP
def all groupids=nil, search_params = {}
custom = self.repository_path
if groupids
search_params = search_params.merge(:groupid => groupids.join(","))
end

custom += "?#{search_params.to_query}" unless search_params.empty?
response = get(custom , self.default_headers)
body = response.body
JSON.parse(body)
search_query = get_repo_search_query(groupids, search_params)

response = get(self.repository_path + search_query , self.default_headers)
JSON.parse(response.body)
rescue RestClientException => e
return nil if e.code.to_i == 404 && !yell_on_404
raise e
Expand Down Expand Up @@ -285,6 +281,27 @@ def distributions repo_id
JSON.parse(body)
end

private
def get_repo_search_query groupids=nil, search_params = {}
search_query = ""

if not groupids.nil?
search_query = "?_intersect=groupid&" + groupids.collect do |gid|
"groupid="+gid
end.join("&")
end

if not search_params.empty?
if search_query.length == 0
search_query = "?" + search_params.to_query
else
search_query += "&" + search_params.to_query
end
end

search_query
end

end
end

Expand Down

0 comments on commit b8b660a

Please sign in to comment.