Skip to content

Commit

Permalink
add use-service-net global flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jrperritt committed Aug 2, 2015
1 parent f4193f2 commit d07512d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion auth/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type CacheItem struct {
// CacheKey returns the cache key formed from the user's authentication credentials.
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, urlType)
return fmt.Sprintf("%s,%s,%s,%s,%s", ao.Username, ao.IdentityEndpoint, region, serviceClientType, urlType)
}
return fmt.Sprintf("%s,%s,%s,%s,%s", ao.TenantID, ao.IdentityEndpoint, region, serviceClientType, urlType)
}
Expand Down
29 changes: 18 additions & 11 deletions auth/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var tenantIDAuthErrSlice = []string{"There are some required Rackspace Cloud cre
"",
}

// Err returns the custom error to print when authentication fails.
func Err(have map[string]commandoptions.Cred, want map[string]string, errMsg []string) error {
haveString := ""
for k, v := range have {
Expand All @@ -58,6 +59,8 @@ func Err(have map[string]commandoptions.Cred, want map[string]string, errMsg []s
return nil
}

// CredentialsResult holds the information acquired from looking for authentication
// credentials.
type CredentialsResult struct {
AuthOpts *gophercloud.AuthOptions
Region string
Expand Down Expand Up @@ -92,23 +95,22 @@ 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) {
func NewClient(c *cli.Context, serviceType string, logger *logrus.Logger, noCache bool, useServiceNet bool) (*gophercloud.ServiceClient, error) {
// get the user's authentication credentials
credsResult, err := Credentials(c, logger)
if err != nil {
return nil, err
}

urlType := URLTypeFromCtx(c)
logMsg := "Using public endpoint"
urlType := gophercloud.AvailabilityPublic
if useServiceNet {
logMsg = "Using service net endpoint"
urlType = gophercloud.AvailabilityInternal
}
logger.Infoln(logMsg)

if noCache {
return authFromScratch(credsResult, serviceType, urlType, logger)
}
Expand Down Expand Up @@ -146,7 +148,7 @@ func NewClient(c *cli.Context, serviceType string, logger *logrus.Logger, noCach
return nil, nil
}

func authFromScratch(credsResult *CredentialsResult, serviceType string, urlType gophercloud.Availability, 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 Down Expand Up @@ -190,6 +192,11 @@ func authFromScratch(credsResult *CredentialsResult, serviceType string, urlTyp
if sc == nil {
return nil, fmt.Errorf("Unable to create service client: Unknown service type: %s\n", serviceType)
}
if sc.Endpoint == "/" {
return nil, fmt.Errorf(strings.Join([]string{"You wanted to use service net for the %s request",
"but the %s service doesn't have an internal URL.\n"}, " "), serviceType, serviceType)
}
logger.Debugf("Created %s service client: %+v", serviceType, sc)
sc.UserAgent.Prepend(util.UserAgent)
return sc, nil
}
Expand Down
2 changes: 1 addition & 1 deletion commandoptions/globalflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GlobalFlags() []cli.Flag {
Usage: "The region to which authenticate.",
},
cli.BoolFlag{
Name: "internal",
Name: "use-service-net",
Usage: "Whether or not to use the internal Rackspace network",
},
cli.StringFlag{
Expand Down
5 changes: 5 additions & 0 deletions docs/globaloptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ This presents a compact format with appropriate CSV headers.

(boolean) Don't set the header for CSV nor tabular output.

``--use-service-net``
~~~~~~~~~~~~~~~~~~~~~

(boolean) Use the Rackspace internal URL to execute the request.

``--help, -h``
~~~~~~~~~~~~~~

Expand Down
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pass in other required **global** information into the tool, these are:
``--no-header``
Don't set the header for CSV nor tabular output.

``--use-service-net``
Use the Rackspace internal URL to execute the request.

``--help, -h``
Show help

Expand Down
30 changes: 22 additions & 8 deletions handler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ type Context struct {
Results chan *Resource

GlobalOptions struct {
output string
noCache bool
noHeader bool
output string
noCache bool
noHeader bool
useServiceNet bool
}

// logger is used to log information acquired while processing the command.
Expand Down Expand Up @@ -79,9 +80,15 @@ func (ctx *Context) storeCredentials() {
}
// get auth credentials
credsResult, err := auth.Credentials(ctx.CLIContext, nil)
ao := credsResult.AuthOpts
region := credsResult.Region
if err == nil {
urlType := gophercloud.AvailabilityPublic
if ctx.GlobalOptions.useServiceNet {
urlType = gophercloud.AvailabilityInternal
}
// form the cache key
cacheKey := auth.CacheKey(credsResult, ctx.ServiceClientType, auth.URLTypeFromCtx(ctx.CLIContext))
cacheKey := auth.CacheKey(*ao, region, ctx.ServiceClientType, urlType)
// initialize the cache
cache := &auth.Cache{}
// set the cache value to the current values
Expand All @@ -103,10 +110,11 @@ func (ctx *Context) handleGlobalOptions() error {

have := make(map[string]commandoptions.Cred)
want := map[string]string{
"output": "",
"no-cache": "",
"no-header": "",
"log": "",
"output": "",
"no-cache": "",
"no-header": "",
"log": "",
"use-service-net": "",
}

// use command-line options if available
Expand Down Expand Up @@ -151,6 +159,12 @@ func (ctx *Context) handleGlobalOptions() error {
ctx.GlobalOptions.noCache = true
}

if ctx.CLIContext.IsSet("use-service-net") {
ctx.GlobalOptions.useServiceNet = true
} else if value, ok := defaultKeysHash["use-service-net"]; ok && value != "" {
ctx.GlobalOptions.useServiceNet = true
}

var logLevel string
if ctx.CLIContext.IsSet("log") {
logLevel = ctx.CLIContext.String("log")
Expand Down
2 changes: 1 addition & 1 deletion handler/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Handle(command Commander) {
errExit1(command, resource)
}

client, err := auth.NewClient(ctx.CLIContext, ctx.ServiceClientType, ctx.logger, ctx.GlobalOptions.noCache)
client, err := auth.NewClient(ctx.CLIContext, ctx.ServiceClientType, ctx.logger, ctx.GlobalOptions.noCache, ctx.GlobalOptions.useServiceNet)
if err != nil {
resource.Err = err
errExit1(command, resource)
Expand Down

0 comments on commit d07512d

Please sign in to comment.