github.com/bigcommerce/nomad@v0.9.3-bc/e2e/framework/provisioner.go (about) 1 package framework 2 3 import ( 4 "fmt" 5 "os" 6 7 capi "github.com/hashicorp/consul/api" 8 napi "github.com/hashicorp/nomad/api" 9 "github.com/hashicorp/nomad/helper/uuid" 10 vapi "github.com/hashicorp/vault/api" 11 ) 12 13 // ProvisionerOptions defines options to be given to the Provisioner when calling 14 // ProvisionCluster 15 type ProvisionerOptions struct { 16 Name string 17 ExpectConsul bool // If true, fails if a Consul client can't be configured 18 ExpectVault bool // If true, fails if a Vault client can't be configured 19 } 20 21 type ClusterInfo struct { 22 ID string 23 Name string 24 NomadClient *napi.Client 25 ConsulClient *capi.Client 26 VaultClient *vapi.Client 27 } 28 29 // Provisioner interface is used by the test framework to provision a Nomad 30 // cluster for each TestCase 31 type Provisioner interface { 32 ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error) 33 DestroyCluster(clusterID string) error 34 } 35 36 // DefaultProvisioner is a noop provisioner that builds clients from environment 37 // variables according to the respective client configuration 38 var DefaultProvisioner Provisioner = new(singleClusterProvisioner) 39 40 type singleClusterProvisioner struct{} 41 42 func (p *singleClusterProvisioner) ProvisionCluster(opts ProvisionerOptions) (*ClusterInfo, error) { 43 // Build ID based off given name 44 info := &ClusterInfo{ 45 ID: uuid.Generate()[:8], 46 Name: opts.Name, 47 } 48 49 // Build Nomad api client 50 nomadClient, err := napi.NewClient(napi.DefaultConfig()) 51 if err != nil { 52 return nil, err 53 } 54 info.NomadClient = nomadClient 55 56 if opts.ExpectConsul { 57 consulClient, err := capi.NewClient(capi.DefaultConfig()) 58 if err != nil && opts.ExpectConsul { 59 return nil, err 60 } 61 info.ConsulClient = consulClient 62 } 63 64 if len(os.Getenv(vapi.EnvVaultAddress)) != 0 { 65 vaultClient, err := vapi.NewClient(vapi.DefaultConfig()) 66 if err != nil && opts.ExpectVault { 67 return nil, err 68 } 69 info.VaultClient = vaultClient 70 } else if opts.ExpectVault { 71 return nil, fmt.Errorf("vault client expected but environment variable %s not set", 72 vapi.EnvVaultAddress) 73 } 74 75 return info, err 76 } 77 78 func (p *singleClusterProvisioner) DestroyCluster(_ string) error { 79 //Maybe try to GC things based on id? 80 return nil 81 }