|
| 1 | +package vaultsecrets_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/client/secret_service" |
| 9 | + secretmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/models" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 12 | + "github.com/hashicorp/terraform-provider-hcp/internal/clients" |
| 13 | + "github.com/hashicorp/terraform-provider-hcp/internal/provider/acctest" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccVaultSecretsResourceIntegrationGitLab(t *testing.T) { |
| 17 | + accessToken := checkRequiredEnvVarOrFail(t, "GITLAB_ACCESS_TOKEN") |
| 18 | + |
| 19 | + integrationName1 := generateRandomSlug() |
| 20 | + integrationName2 := generateRandomSlug() |
| 21 | + |
| 22 | + resource.Test(t, resource.TestCase{ |
| 23 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 24 | + ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories, |
| 25 | + Steps: []resource.TestStep{ |
| 26 | + // Create initial integration with access token |
| 27 | + { |
| 28 | + Config: gitlabConfig(integrationName1, accessToken), |
| 29 | + Check: resource.ComposeTestCheckFunc( |
| 30 | + gitlabCheckFuncs(integrationName1, accessToken)..., |
| 31 | + ), |
| 32 | + }, |
| 33 | + // Changing the name forces a recreation |
| 34 | + { |
| 35 | + Config: gitlabConfig(integrationName2, accessToken), |
| 36 | + Check: resource.ComposeTestCheckFunc( |
| 37 | + gitlabCheckFuncs(integrationName2, accessToken)..., |
| 38 | + ), |
| 39 | + }, |
| 40 | + // Deleting the integration out of band causes a recreation |
| 41 | + { |
| 42 | + PreConfig: func() { |
| 43 | + t.Helper() |
| 44 | + client := acctest.HCPClients(t) |
| 45 | + _, err := client.VaultSecrets.DeleteIntegration(&secret_service.DeleteIntegrationParams{ |
| 46 | + Name: integrationName2, |
| 47 | + OrganizationID: client.Config.OrganizationID, |
| 48 | + ProjectID: client.Config.ProjectID, |
| 49 | + }, nil) |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + }, |
| 54 | + Config: gitlabConfig(integrationName2, accessToken), |
| 55 | + Check: resource.ComposeTestCheckFunc( |
| 56 | + gitlabCheckFuncs(integrationName2, accessToken)..., |
| 57 | + ), |
| 58 | + PlanOnly: true, |
| 59 | + ExpectNonEmptyPlan: true, |
| 60 | + }, |
| 61 | + // Pre-existing integration can be imported |
| 62 | + { |
| 63 | + PreConfig: func() { |
| 64 | + t.Helper() |
| 65 | + client := acctest.HCPClients(t) |
| 66 | + _, err := client.VaultSecrets.CreateIntegration(&secret_service.CreateIntegrationParams{ |
| 67 | + Body: &secretmodels.SecretServiceCreateIntegrationBody{ |
| 68 | + Name: integrationName2, |
| 69 | + Provider: "gitlab", |
| 70 | + Capabilities: []*secretmodels.Secrets20231128Capability{ |
| 71 | + secretmodels.Secrets20231128CapabilitySYNC.Pointer(), |
| 72 | + }, |
| 73 | + GitlabAccessToken: &secretmodels.Secrets20231128GitlabAccessTokenRequest{ |
| 74 | + Token: accessToken, |
| 75 | + }, |
| 76 | + }, |
| 77 | + OrganizationID: client.Config.OrganizationID, |
| 78 | + ProjectID: client.Config.ProjectID, |
| 79 | + }, nil) |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + }, |
| 84 | + Config: gitlabConfig(integrationName2, accessToken), |
| 85 | + Check: resource.ComposeTestCheckFunc( |
| 86 | + gitlabCheckFuncs(integrationName2, accessToken)..., |
| 87 | + ), |
| 88 | + ResourceName: "hcp_vault_secrets_integration.acc_test", |
| 89 | + ImportStateId: integrationName2, |
| 90 | + ImportState: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + CheckDestroy: func(_ *terraform.State) error { |
| 94 | + if integrationExists(t, integrationName1) { |
| 95 | + return fmt.Errorf("test GitLab integration %s was not destroyed", integrationName1) |
| 96 | + } |
| 97 | + if integrationExists(t, integrationName2) { |
| 98 | + return fmt.Errorf("test GitLab integration %s was not destroyed", integrationName2) |
| 99 | + } |
| 100 | + return nil |
| 101 | + }, |
| 102 | + }) |
| 103 | +} |
| 104 | + |
| 105 | +func gitlabCheckFuncs(integrationName, accessToken string) []resource.TestCheckFunc { |
| 106 | + return []resource.TestCheckFunc{ |
| 107 | + resource.TestCheckResourceAttrSet("hcp_vault_secrets_integration.acc_test", "organization_id"), |
| 108 | + resource.TestCheckResourceAttr("hcp_vault_secrets_integration.acc_test", "project_id", os.Getenv("HCP_PROJECT_ID")), |
| 109 | + resource.TestCheckResourceAttrSet("hcp_vault_secrets_integration.acc_test", "resource_id"), |
| 110 | + resource.TestCheckResourceAttrSet("hcp_vault_secrets_integration.acc_test", "resource_name"), |
| 111 | + resource.TestCheckResourceAttr("hcp_vault_secrets_integration.acc_test", "name", integrationName), |
| 112 | + resource.TestCheckResourceAttr("hcp_vault_secrets_integration.acc_test", "capabilities.#", "1"), |
| 113 | + resource.TestCheckResourceAttr("hcp_vault_secrets_integration.acc_test", "capabilities.0", "SYNC"), |
| 114 | + resource.TestCheckResourceAttr("hcp_vault_secrets_integration.acc_test", "provider_type", "gitlab"), |
| 115 | + resource.TestCheckResourceAttr("hcp_vault_secrets_integration.acc_test", "gitlab_access.token", accessToken), |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func gitlabConfig(integrationName, accessToken string) string { |
| 120 | + return fmt.Sprintf(` |
| 121 | + resource "hcp_vault_secrets_integration" "acc_test" { |
| 122 | + name = %q |
| 123 | + capabilities = ["SYNC"] |
| 124 | + provider_type = "gitlab" |
| 125 | + gitlab_access = { |
| 126 | + token = %q |
| 127 | + } |
| 128 | + }`, integrationName, accessToken) |
| 129 | +} |
| 130 | + |
| 131 | +func integrationExists(t *testing.T, name string) bool { |
| 132 | + t.Helper() |
| 133 | + client := acctest.HCPClients(t) |
| 134 | + |
| 135 | + response, err := client.VaultSecrets.GetIntegration( |
| 136 | + secret_service.NewGetIntegrationParamsWithContext(ctx). |
| 137 | + WithOrganizationID(client.Config.OrganizationID). |
| 138 | + WithProjectID(client.Config.ProjectID). |
| 139 | + WithName(name), nil) |
| 140 | + |
| 141 | + if err != nil && !clients.IsResponseCodeNotFound(err) { |
| 142 | + t.Fatal(err) |
| 143 | + } |
| 144 | + |
| 145 | + return !clients.IsResponseCodeNotFound(err) && response != nil && response.Payload != nil && response.Payload.Integration != nil |
| 146 | +} |
0 commit comments