-
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
c78b225
commit 1654c4e
Showing
7 changed files
with
324 additions
and
61 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
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,133 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
var apiKeyNamePrefix = "TerraformAccTest_" | ||
|
||
func TestAccCodefreshAPIKey_ServiceUser(t *testing.T) { | ||
name := apiKeyNamePrefix + acctest.RandString(10) | ||
|
||
resourceName := "codefresh_api_key.test_apikey" | ||
serviceAccountResourceName := "codefresh_service_account.test_apikey" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCodefreshServiceUserAndAPIKeyDestroyed, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCodefreshAPIKeyServiceAccount(name, name), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCodefreshServiceUserAPIKeyExists(resourceName, serviceAccountResourceName), | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttr(resourceName, "scopes.0", "agent"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
RefreshState: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCodefreshServiceUserAPIKeyExists(apiKeyResource string, serviceUserResource string) resource.TestCheckFunc { | ||
return func(state *terraform.State) error { | ||
serviceUserState, ok := state.RootModule().Resources[serviceUserResource] | ||
|
||
if !ok { | ||
return fmt.Errorf("Not found: %s", serviceUserResource) | ||
} | ||
|
||
if serviceUserState.Primary.ID == "" { | ||
return fmt.Errorf("No Record ID is set") | ||
} | ||
|
||
apiKeyState, ok := state.RootModule().Resources[apiKeyResource] | ||
|
||
if !ok { | ||
return fmt.Errorf("Not found: %s", apiKeyResource) | ||
} | ||
|
||
if apiKeyState.Primary.ID == "" { | ||
return fmt.Errorf("No Record ID is set for team") | ||
} | ||
|
||
serviceUserID := serviceUserState.Primary.ID | ||
apiKeyID := apiKeyState.Primary.ID | ||
|
||
apiClient := testAccProvider.Meta().(*cfclient.Client) | ||
_, err := apiClient.GetAPIKeyServiceUser(apiKeyID, serviceUserID) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error fetching service user api key for resource %s. %s", apiKeyID, err) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckCodefreshServiceUserAndAPIKeyDestroyed(s *terraform.State) error { | ||
apiClient := testAccProvider.Meta().(*cfclient.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
|
||
if rs.Type != "codefresh_service_account" && rs.Type != "codefresh_api_key" { | ||
continue | ||
} | ||
|
||
var ( | ||
serviceAccountId string | ||
apiKeyId string | ||
) | ||
|
||
if rs.Type == "codefresh_service_account" { | ||
serviceAccountId = rs.Primary.ID | ||
_, err := apiClient.GetServiceUserByID(serviceAccountId) | ||
|
||
if err == nil { | ||
return fmt.Errorf("Alert service account still exists") | ||
} | ||
} | ||
|
||
if rs.Type == "codefresh_api_key" { | ||
apiKeyId = rs.Primary.ID | ||
_, err := apiClient.GetAPIKeyServiceUser(apiKeyId, serviceAccountId) | ||
|
||
if err == nil { | ||
return fmt.Errorf("Alert api key still exists") | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCodefreshAPIKeyServiceAccount(apiKeyName string, serviceUserName string) string { | ||
return fmt.Sprintf(` | ||
resource "codefresh_service_account" "test_apikey" { | ||
name = "%s" | ||
} | ||
resource "codefresh_api_key" "test_apikey" { | ||
service_account_id = codefresh_service_account.test_apikey.id | ||
name = "%s" | ||
scopes = [ | ||
"agent", | ||
"agents", | ||
"audit", | ||
"api-keys" | ||
] | ||
} | ||
`, serviceUserName, apiKeyName) | ||
} |
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,50 @@ | ||
--- | ||
page_title: "codefresh_service_account Resource - terraform-provider-codefresh" | ||
subcategory: "" | ||
description: |- | ||
A service account is an identity that provides automated processes, applications, and services with the necessary permissions to interact securely with the Codefresh platform | ||
--- | ||
|
||
# codefresh_service_account (Resource) | ||
|
||
A service account is an identity that provides automated processes, applications, and services with the necessary permissions to interact securely with the Codefresh platform | ||
|
||
For more information about service accounts in Codefresh see [official documentation](https://codefresh.io/docs/docs/administration/account-user-management/service-accounts). | ||
|
||
It is also possible to generate API tokens for service accounts, see the documentation for `codefresh_api_key` resource for usage example. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "codefresh_team" "serviceaccounts" { | ||
name = "service-accounts" | ||
} | ||
resource "codefresh_service_account" "example" { | ||
name = "tf-test1" | ||
assign_admin_role = true | ||
assigned_teams = [data.codefresh_team.serviceaccounts.id] | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Service account display name | ||
|
||
### Optional | ||
|
||
- `assign_admin_role` (Boolean) Whether or not to assign account admin role to the service account | ||
- `assigned_teams` (Set of String) A list of team IDs the service account is be assigned to | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
## Import | ||
|
||
```sh | ||
terraform import codefresh_service_account.test xxxxxxxxxxxxxxxxxxx | ||
``` |
Oops, something went wrong.