Skip to content

Commit 26c1683

Browse files
authored
Merge pull request #35 from jtopjian/clientconfig-docs
Doc Update and NewServiceProvider fix
2 parents ab2d1a6 + 59b2b00 commit 26c1683

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

acceptance/openstack/clientconfig/clientconfig_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import (
1414
)
1515

1616
func TestServerCreateDestroy(t *testing.T) {
17-
clientOpts := &cc.ClientOpts{
18-
Cloud: "acctest",
19-
EnvPrefix: "FOO",
20-
}
17+
// This will be populated by environment variables.
18+
clientOpts := &cc.ClientOpts{}
2119

2220
client, err := cc.NewServiceClient("compute", clientOpts)
2321
if err != nil {
24 KB
Binary file not shown.

openstack/clientconfig/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ Example to Create a Provider Client From clouds.yaml
1616
}
1717
1818
19+
Example to Manually Create a Provider Client
20+
21+
opts := &clientconfig.ClientOpts{
22+
AuthInfo: &clientconfig.AuthInfo{
23+
AuthURL: "https://hi.example.com:5000/v3",
24+
Username: "jdoe",
25+
Password: "password",
26+
ProjectName: "Some Project",
27+
DomainName: "default",
28+
},
29+
}
30+
31+
pClient, err := clientconfig.AuthenticatedClient(opts)
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
1937
Example to Create a Service Client from clouds.yaml
2038
2139
opts := &clientconfig.ClientOpts{

openstack/clientconfig/requests.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,33 @@ func AuthenticatedClient(opts *ClientOpts) (*gophercloud.ProviderClient, error)
426426

427427
// NewServiceClient is a convenience function to get a new service client.
428428
func NewServiceClient(service string, opts *ClientOpts) (*gophercloud.ServiceClient, error) {
429-
var cloud *Cloud
429+
cloud := new(Cloud)
430+
431+
// If no opts were passed in, create an empty ClientOpts.
432+
if opts == nil {
433+
opts = new(ClientOpts)
434+
}
435+
436+
// Determine if a clouds.yaml entry should be retrieved.
437+
// Start by figuring out the cloud name.
438+
// First check if one was explicitly specified in opts.
439+
var cloudName string
430440
if opts.Cloud != "" {
441+
cloudName = opts.Cloud
442+
}
443+
444+
// Next see if a cloud name was specified as an environment variable.
445+
envPrefix := "OS_"
446+
if opts.EnvPrefix != "" {
447+
envPrefix = opts.EnvPrefix
448+
}
449+
450+
if v := os.Getenv(envPrefix + "CLOUD"); v != "" {
451+
cloudName = v
452+
}
453+
454+
// If a cloud name was determined, try to look it up in clouds.yaml.
455+
if cloudName != "" {
431456
// Get the requested cloud.
432457
var err error
433458
cloud, err = GetCloudFromYAML(opts)
@@ -436,16 +461,6 @@ func NewServiceClient(service string, opts *ClientOpts) (*gophercloud.ServiceCli
436461
}
437462
}
438463

439-
if cloud == nil {
440-
cloud.AuthInfo = opts.AuthInfo
441-
}
442-
443-
// Environment variable overrides.
444-
envPrefix := "OS_"
445-
if opts != nil && opts.EnvPrefix != "" {
446-
envPrefix = opts.EnvPrefix
447-
}
448-
449464
// Get a Provider Client
450465
pClient, err := AuthenticatedClient(opts)
451466
if err != nil {

0 commit comments

Comments
 (0)