Skip to content

Commit 1bd3974

Browse files
add datasource
1 parent 1654c4e commit 1bd3974

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed

codefresh/data_service_account.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
6+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceServiceAccount() *schema.Resource {
11+
return &schema.Resource{
12+
Description: "This data source retrieves a Codefresh service account by its ID or name.",
13+
Read: dataSourceServiceAccountRead,
14+
Schema: map[string]*schema.Schema{
15+
"_id": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
},
19+
"name": {
20+
Description: "Service account name",
21+
Type: schema.TypeString,
22+
Optional: true,
23+
AtLeastOneOf: []string{"_id", "name"},
24+
},
25+
"assign_admin_role": {
26+
Description: "Whether or not account admin role is assigned to the service account",
27+
Type: schema.TypeBool,
28+
Optional: true,
29+
},
30+
"assigned_teams": {
31+
Description: "A list of team IDs the service account is be assigned to",
32+
Type: schema.TypeSet,
33+
Optional: true,
34+
Elem: &schema.Schema{
35+
Type: schema.TypeString,
36+
},
37+
},
38+
},
39+
}
40+
}
41+
42+
func dataSourceServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
43+
44+
client := meta.(*cfClient.Client)
45+
var serviceAccount *cfClient.ServiceUser
46+
var err error
47+
48+
if _id, _idOk := d.GetOk("_id"); _idOk {
49+
serviceAccount, err = client.GetServiceUserByID(_id.(string))
50+
} else if name, nameOk := d.GetOk("name"); nameOk {
51+
serviceAccount, err = client.GetServiceUserByName(name.(string))
52+
}
53+
54+
if err != nil {
55+
return err
56+
}
57+
58+
if serviceAccount == nil {
59+
return fmt.Errorf("data.codefresh_service_account - cannot find service account")
60+
}
61+
62+
return mapDataServiceAccountToResource(serviceAccount, d)
63+
64+
}
65+
66+
func mapDataServiceAccountToResource(serviceAccount *cfClient.ServiceUser, d *schema.ResourceData) error {
67+
68+
if serviceAccount == nil || serviceAccount.ID == "" {
69+
return fmt.Errorf("data.codefresh_service_account - failed to mapDataServiceAccountToResource")
70+
}
71+
72+
d.SetId(serviceAccount.ID)
73+
d.Set("name", serviceAccount.Name)
74+
d.Set("assign_admin_role", serviceAccount.HasAdminRole())
75+
76+
teamIds := []string{}
77+
78+
for _, team := range serviceAccount.Teams {
79+
teamIds = append(teamIds, team.ID)
80+
}
81+
82+
d.Set("assigned_teams", teamIds)
83+
84+
return nil
85+
}

codefresh/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func Provider() *schema.Provider {
5353
"codefresh_account_idp": dataSourceAccountIdp(),
5454
"codefresh_project": dataSourceProject(),
5555
"codefresh_account_gitops_settings": dataSourceAccountGitopsSettings(),
56+
"codefresh_service_account": dataSourceServiceAccount(),
5657
},
5758
ResourcesMap: map[string]*schema.Resource{
5859
"codefresh_account": resourceAccount(),

codefresh/resource_service_account.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,6 @@ func mapServiceAccountToResource(serviceAccount *cfclient.ServiceUser, d *schema
140140
return nil
141141
}
142142

143-
func flattenServiceAccountTeams(users []cfclient.TeamUser) []string {
144-
res := []string{}
145-
for _, user := range users {
146-
res = append(res, user.ID)
147-
}
148-
return res
149-
}
150-
151143
func mapResourceToServiceAccount(d *schema.ResourceData) *cfclient.ServiceUserCreateUpdate {
152144

153145
return &cfclient.ServiceUserCreateUpdate{

docs/data-sources/service_account.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "codefresh_service_account Data Source - terraform-provider-codefresh"
4+
subcategory: ""
5+
description: |-
6+
This data source retrieves a Codefresh service account by its ID or name.
7+
---
8+
9+
# codefresh_service_account (Data Source)
10+
11+
This data source retrieves a Codefresh service account by its ID or name.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `_id` (String)
21+
- `assign_admin_role` (Boolean) Whether or not account admin role is assigned to the service account
22+
- `assigned_teams` (Set of String) A list of team IDs the service account is be assigned to
23+
- `name` (String) Service account name
24+
25+
### Read-Only
26+
27+
- `id` (String) The ID of this resource.
28+
29+

0 commit comments

Comments
 (0)