-
Notifications
You must be signed in to change notification settings - Fork 23
/
instance_profiles.go
70 lines (55 loc) · 1.72 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
58
59
60
61
62
63
64
65
66
67
68
69
70
package iam
import (
"fmt"
"strings"
awsiam "github.com/aws/aws-sdk-go/service/iam"
"github.com/genevieve/leftovers/aws/common"
)
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) ListOnly(filter string) ([]common.Deletable, error) {
return i.get(filter)
}
func (i InstanceProfiles) List(filter string) ([]common.Deletable, error) {
resources, err := i.get(filter)
if err != nil {
return nil, err
}
var delete []common.Deletable
for _, r := range resources {
proceed := i.logger.PromptWithDetails(r.Type(), r.Name())
if !proceed {
continue
}
delete = append(delete, r)
}
return delete, nil
}
func (i InstanceProfiles) get(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 {
resource := NewInstanceProfile(i.client, p.InstanceProfileName, p.Roles, i.logger)
if !strings.Contains(resource.Name(), filter) {
continue
}
resources = append(resources, resource)
}
return resources, nil
}