|
| 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 | +} |
0 commit comments