~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/vim25/client.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) 2015 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 vim25
 
18
 
 
19
import (
 
20
        "encoding/json"
 
21
 
 
22
        "github.com/juju/govmomi/vim25/methods"
 
23
        "github.com/juju/govmomi/vim25/soap"
 
24
        "github.com/juju/govmomi/vim25/types"
 
25
        "golang.org/x/net/context"
 
26
)
 
27
 
 
28
// Client is a tiny wrapper around the vim25/soap Client that stores session
 
29
// specific state (i.e. state that only needs to be retrieved once after the
 
30
// client has been created). This means the client can be reused after
 
31
// serialization without performing additional requests for initialization.
 
32
type Client struct {
 
33
        *soap.Client
 
34
 
 
35
        ServiceContent types.ServiceContent
 
36
 
 
37
        // RoundTripper is a separate field such that the client's implementation of
 
38
        // the RoundTripper interface can be wrapped by separate implementations for
 
39
        // extra functionality (for example, reauthentication on session timeout).
 
40
        RoundTripper soap.RoundTripper
 
41
}
 
42
 
 
43
// NewClient creates and returns a new client wirh the ServiceContent field
 
44
// filled in.
 
45
func NewClient(ctx context.Context, soapClient *soap.Client) (*Client, error) {
 
46
        serviceContent, err := methods.GetServiceContent(ctx, soapClient)
 
47
        if err != nil {
 
48
                return nil, err
 
49
        }
 
50
 
 
51
        c := Client{
 
52
                Client:         soapClient,
 
53
                ServiceContent: serviceContent,
 
54
                RoundTripper:   soapClient,
 
55
        }
 
56
 
 
57
        return &c, nil
 
58
}
 
59
 
 
60
// RoundTrip dispatches to the RoundTripper field.
 
61
func (c *Client) RoundTrip(ctx context.Context, req, res soap.HasFault) error {
 
62
        return c.RoundTripper.RoundTrip(ctx, req, res)
 
63
}
 
64
 
 
65
type marshaledClient struct {
 
66
        SoapClient     *soap.Client
 
67
        ServiceContent types.ServiceContent
 
68
}
 
69
 
 
70
func (c *Client) MarshalJSON() ([]byte, error) {
 
71
        m := marshaledClient{
 
72
                SoapClient:     c.Client,
 
73
                ServiceContent: c.ServiceContent,
 
74
        }
 
75
 
 
76
        return json.Marshal(m)
 
77
}
 
78
 
 
79
func (c *Client) UnmarshalJSON(b []byte) error {
 
80
        var m marshaledClient
 
81
 
 
82
        err := json.Unmarshal(b, &m)
 
83
        if err != nil {
 
84
                return err
 
85
        }
 
86
 
 
87
        *c = Client{
 
88
                Client:         m.SoapClient,
 
89
                ServiceContent: m.ServiceContent,
 
90
                RoundTripper:   m.SoapClient,
 
91
        }
 
92
 
 
93
        return nil
 
94
}
 
95
 
 
96
// Valid returns whether or not the client is valid and ready for use.
 
97
// This should be called after unmarshalling the client.
 
98
func (c *Client) Valid() bool {
 
99
        if c == nil {
 
100
                return false
 
101
        }
 
102
 
 
103
        if c.Client == nil {
 
104
                return false
 
105
        }
 
106
 
 
107
        // Use arbitrary pointer field in the service content.
 
108
        // Doesn't matter which one, as long as it is populated by default.
 
109
        if c.ServiceContent.SessionManager == nil {
 
110
                return false
 
111
        }
 
112
 
 
113
        return true
 
114
}