Project

General

Profile

Revision a6c1d857

Added by Tomas Strachota almost 5 years ago

Fixes #17624 - Compatibility with authenticators

Fixes #17624 - Compatibility with authenticators

- use existing foreman api connection for csv too
- adds compatibility with authenticators

View differences:

.gitignore
30 30
.ruby-gemset
31 31
.idea
32 32
Gemfile.local
33
Gemfile.local.rb
33 34
hammer_cli_csv*.gem
34 35

  
35 36
# Locale files
.travis.yml
4 4
  - 2.1
5 5
  - 2.2
6 6
before_install:
7
  - echo "gem 'hammer_cli_katello', :git => 'https://github.com/Katello/hammer-cli-katello.git'" > Gemfile.local
8
  - echo "gem 'hammer_cli_foreman', :git => 'https://github.com/theforeman/hammer-cli-foreman.git'" >> Gemfile.local
7 9
  - echo "gem 'hammer_cli', :git => 'https://github.com/theforeman/hammer-cli.git'" >> Gemfile.local
8 10
  - gem update bundler
Gemfile
19 19
end
20 20

  
21 21
# load local gemfile
22
local_gemfile = File.join(File.dirname(__FILE__), 'Gemfile.local')
23
self.instance_eval(Bundler.read_file(local_gemfile)) if File.exist?(local_gemfile)
22
['Gemfile.local.rb', 'Gemfile.local'].map do |file_name|
23
  local_gemfile = File.join(File.dirname(__FILE__), file_name)
24
  self.instance_eval(Bundler.read_file(local_gemfile)) if File.exist?(local_gemfile)
25
end
lib/hammer_cli_csv/base.rb
50 50

  
51 51
    def execute
52 52
      @api = api_connection
53
      @server_status = check_server_status(@server, @username, @password)
53
      @server_status = check_server_status(@server)
54 54

  
55 55
      if option_export?
56 56
        if option_file
......
68 68
      HammerCLI::EX_OK
69 69
    end
70 70

  
71
    def check_server_status(server, username, password)
71
    def authenticate_request(request)
72
      if HammerCLI.context[:api_connection]
73
        HammerCLIForeman.foreman_api_connection.authenticator.authenticate(request, {})
74
      else
75
        request.basic_auth(
76
          HammerCLIForeman.credentials.username,
77
          HammerCLIForeman.credentials.password
78
        )
79
      end
80
      request
81
    end
82

  
83
    def check_server_status(server)
72 84
      url = "#{server}/api/status"
73 85
      uri = URI(url)
74 86
      nethttp = Net::HTTP.new(uri.host, uri.port)
75 87
      nethttp.use_ssl = uri.scheme == 'https'
76 88
      nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
77 89
      server_status = nethttp.start do |http|
78
        request = Net::HTTP::Get.new uri.request_uri
79
        request.basic_auth(username, password)
90
        request = Net::HTTP::Get.new uri
91
        authenticate_request(request)
80 92
        response = http.request(request)
81 93
        JSON.parse(response.body)
82 94
      end
......
87 99
      nethttp.use_ssl = uri.scheme == 'https'
88 100
      nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
89 101
      server_status['plugins'] = nethttp.start do |http|
90
        request = Net::HTTP::Get.new uri.request_uri
91
        request.basic_auth(username, password)
102
        request = Net::HTTP::Get.new uri
103
        authenticate_request(request)
92 104
        response = http.request(request)
93 105
        JSON.parse(response.body)['results']
94 106
      end
lib/hammer_cli_csv/utils/config.rb
1 1
module HammerCLICsv
2 2
  module Utils
3 3
    module Config
4
      def credentials
5
        @credentials ||= HammerCLIForeman::BasicCredentials.new(
6
          :username => (HammerCLI::Settings.get(:_params, :username) || ENV['FOREMAN_USERNAME'] || HammerCLI::Settings.get(:foreman, :username)),
7
          :password => (HammerCLI::Settings.get(:_params, :password) || ENV['FOREMAN_PASSWORD'] || HammerCLI::Settings.get(:foreman, :password))
8
        )
9
        @credentials
10
      end
11

  
12
      def resource_config
13
        config = {}
14
        config[:uri] = HammerCLI::Settings.get(:_params, :host) || HammerCLI::Settings.get(:foreman, :host)
15
        config[:credentials] = credentials
16
        config[:logger] = Logging.logger['API']
17
        config[:api_version] = 2
18
        config[:follow_redirects] = HammerCLI::Settings.get(:foreman, :follow_redirects) || :never
19
        config[:aggressive_cache_checking] = HammerCLI::Settings.get(:foreman, :refresh_cache) || false
20
        config[:headers] = { "Accept-Language" => HammerCLI::I18n.locale }
21
        config[:language] = HammerCLI::I18n.locale
22
        config[:timeout] = HammerCLI::Settings.get(:foreman, :request_timeout)
23
        config[:timeout] = -1 if config[:timeout] && config[:timeout].to_i < 0
24
        config[:apidoc_authenticated] = false
25

  
26
        @username = config[:credentials].username
27
        @password = config[:credentials].password
28
        @server = config[:uri]
29
        config
30
      end
31

  
32 4
      def api_connection
33
        connection = HammerCLI::Connection.create(
34
          'csv',
35
          HammerCLI::Apipie::Command.resource_config.merge(resource_config),
36
          HammerCLI::Apipie::Command.connection_options
37
        )
38
        connection.api
5
        @server ||= HammerCLI::Settings.get(:_params, :host) || HammerCLI::Settings.get(:foreman, :host)
6
        HammerCLIForeman.foreman_api_connection.api
39 7
      end
40 8
    end
41 9
  end
test/csv_test_helper.rb
73 73
end
74 74

  
75 75
def set_user(username, password='changeme')
76
  HammerCLI::Connection.drop_all
77
  HammerCLIForeman.clear_credentials
76
  if HammerCLI.context[:api_connection]
77
    HammerCLI.context[:api_connection].drop_all
78
  else
79
    HammerCLI::Connection.drop_all
80
    HammerCLIForeman.clear_credentials
81
  end
78 82
  HammerCLI::Settings.load({
79 83
                               :_params => {
80 84
                                   :username => username,
test/resources/subnets_test.rb
74 74
        hammer.run(%W{--reload-cache csv subnets --verbose --continue-on-error --file #{file.path}})
75 75
      }
76 76
      lines = stderr.split("\n")
77
      assert_equal "Error: 422 Unprocessable Entity", lines[0]
77
      refute lines[0].empty?
78 78
      lines = stdout.split("\n")
79 79
      assert_equal "Creating subnet 'Bad Subnet'...Updating subnet 'Test Subnet'...done", lines[0]
80 80
      file.unlink

Also available in: Unified diff