Help with PagerDuty Terraform Provider

questions

(Derek Moore) #1

Evening,

I’m trying to write an extension for the Terraform PagerDuty provider to return a list of UserIds for a Team - this is needed to automate the creaiton of schedules.

I’ve got the a new DataSource - dataSourcePagerDutyUsers - and this is building and installed in my local plugin directory. When I run an apply, I can see in the trace that the user ids are being retrieved but the terraform eval is failing. I’ve added the datasource to the Provider and the code is definitely calling the updated provider code. Any clues on what steps I’m missing - sorry, the custom datasource provider doco seems to be pretty light.

Cheers,

Derek

package pagerduty

import (

"fmt"

"log"

"[github.com/hashicorp/terraform/helper/schema](http://github.com/hashicorp/terraform/helper/schema)"

"[github.com/heimweh/go-pagerduty/pagerduty](http://github.com/heimweh/go-pagerduty/pagerduty)"

)

func dataSourcePagerDutyUsers() *schema.Resource {

return &schema.Resource{

Read: dataSourcePagerDutyUsersRead,

Schema: map[string]*schema.Schema{

"team": {

Type:     schema.TypeString,

Required: true,

},

"users": {

Type:     schema.TypeList,

Computed: true,

Elem:     &schema.Schema{Type: schema.TypeString},

},

},

}

}

func dataSourcePagerDutyUsersRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*pagerduty.Client)

log.Printf("[INFO] Reading PagerDuty users")

searchTeam := []string{d.Get("team").(string)}

log.Printf("[INFO] *** Getting users for team: %s", searchTeam)

o := &pagerduty.ListUsersOptions{

TeamIDs: searchTeam,

}

log.Println("[INFO] *** After call")

resp, _, err := client.Users.List(o)

if err != nil {

log.Println("[ERROR] *** We've errored: " + err.Error())

return err

}

filteredUsers := resp.Users[:]

log.Printf("[INFO] *** Got some users: %d", len(filteredUsers))

if len(filteredUsers) < 1 {

return fmt.Errorf("Unable to locate any user for Team: %s", searchTeam)

}

var userids []string

for _, user := range filteredUsers {

userids = append(userids, user.ID)

}

log.Printf("[INFO] *** Setting the user ids: %d", len(userids))

if err := d.Set("users", userids); err != nil {

return fmt.Errorf("error setting users: %s", err)

}

return nil

}

Output from apply

data.pagerduty_users.ap_support: Refreshing state...

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [INFO] Reading PagerDuty users

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [INFO] *** Getting users for team: [*******]

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [INFO] *** After call

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [DEBUG] PagerDuty - Preparing GET request to /users?team_ids%5B%5D=***** with body: %!s(<nil>)

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [DEBUG] PagerDuty API Request Details:

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: ---[ REQUEST ]---------------------------------------

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: GET /users?team_ids%5B%5D=****** HTTP/1.1

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: Host: [api.pagerduty.com](http://api.pagerduty.com/)

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: User-Agent: (darwin amd64) Terraform/0.10.0-dev

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: Accept: application/vnd.pagerduty+json;version=2

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: Content-Type: application/json

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: Accept-Encoding: gzip

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 

2018-11-14T21:13:41.243+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: -----------------------------------------------------

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [DEBUG] PagerDuty API Response Details:

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: ---[ RESPONSE ]--------------------------------------

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: HTTP/2.0 200 OK

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: -----------------------------------------------------

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [INFO] *** Got some users: 6

2018-11-14T21:13:41.598+1300 [DEBUG] plugin.terraform-provider-pagerduty_v1.2.1_x4: 2018/11/14 21:13:41 [INFO] *** Setting the user ids: %!s(int=6)

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalWriteState

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalWriteDiff

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalUpdateStateHook

2018/11/14 21:13:41 [TRACE] [walkApply] Exiting eval tree: data.pagerduty_users.ap_support

2018/11/14 21:13:41 [TRACE] dag/walk: walking "pagerduty_schedule.ap_support"

2018/11/14 21:13:41 [TRACE] vertex 'root.pagerduty_schedule.ap_support': walking

2018/11/14 21:13:41 [TRACE] vertex 'root.pagerduty_schedule.ap_support': evaluating

2018/11/14 21:13:41 [TRACE] [walkApply] Entering eval tree: pagerduty_schedule.ap_support

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalSequence

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalInstanceInfo

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalReadDiff

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalIf

2018/11/14 21:13:41 [TRACE] root: eval: terraform.EvalNoop

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalIf

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalInterpolate

2018/11/14 21:13:41 [TRACE] root: eval: *terraform.EvalInterpolate

2018/11/14 21:13:41 [ERROR] root: eval: *terraform.EvalInterpolate, err: Resource 'data.pagerduty_users.ap_support' not found for variable 'data.pagerduty_users.ap_support.users'

2018/11/14 21:13:41 [ERROR] root: eval: *terraform.EvalSequence, err: Resource 'data.pagerduty_users.ap_support' not found for variable 'data.pagerduty_users.ap_support.users'

2018/11/14 21:13:41 [TRACE] [walkApply] Exiting eval tree: pagerduty_schedule.ap_support

2018/11/14 21:13:41 [TRACE] dag/walk: upstream errored, not walking "provider.pagerduty (close)"

(geeth) #2

Perhaps you can post this issue in the GitHub repository for support on the DataSource directly!

If there’s as aspect where that’s equivalent to the REST API where you GET /users with a specific team_ids, you could pull the users through that platform.

It also appears there was a recent release available from the terraform-providers of that GitHub repo.


(Derek Moore) #3

Thanks Geeth. Have resolved the now.

Derek


(system) #4

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.