Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Add tags to IAM users #472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/terraforming/resource/iam_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions lib/terraforming/template/tf/iam_user.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
32 changes: 32 additions & 0 deletions spec/lib/terraforming/resource/iam_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" {
Expand All @@ -63,6 +91,10 @@ module Resource
"path" => "/",
"unique_id" => "ABCDEFGHIJKLMN1234567",
"force_destroy" => "false",
"tags" => {
"Name" => "test",
"Team" => "developers"
}
}
}
},
Expand Down