Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
Defer fetching of resource data for IAM
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Tichý committed Nov 30, 2018
1 parent 2d34b16 commit 2447859
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 97 deletions.
132 changes: 66 additions & 66 deletions fixtures/vcr_cassettes/iam_group-setup.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lib/puppet/provider/iam_group/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ def self.get_groups
def self.instances
groups = get_groups()
groups.collect do |group|
group_data = iam_client.get_group({ group_name: group.group_name })
member_names = group_data.users.map {|user| user.user_name }

new({
name: group.group_name,
ensure: :present,
path: group.path,
members: member_names,
})
end
end
Expand Down Expand Up @@ -101,6 +97,11 @@ def destroy
@property_hash[:ensure] = :absent
end

def members
group_data = iam_client.get_group({ group_name: name })
@property_hash[:members] = group_data.users.map {|user| user.user_name }
end

def members=(value)
unless @property_hash[:ensure] == :absent
# First all add missing members to the group
Expand Down
30 changes: 15 additions & 15 deletions lib/puppet/provider/iam_policy/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,12 @@
def self.instances
policies = PuppetX::Puppetlabs::Iam_policy.get_policies
policies.collect do |policy|

policy_document_versions = iam_client.list_policy_versions({
policy_arn: policy.arn,
max_items: 1
})

policy_version_data = iam_client.get_policy_version({
policy_arn: policy.arn,
version_id: policy_document_versions.versions[0].version_id
})

policy_data = JSON.parse(URI.unescape(policy_version_data.policy_version.document))
policy_document = JSON.pretty_generate(policy_data)

new({
name: policy.policy_name,
ensure: :present,
path: policy.path,
description: policy.description,
arn: policy.arn,
document: policy_document,
})
end
end
Expand Down Expand Up @@ -86,6 +71,21 @@ def destroy
@property_hash[:ensure] = :absent
end

def document
policy_document_versions = iam_client.list_policy_versions({
policy_arn: arn,
max_items: 1
})

policy_version_data = iam_client.get_policy_version({
policy_arn: arn,
version_id: policy_document_versions.versions[0].version_id
})

policy_data = JSON.parse(URI.unescape(policy_version_data.policy_version.document))
@property_hash[:document] = JSON.pretty_generate(policy_data)
end

def document=(value)
# IAM allows up to 5 managed policies at the time of this writing. As
# such, if we are going to modify a policy, that is, to create a new one,
Expand Down
34 changes: 22 additions & 12 deletions lib/puppet/provider/iam_policy_attachment/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,9 @@ def self.instances
end
end

response = iam_client.list_entities_for_policy({
policy_arn: policy.arn,
})

user_names = response.policy_users.collect {|user| user.user_name }
group_names = response.policy_groups.collect {|group| group.group_name }
role_names = response.policy_roles.collect {|role| role.role_name }

new({
name: policy.policy_name,
users: user_names,
groups: group_names,
roles: role_names,
arn: policy.arn,
arn: policy.arn
})
end
end
Expand All @@ -67,6 +56,21 @@ def self.prefetch(resources)
end
end

def users
return [] unless @property_hash[:name]
@property_hash[:users] = list_entities_for_policy.policy_users.collect {|user| user.user_name }
end

def groups
return [] unless @property_hash[:name]
@property_hash[:groups] = list_entities_for_policy.policy_groups.collect {|group| group.group_name }
end

def roles
return [] unless @property_hash[:name]
@property_hash[:roles] = list_entities_for_policy.policy_roles.collect {|role| role.role_name }
end

def users=(value)
Array(value).flatten.each {|user|
unless @property_hash[:users].include? user
Expand Down Expand Up @@ -133,4 +137,10 @@ def roles=(value)
}
end

private

def list_entities_for_policy
@entities_for_policy ||= iam_client.list_entities_for_policy({ policy_arn: arn })
end

end

0 comments on commit 2447859

Please sign in to comment.