Skip to content

Commit

Permalink
add datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-medvedev-codefresh committed Jan 7, 2025
1 parent 1654c4e commit 1bd3974
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
85 changes: 85 additions & 0 deletions codefresh/data_service_account.go
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
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Provider() *schema.Provider {
"codefresh_account_idp": dataSourceAccountIdp(),
"codefresh_project": dataSourceProject(),
"codefresh_account_gitops_settings": dataSourceAccountGitopsSettings(),
"codefresh_service_account": dataSourceServiceAccount(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_account": resourceAccount(),
Expand Down
8 changes: 0 additions & 8 deletions codefresh/resource_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ func mapServiceAccountToResource(serviceAccount *cfclient.ServiceUser, d *schema
return nil
}

func flattenServiceAccountTeams(users []cfclient.TeamUser) []string {
res := []string{}
for _, user := range users {
res = append(res, user.ID)
}
return res
}

func mapResourceToServiceAccount(d *schema.ResourceData) *cfclient.ServiceUserCreateUpdate {

return &cfclient.ServiceUserCreateUpdate{
Expand Down
29 changes: 29 additions & 0 deletions docs/data-sources/service_account.md
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.


0 comments on commit 1bd3974

Please sign in to comment.