forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add iot event configurations resource
- Loading branch information
1 parent
c36b56c
commit ad5b5a8
Showing
3 changed files
with
202 additions
and
0 deletions.
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
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,112 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iot" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceAwsIotEventConfigurations() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsIotEventConfigurationsUpdate, | ||
Read: resourceAwsIotEventConfigurationsRead, | ||
Update: resourceAwsIotEventConfigurationsUpdate, | ||
Delete: resourceAwsIotEventConfigurationsCleanAll, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"values": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Elem: schema.TypeBool, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsIotEventConfigurationsRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
params := &iot.DescribeEventConfigurationsInput{} | ||
out, err := conn.DescribeEventConfigurations(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Update IoT Event Configuration: %s", out) | ||
|
||
eventConfigurations := out.EventConfigurations | ||
rawValues := make(map[string]interface{}) | ||
for key, value := range eventConfigurations { | ||
rawValues[key] = aws.BoolValue(value.Enabled) | ||
} | ||
|
||
d.Set("values", rawValues) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsIotEventConfigurationsUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
rawValues := d.Get("values").(map[string]interface{}) | ||
eventConfigurations := make(map[string]*iot.Configuration) | ||
for key, enabled := range rawValues { | ||
eventConfigurations[key] = &iot.Configuration{ | ||
Enabled: aws.Bool(enabled.(bool)), | ||
} | ||
} | ||
|
||
params := &iot.UpdateEventConfigurationsInput{ | ||
EventConfigurations: eventConfigurations, | ||
} | ||
|
||
out, err := conn.UpdateEventConfigurations(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("event-configurations") | ||
log.Printf("[DEBUG] Update IoT Event Configuration: %s", out) | ||
return resourceAwsIotEventConfigurationsRead(d, meta) | ||
} | ||
|
||
func resourceAwsIotEventConfigurationsCleanAll(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iotconn | ||
|
||
params := &iot.DescribeEventConfigurationsInput{} | ||
out, err := conn.DescribeEventConfigurations(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
eventConfigurations := out.EventConfigurations | ||
cleanedEventConfigurations := make(map[string]*iot.Configuration) | ||
for key := range eventConfigurations { | ||
cleanedEventConfigurations[key] = &iot.Configuration{ | ||
Enabled: aws.Bool(false), | ||
} | ||
} | ||
|
||
updateParams := &iot.UpdateEventConfigurationsInput{ | ||
EventConfigurations: cleanedEventConfigurations, | ||
} | ||
|
||
_, err = conn.UpdateEventConfigurations(updateParams) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Clean IoT Event Configuration: %s", out) | ||
|
||
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,89 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/iot" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccAWSIoTEventConfigurations_basic(t *testing.T) { | ||
resourceName := "aws_iot_event_configurations.event_configurations" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSIoTEventConfigurationsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSIoTEventConfigurations_basic(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.THING", "true"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.THING_GROUP", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.THING_TYPE", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.THING_GROUP_MEMBERSHIP", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.THING_GROUP_HIERARCHY", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.THING_TYPE_ASSOCIATION", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.JOB", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.JOB_EXECUTION", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.POLICY", "false"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.CERTIFICATE", "true"), | ||
resource.TestCheckResourceAttr("aws_iot_event_configurations.event_configurations", "values.CA_CERTIFICATE", "false"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSIoTEventConfigurationsDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).iotconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_iot_event_configurations" { | ||
continue | ||
} | ||
|
||
params := &iot.DescribeEventConfigurationsInput{} | ||
out, err := conn.DescribeEventConfigurations(params) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
for key, value := range out.EventConfigurations { | ||
if *value.Enabled != false { | ||
return fmt.Errorf("Event configuration under key %s not equals false", key) | ||
} | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccAWSIoTEventConfigurations_basic() string { | ||
return fmt.Sprintf(` | ||
resource "aws_iot_event_configurations" "event_configurations" { | ||
values = { | ||
"THING" = true, | ||
"THING_GROUP" = false, | ||
"THING_TYPE" = false, | ||
"THING_GROUP_MEMBERSHIP" = false, | ||
"THING_GROUP_HIERARCHY" = false, | ||
"THING_TYPE_ASSOCIATION" = false, | ||
"JOB" = false, | ||
"JOB_EXECUTION" = false, | ||
"POLICY" = false, | ||
"CERTIFICATE" = true, | ||
"CA_CERTIFICATE" = false, | ||
} | ||
} | ||
`) | ||
} |