diff --git a/Gemfile.lock b/Gemfile.lock index be1e866..82a91a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) diff --git a/lib/gzr/commands/dashboard/import.rb b/lib/gzr/commands/dashboard/import.rb index 6156df6..64f7486 100644 --- a/lib/gzr/commands/dashboard/import.rb +++ b/lib/gzr/commands/dashboard/import.rb @@ -56,7 +56,20 @@ 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 @@ -64,6 +77,7 @@ def sync_dashboard(source,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" @@ -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) diff --git a/lib/gzr/modules/dashboard.rb b/lib/gzr/modules/dashboard.rb index 4f50fc5..f4da5f5 100644 --- a/lib/gzr/modules/dashboard.rb +++ b/lib/gzr/modules/dashboard.rb @@ -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 diff --git a/lib/gzr/modules/look.rb b/lib/gzr/modules/look.rb index ebf1ff4..b376b46 100644 --- a/lib/gzr/modules/look.rb +++ b/lib/gzr/modules/look.rb @@ -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 @@ -63,16 +77,30 @@ 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 @@ -80,6 +108,7 @@ def upsert_look(user_id, query_id, space_id, source_look) 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 diff --git a/lib/gzr/version.rb b/lib/gzr/version.rb index 499817f..602c943 100644 --- a/lib/gzr/version.rb +++ b/lib/gzr/version.rb @@ -1,3 +1,3 @@ module Gzr - VERSION = "0.1.13" + VERSION = "0.1.14" end