Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 86 additions & 9 deletions auth_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ type AuthOptions struct {
// TokenID allows users to authenticate (possibly as another user) with an
// authentication token ID.
TokenID string `json:"-"`

// AgencyNmae is the name of agnecy
AgencyName string `json:"xrole_name,omitempty"`

// AgencyDomainName is the domain name who created the agency
AgencyDomainName string `json:"domain_name,omitempty"`

// AgencyProjectName is the project name of agency
AgencyProjectName string
}

// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
Expand Down Expand Up @@ -263,13 +272,7 @@ func (opts *AuthOptions) ToTokenV3CreateMap(scope map[string]interface{}) (map[s
}

func (opts *AuthOptions) ToTokenV3ScopeMap() (map[string]interface{}, error) {

var scope struct {
ProjectID string
ProjectName string
DomainID string
DomainName string
}
var scope scopeInfo

if opts.TenantID != "" {
scope.ProjectID = opts.TenantID
Expand All @@ -278,9 +281,31 @@ func (opts *AuthOptions) ToTokenV3ScopeMap() (map[string]interface{}, error) {
scope.ProjectName = opts.TenantName
scope.DomainID = opts.DomainID
scope.DomainName = opts.DomainName
} else {
// support scoping to domain
scope.DomainID = opts.DomainID
scope.DomainName = opts.DomainName
}
}
return scope.BuildTokenV3ScopeMap()
}

func (opts *AuthOptions) CanReauth() bool {
return opts.AllowReauth
}

func (opts *AuthOptions) AuthTokenID() string {
return ""
}

type scopeInfo struct {
ProjectID string
ProjectName string
DomainID string
DomainName string
}

func (scope *scopeInfo) BuildTokenV3ScopeMap() (map[string]interface{}, error) {
if scope.ProjectName != "" {
// ProjectName provided: either DomainID or DomainName must also be supplied.
// ProjectID may not be supplied.
Expand Down Expand Up @@ -349,6 +374,58 @@ func (opts *AuthOptions) ToTokenV3ScopeMap() (map[string]interface{}, error) {
return nil, nil
}

func (opts AuthOptions) CanReauth() bool {
return opts.AllowReauth
type AgencyAuthOptions struct {
TokenID string
AgencyName string
AgencyDomainName string
AgencyProjectName string
}

func (opts *AgencyAuthOptions) CanReauth() bool {
return false
}

func (opts *AgencyAuthOptions) AuthTokenID() string {
return opts.TokenID
}

func (opts *AgencyAuthOptions) ToTokenV3ScopeMap() (map[string]interface{}, error) {
scope := scopeInfo{
ProjectName: opts.AgencyProjectName,
DomainName: opts.AgencyDomainName,
}

return scope.BuildTokenV3ScopeMap()
}

func (opts *AgencyAuthOptions) ToTokenV3CreateMap(scope map[string]interface{}) (map[string]interface{}, error) {
type assumeRoleReq struct {
DomainName string `json:"domain_name"`
AgencyName string `json:"xrole_name"`
}

type identityReq struct {
Methods []string `json:"methods"`
AssumeRole assumeRoleReq `json:"assume_role"`
}

type authReq struct {
Identity identityReq `json:"identity"`
}

var req authReq
req.Identity.Methods = []string{"assume_role"}
req.Identity.AssumeRole = assumeRoleReq{
DomainName: opts.AgencyDomainName,
AgencyName: opts.AgencyName,
}
r, err := BuildRequestBody(req, "auth")
if err != nil {
return r, err
}

if len(scope) != 0 {
r["auth"].(map[string]interface{})["scope"] = scope
}
return r, nil
}
25 changes: 24 additions & 1 deletion openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ func v3auth(client *golangsdk.ProviderClient, endpoint string, opts tokens3.Auth
return err
}

opts1, ok := opts.(*golangsdk.AuthOptions)
if ok && opts1.AgencyDomainName != "" && opts1.AgencyName != "" {
opts2 := golangsdk.AgencyAuthOptions{
TokenID: token.ID,
AgencyName: opts1.AgencyName,
AgencyDomainName: opts1.AgencyDomainName,
AgencyProjectName: opts1.AgencyProjectName,
}
result = tokens3.Create(v3Client, &opts2)
token, err = result.ExtractToken()
if err != nil {
return err
}
}

project, err := result.ExtractProject()
if err != nil {
return err
Expand All @@ -207,7 +222,9 @@ func v3auth(client *golangsdk.ProviderClient, endpoint string, opts tokens3.Auth
}

client.TokenID = token.ID
client.ProjectID = project.ID
if project != nil {
client.ProjectID = project.ID
}

if opts.CanReauth() {
client.ReauthFunc = func() error {
Expand Down Expand Up @@ -523,3 +540,9 @@ func NewHwSFSV2(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*g
sc.Endpoint = strings.Replace(sc.Endpoint, "evs", "sfs", 1)
return sc, err
}

// NewDeHServiceV1 creates a ServiceClient that may be used to access the v1 Dedicated Hosts service.
func NewDeHServiceV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) {
sc, err := initClientOpts(client, eo, "deh")
return sc, err
}
18 changes: 18 additions & 0 deletions openstack/deh/v1/common/common_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common

import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/testhelper/client"
)

const TokenID = client.TokenID

// Fake project id to use.
const ProjectID = "17fbda95add24720a4038ba4b1c705ed"

func ServiceClient() *golangsdk.ServiceClient {
sc := client.ServiceClient()
sc.ResourceBase = sc.Endpoint + ProjectID + "/"
sc.ProjectID = ProjectID
return sc
}
54 changes: 54 additions & 0 deletions openstack/deh/v1/hosts/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package hosts

/*
Package hosts enables management and retrieval of Dedicated Hosts

Example to Allocate Hosts
opts := hosts.AllocateOpts{Name:"c2c-test",HostType:"h1",AvailabilityZone:"eu-de-02",AutoPlacement:"off",Quantity:1}
allocatedHosts ,err := hosts.Allocate(client,opts).Extract()
if err != nil {
panic(err)
}
fmt.Println(allocatedHosts)


Example to Update Hosts
updateopts := hosts.UpdateOpts{Name:"NewName3",AutoPlacement:"on"}
update := hosts.Update(client,"8ea7381e-8d84-4f9f-a7ad-d32f1e1bb5b7",updateopts)
if err != nil {
panic(update.Err)
}
fmt.Println(update)

Example to delete Hosts
delete := hosts.Delete(client,"94d94259-3734-4ad5-bc3b-5f9f3e96d5e8")
if err != nil {
panic(delete.Err)
}
fmt.Println(delete)

Example to List Hosts
listdeh := hosts.ListOpts{}
alldehs, err := hosts.List(client,listdeh).AllPages()
list,err:=hosts.ExtractHosts(alldehs)
if err != nil {
panic(err)
}
fmt.Println(list)

Example to Get Host
result := hosts.Get(client, "66156a61-27c2-4169-936b-910dd9c73da3")
out, err := result.Extract()
fmt.Println(out)

Example to List Servers
listOpts := hosts.ListServerOpts{}
allServers, err := hosts.ListServer(client, "671611d2-b45c-4648-9e78-06eb24522291",listOpts)
if err != nil {
panic(err)
}

for _, server := range allServers {
fmt.Printf("%+v\n", server)
}
*/
Loading