-
Notifications
You must be signed in to change notification settings - Fork 23
/
instance_profiles.go
57 lines (45 loc) · 1.51 KB
/
instance_profiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package iam
import (
"fmt"
"strings"
awsiam "github.com/aws/aws-sdk-go/service/iam"
"github.com/genevieve/leftovers/common"
)
//go:generate faux --interface instanceProfilesClient --output fakes/instance_profiles_client.go
type instanceProfilesClient interface {
ListInstanceProfiles(*awsiam.ListInstanceProfilesInput) (*awsiam.ListInstanceProfilesOutput, error)
RemoveRoleFromInstanceProfile(*awsiam.RemoveRoleFromInstanceProfileInput) (*awsiam.RemoveRoleFromInstanceProfileOutput, error)
DeleteInstanceProfile(*awsiam.DeleteInstanceProfileInput) (*awsiam.DeleteInstanceProfileOutput, error)
}
type InstanceProfiles struct {
client instanceProfilesClient
logger logger
}
func NewInstanceProfiles(client instanceProfilesClient, logger logger) InstanceProfiles {
return InstanceProfiles{
client: client,
logger: logger,
}
}
func (i InstanceProfiles) List(filter string) ([]common.Deletable, error) {
profiles, err := i.client.ListInstanceProfiles(&awsiam.ListInstanceProfilesInput{})
if err != nil {
return nil, fmt.Errorf("List IAM Instance Profiles: %s", err)
}
var resources []common.Deletable
for _, p := range profiles.InstanceProfiles {
r := NewInstanceProfile(i.client, p.InstanceProfileName, p.Roles, i.logger)
if !strings.Contains(r.Name(), filter) {
continue
}
proceed := i.logger.PromptWithDetails(r.Type(), r.Name())
if !proceed {
continue
}
resources = append(resources, r)
}
return resources, nil
}
func (i InstanceProfiles) Type() string {
return "iam-instance-profile"
}