-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
New Resource: aws_iot_thing_type
#3302
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSIotThingType_importBasic(t *testing.T) { | ||
resourceName := "aws_iot_thing_type.foo" | ||
rInt := acctest.RandInt() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSIotThingTypeDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSIotThingTypeConfig_basic(rInt), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iot" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// https://docs.aws.amazon.com/iot/latest/apireference/API_CreateThingType.html | ||
func resourceAwsIotThingType() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsIotThingTypeCreate, | ||
Read: resourceAwsIotThingTypeRead, | ||
Update: resourceAwsIotThingTypeUpdate, | ||
Delete: resourceAwsIotThingTypeDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
d.Set("name", d.Id()) | ||
return []*schema.ResourceData{d}, nil | ||
}, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateIotThingTypeName, | ||
}, | ||
"properties": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validateIotThingTypeDescription, | ||
}, | ||
"searchable_attributes": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
MaxItems: 3, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validateIotThingTypeSearchableAttribute, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"deprecated": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsIotThingTypeCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
params := &iot.CreateThingTypeInput{ | ||
ThingTypeName: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("properties"); ok { | ||
configs := v.([]interface{}) | ||
config, ok := configs[0].(map[string]interface{}) | ||
|
||
if ok && config != nil { | ||
params.ThingTypeProperties = expandIotThingTypeProperties(config) | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] Creating IoT Thing Type: %s", params) | ||
out, err := conn.CreateThingType(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(*out.ThingTypeName) | ||
|
||
if v := d.Get("deprecated").(bool); v { | ||
params := &iot.DeprecateThingTypeInput{ | ||
ThingTypeName: aws.String(d.Id()), | ||
UndoDeprecate: aws.Bool(false), | ||
} | ||
|
||
log.Printf("[DEBUG] Deprecating IoT Thing Type: %s", params) | ||
_, err := conn.DeprecateThingType(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceAwsIotThingTypeRead(d, meta) | ||
} | ||
|
||
func resourceAwsIotThingTypeRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
params := &iot.DescribeThingTypeInput{ | ||
ThingTypeName: aws.String(d.Id()), | ||
} | ||
log.Printf("[DEBUG] Reading IoT Thing Type: %s", params) | ||
out, err := conn.DescribeThingType(params) | ||
|
||
if err != nil { | ||
if isAWSErr(err, iot.ErrCodeResourceNotFoundException, "") { | ||
log.Printf("[WARN] IoT Thing Type %q not found, removing from state", d.Id()) | ||
d.SetId("") | ||
} | ||
return err | ||
} | ||
|
||
if out.ThingTypeMetadata != nil { | ||
d.Set("deprecated", out.ThingTypeMetadata.Deprecated) | ||
} | ||
|
||
d.Set("arn", out.ThingTypeArn) | ||
d.Set("properties", flattenIotThingTypeProperties(out.ThingTypeProperties)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsIotThingTypeUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
if d.HasChange("deprecated") { | ||
params := &iot.DeprecateThingTypeInput{ | ||
ThingTypeName: aws.String(d.Id()), | ||
UndoDeprecate: aws.Bool(!d.Get("deprecated").(bool)), | ||
} | ||
|
||
log.Printf("[DEBUG] Updating IoT Thing Type: %s", params) | ||
_, err := conn.DeprecateThingType(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceAwsIotThingTypeRead(d, meta) | ||
} | ||
|
||
func resourceAwsIotThingTypeDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
// In order to delete an IoT Thing Type, you must deprecate it first and wait | ||
// at least 5 minutes. | ||
deprecateParams := &iot.DeprecateThingTypeInput{ | ||
ThingTypeName: aws.String(d.Id()), | ||
} | ||
log.Printf("[DEBUG] Deprecating IoT Thing Type: %s", deprecateParams) | ||
_, err := conn.DeprecateThingType(deprecateParams) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
deleteParams := &iot.DeleteThingTypeInput{ | ||
ThingTypeName: aws.String(d.Id()), | ||
} | ||
log.Printf("[DEBUG] Deleting IoT Thing Type: %s", deleteParams) | ||
|
||
return resource.Retry(6*time.Minute, func() *resource.RetryError { | ||
_, err := conn.DeleteThingType(deleteParams) | ||
|
||
if err != nil { | ||
if isAWSErr(err, iot.ErrCodeInvalidRequestException, "Please wait for 5 minutes after deprecation and then retry") { | ||
return resource.RetryableError(err) | ||
} | ||
|
||
// As the delay post-deprecation is about 5 minutes, it may have been | ||
// deleted in between, thus getting a Not Found Exception. | ||
if isAWSErr(err, iot.ErrCodeResourceNotFoundException, "") { | ||
return nil | ||
} | ||
|
||
return resource.NonRetryableError(err) | ||
} | ||
|
||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iot" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSIotThingType_basic(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSIotThingTypeDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSIotThingTypeConfig_basic(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("aws_iot_thing_type.foo", "arn"), | ||
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "name", fmt.Sprintf("tf_acc_iot_thing_type_%d", rInt)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSIotThingType_full(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSIotThingTypeDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSIotThingTypeConfig_full(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("aws_iot_thing_type.foo", "arn"), | ||
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "properties.0.description", "MyDescription"), | ||
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "properties.0.searchable_attributes.#", "3"), | ||
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "deprecated", "true"), | ||
), | ||
}, | ||
{ | ||
Config: testAccAWSIotThingTypeConfig_fullUpdated(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "deprecated", "false"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSIotThingTypeDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).iotconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_iot_thing_type" { | ||
continue | ||
} | ||
|
||
params := &iot.DescribeThingTypeInput{ | ||
ThingTypeName: aws.String(rs.Primary.ID), | ||
} | ||
|
||
_, err := conn.DescribeThingType(params) | ||
if err == nil { | ||
return fmt.Errorf("Expected IoT Thing Type to be destroyed, %s found", rs.Primary.ID) | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccAWSIotThingTypeConfig_basic(rName int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_iot_thing_type" "foo" { | ||
name = "tf_acc_iot_thing_type_%d" | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSIotThingTypeConfig_full(rName int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_iot_thing_type" "foo" { | ||
name = "tf_acc_iot_thing_type_%d" | ||
deprecated = true | ||
|
||
properties { | ||
description = "MyDescription" | ||
searchable_attributes = ["foo", "bar", "baz"] | ||
} | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSIotThingTypeConfig_fullUpdated(rName int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_iot_thing_type" "foo" { | ||
name = "tf_acc_iot_thing_type_%d" | ||
deprecated = false | ||
|
||
properties { | ||
description = "MyDescription" | ||
searchable_attributes = ["foo", "bar", "baz"] | ||
} | ||
} | ||
`, rName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm mistaken this will always be triggered for new resources during creation, even though it's IMO not necessary as we can assume new resource doesn't need "undeprecation" (no-op API call)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically we could just modify the condition here to also check for
d.IsNewResource()
and the real value ofdeprecated
.