2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
22
"github.com/juju/govmomi/vim25"
23
"github.com/juju/govmomi/vim25/methods"
24
"github.com/juju/govmomi/vim25/mo"
25
"github.com/juju/govmomi/vim25/types"
26
"golang.org/x/net/context"
31
userSession *types.UserSession
34
func NewManager(client *vim25.Client) *Manager {
42
func (sm Manager) Reference() types.ManagedObjectReference {
43
return *sm.client.ServiceContent.SessionManager
46
func (sm *Manager) Login(ctx context.Context, u *url.Userinfo) error {
52
req.UserName = u.Username()
53
if pw, ok := u.Password(); ok {
58
login, err := methods.Login(ctx, sm.client, &req)
63
sm.userSession = &login.Returnval
67
func (sm *Manager) Logout(ctx context.Context) error {
72
_, err := methods.Logout(ctx, sm.client, &req)
81
// UserSession retrieves and returns the SessionManager's CurrentSession field.
82
// Nil is returned if the session is not authenticated.
83
func (sm *Manager) UserSession(ctx context.Context) (*types.UserSession, error) {
84
var mgr mo.SessionManager
86
err := mo.RetrieveProperties(ctx, sm.client, sm.client.ServiceContent.PropertyCollector, sm.Reference(), &mgr)
88
// It's OK if we can't retrieve properties because we're not authenticated
89
if f, ok := err.(types.HasFault); ok {
90
switch f.Fault().(type) {
91
case *types.NotAuthenticated:
99
return mgr.CurrentSession, nil
102
// SessionIsActive checks whether the session that was created at login is
103
// still valid. This function only works against vCenter.
104
func (sm *Manager) SessionIsActive(ctx context.Context) (bool, error) {
105
if sm.userSession == nil {
109
req := types.SessionIsActive{
110
This: sm.Reference(),
111
SessionID: sm.userSession.Key,
112
UserName: sm.userSession.UserName,
115
active, err := methods.SessionIsActive(ctx, sm.client, &req)
120
return active.Returnval, err