Skip to content
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

Improved resource import #498

Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,13 @@ In that case terraformer will not know with which region resources are associate
* `aws_iam_group_policy_attachment`
* `aws_iam_instance_profile`
* `aws_iam_policy`
* `aws_iam_policy_attachment`
* `aws_iam_role`
* `aws_iam_role_policy`
* `aws_iam_role_policy_attachment`
* `aws_iam_user`
* `aws_iam_user_group_membership`
* `aws_iam_user_policy`
* `aws_iam_user_policy_attachment`
* `igw`
* `aws_internet_gateway`
* `iot`
Expand Down
59 changes: 48 additions & 11 deletions providers/aws/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ func (g *IamGenerator) getRoles(svc *iam.Client) error {
log.Println(err)
continue
}
roleAttachedPoliciesPage := iam.NewListAttachedRolePoliciesPaginator(svc.ListAttachedRolePoliciesRequest(&iam.ListAttachedRolePoliciesInput{
RoleName: &roleName,
}))
for roleAttachedPoliciesPage.Next(context.Background()) {
for _, attachedPolicy := range roleAttachedPoliciesPage.CurrentPage().AttachedPolicies {
g.Resources = append(g.Resources, terraformutils.NewResource(
roleName+"/"+*attachedPolicy.PolicyArn,
roleName+"_"+*attachedPolicy.PolicyName,
"aws_iam_role_policy_attachment",
"aws",
map[string]string{
"role": roleName,
"policy_arn": *attachedPolicy.PolicyArn,
},
IamAllowEmptyValues,
map[string]interface{}{}))
}
}
if err := roleAttachedPoliciesPage.Err(); err != nil {
log.Println(err)
continue
}
}
}
return p.Err()
Expand All @@ -120,6 +142,10 @@ func (g *IamGenerator) getUsers(svc *iam.Client) error {
if err != nil {
log.Println(err)
}
err = g.getUserPolicyAttachment(svc, user.UserName)
if err != nil {
log.Println(err)
}
err = g.getUserGroup(svc, user.UserName)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -170,24 +196,35 @@ func (g *IamGenerator) getUserPolices(svc *iam.Client, userName *string) error {
return p.Err()
}

func (g *IamGenerator) getUserPolicyAttachment(svc *iam.Client, userName *string) error {
p := iam.NewListAttachedUserPoliciesPaginator(svc.ListAttachedUserPoliciesRequest(&iam.ListAttachedUserPoliciesInput{
UserName: userName,
}))
for p.Next(context.Background()) {
for _, attachedPolicy := range p.CurrentPage().AttachedPolicies {
g.Resources = append(g.Resources, terraformutils.NewResource(
*userName+"/"+*attachedPolicy.PolicyArn,
*userName+"_"+*attachedPolicy.PolicyName,
"aws_iam_user_policy_attachment",
"aws",
map[string]string{
"user": *userName,
"policy_arn": *attachedPolicy.PolicyArn,
},
IamAllowEmptyValues,
map[string]interface{}{}))
}
}
return p.Err()
}

func (g *IamGenerator) getPolicies(svc *iam.Client) error {
p := iam.NewListPoliciesPaginator(svc.ListPoliciesRequest(&iam.ListPoliciesInput{Scope: iam.PolicyScopeTypeLocal}))
for p.Next(context.Background()) {
for _, policy := range p.CurrentPage().Policies {
resourceName := aws.StringValue(policy.PolicyName)
policyARN := aws.StringValue(policy.Arn)

g.Resources = append(g.Resources, terraformutils.NewResource(
policyARN,
resourceName,
"aws_iam_policy_attachment",
"aws",
map[string]string{
"policy_arn": policyARN,
"name": resourceName,
},
IamAllowEmptyValues,
IamAdditionalFields))
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
policyARN,
resourceName,
Expand Down
21 changes: 9 additions & 12 deletions terraformutils/providerwrapper/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (

"github.com/GoogleCloudPlatform/terraformer/terraformutils/terraformerstring"

"github.com/zclconf/go-cty/cty/gocty"

"github.com/zclconf/go-cty/cty"

"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -155,19 +153,18 @@ func (p *ProviderWrapper) Refresh(info *terraform.InstanceInfo, state *terraform
})

if resp.Diagnostics.HasErrors() {
// retry with different serialization mechanism
priorState, err = gocty.ToCtyValue(state, impliedType)
if err != nil {
return nil, err
}
resp = p.Provider.ReadResource(providers.ReadResourceRequest{
TypeName: info.Type,
PriorState: priorState,
Private: []byte{},
// retry with regular import command - without resource attributes
importResponse := p.Provider.ImportResourceState(providers.ImportResourceStateRequest{
TypeName: info.Type,
ID: state.ID,
})
if resp.Diagnostics.HasErrors() {
if importResponse.Diagnostics.HasErrors() {
return nil, resp.Diagnostics.Err()
}
if len(importResponse.ImportedResources) == 0 {
return nil, errors.New("not able to import resource for a given ID")
}
return terraform.NewInstanceStateShimmedFromValue(importResponse.ImportedResources[0].State, int(schema.ResourceTypes[info.Type].Version)), nil
}

if resp.NewState.IsNull() {
Expand Down