Ash Wilson | 8fecf33 | 2014-10-20 15:18:46 -0400 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
Ash Wilson | b17536b | 2014-10-22 15:36:45 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/acceptance/tools" |
Ash Wilson | 8fecf33 | 2014-10-20 15:18:46 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/rackspace" |
| 12 | ) |
| 13 | |
| 14 | func newClient() (*gophercloud.ServiceClient, error) { |
| 15 | // Obtain credentials from the environment. |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame] | 16 | options, err := rackspace.AuthOptionsFromEnv() |
| 17 | if err != nil { |
Ash Wilson | b17536b | 2014-10-22 15:36:45 -0400 | [diff] [blame] | 18 | return nil, err |
Ash Wilson | 8fecf33 | 2014-10-20 15:18:46 -0400 | [diff] [blame] | 19 | } |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame] | 20 | options = tools.OnlyRS(options) |
Ash Wilson | 8fecf33 | 2014-10-20 15:18:46 -0400 | [diff] [blame] | 21 | region := os.Getenv("RS_REGION") |
| 22 | |
| 23 | if options.Username == "" { |
| 24 | return nil, errors.New("Please provide a Rackspace username as RS_USERNAME.") |
| 25 | } |
| 26 | if options.APIKey == "" { |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame] | 27 | return nil, errors.New("Please provide a Rackspace API key as RS_API_KEY.") |
Ash Wilson | 8fecf33 | 2014-10-20 15:18:46 -0400 | [diff] [blame] | 28 | } |
| 29 | if region == "" { |
| 30 | return nil, errors.New("Please provide a Rackspace region as RS_REGION.") |
| 31 | } |
| 32 | |
| 33 | client, err := rackspace.AuthenticatedClient(options) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | return rackspace.NewComputeV2(client, gophercloud.EndpointOpts{ |
| 39 | Region: region, |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | type serverOpts struct { |
| 44 | imageID string |
| 45 | flavorID string |
| 46 | } |
| 47 | |
| 48 | func optionsFromEnv() (*serverOpts, error) { |
| 49 | options := &serverOpts{ |
| 50 | imageID: os.Getenv("RS_IMAGE_ID"), |
| 51 | flavorID: os.Getenv("RS_FLAVOR_ID"), |
| 52 | } |
| 53 | if options.imageID == "" { |
| 54 | return nil, errors.New("Please provide a valid Rackspace image ID as RS_IMAGE_ID") |
| 55 | } |
| 56 | if options.flavorID == "" { |
| 57 | return nil, errors.New("Please provide a valid Rackspace flavor ID as RS_FLAVOR_ID") |
| 58 | } |
| 59 | return options, nil |
| 60 | } |