-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
167 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,72 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAccountRead, | ||
Schema: map[string]*schema.Schema{ | ||
"_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"admins": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
|
||
func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
|
||
client := meta.(*cfClient.Client) | ||
var account *cfClient.Account | ||
var err error | ||
|
||
if _id, _idOk := d.GetOk("_id"); _idOk { | ||
account, err = client.GetAccountByID(_id.(string)) | ||
} else if name, nameOk := d.GetOk("name"); nameOk { | ||
account, err = client.GetAccountByName(name.(string)) | ||
} else { | ||
return fmt.Errorf("data.codefresh_account - must specify _id or name") | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if account == nil { | ||
return fmt.Errorf("data.codefresh_account - cannot find account") | ||
} | ||
|
||
return mapDataAccountToResource(account, d) | ||
|
||
} | ||
|
||
func mapDataAccountToResource(account *cfClient.Account, d *schema.ResourceData) error { | ||
|
||
if account == nil || account.ID == "" { | ||
return fmt.Errorf("data.codefresh_account - failed to mapDataAccountToResource") | ||
} | ||
d.SetId(account.ID) | ||
|
||
d.Set("_id", account.ID) | ||
d.Set("name", account.Name) | ||
d.Set("admins", account.Admins) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceTeam() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceTeamRead, | ||
Schema: map[string]*schema.Schema{ | ||
"_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"users": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"tags": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
|
||
func dataSourceTeamRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
|
||
client := meta.(*cfClient.Client) | ||
var team *cfClient.Team | ||
var err error | ||
|
||
if _id, _idOk := d.GetOk("_id"); _idOk { | ||
team, err = client.GetTeamByID(_id.(string)) | ||
} else if name, nameOk := d.GetOk("name"); nameOk { | ||
// accountID, accountOk := d.GetOk("account_id"); | ||
team, err = client.GetTeamByName(name.(string)) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if team == nil { | ||
return fmt.Errorf("data.codefresh_team - cannot find team") | ||
} | ||
|
||
return mapDataTeamToResource(team, d) | ||
|
||
} | ||
|
||
func mapDataTeamToResource(team *cfClient.Team, d *schema.ResourceData) error { | ||
|
||
if team == nil || team.ID == "" { | ||
return fmt.Errorf("data.codefresh_team - failed to mapDataTeamToResource") | ||
} | ||
d.SetId(team.ID) | ||
|
||
d.Set("_id", team.ID) | ||
d.Set("account_id", team.Account) | ||
d.Set("type", team.Type) | ||
|
||
var users []string | ||
for _, user := range team.Users { | ||
users = append(users, user.ID) | ||
} | ||
d.Set("users", users) | ||
d.Set("tags", team.Tags) | ||
|
||
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