forked from russellcardullo/terraform-provider-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request russellcardullo#93 from marcelomilera/feature/add-…
…pingdom-teams-data-source Add pingdom_team data source
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters