Skip to content

Commit

Permalink
resource/aws_iot_policy: Switch old version pruning to use VersionId …
Browse files Browse the repository at this point in the history
…instead of CreateDate
  • Loading branch information
jeandek committed Jul 30, 2019
1 parent 9852ae0 commit 5d528f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions aws/resource_aws_iot_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
Expand Down Expand Up @@ -147,14 +148,22 @@ func iotPolicyPruneVersions(name string, iotconn *iot.IoT) error {
}

var oldestVersion *iot.PolicyVersion
var oldestVersionId int64

for _, version := range versions {
if *version.IsDefaultVersion {
continue
}
if oldestVersion == nil ||
version.CreateDate.Before(*oldestVersion.CreateDate) {

versionId, err := strconv.ParseInt(*version.VersionId, 10, 64)
if err != nil {
log.Printf("[ERROR] Unexpected version id cannot be parsed to an integer. %s", err)
return err
}

if oldestVersion == nil || versionId < oldestVersionId {
oldestVersion = version
oldestVersionId = versionId
}
}

Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_iot_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAccAWSIoTPolicy_invalidJson(t *testing.T) {

func TestAccAWSIoTPolicy_update(t *testing.T) {
rName := acctest.RandomWithPrefix("PubSubToAnyTopic-")
expectedVersions := []string{"1", "2", "3", "5", "6"}
expectedVersions := []string{"2", "3", "4", "5", "6"}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down

0 comments on commit 5d528f6

Please sign in to comment.