forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_pagerduty_priority.go
60 lines (47 loc) · 1.19 KB
/
data_source_pagerduty_priority.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package pagerduty
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)
func dataSourcePagerDutyPriority() *schema.Resource {
return &schema.Resource{
Read: dataSourcePagerDutyPriorityRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the priority to find in the PagerDuty API",
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourcePagerDutyPriorityRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)
log.Printf("[INFO] Reading PagerDuty priority")
searchTeam := d.Get("name").(string)
resp, _, err := client.Priorities.List()
if err != nil {
return err
}
var found *pagerduty.Priority
for _, priority := range resp.Priorities {
if strings.EqualFold(priority.Name, searchTeam) {
found = priority
break
}
}
if found == nil {
return fmt.Errorf("Unable to locate any priority with name: %s", searchTeam)
}
d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("description", found.Description)
return nil
}