Skip to content

Commit

Permalink
feat: cat, import, rm permission sets
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker committed May 16, 2023
1 parent 5c685af commit 1f70cb4
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/gzr/commands/permission/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ def ls(*)
end
end

desc 'cat PERMISSION_SET_ID', 'Output json information about a permission set to screen or file'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :dir, type: :string,
desc: 'Directory to store output file'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
def cat(permission_set_id)
if options[:help]
invoke :help, ['cat']
else
require_relative 'set/cat'
Gzr::Commands::Permission::Set::Cat.new(permission_set_id,options).execute
end
end

desc 'import FILE', 'Import a permission set from a file'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :force, type: :boolean,
desc: 'Overwrite an existing permission set'
method_option :plain, type: :boolean, default: false,
desc: 'print without any extra formatting'
def import(file)
if options[:help]
invoke :help, ['import']
else
require_relative 'set/import'
Gzr::Commands::Permission::Set::Import.new(file, options).execute
end
end

desc 'rm PERMISSION_SET_ID', 'Delete the permission_set given by PERMISSION_SET_ID'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
def rm(permission_set_id)
if options[:help]
invoke :help, ['delete']
else
require_relative 'set/rm'
Gzr::Commands::Permission::Set::Delete.new(permission_set_id,options).execute
end
end

end
end
end
Expand Down
60 changes: 60 additions & 0 deletions lib/gzr/commands/permission/set/cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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/permission/set'
require_relative '../../../modules/filehelper'

module Gzr
module Commands
class Permission
class Set
class Cat < Gzr::Command
include Gzr::Permission::Set
include Gzr::FileHelper
def initialize(permission_set_id,options)
super()
@permission_set_id = permission_set_id
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
data = cat_permission_set(@permission_set_id)
if data.nil?
say_warning "Permission Set #{permission_set_id} not found"
return
end
data = trim_permission_set(data) if @options[:trim]

write_file(@options[:dir] ? "Permission_Set_#{data[:name]}.json" : nil, @options[:dir],nil, output) do |f|
f.puts JSON.pretty_generate(data)
end
end
end
end
end
end
end
end
73 changes: 73 additions & 0 deletions lib/gzr/commands/permission/set/import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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 '../../../../gzr'
require_relative '../../../command'
require_relative '../../../modules/permission/set'
require_relative '../../../modules/filehelper'

module Gzr
module Commands
class Permission
class Set
class Import < Gzr::Command
include Gzr::Permission::Set
include Gzr::FileHelper
def initialize(file, options)
super()
@file = file
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}", output: output) if @options[:debug]
with_session do
permission_set = nil

read_file(@file) do |data|
search_results = search_permission_sets(name: data[:name])
if search_results && search_results.length == 1
name = data[:name]
if !@options[:force]
raise Gzr::CLI::Error, "Permission Set #{name} already exists\nUse --force if you want to overwrite it"
end
data.select! do |k,v|
keys_to_keep('update_permission_set').include? k
end
permission_set = update_permission_set(search_results.first[:id], data)
else
data.select! do |k,v|
keys_to_keep('create_permission_set').include? k
end
permission_set = create_permission_set(data)
end
output.puts "Imported permission set #{permission_set[:id]}" unless @options[:plain]
output.puts permission_set[:id] if @options[:name]
end
end
end
end
end
end
end
end
49 changes: 49 additions & 0 deletions lib/gzr/commands/permission/set/rm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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/permission/set'

module Gzr
module Commands
class Permission
class Set
class Delete < Gzr::Command
include Gzr::Permission::Set
def initialize(permission_set_id,options)
super()
@permission_set_id = permission_set_id
@options = options
end

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]
with_session do
delete_permission_set(@permission_set_id)
end
end
end
end
end
end
end
79 changes: 79 additions & 0 deletions lib/gzr/modules/permission/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,85 @@ def all_permission_sets(fields=nil)
end
end

def cat_permission_set(set_id, fields=nil)
req = {}
req[:fields] = fields if fields
begin
return @sdk.permission_set(set_id,req)&.to_attrs
rescue LookerSDK::NotFound => e
return nil
rescue LookerSDK::Error => e
say_error "Error querying permission_set(#{set_id},#{JSON.pretty_generate(req)})"
say_error e
raise
end
end

def trim_permission_set(data)
trimmed = data.select do |k,v|
(keys_to_keep('update_permission_set') + [:id,:built_in]).include? k
end
trimmed
end

def search_permission_sets(fields: nil, sorts: nil, id: nil, name: nil, all_access: nil, built_in: nil, filter_or: nil)
data = []
begin
req = {}
req[:fields] = fields unless fields.nil?
req[:sorts] = sorts unless sorts.nil?
req[:id] = id unless id.nil?
req[:name] = name unless name.nil?
req[:all_access] = all_access unless all_access.nil?
req[:built_in] = built_in unless built_in.nil?
req[:filter_or] = filter_or unless filter_or.nil?
req[:limit] = 64
loop do
page = @sdk.search_permission_sets(req)
data+=page
break unless page.length == req[:limit]
req[:offset] = (req[:offset] || 0) + req[:limit]
end
rescue LookerSDK::NotFound => e
# do nothing
rescue LookerSDK::Error => e
say_error "Error calling search_permission_sets(#{JSON.pretty_generate(req)})"
say_error e
raise
end
data
end

def update_permission_set(permission_set_id, data)
begin
return @sdk.update_permission_set(permission_set_id, data)&.to_attrs
rescue LookerSDK::Error => e
say_error "Error calling update_permission_set(#{permission_set_id},#{JSON.pretty_generate(data)})"
say_error e
raise
end
end

def create_permission_set(data)
begin
return @sdk.create_permission_set(data)&.to_attrs
rescue LookerSDK::Error => e
say_error "Error calling create_permission_set(#{JSON.pretty_generate(data)})"
say_error e
raise
end
end

def delete_permission_set(permission_set_id)
begin
return @sdk.delete_permission_set(permission_set_id)
rescue LookerSDK::Error => e
say_error "Error calling delete_permission_set(#{permission_set_id}})"
say_error e
raise
end
end

end
end
end

0 comments on commit 1f70cb4

Please sign in to comment.