From 83d2f57643dcd4c1b2b78a3ab977bd5d0d39be10 Mon Sep 17 00:00:00 2001 From: Kasama Date: Thu, 20 Jun 2019 19:14:33 -0300 Subject: [PATCH] Add tags to IAM users --- lib/terraforming/resource/iam_user.rb | 9 +++++- lib/terraforming/template/tf/iam_user.erb | 8 +++++ .../terraforming/resource/iam_user_spec.rb | 32 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/terraforming/resource/iam_user.rb b/lib/terraforming/resource/iam_user.rb index 049cc76e..fe24d10a 100644 --- a/lib/terraforming/resource/iam_user.rb +++ b/lib/terraforming/resource/iam_user.rb @@ -29,6 +29,10 @@ def tfstate "unique_id" => user.user_id, "force_destroy" => "false", } + attributes["tags"] = user.tags.map { |tag| + [tag.key, tag.value] + }.to_h if user.tags.length > 0 + resources["aws_iam_user.#{module_name_of(user)}"] = { "type" => "aws_iam_user", "primary" => { @@ -44,7 +48,10 @@ def tfstate private def iam_users - @client.list_users.map(&:users).flatten + @client.list_users.map(&:users).flatten.map do |user| + user.tags = @client.list_user_tags(user_name: user.user_name).tags + user + end end def module_name_of(user) diff --git a/lib/terraforming/template/tf/iam_user.erb b/lib/terraforming/template/tf/iam_user.erb index 5847817e..d32a7988 100644 --- a/lib/terraforming/template/tf/iam_user.erb +++ b/lib/terraforming/template/tf/iam_user.erb @@ -2,6 +2,14 @@ resource "aws_iam_user" "<%= module_name_of(user) %>" { name = "<%= user.user_name %>" path = "<%= user.path %>" +<% if user[:tags].length > 0 -%> + + tags = { +<% user[:tags].each do |tag| -%> + "<%= tag.key %>" = "<%= tag.value %>" +<% end -%> + } +<% end -%> } <% end -%> diff --git a/spec/lib/terraforming/resource/iam_user_spec.rb b/spec/lib/terraforming/resource/iam_user_spec.rb index 400d88ac..40e947bd 100644 --- a/spec/lib/terraforming/resource/iam_user_spec.rb +++ b/spec/lib/terraforming/resource/iam_user_spec.rb @@ -28,8 +28,31 @@ module Resource ] end + let(:tags_response) do + [ + { + is_truncated: false, + tags: [ + { + key: "Name", + value: "test" + }, + { + key: "Team", + value: "developers" + } + ] + }, + { + is_truncated: false, + tags: [] + } + ] + end + before do client.stub_responses(:list_users, users: users) + client.stub_responses(:list_user_tags, tags_response.first, tags_response.last) end describe ".tf" do @@ -38,6 +61,11 @@ module Resource resource "aws_iam_user" "hoge" { name = "hoge" path = "/" + + tags = { + "Name" = "test" + "Team" = "developers" + } } resource "aws_iam_user" "fuga-piyo" { @@ -63,6 +91,10 @@ module Resource "path" => "/", "unique_id" => "ABCDEFGHIJKLMN1234567", "force_destroy" => "false", + "tags" => { + "Name" => "test", + "Team" => "developers" + } } } },