2
Copyright (c) 2015 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/methods"
23
"github.com/juju/govmomi/vim25/soap"
24
"github.com/juju/govmomi/vim25/types"
25
"golang.org/x/net/context"
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.
35
ServiceContent types.ServiceContent
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
43
// NewClient creates and returns a new client wirh the ServiceContent field
45
func NewClient(ctx context.Context, soapClient *soap.Client) (*Client, error) {
46
serviceContent, err := methods.GetServiceContent(ctx, soapClient)
53
ServiceContent: serviceContent,
54
RoundTripper: soapClient,
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)
65
type marshaledClient struct {
66
SoapClient *soap.Client
67
ServiceContent types.ServiceContent
70
func (c *Client) MarshalJSON() ([]byte, error) {
73
ServiceContent: c.ServiceContent,
76
return json.Marshal(m)
79
func (c *Client) UnmarshalJSON(b []byte) error {
82
err := json.Unmarshal(b, &m)
89
ServiceContent: m.ServiceContent,
90
RoundTripper: m.SoapClient,
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 {
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 {