|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func dataSourceTeam() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + Read: dataSourceTeamRead, |
| 12 | + Schema: map[string]*schema.Schema{ |
| 13 | + "_id": { |
| 14 | + Type: schema.TypeString, |
| 15 | + Optional: true, |
| 16 | + }, |
| 17 | + "name": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Optional: true, |
| 20 | + }, |
| 21 | + "account_id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Optional: true, |
| 24 | + }, |
| 25 | + "type": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + }, |
| 29 | + "users": { |
| 30 | + Type: schema.TypeList, |
| 31 | + Optional: true, |
| 32 | + Elem: &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + }, |
| 35 | + }, |
| 36 | + "tags": { |
| 37 | + Type: schema.TypeList, |
| 38 | + Optional: true, |
| 39 | + Elem: &schema.Schema{ |
| 40 | + Type: schema.TypeString, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +func dataSourceTeamRead(d *schema.ResourceData, meta interface{}) error { |
| 49 | + |
| 50 | + |
| 51 | + client := meta.(*cfClient.Client) |
| 52 | + var team *cfClient.Team |
| 53 | + var err error |
| 54 | + |
| 55 | + if _id, _idOk := d.GetOk("_id"); _idOk { |
| 56 | + team, err = client.GetTeamByID(_id.(string)) |
| 57 | + } else if name, nameOk := d.GetOk("name"); nameOk { |
| 58 | + // accountID, accountOk := d.GetOk("account_id"); |
| 59 | + team, err = client.GetTeamByName(name.(string)) |
| 60 | + } |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if team == nil { |
| 67 | + return fmt.Errorf("data.codefresh_team - cannot find team") |
| 68 | + } |
| 69 | + |
| 70 | + return mapDataTeamToResource(team, d) |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +func mapDataTeamToResource(team *cfClient.Team, d *schema.ResourceData) error { |
| 75 | + |
| 76 | + if team == nil || team.ID == "" { |
| 77 | + return fmt.Errorf("data.codefresh_team - failed to mapDataTeamToResource") |
| 78 | + } |
| 79 | + d.SetId(team.ID) |
| 80 | + |
| 81 | + d.Set("_id", team.ID) |
| 82 | + d.Set("account_id", team.Account) |
| 83 | + d.Set("type", team.Type) |
| 84 | + |
| 85 | + var users []string |
| 86 | + for _, user := range team.Users { |
| 87 | + users = append(users, user.ID) |
| 88 | + } |
| 89 | + d.Set("users", users) |
| 90 | + d.Set("tags", team.Tags) |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
0 commit comments