driftctl/pkg/remote/aws/iam_role_policy_supplier.go

71 lines
2.1 KiB
Go
Raw Normal View History

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"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type IamRolePolicySupplier struct {
reader terraform.ResourceReader
2021-05-21 14:09:45 +00:00
deserializer *resource.Deserializer
repository repository.IAMRepository
runner *terraform.ParallelResourceReader
}
func NewIamRolePolicySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamRolePolicySupplier {
return &IamRolePolicySupplier{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *IamRolePolicySupplier) Resources() ([]resource.Resource, error) {
roles, err := s.repository.ListAllRoles()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsIamRolePolicyResourceType, resourceaws.AwsIamRoleResourceType)
}
policies, err := s.repository.ListAllRolePolicies(roles)
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamRolePolicyResourceType)
}
results := make([]cty.Value, 0)
if len(policies) > 0 {
for _, policy := range policies {
p := policy
s.runner.Run(func() (cty.Value, error) {
return s.readRolePolicy(p)
})
}
results, err = s.runner.Wait()
if err != nil {
return nil, err
}
}
2021-05-21 14:09:45 +00:00
return s.deserializer.Deserialize(resourceaws.AwsIamRolePolicyResourceType, results)
}
func (s *IamRolePolicySupplier) readRolePolicy(policyName string) (cty.Value, error) {
res, err := s.reader.ReadResource(
terraform.ReadResourceArgs{
Ty: resourceaws.AwsIamRolePolicyResourceType,
ID: policyName,
},
)
if err != nil {
logrus.Warnf("Error reading iam role policy %s[%s]: %+v", policyName, resourceaws.AwsIamRolePolicyResourceType, err)
return cty.NilVal, err
}
return *res, nil
}