From 7b9b99dd37bb4317a6d7c4a576ec57bbe9ccbf49 Mon Sep 17 00:00:00 2001 From: Genki Sugawara Date: Sun, 10 May 2015 14:56:13 +0900 Subject: [PATCH] Support json export --- .gitignore | 1 + bin/miam | 33 ++++++++++++++++++++++----------- lib/miam/client.rb | 9 ++++++--- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 0218ae9..2064aa9 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ test.rb IAMfile *.iam account.csv +*.json diff --git a/bin/miam b/bin/miam index 52ca317..06c23b4 100755 --- a/bin/miam +++ b/bin/miam @@ -48,6 +48,7 @@ ARGV.options do |opt| opt.on('-o', '--output FILE') {|v| output_file = v } opt.on('' , '--split') { split = true } opt.on('' , '--split-more') { split = :more } + opt.on('', '--format=FORMAT', [:ruby, :json]) {|v| options[:format] = v } opt.on('' , '--export-concurrency N', Integer) {|v| options[:export_concurrency] = v } opt.on('' , '--target REGEXP') {|v| options[:target] = Regexp.new(v) } opt.on('' , '--no-color') { options[:color] = false } @@ -102,11 +103,12 @@ begin output_file = DEFAULT_FILENAME if output_file == '-' requires = [] - client.export(:split_more => (split == :more)) do |args| + client.export(:split_more => (split == :more), :convert => (options[:format] == :ruby)) do |args| type, dsl = args.values_at(:type, :dsl) - next if dsl.strip.empty? + next if dsl.empty? type = type.to_s + dsl = JSON.pretty_generate(dsl) if options[:format] == :json dir = File.dirname(output_file) if split == :more @@ -122,32 +124,41 @@ begin requires << iam_filename end + if options[:format] == :json + iam_file << '.json' + end + logger.info(" write `#{iam_file}`") open(iam_file, 'wb') do |f| - f.puts MAGIC_COMMENT + f.puts MAGIC_COMMENT if options[:format] == :ruby f.puts dsl end end - logger.info(" write `#{output_file}`") + if options[:format] == :ruby + logger.info(" write `#{output_file}`") - open(output_file, 'wb') do |f| - f.puts MAGIC_COMMENT + open(output_file, 'wb') do |f| + f.puts MAGIC_COMMENT - requires.each do |iam_file| - f.puts "require '#{iam_file}'" + requires.each do |iam_file| + f.puts "require '#{iam_file}'" + end end end else + exported = client.export(:convert => (options[:format] == :ruby)) + exported = JSON.pretty_generate(exported) if options[:format] == :json + if output_file == '-' logger.info('# Export IAM') - puts client.export.strip + puts exported else logger.info("Export IAM to `#{output_file}`") open(output_file, 'wb') do |f| - f.puts MAGIC_COMMENT - f.puts client.export.strip + f.puts MAGIC_COMMENT if options[:format] == :ruby + f.puts exported end end end diff --git a/lib/miam/client.rb b/lib/miam/client.rb index 6db984f..c6a0eed 100644 --- a/lib/miam/client.rb +++ b/lib/miam/client.rb @@ -10,6 +10,7 @@ def initialize(options = {}) end def export(export_options = {}) + export_options = {convert: true}.merge(export_options) exported, group_users, instance_profile_roles = Miam::Exporter.export(@iam, @options) if block_given? @@ -21,15 +22,17 @@ def export(export_options = {}) more_splitted = splitted.dup more_splitted[type] = {} more_splitted[type][name] = attrs - yield(:type => type, :name => name, :dsl => Miam::DSL.convert(more_splitted, @options).strip) + dsl = export_options[:convert] ? Miam::DSL.convert(more_splitted, @options).strip : more_splitted + yield(:type => type, :name => name, :dsl => dsl) end else splitted[type] = exported[type] - yield(:type => type, :dsl => Miam::DSL.convert(splitted, @options).strip) + dsl = export_options[:convert] ? Miam::DSL.convert(splitted, @options).strip : splitted + yield(:type => type, :dsl => dsl) end end else - Miam::DSL.convert(exported, @options) + export_options[:convert] ? Miam::DSL.convert(exported, @options).strip : exported end end