Skip to content

Commit 1654c4e

Browse files
add tests and docs
1 parent c78b225 commit 1654c4e

File tree

7 files changed

+324
-61
lines changed

7 files changed

+324
-61
lines changed

codefresh/resource_api_key.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
func resourceApiKey() *schema.Resource {
1414
return &schema.Resource{
1515
Description: `
16-
Manages an API Key tied to an Account and a User.
17-
Requires a Codefresh admin token and applies only to Codefresh on-premises installations.
16+
Manages an API Key tied to a user within an account or a service account within the current account.
17+
On the Codefresh SaaS platfrom this resource is only usable for service accounts.
18+
Management of API keys for users in other accounts requires admin priveleges and hence can only be done on Codefresh on-premises installations.
1819
`,
1920
Create: resourceApiKeyCreate,
2021
Read: resourceApiKeyRead,

codefresh/resource_api_key_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
)
12+
13+
var apiKeyNamePrefix = "TerraformAccTest_"
14+
15+
func TestAccCodefreshAPIKey_ServiceUser(t *testing.T) {
16+
name := apiKeyNamePrefix + acctest.RandString(10)
17+
18+
resourceName := "codefresh_api_key.test_apikey"
19+
serviceAccountResourceName := "codefresh_service_account.test_apikey"
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { testAccPreCheck(t) },
23+
Providers: testAccProviders,
24+
CheckDestroy: testAccCheckCodefreshServiceUserAndAPIKeyDestroyed,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccCodefreshAPIKeyServiceAccount(name, name),
28+
Check: resource.ComposeTestCheckFunc(
29+
testAccCheckCodefreshServiceUserAPIKeyExists(resourceName, serviceAccountResourceName),
30+
resource.TestCheckResourceAttr(resourceName, "name", name),
31+
resource.TestCheckResourceAttr(resourceName, "scopes.0", "agent"),
32+
),
33+
},
34+
{
35+
ResourceName: resourceName,
36+
RefreshState: true,
37+
},
38+
},
39+
})
40+
}
41+
42+
func testAccCheckCodefreshServiceUserAPIKeyExists(apiKeyResource string, serviceUserResource string) resource.TestCheckFunc {
43+
return func(state *terraform.State) error {
44+
serviceUserState, ok := state.RootModule().Resources[serviceUserResource]
45+
46+
if !ok {
47+
return fmt.Errorf("Not found: %s", serviceUserResource)
48+
}
49+
50+
if serviceUserState.Primary.ID == "" {
51+
return fmt.Errorf("No Record ID is set")
52+
}
53+
54+
apiKeyState, ok := state.RootModule().Resources[apiKeyResource]
55+
56+
if !ok {
57+
return fmt.Errorf("Not found: %s", apiKeyResource)
58+
}
59+
60+
if apiKeyState.Primary.ID == "" {
61+
return fmt.Errorf("No Record ID is set for team")
62+
}
63+
64+
serviceUserID := serviceUserState.Primary.ID
65+
apiKeyID := apiKeyState.Primary.ID
66+
67+
apiClient := testAccProvider.Meta().(*cfclient.Client)
68+
_, err := apiClient.GetAPIKeyServiceUser(apiKeyID, serviceUserID)
69+
70+
if err != nil {
71+
return fmt.Errorf("error fetching service user api key for resource %s. %s", apiKeyID, err)
72+
}
73+
74+
return nil
75+
}
76+
}
77+
78+
func testAccCheckCodefreshServiceUserAndAPIKeyDestroyed(s *terraform.State) error {
79+
apiClient := testAccProvider.Meta().(*cfclient.Client)
80+
81+
for _, rs := range s.RootModule().Resources {
82+
83+
if rs.Type != "codefresh_service_account" && rs.Type != "codefresh_api_key" {
84+
continue
85+
}
86+
87+
var (
88+
serviceAccountId string
89+
apiKeyId string
90+
)
91+
92+
if rs.Type == "codefresh_service_account" {
93+
serviceAccountId = rs.Primary.ID
94+
_, err := apiClient.GetServiceUserByID(serviceAccountId)
95+
96+
if err == nil {
97+
return fmt.Errorf("Alert service account still exists")
98+
}
99+
}
100+
101+
if rs.Type == "codefresh_api_key" {
102+
apiKeyId = rs.Primary.ID
103+
_, err := apiClient.GetAPIKeyServiceUser(apiKeyId, serviceAccountId)
104+
105+
if err == nil {
106+
return fmt.Errorf("Alert api key still exists")
107+
}
108+
}
109+
}
110+
111+
return nil
112+
}
113+
114+
func testAccCodefreshAPIKeyServiceAccount(apiKeyName string, serviceUserName string) string {
115+
return fmt.Sprintf(`
116+
resource "codefresh_service_account" "test_apikey" {
117+
name = "%s"
118+
}
119+
120+
resource "codefresh_api_key" "test_apikey" {
121+
service_account_id = codefresh_service_account.test_apikey.id
122+
name = "%s"
123+
scopes = [
124+
"agent",
125+
"agents",
126+
"audit",
127+
"api-keys"
128+
]
129+
}
130+
131+
132+
`, serviceUserName, apiKeyName)
133+
}

