Method: Gzr::Session#login

Defined in:
lib/gzr/modules/session.rb

#login(min_api_version = "4.0") ⇒ Object

Raises:



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/gzr/modules/session.rb', line 105

def (min_api_version="4.0")
  if (@options[:client_id].nil? && ENV["LOOKERSDK_CLIENT_ID"])
    @options[:client_id] = ENV["LOOKERSDK_CLIENT_ID"]
  end

  if (@options[:client_secret].nil? && ENV["LOOKERSDK_CLIENT_SECRET"])
    @options[:client_secret] = ENV["LOOKERSDK_CLIENT_SECRET"]
  end

  if (@options[:verify_ssl] && ENV["LOOKERSDK_VERIFY_SSL"])
    @options[:verify_ssl] = !(/^f(alse)?$/i =~ ENV["LOOKERSDK_VERIFY_SSL"])
  end

  if ((@options[:host] == 'localhost') && ENV["LOOKERSDK_BASE_URL"])
    base_url = ENV["LOOKERSDK_BASE_URL"]
    @options[:ssl] = !!(/^https/ =~ base_url)
    @options[:host] = /^https?:\/\/([^:\/]+)/.match(base_url)[1]
    md = /:([0-9]+)\/?$/.match(base_url)
    @options[:port] = md[1] if md
  end

  say_ok("using options #{@options.select { |k,v| k != 'client_secret' }.map { |k,v| "#{k}=>#{v}" }}") if @options[:debug]

  @secret = nil
  begin
    conn_hash = build_connection_hash

    sawyer_options = {
      :links_parser => Sawyer::LinkParsers::Simple.new,
      :serializer  => LookerSDK::Client::Serializer.new(JSON),
      :faraday => Faraday.new(conn_hash[:connection_options]) do |conn|
        conn.use LookerSDK::Response::RaiseError
        if @options[:persistent]
          conn.adapter :net_http_persistent
        end
      end
    }

    endpoint = conn_hash[:api_endpoint]
    endpoint_uri = URI.parse(endpoint)
    root = endpoint.slice(0..-endpoint_uri.path.length)

    agent = Sawyer::Agent.new(root, sawyer_options) 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.map {|v| v.version}
      @current_version = versions_response.data.current_version.version || "4.0"
    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
  end

  say_warning "API current_version #{@current_version}" if @options[:debug]
  say_warning "Supported API versions #{@versions}" if @options[:debug]

  api_version = [min_api_version, @current_version].max
  raise Gzr::CLI::Error, "Operation requires API v#{api_version}, which is not available from this host" if api_version && !@versions.any? {|v| v == api_version}

  conn_hash = build_connection_hash(api_version)
  @secret = nil

  say_ok("connecting to #{conn_hash.map { |k,v| "#{k}=>#{(k == :client_secret) ? '*********' : v}" }}") if @options[:debug]

  begin
    faraday = Faraday.new(conn_hash[:connection_options]) do |conn|
      conn.use LookerSDK::Response::RaiseError
      if @options[:persistent]
        conn.adapter :net_http_persistent
      end
    end
    @sdk = LookerSDK::Client.new(conn_hash.merge(faraday: faraday)) unless @sdk

    say_ok "check for connectivity: #{@sdk.alive?}" if @options[:debug]
    say_ok "verify authentication: #{@sdk.authenticated?}" if @options[:debug]
  rescue LookerSDK::Unauthorized => 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 @options[:su] then
    say_ok "su to user #{@options[:su]}" if @options[:debug]
    @access_token_stack.push(@sdk.access_token)
    begin
      @sdk.access_token = @sdk.(@options[:su]).access_token
      say_warning "verify authentication: #{@sdk.authenticated?}" if @options[: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