Skip to content

Commit

Permalink
Merge pull request vmware#1973 from dougm/rest-session-id
Browse files Browse the repository at this point in the history
vapi: sync access to rest.Client.SessionID
  • Loading branch information
dougm authored May 7, 2020
2 parents 967bc58 + 3aa9aab commit cb26631
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion govc/library/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (cmd *export) Run(ctx context.Context, f *flag.FlagSet) error {
download := func(src *url.URL, name string) error {
p := soap.DefaultDownload
p.Headers = map[string]string{
"vmware-api-session-id": c.SessionID,
"vmware-api-session-id": c.SessionID(),
}
if isStdout {
s, _, err := c.Download(ctx, src, &p)
Expand Down
4 changes: 2 additions & 2 deletions govc/session/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ func (cmd *login) setCookie(ctx context.Context, c *vim25.Client) error {

func (cmd *login) setRestCookie(ctx context.Context, c *rest.Client) error {
if cmd.cookie == "" {
cmd.cookie = c.SessionID
cmd.cookie = c.SessionID()
} else {
c.SessionID = cmd.cookie
c.SessionID(cmd.cookie)

// Check the session is still valid
s, err := c.Session(ctx)
Expand Down
2 changes: 1 addition & 1 deletion govc/session/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}
m.SessionList = append(m.SessionList, types.UserSession{
Key: rc.SessionID,
Key: rc.SessionID(),
UserName: rs.User + " (REST)",
LoginTime: rs.Created,
LastActiveTime: rs.LastAccessed,
Expand Down
6 changes: 2 additions & 4 deletions session/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,16 @@ func (s *Session) Login(ctx context.Context, c Client, config func(*soap.Client)
*client = *vc
c = client
case *rest.Client:
vc := &vim25.Client{Client: sc}
rc := rest.NewClient(vc)
client.Client = sc.NewServiceClient(rest.Path, "")

login := s.loginREST
if s.LoginREST != nil {
login = s.LoginREST
}
if err = login(ctx, rc); err != nil {
if err = login(ctx, client); err != nil {
return err
}

*client = *rc
c = client
default:
panic(fmt.Sprintf("unsupported client type=%T", client))
Expand Down
35 changes: 28 additions & 7 deletions vapi/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"

"github.com/vmware/govmomi/vapi/internal"
Expand All @@ -34,8 +35,10 @@ import (

// Client extends soap.Client to support JSON encoding, while inheriting security features, debug tracing and session persistence.
type Client struct {
mu sync.Mutex

*soap.Client
SessionID string
sessionID string
}

// Session information
Expand All @@ -60,7 +63,17 @@ func (m *LocalizableMessage) Error() string {
func NewClient(c *vim25.Client) *Client {
sc := c.Client.NewServiceClient(Path, "")

return &Client{sc, ""}
return &Client{Client: sc}
}

// SessionID is set by calling Login() or optionally with the given id param
func (c *Client) SessionID(id ...string) string {
c.mu.Lock()
defer c.mu.Unlock()
if len(id) != 0 {
c.sessionID = id[0]
}
return c.sessionID
}

type marshaledClient struct {
Expand All @@ -71,7 +84,7 @@ type marshaledClient struct {
func (c *Client) MarshalJSON() ([]byte, error) {
m := marshaledClient{
SoapClient: c.Client,
SessionID: c.SessionID,
SessionID: c.sessionID,
}

return json.Marshal(m)
Expand All @@ -87,7 +100,7 @@ func (c *Client) UnmarshalJSON(b []byte) error {

*c = Client{
Client: m.SoapClient,
SessionID: m.SessionID,
sessionID: m.SessionID,
}

return nil
Expand Down Expand Up @@ -127,8 +140,8 @@ func (c *Client) Do(ctx context.Context, req *http.Request, resBody interface{})

req.Header.Set("Accept", "application/json")

if c.SessionID != "" {
req.Header.Set(internal.SessionCookieName, c.SessionID)
if id := c.SessionID(); id != "" {
req.Header.Set(internal.SessionCookieName, id)
}

if s, ok := ctx.Value(signerContext{}).(Signer); ok {
Expand Down Expand Up @@ -183,7 +196,15 @@ func (c *Client) Login(ctx context.Context, user *url.Userinfo) error {
}
}

return c.Do(ctx, req, &c.SessionID)
var id string
err := c.Do(ctx, req, &id)
if err != nil {
return err
}

c.SessionID(id)

return nil
}

func (c *Client) LoginByToken(ctx context.Context) error {
Expand Down

0 comments on commit cb26631

Please sign in to comment.