From 9add58daa3e06412bf062f488219645a9651bedf Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Wed, 13 Mar 2024 17:19:50 -0700 Subject: [PATCH] Add test that verifies token auth works and add an example in the docs Signed-off-by: Dom Del Nano --- Jenkinsfile | 4 ++++ docs/index.md | 13 ++++++++++++- examples/provider/provider.tf | 8 ++++++++ xoa/acc_setup_test.go | 1 + xoa/data_source_host_test.go | 22 ++++++++++++++++++++++ xoa/provider_test.go | 31 +++++++++++++++++++++++++++++-- 6 files changed, 76 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index c884c9c9..64dd31dc 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -31,6 +31,10 @@ pipeline { stage('Test') { steps { lock('xoa-test-runner') { + sh ''' + set +x + export BYPASS_XOA_TOKEN=$(xo-cli --createToken $XOA_URL $XOA_USER $XOA_PASSWORD | tail -n1) + ''' sh 'cp /opt/terraform-provider-xenorchestra/testdata/images/alpine-virt-3.17.0-x86_64.iso xoa/testdata/alpine-virt-3.17.0-x86_64.iso' sh 'TF_VERSION=${TF_VERSION} TIMEOUT=60m make ci' } diff --git a/docs/index.md b/docs/index.md index 0bd73399..81681a51 100644 --- a/docs/index.md +++ b/docs/index.md @@ -24,6 +24,9 @@ terraform { xenorchestra = { source = "vatesfr/xenorchestra" } + xenorchestra_token_auth = { + source = "vatesfr/xenorchestra" + } } } @@ -41,11 +44,20 @@ provider "xenorchestra" { # used sparingly! insecure = # Or set XOA_INSECURE environment variable to any value } + +provider "xenorchestra_token_auth" { + # XOA_USER and XOA_PASSWORD cannot be set, nor can their arguments + token = "" # or set XOA_TOKEN environment variable +} ``` ## Schema +### Required + +- `url` (String) Hostname of the xoa router. Can be set via the XOA_URL environment variable. + ### Optional - `insecure` (Boolean) Whether SSL should be verified or not. Can be set via the XOA_INSECURE environment variable. @@ -53,5 +65,4 @@ provider "xenorchestra" { - `retry_max_time` (String) If `retry_mode` is set, this specifies the duration for which the backoff method will continue retries. Can be set via the `XOA_RETRY_MAX_TIME` environment variable - `retry_mode` (String) Specifies if retries should be attempted for requests that require eventual . Can be set via the XOA_RETRY_MODE environment variable. - `token` (String) Password for xoa api. Can be set via the XOA_TOKEN environment variable. -- `url` (String) Hostname of the xoa router. Can be set via the XOA_URL environment variable. - `username` (String) User account for xoa api. Can be set via the XOA_USER environment variable. diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index ad6fd037..b66944e2 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -4,6 +4,9 @@ terraform { xenorchestra = { source = "vatesfr/xenorchestra" } + xenorchestra_token_auth = { + source = "vatesfr/xenorchestra" + } } } @@ -21,3 +24,8 @@ provider "xenorchestra" { # used sparingly! insecure = # Or set XOA_INSECURE environment variable to any value } + +provider "xenorchestra_token_auth" { + # XOA_USER and XOA_PASSWORD cannot be set, nor can their arguments + token = "" # or set XOA_TOKEN environment variable +} diff --git a/xoa/acc_setup_test.go b/xoa/acc_setup_test.go index 19a58c01..85879b15 100644 --- a/xoa/acc_setup_test.go +++ b/xoa/acc_setup_test.go @@ -12,6 +12,7 @@ import ( var testObjectIndex int = 1 var accTestPrefix string = "terraform-acc" +var accTestXoToken string = os.Getenv("BYPASS_XOA_TOKEN") var accTestPool client.Pool var accTestHost client.Host var accDefaultSr client.StorageRepository diff --git a/xoa/data_source_host_test.go b/xoa/data_source_host_test.go index cec63e2b..2353f02b 100644 --- a/xoa/data_source_host_test.go +++ b/xoa/data_source_host_test.go @@ -32,6 +32,28 @@ func TestAccXenorchestraDataSource_host(t *testing.T) { ) } +func TestAccXenorchestraDataSource_hostXoTokenAuth(t *testing.T) { + resourceName := "data.xenorchestra_host.host" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccTokenAuthProviders, + Steps: []resource.TestStep{ + { + Config: testAccXenorchestraDataSourceHostConfig(accTestHost.NameLabel), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckXenorchestraDataSourceHost(resourceName), + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrSet(resourceName, "cpus.cores"), + resource.TestCheckResourceAttrSet(resourceName, "cpus.sockets"), + resource.TestCheckResourceAttrSet(resourceName, "memory"), + resource.TestCheckResourceAttrSet(resourceName, "memory_usage"), + resource.TestCheckResourceAttr(resourceName, "name_label", accTestHost.NameLabel)), + }, + }, + }, + ) +} + func TestAccXenorchestraDataSource_hostNotFound(t *testing.T) { resourceName := "data.xenorchestra_host.host" resource.Test(t, resource.TestCase{ diff --git a/xoa/provider_test.go b/xoa/provider_test.go index 252ac173..2761e0c9 100644 --- a/xoa/provider_test.go +++ b/xoa/provider_test.go @@ -9,10 +9,12 @@ import ( ) var testAccProviders map[string]*schema.Provider +var testAccTokenAuthProviders map[string]*schema.Provider var testAccFailToStartAndHaltProviders map[string]*schema.Provider var testAccFailToDeleteVmProviders map[string]*schema.Provider var testAccProvider *schema.Provider +var testAccTokenAuthProvider *schema.Provider var testAccFailToStartHaltVmProvider *schema.Provider var testAccFailToDeleteVmProvider *schema.Provider @@ -22,6 +24,11 @@ func init() { "xenorchestra": testAccProvider, } + testAccTokenAuthProvider = createTokenAuthProvider() + testAccTokenAuthProviders = map[string]*schema.Provider{ + "xenorchestra": testAccTokenAuthProvider, + } + testAccFailToStartHaltVmProvider = Provider() testAccFailToStartHaltVmProvider.ConfigureFunc = internal.GetFailToStartAndHaltXOClient testAccFailToStartAndHaltProviders = map[string]*schema.Provider{ @@ -34,6 +41,26 @@ func init() { } } +func createTokenAuthProvider() *schema.Provider { + provider := Provider() + + // The test suite runs in an environment where the XOA_USER and XOA_PASSWORD environment + // variables are set. Therefore the DefaultFunc's and ConflictsWith's will think that + // username, password and token were supplied and will fail validation. The patching + // below allows this test provider to think only token auth is supplied (ConflictsWith changes) + // and prevents the username and password from being passed through (DefaultFunc changes). + var f schema.SchemaDefaultFunc = func() (interface{}, error) { return "", nil } + provider.Schema["username"].DefaultFunc = f + provider.Schema["username"].ConflictsWith = []string{} + + provider.Schema["password"].DefaultFunc = f + provider.Schema["password"].ConflictsWith = []string{} + + provider.Schema["token"].ConflictsWith = []string{} + provider.Schema["token"].DefaultFunc = schema.EnvDefaultFunc("BYPASS_XOA_TOKEN", nil) + return provider +} + func testAccPreCheck(t *testing.T) { if v := os.Getenv("XOA_URL"); v == "" { t.Fatal("The XOA_URL environment variable must be set") @@ -41,10 +68,10 @@ func testAccPreCheck(t *testing.T) { user := os.Getenv("XOA_USER") password := os.Getenv("XOA_PASSWORD") - token := os.Getenv("XOA_TOKEN") + token := os.Getenv("BYPASS_XOA_TOKEN") if token == "" && (user == "" || password == "") { - t.Fatal("One of the following environment variable(s) must be set: XOA_USER and XOA_PASSWORD or XOA_TOKEN") + t.Fatal("One of the following environment variable(s) must be set: XOA_USER and XOA_PASSWORD or BYPASS_XOA_TOKEN") } if v := os.Getenv("XOA_POOL"); v == "" {