Skip to content

Commit

Permalink
Merge pull request #30 from envato/instance-profile-tags
Browse files Browse the repository at this point in the history
Add support for InstanceProfile tags
  • Loading branch information
viraptor authored Jul 7, 2023
2 parents 5fdf22c + 1c3434f commit 445db58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 8 additions & 4 deletions iamy/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ func (a *AwsFetcher) marshalPolicyDescriptionAsync(policyArn string, target *str
}()
}

func (a *AwsFetcher) fetchPolicyTags(policyArn string, tags *map[string]string) {
log.Println("Fetching tags for", policyArn)
func (a *AwsFetcher) fetchInstanceProfileTags(instanceProfileName string, tags *map[string]string) {
log.Println("Fetching tags for instance profile ", instanceProfileName)

var err error
*tags, err = a.iam.getPolicyTags(policyArn)
*tags, err = a.iam.getInstanceProfileTags(instanceProfileName)
if err != nil {
a.policyTagFetchError = err
}
Expand All @@ -230,7 +230,10 @@ func (a *AwsFetcher) marshalRoleAsync(roleName string, roleDescription *string,

func (a *AwsFetcher) populateInstanceProfileData(resp *iam.ListInstanceProfilesOutput) error {
for _, profileResp := range resp.InstanceProfiles {
if ok, err := a.isSkippableManagedResource(CfnInstanceProfile, *profileResp.InstanceProfileName, map[string]string{}, *profileResp.Path); ok {
tags := make(map[string]string)
a.fetchInstanceProfileTags(*profileResp.InstanceProfileName, &tags)

if ok, err := a.isSkippableManagedResource(CfnInstanceProfile, *profileResp.InstanceProfileName, tags, *profileResp.Path); ok {
log.Printf(err)
continue
}
Expand All @@ -243,6 +246,7 @@ func (a *AwsFetcher) populateInstanceProfileData(resp *iam.ListInstanceProfilesO
role := *(roleResp.RoleName)
profile.Roles = append(profile.Roles, role)
}
profile.Tags = tags
a.data.InstanceProfiles = append(a.data.InstanceProfiles, &profile)
}
return nil
Expand Down
12 changes: 12 additions & 0 deletions iamy/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func (c *iamClient) getPolicyTags(arn string) (map[string]string, error) {
return nil, err
}

func (c *iamClient) getInstanceProfileTags(name string) (map[string]string, error) {
resp, err := c.ListInstanceProfileTags(&iam.ListInstanceProfileTagsInput{InstanceProfileName: &name})
if err == nil && resp.Tags != nil {
tags := make(map[string]string)
for _, tag := range resp.Tags {
tags[*tag.Key] = *tag.Value
}
return tags, err
}
return nil, err
}

func (c *iamClient) getRole(name string) (string, int, error) {
resp, err := c.GetRole(&iam.GetRoleInput{RoleName: &name})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions iamy/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type Role struct {
type InstanceProfile struct {
iamService `json:"-"`
Roles []string `json:"Roles,omitempty"`
Tags map[string]string `json:"Tags,omitempty"`
}

func (ip InstanceProfile) ResourceType() string {
Expand Down

0 comments on commit 445db58

Please sign in to comment.