本文整理匯總了Golang中github.com/heartsg/dasea/keystone/keystoneclient/client.NewSession函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSession函數的具體用法?Golang NewSession怎麽用?Golang NewSession使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewSession函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewAuthToken
func NewAuthToken(opts *Opts) *AuthToken {
// Init session, session's auth shall be a auto-fetch password
// or token authentication for a service user (have super previlege)
// We will use this service user for:
// 1. validate a normal user token (sent in via request's "X-Auth-Token")
// 2. validate another service token (sent in via request's "X-Service-Token")
// 3. Retrieve revocation list from identity server (not implemented)
params := &types.AuthRequestParams{
Username: opts.Username,
UserId: opts.UserId,
Password: opts.Password,
Token: opts.Token,
DomainName: opts.UserDomainName,
DomainId: opts.UserDomainId,
ProjectName: opts.ProjectName,
ProjectId: opts.ProjectId,
Scope: true,
}
var auth client.Auth
if opts.AuthMethod == "password" {
auth = &client.AuthPassword{Params: params}
} else {
auth = &client.AuthToken{Params: params}
}
s := client.NewSession(&opts.Client, auth)
t := cache.NewTokenCache(opts.MemcacheServers, opts.TokenCacheTime)
return &AuthToken{session: s, cache: t, delayAuthDecision: opts.DelayAuthDecision}
}
示例2: TestUserGet
func TestUserGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/users/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/users/xyz", r.URL.Path)
}
w.Write([]byte(`{
"user": {
"default_project_id": "263fd9",
"description": "John Smith's user",
"domain_id": "1789d1",
"email": "[email protected]",
"enabled": true,
"id": "9fe1d3",
"links": {
"self": "https://identity:35357/v3/users/9fe1d3"
},
"name": "jsmith"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
userResource := &User{
Session: session,
}
user, err := userResource.Get("xyz")
if err != nil {
t.Error(err)
}
if user.Id != "9fe1d3" {
t.Errorf("Expected id == %q; got %q", "9fe1d3", user.Id)
}
}
示例3: TestProjectGet
func TestProjectGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/projects/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/projects/xyz", r.URL.Path)
}
w.Write([]byte(`{
"project": {
"description": null,
"domain_id": "default",
"enabled": true,
"id": "0c4e939acacf4376bdcd1129f1a054ad",
"links": {
"self": "http://localhost:5000/v3/projects/0c4e939acacf4376bdcd1129f1a054ad"
},
"name": "admin",
"parent_id": null
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
projectResource := &Project{
Session: session,
}
project, err := projectResource.Get("xyz")
if err != nil {
t.Error(err)
}
if project.Id != "0c4e939acacf4376bdcd1129f1a054ad" {
t.Errorf("Expected id == %q; got %q", "default", project.Id)
}
}
示例4: TestCredentialGet
func TestCredentialGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/credentials/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/credentials/xyz", r.URL.Path)
}
w.Write([]byte(`{
"credential": {
"user_id": "bb5476fd12884539b41d5a88f838d773",
"links": {
"self": "http://localhost:5000/v3/credentials/207e9b76935efc03804d3dd6ab52d22e9b22a0711e4ada4ff8b76165a07311d7"
},
"blob": "{\"access\": \"a42a27755ce6442596b049bd7dd8a563\", \"secret\": \"71faf1d40bb24c82b479b1c6fbbd9f0c\", \"trust_id\": null}",
"project_id": "6e01855f345f4c59812999b5e459137d",
"type": "ec2",
"id": "207e9b76935efc03804d3dd6ab52d22e9b22a0711e4ada4ff8b76165a07311d7"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
credentialResource := &Credential{
Session: session,
}
credential, err := credentialResource.Get("xyz")
if err != nil {
t.Error(err)
}
if credential.Id != "207e9b76935efc03804d3dd6ab52d22e9b22a0711e4ada4ff8b76165a07311d7" {
t.Errorf("Expected id == %q; got %q", "207e9b76935efc03804d3dd6ab52d22e9b22a0711e4ada4ff8b76165a07311d7", credential.Id)
}
}
示例5: TestGroupGet
func TestGroupGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/groups/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/groups/xyz", r.URL.Path)
}
w.Write([]byte(`{
"group": {
"domain_id": "default",
"description": "Contract developers",
"id": "c0d675eac29945ad9dfd08aa1bb75751",
"links": {
"self": "http://localhost:5000/v3/groups/c0d675eac29945ad9dfd08aa1bb75751"
},
"name": "Contract developers"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
groupResource := &Group{
Session: session,
}
group, err := groupResource.Get("xyz")
if err != nil {
t.Error(err)
}
if group.Id != "c0d675eac29945ad9dfd08aa1bb75751" {
t.Errorf("Expected id == %q; got %q", "c0d675eac29945ad9dfd08aa1bb75751", group.Id)
}
}
示例6: TestDomainGet
func TestDomainGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/domains/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/domains/xyz", r.URL.Path)
}
w.Write([]byte(`{
"domain": {
"description": "Owns users and tenants (i.e. projects) available on Identity API v2.",
"enabled": true,
"id": "default",
"links": {
"self": "http://localhost:5000/v3/domains/default"
},
"name": "Default"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
domainResource := &Domain{
Session: session,
}
domain, err := domainResource.Get("xyz")
if err != nil {
t.Error(err)
}
if domain.Id != "default" {
t.Errorf("Expected id == %q; got %q", "default", domain.Id)
}
}
示例7: TestRegionGet
func TestRegionGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/regions/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/regions/xyz", r.URL.Path)
}
w.Write([]byte(`{
"region": {
"description": "My subregion 3",
"id": "RegionThree",
"links": {
"self": "http://localhost:5000/v3/regions/RegionThree"
},
"parent_region_id": "RegionOne"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
regionResource := &Region{
Session: session,
}
region, err := regionResource.Get("xyz")
if err != nil {
t.Error(err)
}
if region.Id != "RegionThree" {
t.Errorf("Expected id == %q; got %q", "RegionThree", region.Id)
}
}
示例8: TestRoleGet
func TestRoleGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/roles/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/roles/xyz", r.URL.Path)
}
w.Write([]byte(`{
"role": {
"id": "1e443fa8cee3482a8a2b6954dd5c8f12",
"links": {
"self": "http://localhost:5000/v3/roles/1e443fa8cee3482a8a2b6954dd5c8f12"
},
"name": "Developer"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
roleResource := &Role{
Session: session,
}
role, err := roleResource.Get("xyz")
if err != nil {
t.Error(err)
}
if role.Id != "1e443fa8cee3482a8a2b6954dd5c8f12" {
t.Errorf("Expected id == %q; got %q", "1e443fa8cee3482a8a2b6954dd5c8f12", role.Id)
}
}
示例9: TestUserChangePassword
func TestUserChangePassword(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.POST {
t.Errorf("Expected method %q; got %q", requests.POST, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/users/xyz/password" {
t.Errorf("Expected 'url' == %q; got %q", "/users/xyz/password", r.URL.Path)
}
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if string(body) != `{"user":{"password":"123","original_password":"456"}}` {
t.Errorf("Expected auth request body == %q; got %q",
`{"user":{"password":"123","original_password":"456"}}`,
string(body))
}
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
userResource := &User{
Session: session,
}
err := userResource.ChangePassword("xyz", "123", "456")
if err != nil {
t.Error(err)
}
}
示例10: TestUserDelete
func TestUserDelete(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.DELETE {
t.Errorf("Expected method %q; got %q", requests.DELETE, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/users/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/users/xyz", r.URL.Path)
}
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
userResource := &User{
Session: session,
}
err := userResource.Delete("xyz")
if err != nil {
t.Error(err)
}
}
示例11: TestGroupList
func TestGroupList(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
//test query parameter
q := r.URL.Query()
if q.Get("domain_id") != "default" {
t.Errorf("Expected 'domain_id' == %q; got %q", "default", q.Get("domain_id"))
}
w.Write([]byte(`{
"links": {
"self": "http://localhost:5000/v3/groups",
"previous": null,
"next": null
},
"groups": [
{
"domain_id": "default",
"description": "non-admin group",
"id": "96372bbb152f475aa37e9a76a25a029c",
"links": {
"self": "http://localhost:5000/v3/groups/96372bbb152f475aa37e9a76a25a029c"
},
"name": "nonadmins"
},
{
"domain_id": "default",
"description": "openstack admin group",
"id": "9ce0ad4e58a84d7a97b92f7955d10c92",
"links": {
"self": "http://localhost:5000/v3/groups/9ce0ad4e58a84d7a97b92f7955d10c92"
},
"name": "admins"
}
]
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
groupResource := &Group{
Session: session,
}
groups, err := groupResource.List("default", "")
if err != nil {
t.Error(err)
}
if groups.Links["self"] != "http://localhost:5000/v3/groups" {
t.Errorf("Expect groups.Links[self] == %q; got %q", "http://localhost:5000/v3/groups", groups.Links["self"])
}
if groups.Groups[0].Id != "96372bbb152f475aa37e9a76a25a029c" {
t.Errorf("Expect groups.Groups[0].Id == %q; got %q", "96372bbb152f475aa37e9a76a25a029c", groups.Groups[0].Id)
}
if groups.Groups[1].Links["self"] != "http://localhost:5000/v3/groups/9ce0ad4e58a84d7a97b92f7955d10c92" {
t.Errorf("Expect groups.Groups[1].Links[self] == %q; got %q", "http://localhost:5000/v3/groups/9ce0ad4e58a84d7a97b92f7955d10c92", groups.Groups[1].Links["self"])
}
}
示例12: TestGroupListUsers
func TestGroupListUsers(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/groups/xyz/users" {
t.Errorf("Expected 'url' == %q; got %q", "/groups/xyz/users", r.URL.Path)
}
q := r.URL.Query()
if q.Get("domain_id") != "default" {
t.Errorf("Expected 'domain_id' == %q; got %q", "default", q.Get("domain_id"))
}
w.Write([]byte(`{
"users": [
{
"name": "admin",
"links": {
"self": "http://localhost:5000/v3/users/fff603a0829d41e48bc0dd0d72ad61ce"
},
"domain_id": "default",
"enabled": true,
"email": null,
"id": "fff603a0829d41e48bc0dd0d72ad61ce"
}
],
"links": {
"self": "http://localhost:5000/v3/groups/9ce0ad4e58a84d7a97b92f7955d10c92/users",
"previous": null,
"next": null
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
groupResource := &Group{
Session: session,
}
users, err := groupResource.ListUsers("xyz", "default", "", "", "true")
if err != nil {
t.Error(err)
}
if users.Links["self"] != "http://localhost:5000/v3/groups/9ce0ad4e58a84d7a97b92f7955d10c92/users" {
t.Errorf("Expect users.Links[self] == %q; got %q", "http://localhost:5000/v3/groups/9ce0ad4e58a84d7a97b92f7955d10c92/users", users.Links["self"])
}
}
示例13: TestGroupUpdate
func TestGroupUpdate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.PATCH {
t.Errorf("Expected method %q; got %q", requests.PATCH, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/groups/xyz" {
t.Errorf("Expected 'url' == %q; got %q", "/groups/xyz", r.URL.Path)
}
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
tmp := `{
"group": {
"description": "Contract developers 2016",
"name": "Contract developers 2016"
}
}`
tmpStruct := &types.GroupResponse{}
_ = json.Unmarshal([]byte(tmp), tmpStruct)
tmpbytes, _ := json.Marshal(tmpStruct)
if string(body) != string(tmpbytes) {
t.Errorf("Expected auth request body == %q; got %q", string(tmpbytes), string(body))
}
w.Write([]byte(`{
"group": {
"domain_id": "default",
"description": "Contract developers 2016",
"id": "c0d675eac29945ad9dfd08aa1bb75751",
"links": {
"self": "http://localhost:5000/v3/groups/c0d675eac29945ad9dfd08aa1bb75751"
},
"name": "Contract developers 2016"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
groupResource := &Group{
Session: session,
}
group, err := groupResource.Update("xyz", &types.Group{
Description: "Contract developers 2016",
Name: "Contract developers 2016",
})
if err != nil {
t.Error(err)
}
if group.Id != "c0d675eac29945ad9dfd08aa1bb75751" {
t.Errorf("Expect group.Id == %q; got %q", "c0d675eac29945ad9dfd08aa1bb75751", group.Id)
}
}
示例14: TestDomainCreate
func TestDomainCreate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.POST {
t.Errorf("Expected method %q; got %q", requests.POST, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
tmp := `{
"domain": {
"description": "Domain description",
"enabled": true,
"name": "myDomain"
}
}`
tmpStruct := &types.DomainResponse{}
_ = json.Unmarshal([]byte(tmp), tmpStruct)
tmpbytes, _ := json.Marshal(tmpStruct)
if string(body) != string(tmpbytes) {
t.Errorf("Expected auth request body == %q; got %q", string(tmpbytes), string(body))
}
w.Write([]byte(`{
"domain": {
"description": "Domain description",
"enabled": true,
"id": "161718",
"links": {
"self": "http://identity:35357/v3/domains/161718"
},
"name": "myDomain"
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
domainResource := &Domain{
Session: session,
}
domain, err := domainResource.Create(&types.Domain{
Description: "Domain description",
Enabled: true,
Name: "myDomain",
})
if err != nil {
t.Error(err)
}
if domain.Id != "161718" {
t.Errorf("Expect domain.Id == %q; got %q", "161718", domain.Id)
}
}
示例15: TestUserListProjects
func TestUserListProjects(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != requests.GET {
t.Errorf("Expected method %q; got %q", requests.GET, r.Method)
}
if r.Header == nil {
t.Errorf("Expected non-nil request Header")
}
if r.Header.Get("User-Agent") != "Dasea Keystone client" {
t.Errorf("Expected 'User-Agent' == %q; got %q", "Dasea Keystone client", r.Header.Get("User-Agent"))
}
if r.Header.Get("X-Auth-Token") != "12345" {
t.Errorf("Expected 'X-Auth-Token' == %q; got %q", "12345", r.Header.Get("X-Auth-Token"))
}
if r.URL.Path != "/users/xyz/projects" {
t.Errorf("Expected 'url' == %q; got %q", "/users/xyz/projects", r.URL.Path)
}
w.Write([]byte(`{
"projects": [
{
"description": "description of this project",
"domain_id": "161718",
"enabled": true,
"id": "456788",
"parent_id": "212223",
"links": {
"self": "http://identity:35357/v3/projects/456788"
},
"name": "a project name"
},
{
"description": "description of this project",
"domain_id": "161718",
"enabled": true,
"id": "456789",
"parent_id": "212223",
"links": {
"self": "http://identity:35357/v3/projects/456789"
},
"name": "another domain"
}
],
"links": {
"self": "http://identity:35357/v3/users/313233/projects",
"previous": null,
"next": null
}
}`))
}))
defer ts.Close()
auth := &client.AuthAccess{
Access: &client.AccessInfo{
Token: "12345",
},
}
session := client.NewSession(&keystoneclient.Opts{AuthUrl: ts.URL}, auth)
userResource := &User{
Session: session,
}
projects, err := userResource.ListProjects("xyz")
if err != nil {
t.Error(err)
}
if projects.Links["self"] != "http://identity:35357/v3/users/313233/projects" {
t.Errorf("Expect projects.Links[self] == %q; got %q", "http://identity:35357/v3/users/313233/projects", projects.Links["self"])
}
if projects.Projects[0].Name != "a project name" {
t.Errorf("Expect projects.Projects[0].Name == %q; got %q", "a project name", projects.Projects[0].Name)
}
if projects.Projects[1].Links["self"] != "http://identity:35357/v3/projects/456789" {
t.Errorf("Expect projects.Projects[1].Links[self] == %q; got %q", "http://identity:35357/v3/projects/456789", projects.Projects[1].Links["self"])
}
}