Commit 30b3629a authored by Daisuke Fujita's avatar Daisuke Fujita

Generate IAM User Policy tfstate

parent 4200663c
......@@ -20,7 +20,24 @@ module Terraforming
end
def tfstate
resources = iam_user_policies.inject({}) do |result, policy|
attributes = {
"id" => iam_user_policy_id_of(policy),
"name" => policy.policy_name,
"policy" => CGI.unescape(policy.policy_document)
}
result["aws_iam_user_policy.#{policy.policy_name}"] = {
"type" => "aws_iam_user_policy",
"primary" => {
"id" => iam_user_policy_id_of(policy),
"attributes" => attributes
}
}
result
end
generate_tfstate(resources)
end
private
......@@ -29,6 +46,10 @@ module Terraforming
@client.list_users.users
end
def iam_user_policy_id_of(policy)
"#{policy.user_name}:#{policy.policy_name}"
end
def iam_user_policy_names_in(user)
@client.list_user_policies(user_name: user.user_name).policy_names
end
......
......@@ -96,7 +96,7 @@ EOF
end
describe ".tfstate" do
xit "should generate tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client)).to eq JSON.pretty_generate({
"version" => 1,
"serial" => 1,
......@@ -106,29 +106,25 @@ EOF
],
"outputs" => {},
"resources" => {
"aws_iam_user.hoge" => {
"type" => "aws_iam_user",
"aws_iam_user_policy.hoge_policy" => {
"type" => "aws_iam_user_policy",
"primary" => {
"id" => "hoge",
"id" => "hoge:hoge_policy",
"attributes" => {
"arn"=> "arn:aws:iam::123456789012:user/hoge",
"id" => "hoge",
"name" => "hoge",
"path" => "/",
"unique_id" => "ABCDEFGHIJKLMN1234567",
"id" => "hoge:hoge_policy",
"name" => "hoge_policy",
"policy" => "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Action\": [\n \"ec2:Describe*\"\n ],\n \"Effect\": \"Allow\",\n \"Resource\": \"*\"\n }\n ]\n}\n",
}
}
},
"aws_iam_user.fuga" => {
"type" => "aws_iam_user",
"aws_iam_user_policy.fuga_policy" => {
"type" => "aws_iam_user_policy",
"primary" => {
"id" => "fuga",
"id" => "fuga:fuga_policy",
"attributes" => {
"arn"=> "arn:aws:iam::345678901234:user/fuga",
"id" => "fuga",
"name" => "fuga",
"path" => "/system/",
"unique_id" => "OPQRSTUVWXYZA8901234",
"id" => "fuga:fuga_policy",
"name" => "fuga_policy",
"policy" => "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Action\": [\n \"ec2:Describe*\"\n ],\n \"Effect\": \"Allow\",\n \"Resource\": \"*\"\n }\n ]\n}\n",
}
}
},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment