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

r/aws_iot_topic_rule: add http action type #16087

Closed
wants to merge 6 commits into from
Closed
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
216 changes: 216 additions & 0 deletions aws/resource_aws_iot_topic_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,40 @@ func resourceAwsIotTopicRule() *schema.Resource {
},
},
},
"http": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsURLWithHTTPS,
},
"confirmation_url": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsURLWithHTTPS,
},
"http_header": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
"iot_analytics": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -461,6 +495,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -513,6 +548,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -585,6 +621,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -629,6 +666,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -677,6 +715,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -717,6 +756,60 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
"error_action.0.lambda",
"error_action.0.republish",
"error_action.0.s3",
"error_action.0.step_functions",
"error_action.0.sns",
"error_action.0.sqs",
},
},
"http": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsURLWithHTTPS,
},
"confirmation_url": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsURLWithHTTPS,
},
"http_header": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
ExactlyOneOf: []string{
"error_action.0.cloudwatch_alarm",
"error_action.0.cloudwatch_metric",
"error_action.0.dynamodb",
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -752,6 +845,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -791,6 +885,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -830,6 +925,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -861,6 +957,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -902,6 +999,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -941,6 +1039,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -980,6 +1079,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -1021,6 +1121,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -1060,6 +1161,7 @@ func resourceAwsIotTopicRule() *schema.Resource {
"error_action.0.dynamodbv2",
"error_action.0.elasticsearch",
"error_action.0.firehose",
"error_action.0.http",
"error_action.0.iot_analytics",
"error_action.0.iot_events",
"error_action.0.kinesis",
Expand Down Expand Up @@ -1155,6 +1257,10 @@ func resourceAwsIotTopicRuleRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error setting firehose: %w", err)
}

if err := d.Set("http", flattenIotHttpActions(out.Rule.Actions)); err != nil {
return fmt.Errorf("error setting http: %w", err)
}

if err := d.Set("iot_analytics", flattenIotIotAnalyticsActions(out.Rule.Actions)); err != nil {
return fmt.Errorf("error setting iot_analytics: %w", err)
}
Expand Down Expand Up @@ -1210,6 +1316,7 @@ func resourceAwsIotTopicRuleUpdate(d *schema.ResourceData, meta interface{}) err
"elasticsearch",
"enabled",
"firehose",
"http",
"iot_analytics",
"iot_events",
"kinesis",
Expand Down Expand Up @@ -1462,6 +1569,42 @@ func expandIotFirehoseAction(tfList []interface{}) *iot.FirehoseAction {
return apiObject
}

func expandIotHttpAction(tfList []interface{}) *iot.HttpAction {
if len(tfList) == 0 || tfList[0] == nil {
return nil
}

apiObject := &iot.HttpAction{}
tfMap := tfList[0].(map[string]interface{})

if v, ok := tfMap["url"].(string); ok && v != "" {
apiObject.Url = aws.String(v)
}

if v, ok := tfMap["confirmation_url"].(string); ok && v != "" {
apiObject.ConfirmationUrl = aws.String(v)
}

if v, ok := tfMap["http_header"].([]interface{}); ok {
headerObjs := []*iot.HttpActionHeader{}
for _, val := range v {
if m, ok := val.(map[string]interface{}); ok {
headerObj := &iot.HttpActionHeader{}
if v, ok := m["key"].(string); ok && v != "" {
headerObj.Key = aws.String(v)
}
if v, ok := m["value"].(string); ok && v != "" {
headerObj.Value = aws.String(v)
}
headerObjs = append(headerObjs, headerObj)
}
}
apiObject.Headers = headerObjs
}

return apiObject
}

func expandIotIotAnalyticsAction(tfList []interface{}) *iot.IotAnalyticsAction {
if len(tfList) == 0 || tfList[0] == nil {
return nil
Expand Down Expand Up @@ -1726,6 +1869,17 @@ func expandIotTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload {
actions = append(actions, &iot.Action{Firehose: action})
}

// Legacy root attribute handling
for _, tfMapRaw := range d.Get("http").(*schema.Set).List() {
action := expandIotHttpAction([]interface{}{tfMapRaw})

if action == nil {
continue
}

actions = append(actions, &iot.Action{Http: action})
}

// Legacy root attribute handling
for _, tfMapRaw := range d.Get("iot_analytics").(*schema.Set).List() {
action := expandIotIotAnalyticsAction([]interface{}{tfMapRaw})
Expand Down Expand Up @@ -1896,6 +2050,16 @@ func expandIotTopicRulePayload(d *schema.ResourceData) *iot.TopicRulePayload {

iotErrorAction = &iot.Action{Firehose: action}
}
case "http":
for _, tfMapRaw := range v.([]interface{}) {
action := expandIotHttpAction([]interface{}{tfMapRaw})

if action == nil {
continue
}

iotErrorAction = &iot.Action{Http: action}
}
case "iot_analytics":
for _, tfMapRaw := range v.([]interface{}) {
action := expandIotIotAnalyticsAction([]interface{}{tfMapRaw})
Expand Down Expand Up @@ -2644,6 +2808,10 @@ func flattenIotErrorAction(errorAction *iot.Action) []map[string]interface{} {
results = append(results, map[string]interface{}{"firehose": flattenIotFirehoseActions(input)})
return results
}
if errorAction.Http != nil {
results = append(results, map[string]interface{}{"http": flattenIotHttpActions(input)})
return results
}
if errorAction.IotAnalytics != nil {
results = append(results, map[string]interface{}{"iot_analytics": flattenIotIotAnalyticsActions(input)})
return results
Expand Down Expand Up @@ -2705,3 +2873,51 @@ func flattenIotStepFunctionsAction(apiObject *iot.StepFunctionsAction) []interfa

return []interface{}{tfMap}
}

// Legacy root attribute handling
func flattenIotHttpActions(actions []*iot.Action) []interface{} {
results := make([]interface{}, 0)

for _, action := range actions {
if action == nil {
continue
}

if v := action.Http; v != nil {
results = append(results, flattenIotHttpAction(v)...)
}
}

return results
}

func flattenIotHttpAction(apiObject *iot.HttpAction) []interface{} {
if apiObject == nil {
return nil
}

tfMap := make(map[string]interface{})

if v := apiObject.Url; v != nil {
tfMap["url"] = aws.StringValue(v)
}

if v := apiObject.ConfirmationUrl; v != nil {
tfMap["confirmation_url"] = aws.StringValue(v)
}

if v := apiObject.Headers; v != nil {
headers := []map[string]string{}

for _, h := range v {
m := map[string]string{
"key": aws.StringValue(h.Key),
"value": aws.StringValue(h.Value),
}
headers = append(headers, m)
}
tfMap["http_header"] = headers
}

return []interface{}{tfMap}
}
Loading