rackspace / gophercloud

A Go SDK for OpenStack. IN FEATURE FREEZE. See Issue #592
http://gophercloud.io
Other
456 stars 185 forks source link

Openstack Identity V2 Tenant List -- Pagination showing one Tenant #599

Open kgrvamsi opened 8 years ago

kgrvamsi commented 8 years ago

I'm not sure if i'm using the library properly but confused with the end result of the code

config.yaml

[dc-alpha]

identityurl = "https://alpha.example.com:443/v2.0"
username= "admin"
password= "KeepItSecret"
tenantid= "2bbb9edfce58478ba2180da955efdaca"
region="RegionOne"
type="Production"

[dc-gama]

identityurl = "https://gama.example.com:443/v2.0"
username= "admin"
password= "KeepItSecret"
tenantid= "2bbb9edfce58478ba2180da955efdaca"
region="RegionOne"
type="Production

 [dc-beta]

 identityurl = "https://beta.example.com:443/v2.0"
username= "admin"
password= "KeepItSecret"
tenantid= "2bbb9edfce58478ba2180da955efdaca"
region="RegionOne"
type="Production

fetcher.go

package main

import (
    _"encoding/csv"
    "fmt"
    _"github.com/olekukonko/tablewriter"
    "github.com/pelletier/go-toml"
    "github.com/rackspace/gophercloud"
    "github.com/rackspace/gophercloud/openstack"
    "github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
    "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
    _ "github.com/rackspace/gophercloud/openstack/identity/v2/users"
    "github.com/rackspace/gophercloud/pagination"
    "os"
    "strconv"
    "strings"
)

func getList() []string {

    config, err := toml.LoadFile("config.toml")
    if err != nil {
        fmt.Println(err)
    }

    list := config.Keys()

    return list
}

func main() {

    data := getList()
    if len(data) == 0 {

        fmt.Print("We ended up with no data to start the show!!!!")

    } else {

        config, err := toml.LoadFile("config.toml")
        if err != nil {
            fmt.Println(err)
        }

        fmt.Println("Listing out the Dacenters available: \n")

        table := tablewriter.NewWriter(os.Stdout)
        table.SetHeader([]string{"No", "Datacenter", "Location", "Type"})

        for i, v := range data {

            dctype := config.Get(v + ".type").(string)

            dcenter := strings.Split(v, "-")[1]
            dc := []string{strconv.Itoa(i + 1), v, dcenter, dctype}

            table.Append(dc)
        }
        table.Render()

        fmt.Println("Which Datacenter you would like to access?")
        var choice int
        fmt.Scan(&choice)

        for k, v := range data {
            if k+1 == choice {

                if config.Get(v+".type").(string) == "Production" {
                    fmt.Println("*************************************************************\n" +
                        "** Carefull !! You are Dealing with a Production Machine ****\n" +
                        "*************************************************************")
                }

                opts := gophercloud.AuthOptions{
                    IdentityEndpoint: config.Get(v + ".identityurl").(string),
                    Username:         config.Get(v + ".username").(string),
                    Password:         config.Get(v + ".password").(string),
                    AllowReauth:      true,
                }

                fmt.Println("Selected DataCenter: " + v + "\n")

                fmt.Println("Which Service you Want to Operate?")

                table := tablewriter.NewWriter(os.Stdout)
                table.SetHeader([]string{"No", "Service"})

                serv := [][]string{
                    []string{"1", "Block Storage"},
                    []string{"2", "Identity"},
                    []string{"3", "Compute"},
                    []string{"4", "Networking"},
                    []string{"5", "Object Storage"},
                }

                for _, l := range serv {
                    table.Append(l)
                }

                table.Render()

                fmt.Scan(&choice)

                if choice == 1 {

                    fmt.Println("Block Storage -- " + v + "")

                } else if choice == 2 {

                    fmt.Println("Identity -- " + v + " \n")

                    fmt.Println("Which Version you want to Access?")
                    fmt.Println("1. v2\n2. v3")
                    fmt.Scan(&choice)

                    if choice == 1 {
                        fmt.Println("Selected the v2 version")
                        provider, err := openstack.AuthenticatedClient(opts)

                        client := openstack.NewIdentityV2(provider)

                        if err != nil {
                            fmt.Println("we are here in the error block")
                            fmt.Println(err)
                        }

                        tenant_opts := tenants.ListOpts{}

                        // Retrieve a pager (i.e. a paginated collection)

                        var opts = tokens.AuthOptions{
                            opts,
                        }

                        var token, _ = tokens.Create(client, opts).ExtractToken()
                        fmt.Println(token)
                        pager := tenants.List(client, &tenant_opts)

                        pager.EachPage(func(page pagination.Page) (bool, error) {
                            tenantList, err := tenants.ExtractTenants(page)

                            for _, t := range tenantList {
                                // "t" will be a tenants.Tenant
                                fmt.Println(t)
                            }

                            return true, nil
                        })
                    }
                }
            }
        }
    }
}

