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

add aws iot logging options #13392

Merged
merged 7 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/13392.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_iot_logging_options
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,7 @@ func Provider() *schema.Provider {
"aws_iot_authorizer": iot.ResourceAuthorizer(),
"aws_iot_certificate": iot.ResourceCertificate(),
"aws_iot_indexing_configuration": iot.ResourceIndexingConfiguration(),
"aws_iot_logging_options": iot.ResourceLoggingOptions(),
"aws_iot_policy": iot.ResourcePolicy(),
"aws_iot_policy_attachment": iot.ResourcePolicyAttachment(),
"aws_iot_provisioning_template": iot.ResourceProvisioningTemplate(),
Expand Down
90 changes: 90 additions & 0 deletions internal/service/iot/logging_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package iot

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfiam "github.com/hashicorp/terraform-provider-aws/internal/service/iam"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceLoggingOptions() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceLoggingOptionsPut,
ReadWithoutTimeout: resourceLoggingOptionsRead,
UpdateWithoutTimeout: resourceLoggingOptionsPut,
DeleteWithoutTimeout: schema.NoopContext,

Schema: map[string]*schema.Schema{
"default_log_level": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(iot.LogLevel_Values(), false),
},
"disable_all_logs": {
Type: schema.TypeBool,
Optional: true,
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
},
}
}

func resourceLoggingOptionsPut(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).IoTConn

input := &iot.SetV2LoggingOptionsInput{}

if v, ok := d.GetOk("default_log_level"); ok {
input.DefaultLogLevel = aws.String(v.(string))
}

if v, ok := d.GetOk("disable_all_logs"); ok {
input.DisableAllLogs = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("role_arn"); ok {
input.RoleArn = aws.String(v.(string))
}

_, err := tfresource.RetryWhenAWSErrMessageContainsContext(ctx, tfiam.PropagationTimeout,
func() (interface{}, error) {
return conn.SetV2LoggingOptionsWithContext(ctx, input)
},
iot.ErrCodeInvalidRequestException, "If the role was just created or updated, please try again in a few seconds.",
)

if err != nil {
return diag.Errorf("setting IoT logging options: %s", err)
}

d.SetId(meta.(*conns.AWSClient).Region)

return resourceLoggingOptionsRead(ctx, d, meta)
}

func resourceLoggingOptionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).IoTConn

output, err := conn.GetV2LoggingOptionsWithContext(ctx, &iot.GetV2LoggingOptionsInput{})

if err != nil {
return diag.Errorf("reading IoT logging options: %s", err)
}

d.Set("default_log_level", output.DefaultLogLevel)
d.Set("disable_all_logs", output.DisableAllLogs)
d.Set("role_arn", output.RoleArn)

return nil
}
141 changes: 141 additions & 0 deletions internal/service/iot/logging_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package iot_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/iot"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccIoTLoggingOptions_serial(t *testing.T) {
testCases := map[string]func(t *testing.T){
"basic": testAccLoggingOptions_basic,
"update": testAccLoggingOptions_update,
}

for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
tc(t)
})
}
}

func testAccLoggingOptions_basic(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_iot_logging_options.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, iot.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccLoggingOptionsConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "default_log_level", "WARN"),
resource.TestCheckResourceAttr(resourceName, "disable_all_logs", "false"),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
),
},
},
})
}

func testAccLoggingOptions_update(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_iot_logging_options.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, iot.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccLoggingOptionsConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "default_log_level", "WARN"),
resource.TestCheckResourceAttr(resourceName, "disable_all_logs", "false"),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
),
},
{
Config: testAccLoggingOptionsUpdatedConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "default_log_level", "DISABLED"),
resource.TestCheckResourceAttr(resourceName, "disable_all_logs", "true"),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
),
},
},
})
}

func testAccLoggingOptionsBaseConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = %[1]q

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Action": "sts:AssumeRole",
"Principal": {"Service": "iot.amazonaws.com"},
"Effect": "Allow"
}]
}
EOF
}

resource "aws_iam_role_policy" "test" {
name = %[1]q
role = aws_iam_role.test.id

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:PutMetricFilter",
"logs:PutRetentionPolicy"
],
"Resource": ["*"]
}]
}
EOF
}
`, rName)
}

func testAccLoggingOptionsConfig(rName string) string {
return acctest.ConfigCompose(testAccLoggingOptionsBaseConfig(rName), `
resource "aws_iot_logging_options" "test" {
default_log_level = "WARN"
role_arn = aws_iam_role.test.arn

depends_on = [aws_iam_role_policy.test]
}
`)
}

func testAccLoggingOptionsUpdatedConfig(rName string) string {
return acctest.ConfigCompose(testAccLoggingOptionsBaseConfig(rName), `
resource "aws_iot_logging_options" "test" {
default_log_level = "DISABLED"
disable_all_logs = true
role_arn = aws_iam_role.test.arn

depends_on = [aws_iam_role_policy.test]
}
`)
}
30 changes: 30 additions & 0 deletions website/docs/r/iot_logging_options.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
subcategory: "IoT"
layout: "aws"
page_title: "AWS: aws_iot_logging_options"
description: |-
Provides a resource to manage default logging options.
---

# Resource: aws_iot_logging_options

Provides a resource to manage [default logging options](https://docs.aws.amazon.com/iot/latest/developerguide/configure-logging.html#configure-logging-console).

## Example Usage

```terraform
resource "aws_iot_logging_options" "example" {
default_log_level = "WARN"
role_arn = aws_iam_role.example.arn
}
```

## Argument Reference

* `default_log_level` - (Optional) The default logging level. Valid Values: `"DEBUG"`, `"INFO"`, `"ERROR"`, `"WARN"`, `"DISABLED"`.
* `disable_all_logs` - (Optional) If `true` all logs are disabled. The default is `false`.
* `role_arn` - (Required) The ARN of the role that allows IoT to write to Cloudwatch logs.

## Attributes Reference

No additional attributes are exported.