Ejemplo n.º 1
0
func newOpenstackClient(config *authConfig) (*OpenstackClient, error) {

	if config == nil {
		err := errors.New("no OpenStack cloud provider config file given")
		return nil, err
	}

	opts := gophercloud.AuthOptions{
		IdentityEndpoint: config.AuthUrl,
		Username:         config.Username,
		Password:         config.Password,
		TenantName:       config.Tenant,
		TenantID:         config.TenantID,
		AllowReauth:      false,
	}

	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		glog.Info("Failed: Starting openstack authenticate client")
		return nil, err
	}
	authClient := openstack.NewIdentityV2(provider)

	return &OpenstackClient{
		provider,
		authClient,
		config,
	}, nil
}
Ejemplo n.º 2
0
func newCinderClient(configFile string) (*cinderClient, error) {
	cfg, err := readConfig(configFile)
	if err != nil {
		return nil, err
	}

	provider, err := openstack.AuthenticatedClient(cfg.toAuthOptions())
	if err != nil {
		return nil, err
	}

	client, err := openstack.NewBlockStorageV2(provider, gophercloud.EndpointOpts{
		Region: cfg.Global.Region,
	})
	if err != nil {
		return nil, err
	}

	cc := cinderClient{
		cinder:  client,
		keyring: cfg.RBD.Keyring,
	}

	return &cc, nil
}
Ejemplo n.º 3
0
func TestAuthenticatedClient(t *testing.T) {
	// Obtain credentials from the environment.
	ao, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		t.Fatalf("Unable to acquire credentials: %v", err)
	}

	client, err := openstack.AuthenticatedClient(ao)
	if err != nil {
		t.Fatalf("Unable to authenticate: %v", err)
	}

	if client.TokenID == "" {
		t.Errorf("No token ID assigned to the client")
	}

	t.Logf("Client successfully acquired a token: %v", client.TokenID)

	// Find the storage service in the service catalog.
	storage, err := openstack.NewObjectStorageV1(client, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
	if err != nil {
		t.Errorf("Unable to locate a storage service: %v", err)
	} else {
		t.Logf("Located a storage service at endpoint: [%s]", storage.Endpoint)
	}
}
Ejemplo n.º 4
0
func (c *Config) client() (*gophercloud.ProviderClient, error) {
	options := gophercloud.AuthOptions{
		IdentityEndpoint: fmt.Sprintf("https://identity.%s.conoha.io/v2.0", c.Region),
		Username:         c.Username,
		Password:         c.Password,
		TenantName:       c.TenantName,
	}

	return openstack.AuthenticatedClient(options)
}
Ejemplo n.º 5
0
func (d *driver) Init(context types.Context, config gofig.Config) error {
	d.config = config

	fields := eff(map[string]interface{}{})
	var err error

	if d.instanceID, err = d.getInstanceID(); err != nil {
		return err
	}

	fields["moduleName"] = context
	fields["instanceId"] = d.instanceID

	if d.region, err = d.getInstanceRegion(); err != nil {
		return err
	}

	fields["region"] = d.region
	d.region = strings.ToUpper(d.region)

	authOpts := d.getAuthOptions()

	fields["identityEndpoint"] = d.authURL()
	fields["userId"] = d.userID()
	fields["userName"] = d.userName()
	if d.password() == "" {
		fields["password"] = ""
	} else {
		fields["password"] = "******"
	}
	fields["tenantId"] = d.tenantID()
	fields["tenantName"] = d.tenantName()
	fields["domainId"] = d.domainID()
	fields["domainName"] = d.domainName()

	if d.provider, err = openstack.AuthenticatedClient(authOpts); err != nil {
		return goof.WithFieldsE(fields,
			"error getting authenticated client", err)
	}

	if d.client, err = openstack.NewComputeV2(d.provider,
		gophercloud.EndpointOpts{Region: d.region}); err != nil {
		goof.WithFieldsE(fields, "error getting newComputeV2", err)
	}

	if d.clientBlockStorage, err = openstack.NewBlockStorageV1(d.provider,
		gophercloud.EndpointOpts{Region: d.region}); err != nil {
		return goof.WithFieldsE(fields,
			"error getting newBlockStorageV1", err)
	}

	log.WithFields(fields).Info("storage driver initialized")
	return nil

}
Ejemplo n.º 6
0
func newClient(t *testing.T) (*gophercloud.ServiceClient, error) {
	ao, err := openstack.AuthOptionsFromEnv()
	th.AssertNoErr(t, err)

	client, err := openstack.AuthenticatedClient(ao)
	th.AssertNoErr(t, err)

	return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
}
Ejemplo n.º 7
0
func (d *driver) Init(r *core.RexRay) error {
	d.r = r
	fields := ef()
	var err error

	if d.instanceID, err = getInstanceID(d.r.Config); err != nil {
		return err
	}

	fields["instanceId"] = d.instanceID

	if d.region, err = getInstanceRegion(d.r.Config); err != nil {
		return err
	}

	fields["region"] = d.region
	d.region = strings.ToUpper(d.region)

	authOpts := getAuthOptions(d.r.Config)

	fields["identityEndpoint"] = d.r.Config.RackspaceAuthURL
	fields["userId"] = d.r.Config.RackspaceUserID
	fields["userName"] = d.r.Config.RackspaceUserName
	if d.r.Config.RackspacePassword == "" {
		fields["password"] = ""
	} else {
		fields["password"] = "******"
	}
	fields["tenantId"] = d.r.Config.RackspaceTenantID
	fields["tenantName"] = d.r.Config.RackspaceTenantName
	fields["domainId"] = d.r.Config.RackspaceDomainID
	fields["domainName"] = d.r.Config.RackspaceDomainName

	if d.provider, err = openstack.AuthenticatedClient(authOpts); err != nil {
		return errors.WithFieldsE(fields,
			"error getting authenticated client", err)
	}

	if d.client, err = openstack.NewComputeV2(d.provider,
		gophercloud.EndpointOpts{Region: d.region}); err != nil {
		errors.WithFieldsE(fields, "error getting newComputeV2", err)
	}

	if d.clientBlockStorage, err = openstack.NewBlockStorageV1(d.provider,
		gophercloud.EndpointOpts{Region: d.region}); err != nil {
		return errors.WithFieldsE(fields,
			"error getting newBlockStorageV1", err)
	}

	log.WithField("provider", providerName).Info("storage driver initialized")

	return nil
}
Ejemplo n.º 8
0
func getProviderClient() (*gophercloud.ProviderClient, error) {
	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		return nil, err
	}
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		if strings.Contains(err.Error(), "provide exactly one of Domain") {
			return nil, fmt.Errorf(envVariablesNotSetError, keystoneDomainIDKey, keystoneDomainNameKey)
		}
	}
	return provider, err
}
Ejemplo n.º 9
0
func newClient(t *testing.T) *gophercloud.ServiceClient {
	ao, err := openstack.AuthOptionsFromEnv()
	th.AssertNoErr(t, err)

	client, err := openstack.AuthenticatedClient(ao)
	th.AssertNoErr(t, err)

	c, err := openstack.NewOrchestrationV1(client, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
	th.AssertNoErr(t, err)
	return c
}
Ejemplo n.º 10
0
// initIdentityClient initializes openstack api using
// gophercloud which handles auth tokens keeping api calls
// simpler. Currently it uses environment variables for
// authenticating with openstack identity.
func initIdentityClient() (*gophercloud.ServiceClient, error) {
	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		log.Println("Error fetching openstack env vars: ", err)
		return nil, err
	}
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		log.Println("Error authenticating with openstack: ", err)
		return nil, err
	}
	return openstack.NewIdentityV2(provider), nil
}
Ejemplo n.º 11
0
// NewKeystoneClient initilizes a keystone client with the provided configuration.
func NewKeystoneClient(config Config) (KeystoneClient, error) {
	opts := config.AuthOptions
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	client := openstack.NewIdentityV3(provider)
	// build a closure for ksClient reauthentication
	client.ReauthFunc = func() error {
		return openstack.AuthenticateV3(client.ProviderClient, opts)
	}
	return &KeystoneClientImpl{client: client, opts: opts, token: nil, monascaURL: nil}, nil
}
Ejemplo n.º 12
0
func newOpenStack(cfg Config) (*OpenStack, error) {
	provider, err := openstack.AuthenticatedClient(cfg.toAuthOptions())
	if err != nil {
		return nil, err
	}

	os := OpenStack{
		provider: provider,
		region:   cfg.Global.Region,
		lbOpts:   cfg.LoadBalancer,
	}
	return &os, nil
}
Ejemplo n.º 13
0
func initClient(config *Config) (*gophercloud.ProviderClient, error) {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: config.Auth_Url,
		Username:         config.User,
		Password:         config.Password,
		TenantName:       config.Tenant,
	}
	client, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	return client, nil
}
Ejemplo n.º 14
0
func New(cfgFile string) CinderDriver {
	conf, err := processConfig(cfgFile)
	isV3 := strings.Contains(conf.Endpoint, "v3")
	if err != nil {
		log.Fatal("Error processing cinder driver config file: ", err)
	}

	_, err = os.Lstat(conf.MountPoint)
	if os.IsNotExist(err) {
		if err := os.MkdirAll(conf.MountPoint, 0755); err != nil {
			log.Fatal("Failed to create Mount directory during driver init: %v", err)
		}
	}

	auth := gophercloud.AuthOptions{
		IdentityEndpoint: conf.Endpoint,
		Username:         conf.Username,
		Password:         conf.Password,
		TenantID:         conf.TenantID,
		AllowReauth:      true,
	}

	if isV3 == true && conf.DomainName == "" {
		log.Warning("V3 endpoint specified, but DomainName not set!")
		log.Warning("Setting to \"Default\", maybe it'll work.")
		auth.DomainName = "Default"
	}

	if conf.DomainName != "" && isV3 == true {
		log.Info("Authorizing to a V3 Endpoint")
		auth.DomainName = conf.DomainName
	}

	providerClient, err := openstack.AuthenticatedClient(auth)
	if err != nil {
		log.Fatal("Error initiating gophercloud provider client: ", err)
	}

	client, err := openstack.NewBlockStorageV2(providerClient,
		gophercloud.EndpointOpts{Region: "RegionOne"})
	if err != nil {
		log.Fatal("Error initiating gophercloud cinder client: ", err)
	}

	d := CinderDriver{
		Conf:   &conf,
		Mutex:  &sync.Mutex{},
		Client: client,
	}
	return d
}
Ejemplo n.º 15
0
func networkingClient() (*gophercloud.ServiceClient, error) {
	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		return nil, err
	}

	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}

	return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
}
Ejemplo n.º 16
0
func NewIdentityClient(a gophercloud.AuthOptions) (*gophercloud.ServiceClient, error) {
	// authenticate with provider
	provider, err := openstack.AuthenticatedClient(a)
	if err != nil {
		return nil, err
	}

	// Create a new client to the provider
	client := openstack.NewIdentityV2(provider)
	if client == nil {
		return nil, errors.New("Could not create new identity client")
	}

	return client, nil
}
Ejemplo n.º 17
0
//NewKeystoneV2Client is an constructor for KeystoneV2Client
func NewKeystoneV2Client(authURL, userName, password, tenantName string) (KeystoneClient, error) {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: authURL,
		Username:         userName,
		Password:         password,
		TenantName:       tenantName,
		AllowReauth:      true,
	}

	client, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	return &keystoneV2Client{client: openstack.NewIdentityV2(client)}, nil
}
Ejemplo n.º 18
0
func (keystoneAuthenticator *KeystoneAuthenticator) AuthenticatePassword(username string, password string) (user.Info, bool, error) {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: keystoneAuthenticator.authURL,
		Username:         username,
		Password:         password,
	}

	_, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		glog.Info("Failed: Starting openstack authenticate client")
		return nil, false, errors.New("Failed to authenticate")
	}

	return &user.DefaultInfo{Name: username}, true, nil
}
Ejemplo n.º 19
0
func TestGetTokenWhenValid(t *testing.T) {
	// setup
	opts := testConfig.AuthOptions
	provider, err := openstack.AuthenticatedClient(opts)
	assert.NoError(t, err)
	client := openstack.NewIdentityV3(provider)
	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}

	// do
	token, err := ksClient.GetToken()

	// assert
	assert.NoError(t, err)
	assert.Equal(t, token, testToken)
}
Ejemplo n.º 20
0
func newClient() (*gophercloud.ServiceClient, error) {
	ao, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		return nil, err
	}

	client, err := openstack.AuthenticatedClient(ao)
	if err != nil {
		return nil, err
	}

	return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
		Region: os.Getenv("OS_REGION_NAME"),
	})
}
Ejemplo n.º 21
0
func NewNeutronMapper() (*NeutronMapper, error) {
	mapper := &NeutronMapper{}

	authURL := config.GetConfig().Section("openstack").Key("auth_url").String()
	username := config.GetConfig().Section("openstack").Key("username").String()
	password := config.GetConfig().Section("openstack").Key("password").String()
	tenantName := config.GetConfig().Section("openstack").Key("tenant_name").String()
	regionName := config.GetConfig().Section("openstack").Key("region_name").String()

	opts := gophercloud.AuthOptions{
		IdentityEndpoint: authURL,
		Username:         username,
		Password:         password,
		TenantName:       tenantName,
	}

	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}

	/* TODO(safchain) add config param for the Availability */
	client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
		Name:         "neutron",
		Region:       regionName,
		Availability: gophercloud.AvailabilityPublic,
	})
	if err != nil {
		return nil, err
	}
	mapper.client = client

	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 30 seconds
	expire, err := config.GetConfig().Section("cache").Key("expire").Int()
	if err != nil {
		return nil, err
	}
	cleanup, err := config.GetConfig().Section("cache").Key("cleanup").Int()
	if err != nil {
		return nil, err
	}
	mapper.cache = cache.New(time.Duration(expire)*time.Second, time.Duration(cleanup)*time.Second)
	mapper.cacheUpdaterChan = make(chan string)
	go mapper.cacheUpdater()

	return mapper, nil
}
// Authenticate is used to authenticate user for given tenant. Request is send to provided Keystone endpoint
// Returns authenticated provider client, which is used as a base for service clients.
func Authenticate(endpoint, user, password, tenant string) (*gophercloud.ProviderClient, error) {
	authOpts := gophercloud.AuthOptions{
		IdentityEndpoint: endpoint,
		Username:         user,
		Password:         password,
		TenantName:       tenant,
		AllowReauth:      true,
	}

	provider, err := openstack.AuthenticatedClient(authOpts)
	if err != nil {
		return nil, err
	}

	return provider, nil
}
Ejemplo n.º 23
0
// initComputeClient initializes openstack api using
// gophercloud which handles auth tokens keeping api calls
// simpler. Currently it uses environment variables for
// authenticating with openstack identity.
func initComputeClient() (*gophercloud.ServiceClient, error) {
	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		log.Println("Error fetching openstack env vars: ", err)
		return nil, err
	}
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		log.Println("Error authenticating with openstack: ", err)
		return nil, err
	}
	return openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Name:   "compute",
		Region: os.Getenv("OS_REGION_NAME"),
	})
}
Ejemplo n.º 24
0
func Init() (storagedriver.Driver, error) {

	instanceID, err := getInstanceID()
	if err != nil {
		return nil, fmt.Errorf("Error: %v", err)
	}

	region, err := getInstanceRegion()
	if err != nil {
		return nil, fmt.Errorf("Error: %v", err)
	}

	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		return nil, fmt.Errorf("Error: %v", err)
	}

	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, fmt.Errorf("Error: %v", err)
	}

	region = strings.ToUpper(region)
	client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		return nil, fmt.Errorf("Error: %v", err)
	}

	clientBlockStorage, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		return nil, fmt.Errorf("Error: %v", err)
	}

	driver := &Driver{
		Provider:           provider,
		Client:             client,
		ClientBlockStorage: clientBlockStorage,
		Region:             region,
		InstanceID:         instanceID,
	}

	return driver, nil
}
Ejemplo n.º 25
0
func TestKeystoneClientReauthenticate(t *testing.T) {
	// setup
	opts := testConfig.AuthOptions
	provider, err := openstack.AuthenticatedClient(opts)
	assert.NoError(t, err)
	client := openstack.NewIdentityV3(provider)
	client.TokenID = "someinvalidtoken"
	client.ReauthFunc = func() error { return openstack.AuthenticateV3(client.ProviderClient, opts) }
	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}

	// do
	token, err := ksClient.GetToken()

	// assert
	assert.NoError(t, err)
	assert.Equal(t, token, testToken)
}
Ejemplo n.º 26
0
func NewObjectStorageClient(a gophercloud.AuthOptions, region string) (*gophercloud.ServiceClient, error) {
	// authenticate with provider
	provider, err := openstack.AuthenticatedClient(a)
	if err != nil {
		return nil, err
	}

	// Create a new client to the provider
	client, err := openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{
		Region: region,
	})
	if err != nil {
		return nil, err
	}

	return client, nil
}
Ejemplo n.º 27
0
func (s *Swift) Auth() error {
	if s.authOptions.Username != "" {
		log.Debugf("(OpenStack) Authenticate by username(%s)", s.authOptions.Username)
	} else if s.authOptions.UserID != "" {
		log.Debugf("(OpenStack) Authenticate by user-id(%s)", s.authOptions.UserID)
	} else {
		log.Debugf("(OpenStack) Authenticate")
	}

	provider, err := openstack.AuthenticatedClient(s.authOptions)
	if err != nil {
		return err
	}

	s.client, err = openstack.NewObjectStorageV1(provider, s.endpointOptions)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 28
0
func (c *Config) loadAndValidate() error {
	ao := gophercloud.AuthOptions{
		Username:         c.Username,
		UserID:           c.UserID,
		Password:         c.Password,
		APIKey:           c.APIKey,
		IdentityEndpoint: c.IdentityEndpoint,
		TenantID:         c.TenantID,
		TenantName:       c.TenantName,
		DomainID:         c.DomainID,
		DomainName:       c.DomainName,
	}

	client, err := openstack.AuthenticatedClient(ao)
	if err != nil {
		return err
	}

	c.osClient = client

	return nil
}
Ejemplo n.º 29
0
func get_token() (*tokens.Token, error) {

	authOpts, err := openstack.AuthOptionsFromEnv()
	// authOpts := gophercloud.AuthOptions{
	// 	IdentityEndpoint: endpoint,
	// 	Username:         username,
	// 	Password:         pass,
	// 	TenantID:         tenantid,
	// }

	provider, err := openstack.AuthenticatedClient(authOpts)

	client := openstack.NewIdentityV2(provider)

	opts := tokens.AuthOptions{authOpts}

	bearer_token, err := tokens.Create(client, opts).ExtractToken()
	if err != nil {
		return nil, err
	}
	return bearer_token, nil
}
Ejemplo n.º 30
0
func NewNeutronMapper(g *graph.Graph, authURL string, username string, password string, tenantName string, regionName string) (*NeutronMapper, error) {
	mapper := &NeutronMapper{graph: g}

	opts := gophercloud.AuthOptions{
		IdentityEndpoint: authURL,
		Username:         username,
		Password:         password,
		TenantName:       tenantName,
		AllowReauth:      true,
	}

	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}

	/* TODO(safchain) add config param for the Availability */
	client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
		Name:         "neutron",
		Region:       regionName,
		Availability: gophercloud.AvailabilityPublic,
	})
	if err != nil {
		return nil, err
	}
	mapper.client = client

	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 30 seconds
	expire := config.GetConfig().GetInt("cache.expire")
	cleanup := config.GetConfig().GetInt("cache.cleanup")
	mapper.cache = cache.New(time.Duration(expire)*time.Second, time.Duration(cleanup)*time.Second)
	mapper.nodeUpdaterChan = make(chan graph.Identifier, 500)

	g.AddEventListener(mapper)

	return mapper, nil
}