Class: HammerCLICsv::CsvCommand::ProductsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/products.rb

Constant Summary collapse

LABEL =
'Label'
ORGANIZATION =
'Organization'
REPOSITORY =
'Repository'
REPOSITORY_TYPE =
'Repository Type'
REPOSITORY_URL =
'Repository Url'
DESCRIPTION =
'Description'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_products_from_csv(line) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hammer_cli_csv/products.rb', line 58

def create_products_from_csv(line)
  if !@existing_products[line[ORGANIZATION]]
    @existing_products[line[ORGANIZATION]] = {}
    @api.resource(:products)\
      .call(:index, {
              'per_page' => 999999,
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
              'enabled' => true
            })['results'].each do |product|
      @existing_products[line[ORGANIZATION]][product['name']] = product['id']

      @api.resource(:repositories)\
        .call(:index, {
                'page_size' => 999999, 'paged' => true,
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                'product_id' => product['id'],
                'enabled' => true,
                'library' => true
              })['results'].each do |repository|
        @existing_repositories[line[ORGANIZATION] + product['name']] ||= {}
        @existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id']
      end
    end
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    product_id = @existing_products[line[ORGANIZATION]][name]
    if product_id.nil?
      print "Creating product '#{name}'..." if option_verbose?
      if line[REPOSITORY_TYPE] =~ /Red Hat/
        raise "Red Hat product '#{name}' does not exist in '#{line[ORGANIZATION]}'"
      end

      product_id = @api.resource(:products)\
        .call(:create, {
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                'name' => name
              })['id']
      @existing_products[line[ORGANIZATION]][name] = product_id
    else
      # Nothing to update for products
      print "Updating product '#{name}'..." if option_verbose?
    end
    @existing_repositories[line[ORGANIZATION] + name] = {}
    print "done\n" if option_verbose?

    repository_name = namify(line[REPOSITORY], number)

    if !@existing_repositories[line[ORGANIZATION] + name][repository_name]
      @api.resource(:repositories)\
        .call(:index, {
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                'library' => true,
                'all' => false,
                'product_id' => product_id
              })['results'].each do |repository|
        @existing_repositories[line[ORGANIZATION] + name][repository['name']] = repository
      end
    end

    repository = @existing_repositories[line[ORGANIZATION] + name][repository_name]
    if !repository
      raise "Red Hat product '#{name}' does not have repository '#{repository_name}'" if line[REPOSITORY_TYPE] =~ /Red Hat/

      print "Creating repository '#{repository_name}' in product '#{name}'..." if option_verbose?
      repository = @api.resource(:repositories)\
        .call(:create, {
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                'name' => repository_name,
                'label' => labelize(repository_name),
                'product_id' => product_id,
                'url' => line[REPOSITORY_URL],
                'content_type' => content_type(line[REPOSITORY_TYPE])
              })
      @existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository
      puts 'done' if option_verbose?
    end

    print "Sync'ing repository '#{repository_name}' in product '#{name}'..." if option_verbose?
    if repository['sync_state'] == 'finished'
      puts 'already done' if option_verbose?
    else
      if line[REPOSITORY_TYPE] =~ /Red Hat/
        print 'skipping Red Hat repo sync... so slow!... '
      else
        sync_repository(line, repository)
      end
      print "done\n" if option_verbose?
    end
  end

rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hammer_cli_csv/products.rb', line 25

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, LABEL, ORGANIZATION, REPOSITORY, REPOSITORY_TYPE, REPOSITORY_URL]
    @api.resource(:organizations)\
      .call(:index, {
              :per_page => 999999
            })['results'].each do |organization|
      @api.resource(:products)\
        .call(:index, {
                'per_page' => 999999,
                'enabled' => true,
                'organization_id' => foreman_organization(:name => organization['name'])
              })['results'].each do |product|
        product['repositories'].each do |repository|
          repository_type = repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat'
          repository_type += " #{repository['content_type'].capitalize}"
          csv << [product['name'], 1, product['label'], organization['name'],
                  repository['name'], repository_type, repository['url']]
        end
      end
    end
  end
end

#importObject



49
50
51
52
53
54
55
56
# File 'lib/hammer_cli_csv/products.rb', line 49

def import
  @existing_products = {}
  @existing_repositories = {}

  thread_import do |line|
    create_products_from_csv(line)
  end
end