Skip to content
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
43 changes: 33 additions & 10 deletions lib/linux_admin/subscription_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ def self.refresh
run("subscription-manager refresh")
end

def self.organizations(options)
raise ArgumentError, "username and password are required" unless options[:username] && options[:password]
cmd = "subscription-manager orgs"

params = {"--username=" => options[:username], "--password=" => options[:password]}
params.merge!(proxy_params(options))
params["--serverurl="] = options[:server_url] if options[:server_url]

output = run(cmd, :params => params, :return_output => true)
parse_output(output).index_by {|i| i[:name]}
end

def self.register(options)
raise ArgumentError, "username and password are required" unless options[:username] && options[:password]
cmd = "subscription-manager register"
Expand All @@ -31,24 +43,35 @@ def self.subscribe(options)
end

def self.available_subscriptions
out = run("subscription-manager list --all --available", :return_output => true)
cmd = "subscription-manager list --all --available"
output = run(cmd, :return_output => true)
parse_output(output).index_by {|i| i[:pool_id]}
end

private

out.split("\n\n").each_with_object({}) do |subscription, subscriptions_hash|
hash = {}
subscription.each_line do |line|
# Strip the header lines if they exist
next if (line.start_with?("+---") && line.end_with?("---+\n")) || line.strip == "Available Subscriptions"
def self.parse_output(output)
# Strip the 3 line header off the top
content = output.split("\n")[3..-1].join("\n")
parse_content(content)
end

def self.parse_content(content)
# Break into content groupings by "\n\n" then process each grouping
content.split("\n\n").each_with_object([]) do |group, group_array|
group = group.split("\n").each_with_object({}) do |line, hash|
next if line.blank?
key, value = line.split(":", 2)
hash[key.strip.downcase.tr(" -", "_").to_sym] = value.strip
end
hash[:ends] = Date.strptime(hash[:ends], "%m/%d/%Y")

subscriptions_hash[hash[:pool_id]] = hash
group_array.push(format_values(group))
end
end

private
def self.format_values(content_group)
content_group[:ends] = Date.strptime(content_group[:ends], "%m/%d/%Y") if content_group[:ends]
content_group
end

def self.proxy_params(options)
config = {}
Expand Down
6 changes: 6 additions & 0 deletions spec/data/subscription_manager/output_orgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
+-------------------------------------------+
myUser Organizations
+-------------------------------------------+

Name: SomeOrg
Key: 1234567
5 changes: 5 additions & 0 deletions spec/subscription_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@
},
})
end

it ".organizations" do
described_class.should_receive(:run).once.with("subscription-manager orgs", {:params=>{"--username="=>"SomeUser", "--password="=>"SomePass", "--proxy="=>"1.2.3.4", "--proxyuser="=>"ProxyUser", "--proxypassword="=>"ProxyPass", "--serverurl="=>"192.168.1.1"}, :return_output => true}).and_return(sample_output("subscription_manager/output_orgs"))
expect(described_class.organizations({:username=>"SomeUser", :password=>"SomePass", :proxy_address=>"1.2.3.4", :proxy_username=>"ProxyUser", :proxy_password=>"ProxyPass", :server_url=>"192.168.1.1"})).to eq({"SomeOrg"=>{:name=>"SomeOrg", :key=>"1234567"}})
end
end