-
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
1 parent
1654c4e
commit 1bd3974
Showing
4 changed files
with
115 additions
and
8 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,85 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
|
||
cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceServiceAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "This data source retrieves a Codefresh service account by its ID or name.", | ||
Read: dataSourceServiceAccountRead, | ||
Schema: map[string]*schema.Schema{ | ||
"_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"name": { | ||
Description: "Service account name", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
AtLeastOneOf: []string{"_id", "name"}, | ||
}, | ||
"assign_admin_role": { | ||
Description: "Whether or not account admin role is assigned to the service account", | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
"assigned_teams": { | ||
Description: "A list of team IDs the service account is be assigned to", | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceServiceAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
client := meta.(*cfClient.Client) | ||
var serviceAccount *cfClient.ServiceUser | ||
var err error | ||
|
||
if _id, _idOk := d.GetOk("_id"); _idOk { | ||
serviceAccount, err = client.GetServiceUserByID(_id.(string)) | ||
} else if name, nameOk := d.GetOk("name"); nameOk { | ||
serviceAccount, err = client.GetServiceUserByName(name.(string)) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if serviceAccount == nil { | ||
return fmt.Errorf("data.codefresh_service_account - cannot find service account") | ||
} | ||
|
||
return mapDataServiceAccountToResource(serviceAccount, d) | ||
|
||
} | ||
|
||
func mapDataServiceAccountToResource(serviceAccount *cfClient.ServiceUser, d *schema.ResourceData) error { | ||
|
||
if serviceAccount == nil || serviceAccount.ID == "" { | ||
return fmt.Errorf("data.codefresh_service_account - failed to mapDataServiceAccountToResource") | ||
} | ||
|
||
d.SetId(serviceAccount.ID) | ||
d.Set("name", serviceAccount.Name) | ||
d.Set("assign_admin_role", serviceAccount.HasAdminRole()) | ||
|
||
teamIds := []string{} | ||
|
||
for _, team := range serviceAccount.Teams { | ||
teamIds = append(teamIds, team.ID) | ||
} | ||
|
||
d.Set("assigned_teams", teamIds) | ||
|
||
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
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
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,29 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "codefresh_service_account Data Source - terraform-provider-codefresh" | ||
subcategory: "" | ||
description: |- | ||
This data source retrieves a Codefresh service account by its ID or name. | ||
--- | ||
|
||
# codefresh_service_account (Data Source) | ||
|
||
This data source retrieves a Codefresh service account by its ID or name. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Optional | ||
|
||
- `_id` (String) | ||
- `assign_admin_role` (Boolean) Whether or not account admin role is assigned to the service account | ||
- `assigned_teams` (Set of String) A list of team IDs the service account is be assigned to | ||
- `name` (String) Service account name | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
|