Skip to content

Commit

Permalink
Merge pull request russellcardullo#93 from marcelomilera/feature/add-…
Browse files Browse the repository at this point in the history
…pingdom-teams-data-source

Add pingdom_team data source
  • Loading branch information
russellcardullo authored Nov 28, 2020
2 parents 0b913da + c13cf2f commit 653f94f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pingdom/data_source_pingdom_team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package pingdom

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/russellcardullo/go-pingdom/pingdom"
)

func dataSourcePingdomTeam() *schema.Resource {
return &schema.Resource{
Read: dataSourcePingdomTeamRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"member_ids": {
Type: schema.TypeSet,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}

func dataSourcePingdomTeamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)
name := d.Get("name").(string)
teams, err := client.Teams.List()
log.Printf("==== teams : %v", teams)
if err != nil {
return fmt.Errorf("Error retrieving team: %s", err)
}
var found *pingdom.TeamResponse
for _, team := range teams {
if team.Name == name {
log.Printf("Team: %v", team)
found = &team
break
}
}
if found == nil {
return fmt.Errorf("User '%s' not found", name)
}
if err = d.Set("name", found.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}

var memberIds []int
for _, member := range found.Members {
memberIds = append(memberIds, member.ID)
}

if err = d.Set("member_ids", memberIds); err != nil {
return err
}

d.SetId(fmt.Sprintf("%d", found.ID))
return nil
}
1 change: 1 addition & 0 deletions pingdom/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Provider() terraform.ResourceProvider {
},
DataSourcesMap: map[string]*schema.Resource{
"pingdom_contact": dataSourcePingdomContact(),
"pingdom_team": dataSourcePingdomTeam(),
},
ConfigureFunc: providerConfigure,
}
Expand Down

0 comments on commit 653f94f

Please sign in to comment.