2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2021-01-20 13:01:57 +00:00
|
|
|
|
2021-05-19 15:08:28 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IamUserSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-06-28 16:21:12 +00:00
|
|
|
repository repository.IAMRepository
|
2020-12-09 15:31:34 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:21:12 +00:00
|
|
|
func NewIamUserSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamUserSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &IamUserSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-06-28 16:21:12 +00:00
|
|
|
repository,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *IamUserSupplier) Resources() ([]resource.Resource, error) {
|
2021-06-28 16:21:12 +00:00
|
|
|
users, err := s.repository.ListAllUsers()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-05-19 15:08:28 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamUserResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
results := make([]cty.Value, 0)
|
|
|
|
if len(users) > 0 {
|
|
|
|
for _, user := range users {
|
|
|
|
u := *user
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
2021-05-19 15:08:28 +00:00
|
|
|
return s.readUser(&u)
|
2020-12-09 15:31:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
results, err = s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(resourceaws.AwsIamUserResourceType, results)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:08:28 +00:00
|
|
|
func (s *IamUserSupplier) readUser(user *iam.User) (cty.Value, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
res, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsIamUserResourceType,
|
|
|
|
ID: *user.UserName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error reading iam user %s[%s]: %+v", *user.UserName, resourceaws.AwsIamUserResourceType, err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *res, nil
|
|
|
|
}
|