diff --git a/lib/gzr/commands/role.rb b/lib/gzr/commands/role.rb index b3b8b7d..8576254 100644 --- a/lib/gzr/commands/role.rb +++ b/lib/gzr/commands/role.rb @@ -1,6 +1,6 @@ # The MIT License (MIT) -# Copyright (c) 2018 Mike DeAngelo Looker Data Sciences, Inc. +# Copyright (c) 2023 Mike DeAngelo Google, Inc. # Permission is hereby granted, free of charge, to any person obtaining a copy of # this software and associated documentation files (the "Software"), to deal in @@ -132,6 +132,8 @@ def rm(role_id) desc: 'Display usage information' method_option :dir, type: :string, desc: 'Directory to get output file' + method_option :trim, type: :boolean, + desc: 'Trim output to minimal set of fields for later import' def cat(role_id) if options[:help] invoke :help, ['cat'] @@ -158,6 +160,22 @@ def ls(*) Gzr::Commands::Role::Ls.new(options).execute end end + + desc 'create ROLE_NAME PERMISSION_SET_ID MODEL_SET_ID', "Create new role with the given permission and model sets" + method_option :help, aliases: '-h', type: :boolean, + desc: 'Display usage information' + method_option :plain, type: :boolean, default: false, + desc: 'print without any extra formatting' + + def create(name, pset, mset) + if options[:help] + invoke :help, ['create'] + else + require_relative 'role/create' + Gzr::Commands::Role::Create.new(name, pset, mset, options).execute + end + end + end end end diff --git a/lib/gzr/commands/role/cat.rb b/lib/gzr/commands/role/cat.rb index 68cf869..2c6a2ad 100644 --- a/lib/gzr/commands/role/cat.rb +++ b/lib/gzr/commands/role/cat.rb @@ -40,9 +40,10 @@ def initialize(role_id,options) def execute(input: $stdin, output: $stdout) say_warning("options: #{@options.inspect}") if @options[:debug] with_session do - data = query_role(@role_id) - write_file(@options[:dir] ? "Role_#{data.id}_#{data.name}.json" : nil, @options[:dir], nil, output) do |f| - f.puts JSON.pretty_generate(data.to_attrs) + data = query_role(@role_id)&.to_attrs + data = trim_role(data) if @options[:trim] + write_file(@options[:dir] ? "Role_#{data[:id]}_#{data[:name]}.json" : nil, @options[:dir], nil, output) do |f| + f.puts JSON.pretty_generate(data) end end end diff --git a/lib/gzr/commands/role/create.rb b/lib/gzr/commands/role/create.rb new file mode 100644 index 0000000..07e152d --- /dev/null +++ b/lib/gzr/commands/role/create.rb @@ -0,0 +1,51 @@ +# The MIT License (MIT) + +# Copyright (c) 2023 Mike DeAngelo Google, Inc. + +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +# the Software, and to permit persons to whom the Software is furnished to do so, +# subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +# frozen_string_literal: true + +require_relative '../../command' +require_relative '../../modules/role' + +module Gzr + module Commands + class Role + class Create < Gzr::Command + include Gzr::Role + def initialize(name,pset,mset, options) + super() + @name = name + @pset = pset + @mset = mset + @options = options + end + + def execute(input: $stdin, output: $stdout) + folder = nil + with_session do + role = create_role(@name, @pset, @mset) + output.puts "Created role #{role[:id]} #{role[:name]}" unless @options[:plain] + output.puts role[:id] if @options[:plain] + end + end + end + end + end +end diff --git a/lib/gzr/modules/role.rb b/lib/gzr/modules/role.rb index 91e0393..303e137 100644 --- a/lib/gzr/modules/role.rb +++ b/lib/gzr/modules/role.rb @@ -53,6 +53,18 @@ def query_role(role_id) end data end + def trim_role(data) + trimmed = data.select do |k,v| + (keys_to_keep('create_role') + [:id]).include? k + end + trimmed[:permission_set] = data[:permission_set].select do |k,v| + (keys_to_keep('create_permission_set') + [:id,:built_in,:all_access]).include? k + end + trimmed[:model_set] = data[:model_set].select do |k,v| + (keys_to_keep('create_model_set') + [:id,:built_in,:all_access]).include? k + end + trimmed + end def delete_role(role_id) data = nil begin @@ -124,5 +136,16 @@ def set_role_users(role_id,users=[]) end data end + def create_role(name,pset,mset) + req = { name: name, permission_set_id: pset, model_set_id: mset } + begin + return @sdk.create_role(req)&.to_attrs + rescue LookerSDK::Error => e + say_error "Unable to call create_role(#{JSON.pretty_generate(req)})" + say_error e + raise + end + end + end end diff --git a/spec/integration/role/cat_spec.rb b/spec/integration/role/cat_spec.rb index 466dec2..b288258 100644 --- a/spec/integration/role/cat_spec.rb +++ b/spec/integration/role/cat_spec.rb @@ -29,6 +29,7 @@ Options: -h, [--help], [--no-help] # Display usage information [--dir=DIR] # Directory to get output file + [--trim], [--no-trim] # Trim output to minimal set of fields for later import Output the JSON representation of a role to screen/file OUT diff --git a/spec/integration/role_spec.rb b/spec/integration/role_spec.rb index 5393aa8..718271f 100644 --- a/spec/integration/role_spec.rb +++ b/spec/integration/role_spec.rb @@ -25,6 +25,7 @@ expected_output = <<-OUT Commands: gzr role cat ROLE_ID # Output the JSON representation of a role to screen/file + gzr role create ROLE_NAME PERMISSION_SET_ID MODEL_SET_ID # Create new role with the given permission and model sets gzr role group_add ROLE_ID GROUP_ID GROUP_ID GROUP_ID ... # Add indicated groups to role gzr role group_ls ROLE_ID # List the groups assigned to a role gzr role group_rm ROLE_ID GROUP_ID GROUP_ID GROUP_ID ... # Remove indicated groups from role