Project

General

Profile

Revision f4fb9c20

Added by Thomas McKay about 6 years ago

fixes #17259 - products columns for docker content

View differences:

lib/hammer_cli_csv/products.rb
13 13
      RELEASE = 'Release'
14 14
      REPOSITORY_URL = 'Repository Url'
15 15
      DESCRIPTION = 'Description'
16
      VERIFY_SSL = 'Verify SSL'
17
      UPSTREAM_USERNAME = 'Username'
18
      UPSTREAM_PASSWORD = 'Password'
19
      DOWNLOAD_POLICY = 'Download Policy'
20
      MIRROR_ON_SYNC = 'Mirror on Sync'
21
      UNPROTECTED = 'Publish via HTTP'
16 22

  
17 23
      def export(csv)
18 24
        csv << [NAME, LABEL, ORGANIZATION, DESCRIPTION, REPOSITORY, REPOSITORY_TYPE,
19
                CONTENT_SET, RELEASE, REPOSITORY_URL]
20
        # TODO: DOWNLOAD_POLICY
25
                CONTENT_SET, RELEASE, REPOSITORY_URL, VERIFY_SSL, UNPROTECTED, MIRROR_ON_SYNC, DOWNLOAD_POLICY,
26
                UPSTREAM_USERNAME, UPSTREAM_PASSWORD]
21 27
        @api.resource(:organizations).call(:index, {
22 28
            :per_page => 999999
23 29
        })['results'].each do |organization|
......
34 40
              repository = @api.resource(:repositories).call(:show, {:id => repository['id']})
35 41
              if repository['product_type'] == 'custom'
36 42
                repository_type = "Custom #{repository['content_type'].capitalize}"
37
                content_set = nil
43
                if repository['content_type'] == 'docker'
44
                  content_set = repository['docker_upstream_name']
45
                else
46
                  content_set = nil
47
                end
38 48
              else
39 49
                repository_type = "Red Hat #{repository['content_type'].capitalize}"
40 50
                content_set = get_content_set(organization, product, repository)
......
42 52
              release = repository['minor'] #=~ /Server/ ? repository['minor'] : "#{repository['major']}.#{repository['minor']}"
43 53
              csv << [product['name'], product['label'], organization['name'],
44 54
                      product['description'], repository['name'], repository_type,
45
                      content_set, release, repository['url']]
55
                      content_set, release, repository['url'],
56
                      repository['verify_ssl_on_sync'] ? 'Yes' : 'No',
57
                      repository['unprotected'] ? 'Yes' : 'No',
58
                      repository['mirror_on_sync'] ? 'Yes' : 'No', repository['download_policy'],
59
                      repository['upstream_username'],nil]
46 60
            end
47 61
          end
48 62
        end
......
71 85
      private
72 86

  
73 87
      def create_or_update_product(line, number)
74
        if !@existing_products[line[ORGANIZATION]]
75
          get_existing_products(line)
76
        end
77

  
78 88
        product_name = namify(line[NAME], number)
89
        get_existing_product(product_name, line)
79 90
        if line[REPOSITORY_TYPE] =~ /Red Hat/
80 91
          product = enable_red_hat_product(line, product_name)
81 92
        else
......
101 112
        return product
102 113
      end
103 114

  
115
      # rubocop:disable CyclomaticComplexity
104 116
      def create_or_update_repository(line, number, product)
105 117
        repository_name = namify(line[REPOSITORY], number)
106 118
        repository = get_repository(line, product['name'], product['id'], repository_name)
119

  
120
        params = {
121
            'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
122
            'name' => repository_name,
123
            'url' => line[REPOSITORY_URL]
124
        }
125
        params['verify_ssl'] = line[VERIFY_SSL] == 'Yes' ? true : false if !line[VERIFY_SSL].nil? && !line[VERIFY_SSL].empty?
126
        params['unprotected'] = line[UNPROTECTED] == 'Yes' ? true : false if !line[UNPROTECTED].nil? && !line[UNPROTECTED].empty?
