Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create role command #204

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/gzr/commands/role.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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']
Expand All @@ -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
7 changes: 4 additions & 3 deletions lib/gzr/commands/role/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
drstrangelooker marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
51 changes: 51 additions & 0 deletions lib/gzr/commands/role/create.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions lib/gzr/modules/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/integration/role/cat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/integration/role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down