Project

General

Profile

Revision 9e11b4c5

Added by Thomas McKay over 7 years ago

updated to work with foreman-csv plugin

fixed roles

updated comment

View differences:

lib/hammer_cli_csv/base.rb
75 75
        JSON.parse(response.body)
76 76
      end
77 77

  
78
      url = "#{server}/api/v2/plugins"
79
      uri = URI(url)
80
      nethttp = Net::HTTP.new(uri.host, uri.port)
81
      nethttp.use_ssl = uri.scheme == 'https'
82
      nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
83
      server_status['plugins'] = nethttp.start do |http|
84
        request = Net::HTTP::Get.new uri.request_uri
85
        request.basic_auth(username, password)
86
        response = http.request(request)
87
        JSON.parse(response.body)['results']
88
      end
89

  
78 90
      server_status
79 91
    end
80 92

  
lib/hammer_cli_csv/content_hosts.rb
158 158
      end
159 159

  
160 160
      def import
161
        remote = @server_status['plugins'].detect { |plugin| plugin['name'] == 'foreman_csv' }
162
        if remote.nil?
163
          import_locally
164
        else
165
          import_remotely
166
        end
167
      end
168

  
169
      def import_remotely
170
        @api.resource(:csv).call(:import_content_hosts, {
171
            'content' => ::File.new(::File.expand_path(option_csv_file), 'rb')
172
        }, {
173
            :content_type => 'multipart/form-data',
174
            :multipart => true
175
        })
176
      end
177

  
178
      def import_locally
161 179
        @existing = {}
162 180
        @hypervisor_guests = {}
163 181

  
......
165 183
          create_content_hosts_from_csv(line)
166 184
        end
167 185

  
168
        print(_('Updating hypervisor and guest associations...')) if option_verbose? && !@hypervisor_guests.empty?
169
        @hypervisor_guests.each do |host_id, guest_ids|
170
          @api.resource(:systems).call(:update, {
171
              'id' => host_id,
172
              'guest_ids' => guest_ids
173
          })
186
        if !@hypervisor_guests.empty?
187
          print(_('Updating hypervisor and guest associations...')) if option_verbose?
188
          @hypervisor_guests.each do |host_id, guest_ids|
189
            @api.resource(:systems).call(:update, {
190
                'id' => host_id,
191
                'guest_ids' => guest_ids
192
            })
193
          end
174 194
        end
175
        puts _('done') if option_verbose? && !@hypervisor_guests.empty?
195
        puts _('done') if option_verbose?
176 196
      end
177 197

  
178 198
      def create_content_hosts_from_csv(line)
......
225 245
                    'facts' => facts(name, line),
226 246
                    'installed_products' => products(line)
227 247
                },
248
                'facts' => facts(name, line),
228 249
                'installed_products' => products(line),  # TODO: http://projects.theforeman.org/issues/9191,
229 250
                'service_level' => line[SLA]
230 251
            })['uuid']
lib/hammer_cli_csv/roles.rb
84 84
            @existing_roles[name] = role['id']
85 85
          else
86 86
            print "Updating role '#{name}'..." if option_verbose?
87
            @api.resource(:roles).call(:update, {
88
                'id' => @existing_roles[name]
89
            })
87
            # Nothing to update on the role object itself, just filters below
90 88
          end
91 89

  
92 90
          filter_id = foreman_filter(name, line[RESOURCE], search)
......
104 102
            print " updating filter #{line[RESOURCE]}..."
105 103
            @api.resource(:filters).call(:update, {
106 104
                'id' => filter_id,
107
                'search' => search,
108
                'unlimited' => search.nil? || search.empty?,
109
                'organization_ids' => organizations,
110
                'location_ids' => locations,
111
                'permission_ids' => permissions
105
                'filter' => {
106
                    'search' => search,
107
                    'unlimited' => search.nil? || search.empty?,
108
                    'organization_ids' => organizations,
109
                    'location_ids' => locations,
110
                    'permission_ids' => permissions
111
                }
112 112
            })
113 113
          end
114 114

  
test/data/content-hosts.csv
1 1
Name,Count,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions
2
host%d,1,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4GB,1,Standard,"69|Red Hat Enterprise Linux Server,79|Red Hat Enterprise Linux Server",|RH0103708|Red Hat Enterprise Linux Server Premium (8 sockets) (Up to 4 guests)
3
guest%d,1,Mega Corporation,Library,Default Organization View,"Mega Corp HQ,Accounting",Yes,host0,RHEL 6.4,x86_64,1,4GB,1,Standard,"69|Red Hat Enterprise Linux Server,79|Red Hat Enterprise Linux Server",|RH0103708|Red Hat Enterprise Linux Server Premium (8 sockets) (Up to 4 guests)
4
dhcp129-%03d.megacorp.com,255,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4GB,1,Standard,"69|Red Hat Enterprise Linux Server,79|Red Hat Enterprise Linux Server",|RH0103708|Red Hat Enterprise Linux Server Premium (8 sockets) (Up to 4 guests)
2
host%d,1,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
3
guest%d,4,Mega Corporation,Library,Default Organization View,"Mega Corp HQ,Accounting",Yes,host0,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
4
dhcp129-%03d.megacorp.com,999,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
test/data/organizations.csv
1 1
"Name","Count","Label","Description"
2 2
"Mega Corporation","1","megacorp","The number one mega company in the world!"
3
"Mega Subsidiary","1","subcorp","The number one subsidiary of the number one mega company in the world!"
4 3

  
5 4

  
test/data/roles.csv
15 15
cinthiacadieux,1,ActivationKey,name = cinthia.cadieux@megacorp.com,"view_activation_keys",Mega Corporation,
16 16
coraleeculbert,1,ActivationKey,name = coralee.culbert@megacorp.com,"view_activation_keys",Mega Corporation,
17 17
damondials,1,ActivationKey,name = damon.dials@megacorp.com,"view_activation_keys",Mega Corporation,
18
damondials,1,Organization,"name = ""Mega Corporation""","view_organizations",,
19 18
danaedrayer,1,ActivationKey,name = danae.drayer@megacorp.com,"view_activation_keys",Mega Corporation,
20 19
danettedewitt,1,ActivationKey,name = danette.dewitt@megacorp.com,"view_activation_keys",Mega Corporation,
21 20
danilodilbeck,1,ActivationKey,name = danilo.dilbeck@megacorp.com,"view_activation_keys",Mega Corporation,
test/data/subscriptions.csv
6 6
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6.3
7 7
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6.4
8 8
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (RPMs),x86_64,6.5
9
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (Kickstart),x86_64,6Server
10 9
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (Kickstart),x86_64,6.1
11 10
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (Kickstart),x86_64,6.2
12 11
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 6 Server (Kickstart),x86_64,6.3
......
15 14
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 7 Server (RPMs),x86_64,7Server
16 15
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 7 Server (RPMs),x86_64,7.0
17 16
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 7 Server (Kickstart),x86_64,7.0
18
Red Hat Enterprise Linux Server,1,Mega Corporation,,Red Hat Enterprise Linux 7 Server (Kickstart),x86_64,7Server
19 17

  

Also available in: Unified diff