Created
October 26, 2020 02:56
-
-
Save holysugar/b22b31f4a48b4add5a5671267dd8ccde to your computer and use it in GitHub Desktop.
GCPの特定プロジェクトのメンバーリストをメンバー単位で出力する雑スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "json" | |
class GcpProjectMember | |
attr_reader :users, :service_accounts | |
def initialize | |
@users = Hash.new{|h,k| h[k] = [] } | |
@service_accounts = Hash.new{|h,k| h[k] = [] } | |
end | |
def parse(json) | |
obj = JSON.parse(json) | |
obj["bindings"].each do |b| | |
b["members"].each do |m| | |
prefix, name = m.split(/:/) | |
case prefix | |
when "user" | |
@users[name] << b["role"] | |
when "serviceAccount" | |
@service_accounts[name] << b["role"] | |
else | |
warn "unexpected format member: #{m}" | |
end | |
end | |
end | |
[@users, @service_accounts] | |
end | |
def format_txt(users = @users, item_sep: "\t", user_sep: "\n", role_sep: ",") | |
users.map { |name, roles| | |
role = roles.join(role_sep) | |
[name, role].join(item_sep) | |
}.join(user_sep) | |
end | |
def gcloud_get_iam_policy(proj, *args) | |
`gcloud projects get-iam-policy --format=json #{proj} #{args.join(" ")}` | |
end | |
def main(proj, args) | |
json = gcloud_get_iam_policy(proj, *args) | |
parse(json) | |
format_txt | |
end | |
end | |
if __FILE__ == $0 | |
proj = ARGV.shift || ENV["GOOGLE_PROJECT"] | |
unless proj | |
raise "$0 <project-name>" | |
end | |
puts GcpProjectMember.new.main(proj, ARGV) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment