예제 #1
0
func (client *keystoneV2Client) getTenant(filter func(*v2tenants.Tenant) bool) (*v2tenants.Tenant, error) {
	opts := v2tenants.ListOpts{}
	pager := v2tenants.List(client.client, &opts)
	var result *v2tenants.Tenant
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		tenantsList, err := v2tenants.ExtractTenants(page)
		if err != nil {
			return false, err
		}

		for _, tenant := range tenantsList {
			if filter(&tenant) {
				result = &tenant
				return false, nil
			}
		}

		return true, nil
	})
	if err != nil {
		return nil, err
	}

	return result, nil
}
예제 #2
0
파일: tenant.go 프로젝트: romana/core
// GetTenantUUID returns openstack tenant UUID
// corresponding to the given tenantName.
func GetTenantUUID(tenantName string) (string, error) {
	var uuid string

	c, err := getIdentityClient()
	if err != nil {
		log.Println("Error getting Identity Client: ", err)
		return "", err
	}

	opts := tenants.ListOpts{Limit: 20}
	pager := tenants.List(c, &opts)
	// brute force the whole tenant list to get the name?
	pager.EachPage(
		func(page pagination.Page) (bool, error) {
			tenantList, _ := tenants.ExtractTenants(page)
			for _, t := range tenantList {
				// "t" is tenants.Tenant
				if t.Name == tenantName {
					uuid = t.ID
					// stop iterating and return tenant.Name
					return false, nil
				}
			}
			return true, nil
		},
	)

	if uuid == "" {
		log.Printf("Tenant (Name: %s) not found.\n", tenantName)
		return "", util.ErrTenantNotFound
	}

	return uuid, nil
}
예제 #3
0
파일: tenant.go 프로젝트: romana/core
// TenantExists returns true/false depending on
// openstack tenant name or uuid exists.
func TenantExists(name string) bool {
	var tenant bool

	c, err := getIdentityClient()
	if err != nil {
		log.Println("Error getting Identity Client: ", err)
		return false
	}

	opts := tenants.ListOpts{Limit: 20}
	pager := tenants.List(c, &opts)
	// brute force the whole tenant list to get tenant details?
	pager.EachPage(
		func(page pagination.Page) (bool, error) {
			tenantList, _ := tenants.ExtractTenants(page)
			for _, t := range tenantList {
				// "t" is tenants.Tenant
				if t.ID == name || t.Name == name {
					tenant = true
					// stop iterating and return tenant
					return false, nil
				}
			}
			return true, nil
		},
	)
	return tenant
}
// 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
}
예제 #5
0
파일: tenant.go 프로젝트: romana/core
// GetTenantList returns openstack tenant list.
func GetTenantList() ([]tenants.Tenant, error) {
	c, err := getIdentityClient()
	if err != nil {
		return nil, err
	}

	opts := tenants.ListOpts{}
	pager := tenants.List(c, &opts)
	page, err := pager.AllPages()
	if err == nil {
		return tenants.ExtractTenants(page)
	}
	return nil, err
}
예제 #6
0
func findTenant(t *testing.T, client *gophercloud.ServiceClient) string {
	var tenantID string
	err := tenants.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
		tenantList, err := tenants.ExtractTenants(page)
		th.AssertNoErr(t, err)

		for _, t := range tenantList {
			tenantID = t.ID
			break
		}

		return true, nil
	})
	th.AssertNoErr(t, err)

	return tenantID
}
예제 #7
0
func TestEnumerateTenants(t *testing.T) {
	service := authenticatedClient(t)

	t.Logf("Tenants to which your current token grants access:")
	count := 0
	err := tenants2.List(service, nil).EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page %02d ---", count)

		tenants, err := tenants2.ExtractTenants(page)
		th.AssertNoErr(t, err)
		for i, tenant := range tenants {
			t.Logf("[%02d] name=[%s] id=[%s] description=[%s] enabled=[%v]",
				i, tenant.Name, tenant.ID, tenant.Description, tenant.Enabled)
		}

		count++
		return true, nil
	})
	th.AssertNoErr(t, err)
}
예제 #8
0
func (osClient *OpenstackClient) getTenant() (tenant *tenants.Tenant, err error) {
	tenantList := make([]tenants.Tenant, 0)
	opts := tenants.ListOpts{}
	pager := tenants.List(osClient.authClient, &opts)
	err = pager.EachPage(func(page pagination.Page) (bool, error) {
		tenantList, err = tenants.ExtractTenants(page)
		if err != nil {
			return false, err
		}
		return true, nil
	})
	if err != nil {
		return nil, err
	}
	if len(tenantList) > 1 {
		return nil, errors.New("too much tenants")
	} else if len(tenantList) != 1 {
		return nil, errors.New("no tenants")
	}
	return &tenantList[0], nil
}
예제 #9
0
func (c *GenericClient) GetTenantID(d *Driver) (string, error) {
	pager := tenants.List(c.Identity, nil)
	tenantId := ""

	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		tenantList, err := tenants.ExtractTenants(page)
		if err != nil {
			return false, err
		}

		for _, i := range tenantList {
			if i.Name == d.TenantName {
				tenantId = i.ID
				return false, nil
			}
		}

		return true, nil
	})

	return tenantId, err
}
예제 #10
0
// ExtractTenants interprets a page of List results as a more usable slice of Tenant structs.
func ExtractTenants(page pagination.Page) ([]os.Tenant, error) {
	return os.ExtractTenants(page)
}