Skip to content

Commit

Permalink
Add iot event configurations resource
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiyVeretiuk committed Oct 15, 2019
1 parent c36b56c commit ad5b5a8
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ func Provider() terraform.ResourceProvider {
"aws_inspector_resource_group": resourceAWSInspectorResourceGroup(),
"aws_instance": resourceAwsInstance(),
"aws_internet_gateway": resourceAwsInternetGateway(),
"aws_iot_event_configurations": resourceAwsIotEventConfigurations(),
"aws_iot_certificate": resourceAwsIotCertificate(),
"aws_iot_policy": resourceAwsIotPolicy(),
"aws_iot_policy_attachment": resourceAwsIotPolicyAttachment(),
Expand Down
112 changes: 112 additions & 0 deletions aws/resource_aws_iot_event_configurations.go
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
}
89 changes: 89 additions & 0 deletions aws/resource_aws_iot_event_configurations_test.go
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,
}
}
`)
}

0 comments on commit ad5b5a8

Please sign in to comment.