diff --git a/codefresh/data_current_account_user.go b/codefresh/data_current_account_user.go new file mode 100644 index 0000000..93b516e --- /dev/null +++ b/codefresh/data_current_account_user.go @@ -0,0 +1,87 @@ +package codefresh + +import ( + "fmt" + + "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceCurrentAccountUser() *schema.Resource { + return &schema.Resource{ + Description: "Returns a user the current Codefresh account by name or email.", + Read: dataSourceCurrentAccountUserRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + ExactlyOneOf: []string{"name", "email"}, + Optional: true, + }, + "email": { + Type: schema.TypeString, + ExactlyOneOf: []string{"name", "email"}, + Optional: true, + }, + }, + } +} + +func dataSourceCurrentAccountUserRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*cfclient.Client) + var currentAccount *cfclient.CurrentAccount + var err error + + currentAccount, err = client.GetCurrentAccount() + + if err != nil { + return err + } + + if currentAccount == nil { + return fmt.Errorf("data.codefresh_current_account - failed to get current_account") + } + + var ( + userAttributeName string + userAttributeValue string + ) + + if _email, _emailOk := d.GetOk("email"); _emailOk { + userAttributeName = "email" + userAttributeValue = _email.(string) + } else if _name, _nameOk := d.GetOk("name"); _nameOk { + userAttributeName = "name" + userAttributeValue = _name.(string) + } else { + return fmt.Errorf("data.codefresh_current_account_user - must specify name or email") + } + + return mapDataCurrentAccountUserToResource(currentAccount, d, userAttributeName, userAttributeValue) + +} + +func mapDataCurrentAccountUserToResource(currentAccount *cfclient.CurrentAccount, d *schema.ResourceData, userAttributeName string, userAttributeValue string) error { + + if currentAccount == nil || currentAccount.ID == "" { + return fmt.Errorf("data.codefresh_current_account - failed to mapDataCurrentAccountUserToResource no id for current account set") + } + + isFound := false + + + for _, user := range currentAccount.Users { + if (userAttributeName == "name" && user.UserName == userAttributeValue) || (userAttributeName == "email" && user.Email == userAttributeValue) { + isFound = true + d.SetId(user.ID) + d.Set("name", user.UserName) + d.Set("email", user.Email) + break + } + } + + if !isFound { + return fmt.Errorf("data.codefresh_current_account_user - cannot find user with %s %s", userAttributeName, userAttributeValue) + } + + return nil +} diff --git a/codefresh/provider.go b/codefresh/provider.go index 1121ccd..39d2fbf 100644 --- a/codefresh/provider.go +++ b/codefresh/provider.go @@ -53,6 +53,7 @@ func Provider() *schema.Provider { "codefresh_account_idp": dataSourceAccountIdp(), "codefresh_project": dataSourceProject(), "codefresh_account_gitops_settings": dataSourceAccountGitopsSettings(), + "codefresh_current_account_user": dataSourceCurrentAccountUser(), "codefresh_service_account": dataSourceServiceAccount(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/docs/data-sources/current_account_user.md b/docs/data-sources/current_account_user.md new file mode 100644 index 0000000..64d8b26 --- /dev/null +++ b/docs/data-sources/current_account_user.md @@ -0,0 +1,27 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "codefresh_current_account_user Data Source - terraform-provider-codefresh" +subcategory: "" +description: |- + Returns a user the current Codefresh account by name or email. +--- + +# codefresh_current_account_user (Data Source) + +Returns a user the current Codefresh account by name or email. + + + + +## Schema + +### Optional + +- `email` (String) +- `name` (String) + +### Read-Only + +- `id` (String) The ID of this resource. + +