driftctl/pkg/remote/aws/iam_user_policy_enumerator.go

51 lines
1.4 KiB
Go
Raw Normal View History

2021-07-09 11:39:50 +00:00
package aws
import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
type IamUserPolicyEnumerator struct {
repository repository.IAMRepository
factory resource.ResourceFactory
}
func NewIamUserPolicyEnumerator(repo repository.IAMRepository, factory resource.ResourceFactory) *IamUserPolicyEnumerator {
return &IamUserPolicyEnumerator{
repository: repo,
factory: factory,
}
}
func (e *IamUserPolicyEnumerator) SupportedType() resource.ResourceType {
return aws.AwsIamUserPolicyResourceType
}
2021-08-09 14:03:04 +00:00
func (e *IamUserPolicyEnumerator) Enumerate() ([]*resource.Resource, error) {
2021-07-09 11:39:50 +00:00
users, err := e.repository.ListAllUsers()
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsIamUserResourceType)
2021-07-09 11:39:50 +00:00
}
userPolicies, err := e.repository.ListAllUserPolicies(users)
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
2021-07-09 11:39:50 +00:00
}
2021-08-09 14:03:04 +00:00
results := make([]*resource.Resource, len(userPolicies))
2021-07-09 11:39:50 +00:00
for _, userPolicy := range userPolicies {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
userPolicy,
map[string]interface{}{},
),
)
}
return results, err
}