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

Address: pagerduty_schedule output not showing rendered_coverage_percentage #528

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
36 changes: 36 additions & 0 deletions pagerduty/resource_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func resourcePagerDutySchedule() *schema.Resource {
},
},

"rendered_coverage_percentage": {
Type: schema.TypeString,
Computed: true,
},

"restriction": {
Optional: true,
Type: schema.TypeList,
Expand Down Expand Up @@ -164,6 +169,23 @@ func resourcePagerDutySchedule() *schema.Resource {
Type: schema.TypeString,
},
},

"final_schedule": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"rendered_coverage_percentage": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -248,6 +270,9 @@ func resourcePagerDutyScheduleRead(d *schema.ResourceData, meta interface{}) err
if err := d.Set("teams", flattenShedTeams(schedule.Teams)); err != nil {
return resource.NonRetryableError(fmt.Errorf("error setting teams: %s", err))
}
if err := d.Set("final_schedule", flattenScheFinalSchedule(schedule.FinalSchedule)); err != nil {
return resource.NonRetryableError(fmt.Errorf("error setting final_schedule: %s", err))
}

}
return nil
Expand Down Expand Up @@ -445,6 +470,7 @@ func flattenScheduleLayers(v []*pagerduty.ScheduleLayer) ([]map[string]interface
"start": sl.Start,
"rotation_virtual_start": sl.RotationVirtualStart,
"rotation_turn_length_seconds": sl.RotationTurnLengthSeconds,
"rendered_coverage_percentage": renderRoundedPercentage(sl.RenderedCoveragePercentage),
}

var users []string
Expand Down Expand Up @@ -510,3 +536,13 @@ func flattenShedTeams(teams []*pagerduty.TeamReference) []string {

return res
}

func flattenScheFinalSchedule(finalSche *pagerduty.SubSchedule) []map[string]interface{} {
var res []map[string]interface{}
elem := make(map[string]interface{})
elem["name"] = finalSche.Name
elem["rendered_coverage_percentage"] = renderRoundedPercentage(finalSche.RenderedCoveragePercentage)
res = append(res, elem)

return res
}
4 changes: 4 additions & 0 deletions pagerduty/resource_pagerduty_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func TestAccPagerDutySchedule_Basic(t *testing.T) {
"pagerduty_schedule.foo", "layer.0.name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "layer.0.start", start),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "layer.0.rendered_coverage_percentage", "0.00"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "final_schedule.0.rendered_coverage_percentage", "0.00"),
resource.TestCheckResourceAttr(
"pagerduty_schedule.foo", "layer.0.rotation_virtual_start", rotationVirtualStart),
),
Expand Down
8 changes: 8 additions & 0 deletions pagerduty/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"math"
"strings"
"time"

Expand Down Expand Up @@ -152,3 +153,10 @@ func intTypeToIntPtr(v int) *int {
}
return &v
}

// renderRoundedPercentage is a helper function to render percetanges
// represented as float64 numbers, by its round with two decimals string
// representation.
func renderRoundedPercentage(p float64) string {
return fmt.Sprintf("%.2f", math.Round(p*100))
}