Ejemplo n.º 1
0
func GetClient(project string, region string, cache_path string) (Client, error) {
	c := Client{}
	cache, err := config.LoadCredCache(cache_path)
	if err != nil {
		fmt.Println("unable to get cache")
		return c, err
	}
	c.cache = cache
	if region == "" {
		region = defaultRegion
	}
	c.region = region
	if c.cache.Rackspace.User == "" || c.cache.Rackspace.APIKey == "" {
		var rack_user string
		var rack_pass string
		fmt.Printf("rackspace user: "******"%s", &rack_user)
		if err != nil {
			return c, err
		}
		fmt.Printf("rackspace api key: ")
		_, err = fmt.Scanf("%s", &rack_pass)
		if err != nil {
			return c, err
		}
		c.cache.Rackspace.User = rack_user
		c.cache.Rackspace.APIKey = rack_pass
		c.cache.Save()
		if err != nil {
			return c, err
		}
	}
	ao := gophercloud.AuthOptions{
		Username:    c.cache.Rackspace.User,
		ApiKey:      c.cache.Rackspace.APIKey,
		AllowReauth: true,
	}
	access, err := gophercloud.Authenticate("rackspace-us", ao)
	if err != nil {
		c.cache.Rackspace.User = ""
		c.cache.Rackspace.APIKey = ""
		c.cache.Save()
		return c, err
	}
	ac, err := gophercloud.PopulateApi("rackspace")
	if err != nil {
		return c, err
	}
	client, err := gophercloud.ServersApi(access, ac)
	if err != nil {
		return c, err
	}
	c.client = client
	return c, nil
}
Ejemplo n.º 2
0
Archivo: do.go Proyecto: polvi/coreup
func GetClient(project string, region string, cache_path string) (*Client, error) {
	cache, err := config.LoadCredCache(cache_path)
	if err != nil {
		return nil, err
	}
	if region == "" {
		region = defaultRegion
	}
	cfg := &oauth.Config{
		ClientId:     "69d74afe2eb5cfde808a333e448cdd2a0bd60672ab483850ac38fe68b383e1db",
		ClientSecret: "cf430afc2cff561a97883309700dc7966fec0ca5520e2a0fe0e2d8b382f6538a",
		Scope:        "read write",
		RedirectURL:  "http://localhost:8016/oauth2callback",
		AuthURL:      "https://cloud.digitalocean.com/v1/oauth/authorize",
		TokenURL:     "https://cloud.digitalocean.com/v1/oauth/token",
	}
	if cache.DO.Token.Expiry.Before(time.Now()) {
		token, err := authRefreshToken(cfg)
		if err != nil {
			return nil, err
		}
		cache.DO.Token = *token
		cache.Save()
	}
	token := &cache.DO.Token
	transport := &oauth.Transport{
		Config:    cfg,
		Token:     token,
		Transport: http.DefaultTransport,
	}
	svc := &do.Client{
		Client: transport.Client(),
	}
	if err != nil {
		return nil, err
	}
	return &Client{
		service: svc,
		cache:   cache,
		region:  region,
	}, nil
}
Ejemplo n.º 3
0
Archivo: ec2.go Proyecto: polvi/coreup
func GetClient(project string, region string, cache_path string) (Client, error) {
	// this will cause the google cache to be populated
	gce.GetClient(project, region, cache_path)
	c := Client{}
	cache, err := config.LoadCredCache(cache_path)
	if err != nil {
		return c, err
	}
	c.cache = cache
	if region == "" {
		region = defaultRegion
	}
	c.region = region
	if cache.AWS.RoleARN == "" {
		var arn string
		fmt.Printf("amazon role arn: ")
		_, err = fmt.Scanf("%s", &arn)
		if err != nil {
			return c, err
		}
		c.cache.AWS.RoleARN = strings.TrimSpace(arn)
		c.cache.Save()
	}
	if c.cache.AWS.Token.Expiry.Before(time.Now()) {
		auth, err := authAWSFromToken(&c.cache.GCE.Token, c.cache.AWS.RoleARN)
		if err != nil {
			return c, err
		}
		c.cache.AWS.Token = *auth
		c.cache.Save()
	}
	if _, ok := aws.Regions[c.region]; !ok {
		return c, errors.New("could not find region " + c.region)
	}
	c.client = ec2.New(c.cache.AWS.Token.Auth, aws.Regions[c.region])
	return c, nil
}
Ejemplo n.º 4
0
func GetClient(project string, region string, cache_path string) (*Client, error) {
	cache, err := config.LoadCredCache(cache_path)
	if err != nil {
		return nil, err
	}
	if region == "" {
		region = defaultRegion
	}
	if cache.GCE.Project == "" {
		var project string
		fmt.Printf("google project id: ")
		_, err = fmt.Scanf("%s", &project)
		if err != nil {
			return nil, err
		}
		cache.GCE.Project = strings.TrimSpace(project)
		cache.Save()
	}
	if cache.GCE.SSOClientID == "" || cache.GCE.SSOClientSecret == "" {
		var client_id string
		var client_secret string
		fmt.Printf("google client id: ")
		_, err = fmt.Scanf("%s", &client_id)
		if err != nil {
			return nil, err
		}
		cache.GCE.SSOClientID = strings.TrimSpace(client_id)
		fmt.Printf("google client secret: ")
		_, err = fmt.Scanf("%s", &client_secret)
		if err != nil {
			return nil, err
		}
		cache.GCE.SSOClientSecret = strings.TrimSpace(client_secret)
		if err != nil {
			return nil, err
		}
		cache.Save()
	}
	cfg := &oauth.Config{
		ClientId:     cache.GCE.SSOClientID,
		ClientSecret: cache.GCE.SSOClientSecret,
		Scope:        "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/compute https://www.googleapis.com/auth/devstorage.read_write https://www.googleapis.com/auth/userinfo.email",
		RedirectURL:  "http://localhost:8016/oauth2callback",
		AuthURL:      "https://accounts.google.com/o/oauth2/auth",
		TokenURL:     "https://accounts.google.com/o/oauth2/token",
	}
	if cache.GCE.Token.Expiry.Before(time.Now()) {
		token, err := authRefreshToken(cfg)
		if err != nil {
			return nil, err
		}
		cache.GCE.Token = *token
		cache.Save()

	}
	token := &cache.GCE.Token
	transport := &oauth.Transport{
		Config:    cfg,
		Token:     token,
		Transport: http.DefaultTransport,
	}
	svc, err := compute.New(transport.Client())
	if err != nil {
		return nil, err
	}
	return &Client{
		service:    svc,
		cache:      cache,
		region:     region,
		project_id: cache.GCE.Project,
	}, nil
}