Skip to content

Commit

Permalink
Add test that verifies token auth works and add an example in the docs
Browse files Browse the repository at this point in the history
Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Mar 14, 2024
1 parent e31a949 commit b3b5165
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
13 changes: 12 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ terraform {
xenorchestra = {
source = "vatesfr/xenorchestra"
}
xenorchestra_token_auth = {
source = "vatesfr/xenorchestra"
}
}
}
Expand All @@ -41,17 +44,25 @@ provider "xenorchestra" {
# used sparingly!
insecure = <false|true> # 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 = "<token from XO>" # or set XOA_TOKEN environment variable
}
```

<!-- schema generated by tfplugindocs -->
## 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.
- `password` (String) Password for xoa api. Can be set via the XOA_PASSWORD environment variable.
- `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.
8 changes: 8 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ terraform {
xenorchestra = {
source = "vatesfr/xenorchestra"
}
xenorchestra_token_auth = {
source = "vatesfr/xenorchestra"
}
}
}

Expand All @@ -21,3 +24,8 @@ provider "xenorchestra" {
# used sparingly!
insecure = <false|true> # 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 = "<token from XO>" # or set XOA_TOKEN environment variable
}
1 change: 1 addition & 0 deletions xoa/acc_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions xoa/data_source_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
26 changes: 24 additions & 2 deletions xoa/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,6 +24,11 @@ func init() {
"xenorchestra": testAccProvider,
}

testAccTokenAuthProvider = Provider()
testAccTokenAuthProviders = map[string]*schema.Provider{
"xenorchestra": testAccTokenAuthProvider,
}

testAccFailToStartHaltVmProvider = Provider()
testAccFailToStartHaltVmProvider.ConfigureFunc = internal.GetFailToStartAndHaltXOClient
testAccFailToStartAndHaltProviders = map[string]*schema.Provider{
Expand All @@ -34,17 +41,32 @@ func init() {
}
}

func createTokenAuthProvider() *schema.Provider {
provider := Provider()

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")
}

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 == "" {
Expand Down

0 comments on commit b3b5165

Please sign in to comment.