codefresh/resource_service_account_test.go

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ import (
1313

1414
var serviceUserNamePrefix = "TerraformAccTest_"
1515

16-
func TestAccCodefreshServiceUser_basic(t *testing.T) {
16+
func TestAccCodefreshServiceUser_WithTeamAssignment(t *testing.T) {
1717
name := serviceUserNamePrefix + acctest.RandString(10)
1818

19-
resourceName := "codefresh_service_account.test"
20-
teamResourceName := "codefresh_team.test"
19+
resourceName := "codefresh_service_account.test_serviceaccount"
20+
teamResourceName := "codefresh_team.test_serviceaccount"
2121

2222
resource.Test(t, resource.TestCase{
2323
PreCheck: func() { testAccPreCheck(t) },
2424
Providers: testAccProviders,
2525
CheckDestroy: testAccCheckCodefreshServiceUserDestroy,
2626
Steps: []resource.TestStep{
2727
{
28-
Config: testAccCodefreshServiceUserTeamToken(name, name),
28+
Config: testAccCodefreshServiceUserTeam(name, name, false),
2929
Check: resource.ComposeTestCheckFunc(
3030
testAccCheckCodefreshServiceUserExists(resourceName),
3131
testAccCheckCodefreshServiceUserAssignedToTeam(resourceName, teamResourceName),
3232
resource.TestCheckResourceAttr(resourceName, "name", name),
33+
resource.TestCheckResourceAttr(resourceName, "assign_admin_role", "false"),
3334
),
3435
},
3536
{
@@ -137,52 +138,17 @@ func testAccCheckCodefreshServiceUserDestroy(s *terraform.State) error {
137138
return nil
138139
}
139140

140-
func testAccCodefreshServiceUserTeamToken(serviceUserName string, teamName string) string {
141+
func testAccCodefreshServiceUserTeam(serviceUserName string, teamName string, assignAdminRole bool) string {
141142
return fmt.Sprintf(`
142-
resource "codefresh_team" "test" {
143+
resource "codefresh_team" "test_serviceaccount" {
143144
name = "%s"
144145
}
145146
146-
resource "codefresh_service_account" "test" {
147+
resource "codefresh_service_account" "test_serviceaccount" {
147148
name = "%s"
148-
assigned_teams = [codefresh_team.test.id]
149-
}
150-
`, serviceUserName, teamName)
151-
}
152-
153-
// CONFIGS
154-
func testAccCodefreshServiceUserBasicConfig(rName string) string {
155-
return fmt.Sprintf(`
156-
resource "codefresh_service_account" "test" {
157-
name = "%s"
158-
}
159-
`, rName)
160-
}
149+
assigned_teams = [codefresh_team.test_serviceaccount.id]
150+
assign_admin_role = %t
161151
162-
func testAccCodefreshServiceUserBasicConfigTags(rName, tag1, tag2 string) string {
163-
return fmt.Sprintf(`
164-
resource "codefresh_service_user" "test" {
165-
name = "%s"
166-
tags = [
167-
%q,
168-
%q,
169-
]
170-
}
171-
`, rName, tag1, tag2)
172-
}
173-
174-
func testAccCodefreshServiceUserBasicConfigVariables(rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value string) string {
175-
return fmt.Sprintf(`
176-
resource "codefresh_serviceUser" "test" {
177-
name = "%s"
178-
variables = {
179-
%q = %q
180-
%q = %q
181-
}
182-
183-
encrypted_variables = {
184-
%q = %q
185-
}
186152
}
187-
`, rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value)
153+
`, serviceUserName, teamName, assignAdminRole)
188154
}

docs/resources/api_key.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,60 @@
22
page_title: "codefresh_api_key Resource - terraform-provider-codefresh"
33
subcategory: ""
44
description: |-
5-
Manages an API Key tied to an Account and a User.
6-
Requires a Codefresh admin token and applies only to Codefresh on-premises installations.
5+
Manages an API Key tied to a user within an account or a service account within the current account.
6+
On the Codefresh SaaS platfrom this resource is only usable for service accounts.
7+
Management of API keys for users in other accounts requires admin priveleges and hence can only be done on Codefresh on-premises installations.
78
---
89

910
# codefresh_api_key (Resource)
1011

11-
Manages an API Key tied to an Account and a User.
12-
Requires a Codefresh admin token and applies only to Codefresh on-premises installations.
13-
14-
terraform-provider-codefresh itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
15-
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.
12+
Manages an API Key tied to a user within an account or a service account within the current account.
13+
On the Codefresh SaaS platfrom this resource is only usable for service accounts.
14+
Management of API keys for users in other accounts requires admin priveleges and hence can only be done on Codefresh on-premises installations.
1615

16+
terraform-provider-codefresh itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
1717

1818
## Example usage
1919

20+
### With service accounts
21+
22+
```hcl
23+
provider "codefresh" {
24+
api_url = "my API URL"
25+
token = "my init API token"
26+
}
27+
28+
resource "codefresh_service_account" "example" {
29+
name = "example-service-account"
30+
}
31+
32+
resource "codefresh_api_key" "example" {
33+
service_account_id = codefresh_service_account.example.id
34+
name = "example-token"
35+
scopes = [
36+
"project"
37+
]
38+
}
39+
40+
provider "codefresh" {
41+
alias = "project_creator_sa"
42+
api_url = "my API URL"
43+
token = codefresh_api_key.example.token
44+
}
45+
46+
resource "codefresh_project" "example" {
47+
48+
provider = codefresh.project_creator_sa
49+
50+
name = "myproject"
51+
52+
tags = [
53+
"team_1"
54+
]
55+
}
56+
```
57+
58+
### With user and account combination (on-premise only)
2059
```hcl
2160
provider "codefresh" {
2261
api_url = "my API URL"
@@ -79,12 +118,11 @@ resource "codefresh_team" "team_1" {
79118

80119
### Required
81120

82-
- `account_id` (String) The ID of account in which the API key will be created.
83121
- `name` (String) The display name for the API key.
84-
- `user_id` (String) The ID of a user within the referenced `account_id` that will own the API key.
85122

86123
### Optional
87124

125+
- `account_id` (String) The ID of account in which the API key will be created. Required if user_id is set.
88126
- `scopes` (Set of String) A list of access scopes for the API key. The possible values:
89127
* agent
90128
* agents
@@ -104,8 +142,10 @@ resource "codefresh_team" "team_1" {
104142
* step-types
105143
* view
106144
* workflow
145+
- `service_account_id` (String) The ID of the service account to create the API key for.
146+
- `user_id` (String) The ID of a user within the referenced `account_id` that will own the API key. Requires a Codefresh admin token and can be used only in Codefresh on-premises installations.
107147

108148
### Read-Only
109149

110150
- `id` (String) The ID of this resource.
111-
- `token` (String, Sensitive) The resulting API key.
151+
- `token` (String, Sensitive) The resulting API key.

docs/resources/service_account.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
page_title: "codefresh_service_account Resource - terraform-provider-codefresh"
3+
subcategory: ""
4+
description: |-
5+
A service account is an identity that provides automated processes, applications, and services with the necessary permissions to interact securely with the Codefresh platform
6+
---
7+
8+
# codefresh_service_account (Resource)
9+
10+
A service account is an identity that provides automated processes, applications, and services with the necessary permissions to interact securely with the Codefresh platform
11+
12+
For more information about service accounts in Codefresh see [official documentation](https://codefresh.io/docs/docs/administration/account-user-management/service-accounts).
13+
14+
It is also possible to generate API tokens for service accounts, see the documentation for `codefresh_api_key` resource for usage example.
15+
16+
## Example Usage
17+
18+
```hcl
19+
data "codefresh_team" "serviceaccounts" {
20+
name = "service-accounts"
21+
}
22+
23+
resource "codefresh_service_account" "example" {
24+
name = "tf-test1"
25+
assign_admin_role = true
26+
assigned_teams = [data.codefresh_team.serviceaccounts.id]
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Required
34+
35+
- `name` (String) Service account display name
36+
37+
### Optional
38+
39+
- `assign_admin_role` (Boolean) Whether or not to assign account admin role to the service account
40+
- `assigned_teams` (Set of String) A list of team IDs the service account is be assigned to
41+
42+
### Read-Only
43+
44+
- `id` (String) The ID of this resource.
45+
46+
## Import
47+
48+
```sh
49+
terraform import codefresh_service_account.test xxxxxxxxxxxxxxxxxxx
50+
```

0 commit comments

Comments
 (0)