hammer-cli-csv / lib / hammer_cli_csv / activation_keys.rb @ ee942928
1 |
# Copyright (c) 2013 Red Hat
|
---|---|
2 |
#
|
3 |
# MIT License
|
4 |
#
|
5 |
# Permission is hereby granted, free of charge, to any person obtaining
|
6 |
# a copy of this software and associated documentation files (the
|
7 |
# "Software"), to deal in the Software without restriction, including
|
8 |
# without limitation the rights to use, copy, modify, merge, publish,
|
9 |
# distribute, sublicense, and/or sell copies of the Software, and to
|
10 |
# permit persons to whom the Software is furnished to do so, subject to
|
11 |
# the following conditions:
|
12 |
#
|
13 |
# The above copyright notice and this permission notice shall be
|
14 |
# included in all copies or substantial portions of the Software.
|
15 |
#
|
16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20 |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21 |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23 |
#
|
24 |
#
|
25 |
# -= Activation Key CSV =-
|
26 |
#
|
27 |
# Columns
|
28 |
# Name
|
29 |
# - Activation key name
|
30 |
# - May contain '%d' which will be replaced with current iteration number of Count
|
31 |
# - eg. "group%d" -> "group1"
|
32 |
# Count
|
33 |
# - Number of times to iterate on this line of the CSV file
|
34 |
# Org Label
|
35 |
# Limit
|
36 |
# Description
|
37 |
#
|
38 |
|
39 |
require 'hammer_cli'
|
40 |
require 'katello_api'
|
41 |
require 'json'
|
42 |
require 'csv'
|
43 |
|
44 |
module HammerCLICsv |
45 |
class ActivationKeysCommand < BaseCommand |
46 |
|
47 |
def initialize(*args) |
48 |
super(args)
|
49 |
@activationkey_api = KatelloApi::Resources::ActivationKey.new(@init_options) |
50 |
@organization_api = KatelloApi::Resources::Organization.new(@init_options) |
51 |
@environment_api = KatelloApi::Resources::Environment.new(@init_options) |
52 |
@contentview_api = KatelloApi::Resources::ContentView.new(@init_options) |
53 |
end
|
54 |
|
55 |
def execute |
56 |
csv_export? ? export : import |
57 |
|
58 |
HammerCLI::EX_OK |
59 |
end
|
60 |
|
61 |
def export |
62 |
CSV.open(csv_file, 'wb') do |csv| |
63 |
csv << ['Name', 'Count', 'Org Label', 'Description', 'Limit', 'Environment', 'Content View', 'System Groups'] |
64 |
@organization_api.index[0].each do |organization| |
65 |
@activationkey_api.index({'organization_id' => organization['label']})[0].each do |activationkey| |
66 |
puts "Writing activation key '#{activationkey['name']}'"
|
67 |
csv << [activationkey['name'], 1, organization['label'], |
68 |
activationkey['description'],
|
69 |
activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : sytemgroup['usage_limit']] |
70 |
end
|
71 |
end
|
72 |
end
|
73 |
end
|
74 |
|
75 |
def import |
76 |
@existing = {}
|
77 |
|
78 |
thread_import do |line|
|
79 |
create_activationkeys_from_csv(line) |
80 |
end
|
81 |
end
|
82 |
|
83 |
def create_activationkeys_from_csv(line) |
84 |
details = parse_activationkey_csv(line) |
85 |
|
86 |
if !@existing[details[:org_label]] |
87 |
@existing[details[:org_label]] = {} |
88 |
@activationkey_api.index({'organization_id' => details[:org_label]})[0].each do |activationkey| |
89 |
@existing[details[:org_label]][activationkey['name']] = activationkey['id'] |
90 |
end
|
91 |
@environments = {}
|
92 |
@environments[details[:org_label]] = {} |
93 |
@environment_api.index({'organization_id' => details[:org_label]})[0].each do |environment| |
94 |
@environments[details[:org_label]][details[:environment]] = environment['id'] |
95 |
end
|
96 |
@contentviews = {}
|
97 |
@contentviews[details[:org_label]] = {} |
98 |
@contentview_api.index({'organization_id' => details[:org_label]})[0].each do |contentview| |
99 |
@contentviews[details[:org_label]][details[:content_view]] = contentview['id'] |
100 |
end
|
101 |
end
|
102 |
|
103 |
details[:count].times do |number| |
104 |
name = namify(details[:name_format], number)
|
105 |
if !@existing[details[:org_label]].include? name |
106 |
puts "Creating activationkey '#{name}'" if verbose? |
107 |
@activationkey_api.create({
|
108 |
'environment_id' => @environments[details[:org_label]][details[:environment]], |
109 |
'activation_key' => {
|
110 |
'name' => name,
|
111 |
'content_view_id' => details[:content_view], |
112 |
'description' => details[:description] |
113 |
} |
114 |
}) |
115 |
else
|
116 |
puts "Updating activationkey '#{name}'" if verbose? |
117 |
@activationkey_api.update({
|
118 |
'organization_id' => details[:org_label], |
119 |
'id' => @existing[details[:org_label]][name], |
120 |
'activation_key' => {
|
121 |
'name' => name,
|
122 |
'description' => details[:description] |
123 |
} |
124 |
}) |
125 |
end
|
126 |
end
|
127 |
end
|
128 |
|
129 |
def parse_activationkey_csv(line) |
130 |
keys = [:name_format, :count, :org_label, :description, :limit, :environment, :content_view, :system_groups] |
131 |
details = CSV.parse(line).map { |a| Hash[keys.zip(a)] }[0] |
132 |
|
133 |
details[:count] = details[:count].to_i |
134 |
|
135 |
details |
136 |
end
|
137 |
end
|
138 |
|
139 |
HammerCLI::MainCommand.subcommand("csv:activationkeys", "ping the katello server", HammerCLICsv::ActivationKeysCommand) |
140 |
end
|