Skip to content

Commit

Permalink
add global --internal flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Hannaford authored and jrperritt committed Aug 2, 2015
1 parent 966440d commit f4193f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions auth/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type CacheItem struct {
}

// CacheKey returns the cache key formed from the user's authentication credentials.
func CacheKey(ao gophercloud.AuthOptions, region string, serviceClientType string) string {
func CacheKey(ao gophercloud.AuthOptions, region, serviceClientType string, urlType gophercloud.Availability) string {
if ao.Username != "" {
return fmt.Sprintf("%s,%s,%s,%s", ao.Username, ao.IdentityEndpoint, region, serviceClientType)
return fmt.Sprintf("%s,%s,%s,%s", ao.Username, ao.IdentityEndpoint, region, serviceClientType, urlType)
}
return fmt.Sprintf("%s,%s,%s,%s", ao.TenantID, ao.IdentityEndpoint, region, serviceClientType)
return fmt.Sprintf("%s,%s,%s,%s,%s", ao.TenantID, ao.IdentityEndpoint, region, serviceClientType, urlType)
}

func cacheFile() (string, error) {
Expand Down
29 changes: 21 additions & 8 deletions auth/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func reauthFunc(pc *gophercloud.ProviderClient, ao gophercloud.AuthOptions) func
}
}

func URLTypeFromCtx(c *cli.Context) gophercloud.Availability {
urlType := gophercloud.AvailabilityPublic
if c.GlobalIsSet("internal") {
urlType = gophercloud.AvailabilityInternal
}
return urlType
}

// NewClient creates and returns a Rackspace client for the given service.
func NewClient(c *cli.Context, serviceType string, logger *logrus.Logger, noCache bool) (*gophercloud.ServiceClient, error) {
// get the user's authentication credentials
Expand All @@ -100,15 +108,16 @@ func NewClient(c *cli.Context, serviceType string, logger *logrus.Logger, noCach
return nil, err
}

urlType := URLTypeFromCtx(c)
if noCache {
return authFromScratch(credsResult, serviceType, logger)
return authFromScratch(credsResult, serviceType, urlType, logger)
}

ao := credsResult.AuthOpts
region := credsResult.Region

// form the cache key
cacheKey := CacheKey(*ao, region, serviceType)
cacheKey := CacheKey(*ao, region, serviceType, urlType)
// initialize cache
cache := &Cache{}
logger.Infof("Looking in the cache for cache key: %s\n", cacheKey)
Expand All @@ -131,13 +140,13 @@ func NewClient(c *cli.Context, serviceType string, logger *logrus.Logger, noCach
}, nil
}
} else {
return authFromScratch(credsResult, serviceType, logger)
return authFromScratch(credsResult, serviceType, urlType, logger)
}

return nil, nil
}

func authFromScratch(credsResult *CredentialsResult, serviceType string, logger *logrus.Logger) (*gophercloud.ServiceClient, error) {
func authFromScratch(credsResult *CredentialsResult, serviceType string, urlType gophercloud.Availability, logger *logrus.Logger) (*gophercloud.ServiceClient, error) {
logger.Info("Not using cache; Authenticating from scratch.\n")

ao := credsResult.AuthOpts
Expand All @@ -152,22 +161,26 @@ func authFromScratch(credsResult *CredentialsResult, serviceType string, logger
switch serviceType {
case "compute":
sc, err = rackspace.NewComputeV2(pc, gophercloud.EndpointOpts{
Region: region,
Region: region,
Availability: urlType,
})
break
case "object-store":
sc, err = rackspace.NewObjectStorageV1(pc, gophercloud.EndpointOpts{
Region: region,
Region: region,
Availability: urlType,
})
break
case "blockstorage":
sc, err = rackspace.NewBlockStorageV1(pc, gophercloud.EndpointOpts{
Region: region,
Region: region,
Availability: urlType,
})
break
case "network":
sc, err = rackspace.NewNetworkV2(pc, gophercloud.EndpointOpts{
Region: region,
Region: region,
Availability: urlType,
})
break
}
Expand Down
4 changes: 4 additions & 0 deletions commandoptions/globalflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func GlobalFlags() []cli.Flag {
Name: "region",
Usage: "The region to which authenticate.",
},
cli.BoolFlag{
Name: "internal",
Usage: "Whether or not to use the internal Rackspace network",
},
cli.StringFlag{
Name: "profile",
Usage: "The config file profile to use for authentication.",
Expand Down
2 changes: 1 addition & 1 deletion handler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (ctx *Context) storeCredentials() {
credsResult, err := auth.Credentials(ctx.CLIContext, nil)
if err == nil {
// form the cache key
cacheKey := auth.CacheKey(*credsResult.AuthOpts, credsResult.Region, ctx.ServiceClientType)
cacheKey := auth.CacheKey(credsResult, ctx.ServiceClientType, auth.URLTypeFromCtx(ctx.CLIContext))
// initialize the cache
cache := &auth.Cache{}
// set the cache value to the current values
Expand Down

0 comments on commit f4193f2

Please sign in to comment.