Skip to content

Commit

Permalink
Simplify flatten functions
Browse files Browse the repository at this point in the history
added DiffSuppressFunc to work around AWS returning empty objects
  • Loading branch information
marcoreni committed Sep 28, 2018
1 parent a40c8ce commit 4d39884
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 48 deletions.
69 changes: 27 additions & 42 deletions aws/resource_aws_pinpoint_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func resourceAwsPinpointApp() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lambda_function_name": {
Expand All @@ -72,6 +78,12 @@ func resourceAwsPinpointApp() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"daily": {
Expand All @@ -97,6 +109,12 @@ func resourceAwsPinpointApp() *schema.Resource {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"end": {
Expand Down Expand Up @@ -272,21 +290,9 @@ func flattenPinpointCampaignHook(ch *pinpoint.CampaignHook) []interface{} {

m := map[string]interface{}{}

if ch.LambdaFunctionName != nil {
m["lambda_function_name"] = *ch.LambdaFunctionName
}

if ch.Mode != nil && *ch.Mode != "" {
m["mode"] = *ch.Mode
}

if ch.WebUrl != nil {
m["web_url"] = *ch.WebUrl
}

if len(m) <= 0 {
return nil
}
m["lambda_function_name"] = aws.StringValue(ch.LambdaFunctionName)
m["mode"] = aws.StringValue(ch.Mode)
m["web_url"] = aws.StringValue(ch.WebUrl)

l = append(l, m)

Expand Down Expand Up @@ -326,23 +332,10 @@ func flattenPinpointCampaignLimits(cl *pinpoint.CampaignLimits) []interface{} {

m := map[string]interface{}{}

if cl.Daily != nil {
m["daily"] = *cl.Daily
}
if cl.MaximumDuration != nil {
m["maximum_duration"] = *cl.MaximumDuration
}
if cl.MessagesPerSecond != nil {
m["messages_per_second"] = *cl.MessagesPerSecond
}

if cl.Total != nil {
m["total"] = *cl.Total
}

if len(m) <= 0 {
return nil
}
m["daily"] = aws.Int64Value(cl.Daily)
m["maximum_duration"] = aws.Int64Value(cl.MaximumDuration)
m["messages_per_second"] = aws.Int64Value(cl.MessagesPerSecond)
m["total"] = aws.Int64Value(cl.Total)

l = append(l, m)

Expand Down Expand Up @@ -374,16 +367,8 @@ func flattenPinpointQuietTime(qt *pinpoint.QuietTime) []interface{} {

m := map[string]interface{}{}

if qt.End != nil {
m["end"] = *qt.End
}
if qt.Start != nil {
m["start"] = *qt.Start
}

if len(m) <= 0 {
return nil
}
m["end"] = aws.StringValue(qt.End)
m["start"] = aws.StringValue(qt.Start)

l = append(l, m)

Expand Down
6 changes: 0 additions & 6 deletions aws/resource_aws_pinpoint_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func TestAccAWSPinpointApp_CampaignHookLambda(t *testing.T) {
testAccCheckAWSPinpointAppExists(resourceName, &application),
resource.TestCheckResourceAttr(resourceName, "campaign_hook.#", "1"),
resource.TestCheckResourceAttr(resourceName, "campaign_hook.0.mode", "DELIVERY"),
resource.TestCheckResourceAttr(resourceName, "limits.#", "0"),
resource.TestCheckResourceAttr(resourceName, "quiet_time.#", "0"),
),
},
{
Expand Down Expand Up @@ -94,10 +92,8 @@ func TestAccAWSPinpointApp_Limits(t *testing.T) {
Config: testAccAWSPinpointAppConfig_Limits,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSPinpointAppExists(resourceName, &application),
resource.TestCheckResourceAttr(resourceName, "campaign_hook.#", "0"),
resource.TestCheckResourceAttr(resourceName, "limits.#", "1"),
resource.TestCheckResourceAttr(resourceName, "limits.0.total", "500"),
resource.TestCheckResourceAttr(resourceName, "quiet_time.#", "0"),
),
},
{
Expand Down Expand Up @@ -127,8 +123,6 @@ func TestAccAWSPinpointApp_QuietTime(t *testing.T) {
Config: testAccAWSPinpointAppConfig_QuietTime,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSPinpointAppExists(resourceName, &application),
resource.TestCheckResourceAttr(resourceName, "campaign_hook.#", "0"),
resource.TestCheckResourceAttr(resourceName, "limits.#", "0"),
resource.TestCheckResourceAttr(resourceName, "quiet_time.#", "1"),
resource.TestCheckResourceAttr(resourceName, "quiet_time.0.start", "00:00"),
),
Expand Down

0 comments on commit 4d39884

Please sign in to comment.