~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/session/manager.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
 
3
 
 
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
 
7
 
 
8
    http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
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.
 
15
*/
 
16
 
 
17
package session
 
18
 
 
19
import (
 
20
        "net/url"
 
21
 
 
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"
 
27
)
 
28
 
 
29
type Manager struct {
 
30
        client      *vim25.Client
 
31
        userSession *types.UserSession
 
32
}
 
33
 
 
34
func NewManager(client *vim25.Client) *Manager {
 
35
        m := Manager{
 
36
                client: client,
 
37
        }
 
38
 
 
39
        return &m
 
40
}
 
41
 
 
42
func (sm Manager) Reference() types.ManagedObjectReference {
 
43
        return *sm.client.ServiceContent.SessionManager
 
44
}
 
45
 
 
46
func (sm *Manager) Login(ctx context.Context, u *url.Userinfo) error {
 
47
        req := types.Login{
 
48
                This: sm.Reference(),
 
49
        }
 
50
 
 
51
        if u != nil {
 
52
                req.UserName = u.Username()
 
53
                if pw, ok := u.Password(); ok {
 
54
                        req.Password = pw
 
55
                }
 
56
        }
 
57
 
 
58
        login, err := methods.Login(ctx, sm.client, &req)
 
59
        if err != nil {
 
60
                return err
 
61
        }
 
62
 
 
63
        sm.userSession = &login.Returnval
 
64
        return nil
 
65
}
 
66
 
 
67
func (sm *Manager) Logout(ctx context.Context) error {
 
68
        req := types.Logout{
 
69
                This: sm.Reference(),
 
70
        }
 
71
 
 
72
        _, err := methods.Logout(ctx, sm.client, &req)
 
73
        if err != nil {
 
74
                return err
 
75
        }
 
76
 
 
77
        sm.userSession = nil
 
78
        return nil
 
79
}
 
80
 
 
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
 
85
 
 
86
        err := mo.RetrieveProperties(ctx, sm.client, sm.client.ServiceContent.PropertyCollector, sm.Reference(), &mgr)
 
87
        if err != nil {
 
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:
 
92
                                return nil, nil
 
93
                        }
 
94
                }
 
95
 
 
96
                return nil, err
 
97
        }
 
98
 
 
99
        return mgr.CurrentSession, nil
 
100
}
 
101
 
 
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 {
 
106
                return false, nil
 
107
        }
 
108
 
 
109
        req := types.SessionIsActive{
 
110
                This:      sm.Reference(),
 
111
                SessionID: sm.userSession.Key,
 
112
                UserName:  sm.userSession.UserName,
 
113
        }
 
114
 
 
115
        active, err := methods.SessionIsActive(ctx, sm.client, &req)
 
116
        if err != nil {
 
117
                return false, err
 
118
        }
 
119
 
 
120
        return active.Returnval, err
 
121
}