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

r/vpc_ipam_scope: prevent panic and add pagination when describing existing scopes by ID #24188

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .changelog/24188.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_vpc_ipam_scope: Prevent panic when describing IPAM Scopes by ID
```

```release-note:enhancement
resource/aws_vpc_ipam_scope: Add pagination when describing IPAM Scopes
```
53 changes: 45 additions & 8 deletions internal/service/ec2/vpc_ipam_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,28 @@ func ResourceVPCIpamScopeRead(d *schema.ResourceData, meta interface{}) error {
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

scope, err := findIpamScopeById(conn, d.Id())
ipamId := strings.Split(*scope.IpamArn, "/")[1]

if err != nil && !tfawserr.ErrCodeEquals(err, InvalidIpamScopeIdNotFound) {
return err
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] IPAM Scope (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading IPAM Scope (%s): %w", d.Id(), err)
}

if !d.IsNewResource() && scope == nil {
if scope == nil {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
if d.IsNewResource() {
return fmt.Errorf("error reading IPAM Scope (%s): not found after creation", d.Id())
}
log.Printf("[WARN] IPAM Scope (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

ipamId := strings.Split(aws.StringValue(scope.IpamArn), "/")[1]

d.Set("arn", scope.IpamScopeArn)
d.Set("description", scope.Description)
d.Set("ipam_arn", scope.IpamArn)
Expand Down Expand Up @@ -199,17 +209,44 @@ func findIpamScopeById(conn *ec2.EC2, id string) (*ec2.IpamScope, error) {
IpamScopeIds: aws.StringSlice([]string{id}),
}

output, err := conn.DescribeIpamScopes(input)
var results []*ec2.IpamScope

err := conn.DescribeIpamScopesPages(input, func(page *ec2.DescribeIpamScopesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, scope := range page.IpamScopes {
if scope == nil {
continue
}

results = append(results, scope)
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, InvalidIpamScopeIdNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || len(output.IpamScopes) == 0 || output.IpamScopes[0] == nil {
return nil, nil
if len(results) == 0 {
return nil, tfresource.NewEmptyResultError(input)
}

if count := len(results); count > 1 {
return nil, tfresource.NewTooManyResultsError(count, input)
}

return output.IpamScopes[0], nil
return results[0], nil
}

func waitIpamScopeAvailable(conn *ec2.EC2, ipamScopeId string, timeout time.Duration) (*ec2.IpamScope, error) {
Expand Down