gazer 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/lib/gzr/cli.rb +8 -0
- data/lib/gzr/command.rb +9 -0
- data/lib/gzr/commands/connection/cat.rb +66 -0
- data/lib/gzr/commands/connection/import.rb +78 -0
- data/lib/gzr/commands/connection/rm.rb +50 -0
- data/lib/gzr/commands/connection/test.rb +69 -0
- data/lib/gzr/commands/connection.rb +67 -1
- data/lib/gzr/commands/project/cat.rb +58 -0
- data/lib/gzr/commands/project/deploy_key.rb +60 -0
- data/lib/gzr/commands/project/import.rb +68 -0
- data/lib/gzr/commands/project/ls.rb +73 -0
- data/lib/gzr/commands/project/update.rb +69 -0
- data/lib/gzr/commands/project.rb +104 -0
- data/lib/gzr/commands/session/get.rb +47 -0
- data/lib/gzr/commands/session/login.rb +50 -0
- data/lib/gzr/commands/session/logout.rb +47 -0
- data/lib/gzr/commands/session/update.rb +51 -0
- data/lib/gzr/commands/session.rb +76 -0
- data/lib/gzr/modules/connection.rb +80 -0
- data/lib/gzr/modules/project.rb +106 -0
- data/lib/gzr/modules/session.rb +81 -7
- data/lib/gzr/version.rb +1 -1
- metadata +18 -2
@@ -0,0 +1,106 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
# Copyright (c) 2023 Mike DeAngelo Google, Inc.
|
4
|
+
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
# this software and associated documentation files (the "Software"), to deal in
|
7
|
+
# the Software without restriction, including without limitation the rights to
|
8
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
# subject to the following conditions:
|
11
|
+
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
# frozen_string_literal: true
|
23
|
+
|
24
|
+
module Gzr
|
25
|
+
module Project
|
26
|
+
|
27
|
+
def all_projects(fields: 'id')
|
28
|
+
data = []
|
29
|
+
begin
|
30
|
+
data = @sdk.all_projects({ fields: fields })
|
31
|
+
rescue LookerSDK::NotFound => e
|
32
|
+
return []
|
33
|
+
rescue LookerSDK::Error => e
|
34
|
+
say_error "Error querying all_projects(#{fields})"
|
35
|
+
say_error e
|
36
|
+
raise
|
37
|
+
end
|
38
|
+
data
|
39
|
+
end
|
40
|
+
|
41
|
+
def cat_project(project_id)
|
42
|
+
begin
|
43
|
+
return @sdk.project(project_id)&.to_attrs
|
44
|
+
rescue LookerSDK::NotFound => e
|
45
|
+
return nil
|
46
|
+
rescue LookerSDK::Error => e
|
47
|
+
say_error "Error getting project(#{project_id})"
|
48
|
+
say_error e
|
49
|
+
raise
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def trim_project(data)
|
54
|
+
data.select do |k,v|
|
55
|
+
keys_to_keep('create_project').include? k
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_project(body)
|
60
|
+
begin
|
61
|
+
return @sdk.create_project(body)&.to_attrs
|
62
|
+
rescue LookerSDK::Error => e
|
63
|
+
say_error "Error running create_project(#{JSON.pretty_generate(body)})"
|
64
|
+
say_error e
|
65
|
+
raise
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def update_project(id,body)
|
70
|
+
begin
|
71
|
+
return @sdk.update_project(id,body)&.to_attrs
|
72
|
+
rescue LookerSDK::NotFound => e
|
73
|
+
return nil
|
74
|
+
rescue LookerSDK::Error => e
|
75
|
+
say_error "Error running update_project(#{id},#{JSON.pretty_generate(body)})"
|
76
|
+
say_error e
|
77
|
+
raise
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def git_deploy_key(id)
|
82
|
+
begin
|
83
|
+
return @sdk.git_deploy_key(id)
|
84
|
+
rescue LookerSDK::NotFound => e
|
85
|
+
return nil
|
86
|
+
rescue LookerSDK::Error => e
|
87
|
+
say_error "Error running git_deploy_key(#{id})"
|
88
|
+
say_error e
|
89
|
+
raise
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def create_git_deploy_key(id)
|
94
|
+
begin
|
95
|
+
return @sdk.create_git_deploy_key(id)
|
96
|
+
rescue LookerSDK::NotFound => e
|
97
|
+
return nil
|
98
|
+
rescue LookerSDK::Error => e
|
99
|
+
say_error "Error running create_git_deploy_key(#{id})"
|
100
|
+
say_error e
|
101
|
+
raise
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
data/lib/gzr/modules/session.rb
CHANGED
@@ -55,6 +55,38 @@ module Gzr
|
|
55
55
|
!versions.drop_while {|v| v < minimum_version}.reverse.drop_while {|v| v > given_version}.empty?
|
56
56
|
end
|
57
57
|
|
58
|
+
def token_file
|
59
|
+
"#{ENV["HOME"]}/.gzr_auth"
|
60
|
+
end
|
61
|
+
|
62
|
+
def read_token_data
|
63
|
+
return nil unless File.exist?(token_file)
|
64
|
+
s = File.stat(token_file)
|
65
|
+
if !(s.mode.to_s(8)[3..5] == "600")
|
66
|
+
say_error "#{token_file} mode is #{s.mode.to_s(8)[3..5]}. It must be 600. Ignoring."
|
67
|
+
return nil
|
68
|
+
end
|
69
|
+
token_data = nil
|
70
|
+
file = nil
|
71
|
+
begin
|
72
|
+
file = File.open(token_file)
|
73
|
+
token_data = JSON.parse(file.read,{:symbolize_names => true})
|
74
|
+
ensure
|
75
|
+
file.close if file
|
76
|
+
end
|
77
|
+
token_data
|
78
|
+
end
|
79
|
+
|
80
|
+
def write_token_data(token_data)
|
81
|
+
file = nil
|
82
|
+
begin
|
83
|
+
file = File.new(token_file, "wt")
|
84
|
+
file.chmod(0600)
|
85
|
+
file.write JSON.pretty_generate(token_data)
|
86
|
+
ensure
|
87
|
+
file.close if file
|
88
|
+
end
|
89
|
+
end
|
58
90
|
|
59
91
|
def build_connection_hash(api_version=nil)
|
60
92
|
conn_hash = Hash.new
|
@@ -86,6 +118,9 @@ module Gzr
|
|
86
118
|
}
|
87
119
|
end
|
88
120
|
conn_hash[:user_agent] = "Gazer #{Gzr::VERSION}"
|
121
|
+
|
122
|
+
return conn_hash if @options[:token] || @options[:token_file]
|
123
|
+
|
89
124
|
if @options[:client_id] then
|
90
125
|
conn_hash[:client_id] = @options[:client_id]
|
91
126
|
if @options[:client_secret] then
|
@@ -103,12 +138,14 @@ module Gzr
|
|
103
138
|
end
|
104
139
|
|
105
140
|
def login(min_api_version="4.0")
|
106
|
-
if
|
107
|
-
@options[:client_id]
|
108
|
-
|
141
|
+
if !@options[:token] && !@options[:token_file]
|
142
|
+
if (@options[:client_id].nil? && ENV["LOOKERSDK_CLIENT_ID"])
|
143
|
+
@options[:client_id] = ENV["LOOKERSDK_CLIENT_ID"]
|
144
|
+
end
|
109
145
|
|
110
|
-
|
111
|
-
|
146
|
+
if (@options[:client_secret].nil? && ENV["LOOKERSDK_CLIENT_SECRET"])
|
147
|
+
@options[:client_secret] = ENV["LOOKERSDK_CLIENT_SECRET"]
|
148
|
+
end
|
112
149
|
end
|
113
150
|
|
114
151
|
if (@options[:verify_ssl] && ENV["LOOKERSDK_VERIFY_SSL"])
|
@@ -183,6 +220,31 @@ module Gzr
|
|
183
220
|
@sdk = LookerSDK::Client.new(conn_hash.merge(faraday: faraday)) unless @sdk
|
184
221
|
|
185
222
|
say_ok "check for connectivity: #{@sdk.alive?}" if @options[:debug]
|
223
|
+
if @options[:token_file]
|
224
|
+
entry = read_token_data&.fetch(@options[:host].to_sym,nil)&.fetch(@options[:su]&.to_sym || :default,nil)
|
225
|
+
if entry.nil?
|
226
|
+
say_error "No token found for host #{@options[:host]} and user #{@options[:su] || "default"}"
|
227
|
+
say_error "login with `gzr session login --host #{@options[:host]}` to set a token"
|
228
|
+
raise LookerSDK::Unauthorized.new
|
229
|
+
end
|
230
|
+
(day, time, tz) = entry[:expiration].split(' ')
|
231
|
+
day_parts = day.split('-')
|
232
|
+
time_parts = time.split(':')
|
233
|
+
date_time_parts = day_parts + time_parts + [tz]
|
234
|
+
expiration = Time.new(*date_time_parts)
|
235
|
+
if expiration < (Time.now + 300)
|
236
|
+
if expiration < Time.now
|
237
|
+
say_error "token expired at #{expiration}"
|
238
|
+
else
|
239
|
+
say_error "token expires at #{expiration}, which is in the next 5 minutes"
|
240
|
+
end
|
241
|
+
say_error "login again with `gzr session login --host #{@options[:host]}`"
|
242
|
+
raise LookerSDK::Unauthorized.new
|
243
|
+
end
|
244
|
+
@sdk.access_token = entry[:token]
|
245
|
+
elsif @options[:token]
|
246
|
+
@sdk.access_token = @options[:token]
|
247
|
+
end
|
186
248
|
say_ok "verify authentication: #{@sdk.authenticated?}" if @options[:debug]
|
187
249
|
rescue LookerSDK::Unauthorized => e
|
188
250
|
say_error "Unauthorized - credentials are not valid"
|
@@ -196,7 +258,7 @@ module Gzr
|
|
196
258
|
raise Gzr::CLI::Error, "Invalid credentials" unless @sdk.authenticated?
|
197
259
|
|
198
260
|
|
199
|
-
if @options[:su] then
|
261
|
+
if @options[:su] && !(@options[:token] || @options[:token_file])then
|
200
262
|
say_ok "su to user #{@options[:su]}" if @options[:debug]
|
201
263
|
@access_token_stack.push(@sdk.access_token)
|
202
264
|
begin
|
@@ -247,8 +309,20 @@ module Gzr
|
|
247
309
|
e.backtrace.each { |b| say_error b } if @options[:debug]
|
248
310
|
raise Gzr::CLI::Error, e.message
|
249
311
|
ensure
|
250
|
-
logout_all
|
312
|
+
logout_all unless @options[:token] || @options[:token_file]
|
251
313
|
end
|
252
314
|
end
|
315
|
+
|
316
|
+
def update_auth(workspace_id)
|
317
|
+
body = {}
|
318
|
+
body[:workspace_id] = workspace_id
|
319
|
+
begin
|
320
|
+
return @sdk.update_session(body)&.to_attrs
|
321
|
+
rescue LookerSDK::Error => e
|
322
|
+
say_error "Unable to run update_session(#{JSON.pretty_generate(body)})"
|
323
|
+
say_error e
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
253
327
|
end
|
254
328
|
end
|
data/lib/gzr/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gazer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike DeAngelo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-reader
|
@@ -291,8 +291,12 @@ files:
|
|
291
291
|
- lib/gzr/commands/attribute/rm.rb
|
292
292
|
- lib/gzr/commands/attribute/set_group_value.rb
|
293
293
|
- lib/gzr/commands/connection.rb
|
294
|
+
- lib/gzr/commands/connection/cat.rb
|
294
295
|
- lib/gzr/commands/connection/dialects.rb
|
296
|
+
- lib/gzr/commands/connection/import.rb
|
295
297
|
- lib/gzr/commands/connection/ls.rb
|
298
|
+
- lib/gzr/commands/connection/rm.rb
|
299
|
+
- lib/gzr/commands/connection/test.rb
|
296
300
|
- lib/gzr/commands/dashboard.rb
|
297
301
|
- lib/gzr/commands/dashboard/cat.rb
|
298
302
|
- lib/gzr/commands/dashboard/import.rb
|
@@ -328,6 +332,12 @@ files:
|
|
328
332
|
- lib/gzr/commands/plan/ls.rb
|
329
333
|
- lib/gzr/commands/plan/rm.rb
|
330
334
|
- lib/gzr/commands/plan/run.rb
|
335
|
+
- lib/gzr/commands/project.rb
|
336
|
+
- lib/gzr/commands/project/cat.rb
|
337
|
+
- lib/gzr/commands/project/deploy_key.rb
|
338
|
+
- lib/gzr/commands/project/import.rb
|
339
|
+
- lib/gzr/commands/project/ls.rb
|
340
|
+
- lib/gzr/commands/project/update.rb
|
331
341
|
- lib/gzr/commands/query.rb
|
332
342
|
- lib/gzr/commands/query/runquery.rb
|
333
343
|
- lib/gzr/commands/role.rb
|
@@ -340,6 +350,11 @@ files:
|
|
340
350
|
- lib/gzr/commands/role/user_add.rb
|
341
351
|
- lib/gzr/commands/role/user_ls.rb
|
342
352
|
- lib/gzr/commands/role/user_rm.rb
|
353
|
+
- lib/gzr/commands/session.rb
|
354
|
+
- lib/gzr/commands/session/get.rb
|
355
|
+
- lib/gzr/commands/session/login.rb
|
356
|
+
- lib/gzr/commands/session/logout.rb
|
357
|
+
- lib/gzr/commands/session/update.rb
|
343
358
|
- lib/gzr/commands/subcommandbase.rb
|
344
359
|
- lib/gzr/commands/user.rb
|
345
360
|
- lib/gzr/commands/user/cat.rb
|
@@ -359,6 +374,7 @@ files:
|
|
359
374
|
- lib/gzr/modules/model.rb
|
360
375
|
- lib/gzr/modules/permissions.rb
|
361
376
|
- lib/gzr/modules/plan.rb
|
377
|
+
- lib/gzr/modules/project.rb
|
362
378
|
- lib/gzr/modules/role.rb
|
363
379
|
- lib/gzr/modules/session.rb
|
364
380
|
- lib/gzr/modules/user.rb
|