Project

General

Profile

Revision bfc065ce

Added by Thomas McKay over 8 years ago

roles-test - wip

roles-test - users now get roles, orgs, and locations

View differences:

lib/hammer_cli_csv/base.rb
82 82
      @f_permission_api ||= ForemanApi::Resources::Permission.new(@init_options)
83 83
      @f_partitiontable_api ||= ForemanApi::Resources::Ptable.new(@init_options)
84 84
      @f_puppetfacts_api ||= ForemanApi::Resources::FactValue.new(@init_options)
85
      @f_report_api ||= ForemanApi::Resources::Report.new(@init_options)
85 86
      @f_role_api ||= ForemanApi::Resources::Role.new(@init_options)
86 87
      @f_user_api ||= ForemanApi::Resources::User.new(@init_options)
87 88

  
......
89 90
      HammerCLI::EX_OK
90 91
    end
91 92

  
92
    def get_lines(filename)
93
      file = File.open(filename ,'r')
94
      contents = file.readlines
95
      file.close
96
      contents
97
    end
98

  
99 93
    def namify(name_format, number=0)
100 94
      if name_format.index('%')
101 95
        name = name_format % number
......
144 138
        return nil if options[:name].nil? || options[:name].empty?
145 139
        options[:id] = @organizations[options[:name]]
146 140
        if !options[:id]
147
          organization = @f_organization_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
141
          organization = @f_organization_api.index({'search' => "title=\"#{options[:name]}\""})[0]['results']
148 142
          raise RuntimeError, "Organization '#{options[:name]}' not found" if !organization || organization.empty?
149 143
          options[:id] = organization[0]['id']
150 144
          @organizations[options[:name]] = options[:id]
......
165 159
      result
166 160
    end
167 161

  
162
    def katello_organization(options={})
163
      @organizations ||= {}
164

  
165
      if options[:name]
166
        return nil if options[:name].nil? || options[:name].empty?
167
        options[:id] = @organizations[options[:name]]
168
        if !options[:id]
169
          organization = @k_organization_api.index({'search' => "title=\"#{options[:name]}\""})[0]['results']
170
          raise RuntimeError, "Organization '#{options[:name]}' not found" if !organization || organization.empty?
171
          options[:id] = organization[0]['label']
172
          @organizations[options[:name]] = options[:id]
173
        end
174
        result = options[:id]
175
      else
176
        return nil if options[:id].nil?
177
        options[:name] = @organizations.key(options[:id])
178
        if !options[:name]
179
          organization = @k_organization_api.show({'id' => options[:id]})[0]
180
          raise "Organization 'id=#{options[:id]}' not found" if !organization || organization.empty?
181
          options[:name] = organization['name']
182
          @organizations[options[:name]] = options[:id]
183
        end
184
        result = options[:name]
185
      end
186

  
187
      result
188
    end
189

  
168 190
    def foreman_location(options={})
169 191
      @locations ||= {}
170 192

  
......
193 215
      result
194 216
    end
195 217

  
218
    def foreman_role(options={})
219
      @roles ||= {}
220

  
221
      if options[:name]
222
        return nil if options[:name].nil? || options[:name].empty?
223
        options[:id] = @roles[options[:name]]
224
        if !options[:id]
225
          role = @f_role_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
226
          raise RuntimeError, "Role '#{options[:name]}' not found" if !role || role.empty?
227
          options[:id] = role[0]['id']
228
          @roles[options[:name]] = options[:id]
229
        end
230
        result = options[:id]
231
      else
232
        return nil if options[:id].nil?
233
        options[:name] = @roles.key(options[:id])
234
        if !options[:name]
235
          role = @f_role_api.show({'id' => options[:id]})[0]
236
          raise "Role 'id=#{options[:id]}' not found" if !role || role.empty?
237
          options[:name] = role['name']
238
          @roles[options[:name]] = options[:id]
239
        end
240
        result = options[:name]
241
      end
242

  
243
      result
244
    end
245

  
196 246
    def foreman_permission(options={})
197 247
      @permissions ||= {}
198 248

  
......
406 456
        return nil if options[:name].nil? || options[:name].empty?
407 457
        options[:id] = @environments[organization][options[:name]]
408 458
        if !options[:id]
409
          @k_environment_api.index({'organization_id' => organization})[0]['results'].each do |environment|
459
          @k_environment_api.index({'organization_id' => katello_organization(:name => organization)})[0]['results'].each do |environment|
410 460
            @environments[organization][environment['name']] = environment['id']
411 461
          end
412 462
          options[:id] = @environments[organization][options[:name]]
......
436 486
        return nil if options[:name].nil? || options[:name].empty?
437 487
        options[:id] = @contentviews[organization][options[:name]]
438 488
        if !options[:id]
439
          @k_contentview_api.index({'organization_id' => organization})[0]['results'].each do |contentview|
489
          @k_contentview_api.index({'organization_id' => katello_organization(:name => organization)})[0]['results'].each do |contentview|
440 490
            @contentviews[organization][contentview['name']] = contentview['id']
441 491
          end
442 492
          options[:id] = @contentviews[organization][options[:name]]
......
467 517
        options[:id] = @subscriptions[organization][options[:name]]
468 518
        if !options[:id]
469 519
          results = @k_subscription_api.index({
470
                                                'organization_id' => organization,
520
                                                'organization_id' => katello_organization(:name => organization),
471 521
                                                'search' => "name:\"#{options[:name]}\""
472 522
                                              })[0]
473 523
          raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
......
502 552
        options[:id] = @systemgroups[organization][options[:name]]
503 553
        if !options[:id]
504 554
          @k_systemgroup_api.index({
505
                                     'organization_id' => organization,
555
                                     'organization_id' => katello_organization(:name => organization),
506 556
                                     'search' => "name:\"#{options[:name]}\""
507 557
                                   })[0]['results'].each do |systemgroup|
508 558
            @systemgroups[organization][systemgroup['name']] = systemgroup['id'] if systemgroup
......
532 582
      name
533 583
    end
534 584

  
585
    # "Red Hat 6.4" => "Red Hat", "6", "4"
586
    # "Red Hat 6"   => "Red Hat", "6", ""
535 587
    def split_os_name(name)
536
      (name, major, minor) = name.split(' ').collect {|s| s.split('.')}.flatten
588
      tokens = name.split(' ')
589
      is_number = Float(tokens[-1]) rescue false
590
      if is_number
591
        (major, minor) = tokens[-1].split('.').flatten
592
        name = tokens[0...-1].join(' ')
593
      else
594
        name = tokens.join(' ')
595
      end
537 596
      [name, major || "", minor || ""]
538 597
    end
539 598
  end

Also available in: Unified diff