127
        params['mirror_on_sync'] = line[MIRROR_ON_SYNC] == 'Yes' ? true : false if !line[MIRROR_ON_SYNC].nil? && !line[MIRROR_ON_SYNC].empty?
128
        params['download_policy'] = line[DOWNLOAD_POLICY] if !line[DOWNLOAD_POLICY].nil? && !line[DOWNLOAD_POLICY].empty?
129
        params['upstream_username'] = line[UPSTREAM_USERNAME] if !line[UPSTREAM_USERNAME].nil? && !line[UPSTREAM_USERNAME].empty?
130
        params['upstream_password'] = line[UPSTREAM_PASSWORD] if !line[UPSTREAM_PASSWORD].nil? && !line[UPSTREAM_PASSWORD].empty?
131
        if line[REPOSITORY_TYPE] == 'Custom Docker'
132
          params['docker_upstream_name'] = line[CONTENT_SET]
133
        end
134

  
107 135
        if !repository
108 136
          if line[REPOSITORY_TYPE] =~ /Red Hat/
109 137
            raise _("Red Hat product '%{product_name}' does not have repository '%{repository_name}'") %
110 138
              {:product_name => product['name'], :repository_name => repository_name}
111 139
          end
140
          params['label'] = labelize(repository_name)
141
          params['product_id'] = product['id']
142
          params['content_type'] = content_type(line[REPOSITORY_TYPE])
112 143

  
113
          if option_verbose?
114
            print _("Creating repository '%{repository_name}' in product '%{product_name}'...") %
115
              {:repository_name => repository_name, :product_name => product['name']}
116
          end
117
          # TODO: add repo label column (optional)
118
          repository = @api.resource(:repositories).call(:create, {
119
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
120
              'name' => repository_name,
121
              'label' => labelize(repository_name),
122
              'product_id' => product['id'],
123
              'url' => line[REPOSITORY_URL],
124
              'content_type' => content_type(line[REPOSITORY_TYPE])
125
          })
126
          @existing_repositories[line[ORGANIZATION] + product['name']][line[LABEL]] = repository
144
          print _("Creating repository '%{repository_name}'...") % {:repository_name => repository_name} if option_verbose?
145
          repository = @api.resource(:repositories).call(:create, params)
146
          @existing_repositories[line[ORGANIZATION] + product['name']][repository_name] = repository
127 147
        else
128
          # TODO: update url
148
          print _("Updating repository '%{repository_name}'...") % {:repository_name => repository_name} if option_verbose?
149
          params['id'] = repository['id']
150
          repository = @api.resource(:repositories).call(:update, params)
129 151
        end
130 152

  
131 153
        sync_repository(line, product['name'], repository)
132 154
      end
155
      # rubocop:enable CyclomaticComplexity
133 156

  
134
      def get_existing_products(line)
157
      def get_existing_product(product_name, line)
135 158
        @existing_products[line[ORGANIZATION]] = {}
136 159
        @api.resource(:products).call(:index, {
137 160
            'per_page' => 999999,
138 161
            'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
139
            'enabled' => true
162
            'enabled' => true,
163
            'search' => "name=\"#{product_name}\""
140 164
        })['results'].each do |product|
141 165
          @existing_products[line[ORGANIZATION]][product['name']] = product
142 166

  
......
148 172
              'library' => true
149 173
          })['results'].each do |repository|
150 174
            @existing_repositories[line[ORGANIZATION] + product['name']] ||= {}
151
            @existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id']
175
            @existing_repositories[line[ORGANIZATION] + product['name']][repository['name']] = repository
152 176
          end
153 177
        end
154 178
      end
......
235 259
          'yum'
236 260
        when /puppet/i
237 261
          'puppet'
262
        when /docker/i
263
          'docker'
238 264
        else
239 265
          raise "Unrecognized repository type '#{repository_type}'"
240 266
        end

Also available in: Unified diff