Exemplo n.º 1
0
func configfile(c *cli.Context, have map[string]authCred, need map[string]string) error {
	dir, err := util.RackDir()
	if err != nil {
		// return fmt.Errorf("Error retrieving rack directory: %s\n", err)
		return nil
	}
	f := path.Join(dir, "config")
	cfg, err := ini.Load(f)
	if err != nil {
		// return fmt.Errorf("Error loading config file: %s\n", err)
		return nil
	}
	cfg.BlockMode = false
	var profile string
	if c.GlobalIsSet("profile") {
		profile = c.GlobalString("profile")
	} else if c.IsSet("profile") {
		profile = c.String("profile")
	}
	section, err := cfg.GetSection(profile)
	if err != nil && profile != "" {
		return fmt.Errorf("Invalid config file profile: %s\n", profile)
	}

	for opt := range need {
		if val := section.Key(opt).String(); val != "" {
			have[opt] = authCred{value: val, from: fmt.Sprintf("config file (profile: %s)", section.Name())}
			delete(need, opt)
		}
	}
	return nil
}
Exemplo n.º 2
0
func CLIopts(c *cli.Context, have map[string]Cred, need map[string]string) {
	for opt := range need {
		if c.IsSet(opt) {
			have[opt] = Cred{Value: c.String(opt), From: "command-line"}
			delete(need, opt)
		}
	}
}
Exemplo n.º 3
0
func cliopts(c *cli.Context, have map[string]authCred, need map[string]string) {
	for opt := range need {
		if c.GlobalIsSet(opt) {
			have[opt] = authCred{value: c.GlobalString(opt), from: "command-line"}
			delete(need, opt)
		} else if c.IsSet(opt) {
			have[opt] = authCred{value: c.String(opt), from: "command-line"}
			delete(need, opt)
		}
	}
}
Exemplo n.º 4
0
// NewClient creates and returns a Rackspace client for the given service.
func NewClient(c *cli.Context, serviceType string, logger *logrus.Logger) (*gophercloud.ServiceClient, error) {
	// get the user's authentication credentials
	ao, region, err := Credentials(c, logger)
	if err != nil {
		return nil, err
	}

	if c.GlobalIsSet("no-cache") || c.IsSet("no-cache") {
		return authFromScratch(*ao, region, serviceType, logger)
	}

	// form the cache key
	cacheKey := CacheKey(*ao, region, serviceType)
	// initialize cache
	cache := &Cache{}
	// get the value from the cache
	creds, err := cache.Value(cacheKey)
	// if there was an error accessing the cache or there was nothing in the cache,
	// authenticate from scratch
	if err == nil && creds != nil {
		// we successfully retrieved a value from the cache
		logger.Infof("Using token from cache: %s\n", creds.TokenID)
		pc, err := rackspace.NewClient(ao.IdentityEndpoint)
		if err == nil {
			pc.TokenID = creds.TokenID
			pc.ReauthFunc = reauthFunc(pc, *ao)
			pc.UserAgent.Prepend(util.UserAgent)
			pc.HTTPClient = NewHTTPClient()
			return &gophercloud.ServiceClient{
				ProviderClient: pc,
				Endpoint:       creds.ServiceEndpoint,
			}, nil
		}
	} else {
		return authFromScratch(*ao, region, serviceType, logger)
	}

	return nil, nil
}
Exemplo n.º 5
0
func ConfigFile(c *cli.Context, have map[string]Cred, need map[string]string) error {
	var profile string
	if c.IsSet("profile") {
		profile = c.String("profile")
	}

	section, err := ProfileSection(profile)
	if err != nil {
		return err
	}

	if section == nil {
		return nil
	}

	for opt := range need {
		if val := section.Key(opt).String(); val != "" {
			have[opt] = Cred{Value: val, From: fmt.Sprintf("config file (profile: %s)", section.Name())}
			delete(need, opt)
		}
	}

	if profile != "" {
		section, err := ProfileSection("")
		if err != nil {
			return err
		}

		for opt := range need {
			if val := section.Key(opt).String(); val != "" {
				have[opt] = Cred{Value: val, From: fmt.Sprintf("config file (profile: default)")}
				delete(need, opt)
			}
		}
	}

	return nil
}