8000 Split aws_iam_role_policy_attachment by eliecharra · Pull Request #839 · snyk/driftctl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Split aws_iam_role_policy_attachment #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pkg/remote/aws/iam_role_policy_attachment_details_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aws

import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)

type IamRolePolicyAttachmentDetailsFetcher struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
}

func NewIamRolePolicyAttachmentDetailsFetcher(provider terraform.ResourceReader, deserializer *resource.Deserializer) *IamRolePolicyAttachmentDetailsFetcher {
return &IamRolePolicyAttachmentDetailsFetcher{
reader: provider,
deserializer: deserializer,
}
}

func (r *IamRolePolicyAttachmentDetailsFetcher) ReadDetails(res resource.Resource) (resource.Resource, error) {
ctyVal, err := r.reader.ReadResource(terraform.ReadResourceArgs{
Ty: aws.AwsIamRolePolicyAttachmentResourceType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we already did that in every other details fetchers, but is there a reason to not use the resource type defined by the enumerator ? We could just call res.TerraformType() here

ID: res.TerraformId(),
Attributes: map[string]string{
"role": *res.Attributes().GetString("role"),
"policy_arn": *res.Attributes().GetString("policy_arn"),
},
})
if err != nil {
return nil, err
}
deserializedRes, err := r.deserializer.DeserializeOne(aws.AwsIamRolePolicyAttachmentResourceType, *ctyVal)
if err != nil {
return nil, err
}

return deserializedRes, nil
}
69 changes: 69 additions & 0 deletions pkg/remote/aws/iam_role_policy_attachment_enumerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/service/iam"
"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"
)

type IamRolePolicyAttachmentEnumerator struct {
repository repository.IAMRepository
factory resource.ResourceFactory
}

func NewIamRolePolicyAttachmentEnumerator(repository repository.IAMRepository, factory resource.ResourceFactory) *IamRolePolicyAttachmentEnumerator {
return &IamRolePolicyAttachmentEnumerator{
repository,
factory,
}
}

func (e *IamRolePolicyAttachmentEnumerator) SupportedType() resource.ResourceType {
return resourceaws.AwsIamRolePolicyAttachmentResourceType
}

func (e *IamRolePolicyAttachmentEnumerator) Enumerate() ([]resource.Resource, error) {
roles, err := e.repository.ListAllRoles()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, string(e.SupportedType()), resourceaws.AwsIamRoleResourceType)
}

results := make([]resource.Resource, 0)
rolesNotIgnored := make([]*iam.Role, 0)

for _, role := range roles {
if role.RoleName != nil && awsIamRoleShouldBeIgnored(*role.RoleName) {
continue
}
rolesNotIgnored = append(rolesNotIgnored, role)
}

if len(rolesNotIgnored) == 0 {
return results, nil
}

policyAttachments, err := e.repository.ListAllRolePolicyAttachments(rolesNotIgnored)
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}

for _, attachedPol := range policyAttachments {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
fmt.Sprintf("%s-%s", *attachedPol.PolicyName, attachedPol.RoleName),
map[string]interface{}{
"role": attachedPol.RoleName,
"policy_arn": *attachedPol.PolicyArn,
},
),
)
}

return results, nil
}
78 changes: 0 additions & 78 deletions pkg/remote/aws/iam_role_policy_attachment_supplier.go

This file was deleted.

168 changes: 0 additions & 168 deletions pkg/remote/aws/iam_role_policy_attachment_supplier_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/remote/aws/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsIamRoleResourceType, common.NewGenericDetailsFetcher(aws.AwsIamRoleResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewIamAccessKeyEnumerator(iamRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsIamAccessKeyResourceType, NewIamAccessKeyDetailsFetcher(provider, deserializer))
remoteLibrary.AddEnumerator(NewIamRolePolicyAttachmentEnumerator(iamRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsIamRolePolicyAttachmentResourceType, NewIamRolePolicyAttachmentDetailsFetcher(provider, deserializer))

remoteLibrary.AddEnumerator(NewECRRepositoryEnumerator(ecrRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsEcrRepositoryResourceType, common.NewGenericDetailsFetcher(aws.AwsEcrRepositoryResourceType, provider, deserializer))

supplierLibrary.AddSupplier(NewIamUserPolicyAttachmentSupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewIamRolePolicySupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewIamRolePolicyAttachmentSupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository))

err = resourceSchemaRepository.Init(version, provider.Schema())
Expand Down
Loading
0