Output gophercloud-error-new

Note:

1)Every Dc have around 100+ Tenants 2)Sorry if my standard of programming looks messy anywhere.Recommendations are welcome.

kgrvamsi commented 8 years ago

@jrperritt @jamiehannaford Ping

jamiehannaford commented 8 years ago

What are you trying to do with the list of tenants and the token? What is the problem you're running into?

kgrvamsi commented 8 years ago

I'm more concerned on the tenant list not on the tokens (the code for token above is just to check the function) and my problem here is that when I'm hitting the identity api and trying to retrieve the list of tenants available in that dc I only see one tenant which I'm part of the tenant space but not all the tenants available in that specific dc.

jrperritt commented 8 years ago
  1. see example here
  2. this operation only lists tenants to which the token has access. it looks like the credentials you used don't have appropriate permissions to see the other tenants
kgrvamsi commented 8 years ago

Thanks @jrperritt @jamiehannaford i just have checked the code there and i tried tweaking a bit creating the token and making a request accordingly and here is the snippet not sure if the request flow to fetch the data that i'm doing is correct or not

opts := gophercloud.AuthOptions{
                    IdentityEndpoint: config.Get(v + ".identityurl").(string),
                    Username:         config.Get(v + ".username").(string),
                    Password:         config.Get(v + ".password").(string),
                                        TenantID:         config.Get(v + ".tenantid").(string),
                    AllowReauth: true,
          }
provider, err := openstack.AuthenticatedClient(opts)

    client := openstack.NewIdentityV2(provider)

    fmt.Println(client)

    if err != nil {
        fmt.Println("we are here in the error block")
        fmt.Println(err)
    }

    var opts = tokens.AuthOptions{
        opts,
    }

    result := tokens.Create(client, opts)

    fmt.Println(result)

    token, err := result.ExtractToken()
    fmt.Println(token.ID,token.Name)

    getResult := tokens.Get(client, token.ID)

    user, err := getResult.ExtractUser()
    if err != nil {
        fmt.Println("We are here in the err block")
        fmt.Println(err)
    }
    fmt.Println(user)
       count := 0
    err := tenants.List(client, &tenant_opts).EachPage(func(page pagination.Page) (bool, error) {
        tenantList, err := tenants.ExtractTenants(page)

        for _, t := range tenantList {
            // "t" will be a tenants.Tenant
            fmt.Println(t)
        }
           count++
           return true,nil
    })
}

Output

I get the client object with all the endpoint urls and the roles for the specified user and the user have member and the admin user and the same user in python can get all the tenants and users with respective their roles but here i see single tenant user as show in my first comment screenshot

kgrvamsi commented 8 years ago

I hit the wrong Function to grab all the tenants list and the correct function is NewIdentityAdminV2 and the documentation looks wrong with the example there

client-gophercloud

lingxiankong commented 6 years ago

@kgrvamsi thanks for the hint, I also met with the issue.