Skip to content

Commit

Permalink
More improvements for slug handling. Bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike DeAngelo committed Aug 30, 2018
1 parent 5a53286 commit 461edae
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
gazer (0.1.13)
gazer (0.1.14)
looker-sdk-fork (~> 0.0.6)
netrc (~> 0.11.0)
pastel (~> 0.7.2)
Expand Down Expand Up @@ -30,7 +30,7 @@ GEM
pastel (0.7.2)
equatable (~> 0.5.0)
tty-color (~> 0.4.0)
public_suffix (3.0.2)
public_suffix (3.0.3)
rake (10.5.0)
rouge (3.1.1)
rspec (3.7.0)
Expand Down
17 changes: 16 additions & 1 deletion lib/gzr/commands/dashboard/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,28 @@ def execute(input: $stdin, output: $stdout)
end

def sync_dashboard(source,target_space_id)
existing_dashboard = search_dashboards(source[:title], target_space_id).fetch(0,nil)
existing_dashboard = search_dashboards_by_title(source[:title], target_space_id).fetch(0,nil)
slug_used = search_dashboards_by_slug(source[:slug]).fetch(0,nil) if source[:slug]

if slug_used then
if existing_dashboard then
if !(existing_dashboard.space_id == slug_used.space_id && existing_dashboard.title == slug_used.title) then
say_warning "slug #{slug_used.slug} already used for dashboard #{slug_used.title} in space #{slug_used.space_id}"
say_warning "dashboard will be imported with new slug"
end
else
say_warning "slug #{slug_used.slug} already used for dashboard #{slug_used.title} in space #{slug_used.space_id}"
say_warning "dashboard will be imported with new slug"
end
end

if existing_dashboard then
if @options[:force] then
say_ok "Modifying existing dashboard #{existing_dashboard.id} #{existing_dashboard[:title]} in space #{target_space_id}"
new_dash = source.select do |k,v|
(keys_to_keep('update_dashboard') - [:space_id,:user_id,:title,:slug]).include? k
end
new_dash[:slug] = source[:slug] unless slug_used
return update_dashboard(existing_dashboard.id,new_dash)
else
raise Gzr::CLI::Error, "Dashboard #{source[:title]} already exists in space #{target_space_id}\nUse --force if you want to overwrite it"
Expand All @@ -72,6 +86,7 @@ def sync_dashboard(source,target_space_id)
new_dash = source.select do |k,v|
(keys_to_keep('create_dashboard') - [:space_id,:user_id,:slug]).include? k
end
new_dash[:slug] = source[:slug] unless slug_used
new_dash[:space_id] = target_space_id
new_dash[:user_id] = @me.id
return create_dashboard(new_dash)
Expand Down
18 changes: 16 additions & 2 deletions lib/gzr/modules/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,28 @@ def delete_dashboard(dash)
data
end

def search_dashboards(title, space_id=nil)
def search_dashboards_by_slug(slug, space_id=nil)
data = nil
begin
req = { :slug => slug }
req[:space_id] = space_id if space_id
data = @sdk.search_dashboards(req)
rescue LookerSDK::Error => e
say_error "Error search_dashboards_by_slug(#{JSON.pretty_generate(req)})"
say_error e.message
raise
end
data
end

def search_dashboards_by_title(title, space_id=nil)
data = nil
begin
req = { :title => title }
req[:space_id] = space_id if space_id
data = @sdk.search_dashboards(req)
rescue LookerSDK::Error => e
say_error "Error search_dashboards(#{JSON.pretty_generate(req)})"
say_error "Error search_dashboards_by_title(#{JSON.pretty_generate(req)})"
say_error e.message
raise
end
Expand Down
39 changes: 34 additions & 5 deletions lib/gzr/modules/look.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,28 @@ def query_look(look_id)
data
end

def search_looks(title, space_id=nil)
def search_looks_by_slug(slug, space_id=nil)
data = nil
begin
req = { :slug => slug }
req[:space_id] = space_id if space_id
data = @sdk.search_looks(req)
rescue LookerSDK::Error => e
say_error "Error search_looks_by_slug(#{JSON.pretty_generate(req)})"
say_error e.message
raise
end
data
end

def search_looks_by_title(title, space_id=nil)
data = nil
begin
req = { :title => title }
req[:space_id] = space_id if space_id
data = @sdk.search_looks(req)
rescue LookerSDK::Error => e
say_error "Error search_looks(#{JSON.pretty_generate(req)})"
say_error "Error search_looks_by_title(#{JSON.pretty_generate(req)})"
say_error e.message
raise
end
Expand Down Expand Up @@ -63,23 +77,38 @@ def delete_look(look_id)
end

def upsert_look(user_id, query_id, space_id, source_look)
existing_looks = search_looks(source_look[:title], space_id)
existing_look = search_looks_by_title(source_look[:title], space_id).fetch(0,nil)
slug_used = search_looks_by_slug(source_look[:slug]).fetch(0,nil) if source_look[:slug]

if slug_used then
if existing_look then
if !(existing_look.space_id == slug_used.space_id && existing_look.title == slug_used.title) then
say_warning "slug #{slug_used.slug} already used for look #{slug_used.title} in space #{slug_used.space_id}"
say_warning "look will be imported with new slug"
end
else
say_warning "slug #{slug_used.slug} already used for look #{slug_used.title} in space #{slug_used.space_id}"
say_warning "look will be imported with new slug"
end
end

if existing_looks.length > 0 then
if existing_look then
if @options[:force] then
say_ok "Modifying existing Look #{source_look[:title]} in space #{space_id}"
new_look = source_look.select do |k,v|
(keys_to_keep('update_look') - [:space_id,:user_id,:query_id,:slug]).include? k
end
new_look[:slug] = source_look[:slug] unless slug_used
new_look[:query_id] = query_id
return update_look(existing_looks.first.id,new_look)
return update_look(existing_look.id,new_look)
else
raise Gzr::CLI::Error, "Look #{source_look[:title]} already exists in space #{space_id}\nUse --force if you want to overwrite it"
end
else
new_look = source_look.select do |k,v|
(keys_to_keep('create_look') - [:space_id,:user_id,:query_id,:slug]).include? k
end
new_look[:slug] = source_look[:slug] unless slug_used
new_look[:query_id] = query_id
new_look[:user_id] = user_id
new_look[:space_id] = space_id
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Gzr
VERSION = "0.1.13"
VERSION = "0.1.14"
end

0 comments on commit 461edae

Please sign in to comment.