Module: Gzr::Session
- Included in:
- Command
- Defined in:
- lib/gzr/modules/session.rb
Instance Method Summary collapse
- #build_connection_hash(api_version = '3.0') ⇒ Object
- #login(api_version) ⇒ Object
- #logout_all ⇒ Object
- #pastel ⇒ Object
- #say_error(data, output: $stdout) ⇒ Object
- #say_ok(data, output: $stdout) ⇒ Object
- #say_warning(data, output: $stdout) ⇒ Object
- #v3_1_available? ⇒ Boolean
- #with_session(api_version = "3.0") ⇒ Object
Instance Method Details
#build_connection_hash(api_version = '3.0') ⇒ Object
53 54 55 56 57 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 |
# File 'lib/gzr/modules/session.rb', line 53 def build_connection_hash(api_version='3.0') conn_hash = Hash.new conn_hash[:api_endpoint] = "http#{@options[:ssl] ? "s" : ""}://#{@options[:host]}:#{@options[:port]}/api/#{api_version}" if [:http_proxy] conn_hash[:connection_options] ||= {} conn_hash[:connection_options][:proxy] = { :uri => [:http_proxy] } end if [:ssl] conn_hash[:connection_options] ||= {} if [:verify_ssl] then conn_hash[:connection_options][:ssl] = { :verify => true, :verify_mode => (OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT) } else conn_hash[:connection_options][:ssl] = { :verify => false } end end if [:timeout] conn_hash[:connection_options] ||= {} conn_hash[:connection_options][:request] = { :timeout => [:timeout] } end conn_hash[:user_agent] = "Gazer #{Gzr::VERSION}" if [:client_id] then conn_hash[:client_id] = [:client_id] if [:client_secret] then conn_hash[:client_secret] = [:client_secret] else reader = TTY::Reader.new @secret ||= reader.read_line("Enter your client_secret:", echo: false) conn_hash[:client_secret] = @secret end else conn_hash[:netrc] = true conn_hash[:netrc_file] = "~/.netrc" end conn_hash end |
#login(api_version) ⇒ Object
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gzr/modules/session.rb', line 98 def login(api_version) @secret = nil versions = nil current_version = nil begin conn_hash = build_connection_hash = { :links_parser => Sawyer::LinkParsers::Simple.new, :serializer => LookerSDK::Client::Serializer.new(JSON), :faraday => Faraday.new(conn_hash[:connection_options]) } endpoint = conn_hash[:api_endpoint] endpoint_uri = URI.parse(endpoint) root = endpoint.slice(0..-endpoint_uri.path.length) agent = Sawyer::Agent.new(root, ) do |http| http.headers[:accept] = 'application/json' http.headers[:user_agent] = conn_hash[:user_agent] end begin versions_response = agent.call(:get,"/versions") versions = versions_response.data.supported_versions current_version = versions_response.data.current_version rescue Faraday::SSLError => e raise Gzr::CLI::Error, "SSL Certificate could not be verified\nDo you need the --no-verify-ssl option or the --no-ssl option?" rescue Faraday::ConnectionFailed => cf raise Gzr::CLI::Error, "Connection Failed.\nDid you specify the --no-ssl option for an ssl secured server?\nYou may need to use --port=443 in some cases as well." rescue LookerSDK::NotFound => nf say_warning "endpoint #{root}/versions was not found" end versions.each do |v| @v3_1_available = true if v.version == "3.1" end end say_warning "API 3.1 available? #{v3_1_available?}" if [:debug] raise Gzr::CLI::Error, "Operation requires API v3.1, but user specified a different version" if (api_version == "3.1") && [:api_version] && !("3.1" == [:api_version]) raise Gzr::CLI::Error, "Operation requires API v3.1, which is not available from this host" if (api_version == "3.1") && !v3_1_available? conn_hash = build_connection_hash([:api_version] || current_version.version) @secret = nil say_ok("connecting to #{conn_hash.map { |k,v| "#{k}=>#{(k == :client_secret) ? '*********' : v}" }}") if [:debug] begin @sdk = LookerSDK::Client.new(conn_hash) unless @sdk say_ok "check for connectivity: #{@sdk.alive?}" if [:debug] say_ok "verify authentication: #{@sdk.authenticated?}" if [:debug] rescue LookerSDK:: => e say_error "Unauthorized - credentials are not valid" raise rescue LookerSDK::Error => e say_error "Unable to connect" say_error e. say_error e.errors if e.respond_to?(:errors) && e.errors raise end raise Gzr::CLI::Error, "Invalid credentials" unless @sdk.authenticated? if [:su] then say_ok "su to user #{@options[:su]}" if [:debug] @access_token_stack.push(@sdk.access_token) begin @sdk.access_token = @sdk.login_user([:su]).access_token say_warning "verify authentication: #{@sdk.authenticated?}" if [:debug] rescue LookerSDK::Error => e say_error "Unable to su to user #{@options[:su]}" say_error e. say_error e.errors if e.respond_to?(:errors) && e.errors raise end end @sdk end |
#logout_all ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/gzr/modules/session.rb', line 178 def logout_all pastel = Pastel.new(enabled: true) say_ok "logout" if [:debug] begin @sdk.logout rescue LookerSDK::Error => e say_error "Unable to logout" say_error e. say_error e.errors if e.respond_to?(:errors) && e.errors end if @sdk loop do token = @access_token_stack.pop break unless token say_ok "logout the parent session" if [:debug] @sdk.access_token = token begin @sdk.logout rescue LookerSDK::Error => e say_error "Unable to logout" say_error e. say_error e.errors if e.respond_to?(:errors) && e.errors end end end |
#pastel ⇒ Object
33 34 35 |
# File 'lib/gzr/modules/session.rb', line 33 def pastel @pastel ||= Pastel.new end |
#say_error(data, output: $stdout) ⇒ Object
45 46 47 |
# File 'lib/gzr/modules/session.rb', line 45 def say_error(data, output: $stdout) output.puts pastel.red data end |
#say_ok(data, output: $stdout) ⇒ Object
37 38 39 |
# File 'lib/gzr/modules/session.rb', line 37 def say_ok(data, output: $stdout) output.puts pastel.green data end |
#say_warning(data, output: $stdout) ⇒ Object
41 42 43 |
# File 'lib/gzr/modules/session.rb', line 41 def say_warning(data, output: $stdout) output.puts pastel.yellow data end |
#v3_1_available? ⇒ Boolean
49 50 51 |
# File 'lib/gzr/modules/session.rb', line 49 def v3_1_available? @v3_1_available ||= false end |
#with_session(api_version = "3.0") ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/gzr/modules/session.rb', line 203 def with_session(api_version="3.0") return nil unless block_given? begin login(api_version) unless @sdk yield rescue LookerSDK::Error => e say_error e.errors if e.respond_to?(:errors) && e.errors e.backtrace.each { |b| say_error b } if [:debug] raise Gzr::CLI::Error, e. ensure logout_all end end |