Exemplo 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
}
// GetTenants is used to retrieve list of available tenant for authenticated user
// List of tenants can then be used to authenticate user for each given tenant
func (c Common) GetTenants(endpoint, user, password string) (map[string]string, error) {
	tnts := map[string]string{}

	provider, err := Authenticate(endpoint, user, password, "")
	if err != nil {
		return nil, err
	}

	client := openstack.NewIdentityV2(provider)

	opts := tenants.ListOpts{}
	pager := tenants.List(client, &opts)

	page, err := pager.AllPages()
	if err != nil {
		return tnts, err
	}

	tenantList, err := tenants.ExtractTenants(page)
	if err != nil {
		return tnts, err
	}

	for _, t := range tenantList {
		tnts[t.ID] = t.Name
	}

	return tnts, nil
}
Exemplo n.º 3
0
func TestGetQuotaset(t *testing.T) {
	client, err := newClient()
	if err != nil {
		t.Fatalf("Unable to create a compute client: %v", err)
	}

	idclient := openstack.NewIdentityV2(client.ProviderClient)
	quotaset, err := quotasets.Get(client, findTenant(t, idclient)).Extract()
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("QuotaSet details:\n")
	t.Logf("                   instances=[%d]\n", quotaset.Instances)
	t.Logf("                       cores=[%d]\n", quotaset.Cores)
	t.Logf("                         ram=[%d]\n", quotaset.Ram)
	t.Logf("                   key_pairs=[%d]\n", quotaset.KeyPairs)
	t.Logf("              metadata_items=[%d]\n", quotaset.MetadataItems)
	t.Logf("             security_groups=[%d]\n", quotaset.SecurityGroups)
	t.Logf("        security_group_rules=[%d]\n", quotaset.SecurityGroupRules)
	t.Logf("                   fixed_ips=[%d]\n", quotaset.FixedIps)
	t.Logf("                floating_ips=[%d]\n", quotaset.FloatingIps)
	t.Logf(" injected_file_content_bytes=[%d]\n", quotaset.InjectedFileContentBytes)
	t.Logf("    injected_file_path_bytes=[%d]\n", quotaset.InjectedFilePathBytes)
	t.Logf("              injected_files=[%d]\n", quotaset.InjectedFiles)

}
Exemplo n.º 4
0
func (c *GenericClient) InitIdentityClient(d *Driver) error {
	if c.Identity != nil {
		return nil
	}

	identity := openstack.NewIdentityV2(c.Provider)
	c.Identity = identity
	return nil
}
Exemplo n.º 5
0
func createClient(t *testing.T, auth bool) *gophercloud.ServiceClient {
	ao := v2AuthOptions(t)

	provider, err := openstack.NewClient(ao.IdentityEndpoint)
	th.AssertNoErr(t, err)

	if auth {
		err = openstack.AuthenticateV2(provider, ao)
		th.AssertNoErr(t, err)
	}

	return openstack.NewIdentityV2(provider)
}
Exemplo n.º 6
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
}
Exemplo n.º 7
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
}
Exemplo n.º 8
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
}
Exemplo n.º 9
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
}
Exemplo n.º 10
0
//GetOpenstackClient makes openstack client
func GetOpenstackClient(authURL, userName, password, domainName, tenantName, version string) (*gophercloud.ServiceClient, error) {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: authURL,
		Username:         userName,
		Password:         password,
		DomainName:       domainName,
		TenantName:       tenantName,
		AllowReauth:      true,
	}

	client, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	client.HTTPClient = cloud.NewHTTPClient()
	if version == "v2.0" {
		return openstack.NewIdentityV2(client), nil
	} else if version == "v3" {
		return openstack.NewIdentityV3(client), nil
	} else {
		return nil, fmt.Errorf("Unsupported keystone version: %s", version)
	}
}