~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/object/http_nfc_lease.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 object
 
18
 
 
19
import (
 
20
        "errors"
 
21
        "fmt"
 
22
 
 
23
        "github.com/juju/govmomi/property"
 
24
        "github.com/juju/govmomi/vim25"
 
25
        "github.com/juju/govmomi/vim25/methods"
 
26
        "github.com/juju/govmomi/vim25/mo"
 
27
        "github.com/juju/govmomi/vim25/types"
 
28
        "golang.org/x/net/context"
 
29
)
 
30
 
 
31
type HttpNfcLease struct {
 
32
        Common
 
33
}
 
34
 
 
35
func NewHttpNfcLease(c *vim25.Client, ref types.ManagedObjectReference) *HttpNfcLease {
 
36
        return &HttpNfcLease{
 
37
                Common: NewCommon(c, ref),
 
38
        }
 
39
}
 
40
 
 
41
// HttpNfcLeaseAbort wraps methods.HttpNfcLeaseAbort
 
42
func (o HttpNfcLease) HttpNfcLeaseAbort(ctx context.Context, fault *types.LocalizedMethodFault) error {
 
43
        req := types.HttpNfcLeaseAbort{
 
44
                This:  o.Reference(),
 
45
                Fault: fault,
 
46
        }
 
47
 
 
48
        _, err := methods.HttpNfcLeaseAbort(ctx, o.c, &req)
 
49
        if err != nil {
 
50
                return err
 
51
        }
 
52
 
 
53
        return nil
 
54
}
 
55
 
 
56
// HttpNfcLeaseComplete wraps methods.HttpNfcLeaseComplete
 
57
func (o HttpNfcLease) HttpNfcLeaseComplete(ctx context.Context) error {
 
58
        req := types.HttpNfcLeaseComplete{
 
59
                This: o.Reference(),
 
60
        }
 
61
 
 
62
        _, err := methods.HttpNfcLeaseComplete(ctx, o.c, &req)
 
63
        if err != nil {
 
64
                return err
 
65
        }
 
66
 
 
67
        return nil
 
68
}
 
69
 
 
70
// HttpNfcLeaseGetManifest wraps methods.HttpNfcLeaseGetManifest
 
71
func (o HttpNfcLease) HttpNfcLeaseGetManifest(ctx context.Context) error {
 
72
        req := types.HttpNfcLeaseGetManifest{
 
73
                This: o.Reference(),
 
74
        }
 
75
 
 
76
        _, err := methods.HttpNfcLeaseGetManifest(ctx, o.c, &req)
 
77
        if err != nil {
 
78
                return err
 
79
        }
 
80
 
 
81
        return nil
 
82
}
 
83
 
 
84
// HttpNfcLeaseProgress wraps methods.HttpNfcLeaseProgress
 
85
func (o HttpNfcLease) HttpNfcLeaseProgress(ctx context.Context, percent int) error {
 
86
        req := types.HttpNfcLeaseProgress{
 
87
                This:    o.Reference(),
 
88
                Percent: percent,
 
89
        }
 
90
 
 
91
        _, err := methods.HttpNfcLeaseProgress(ctx, o.c, &req)
 
92
        if err != nil {
 
93
                return err
 
94
        }
 
95
 
 
96
        return nil
 
97
}
 
98
 
 
99
func (o HttpNfcLease) Wait(ctx context.Context) (*types.HttpNfcLeaseInfo, error) {
 
100
        var lease mo.HttpNfcLease
 
101
 
 
102
        pc := property.DefaultCollector(o.c)
 
103
        err := property.Wait(ctx, pc, o.Reference(), []string{"state", "info", "error"}, func(pc []types.PropertyChange) bool {
 
104
                done := false
 
105
 
 
106
                for _, c := range pc {
 
107
                        if c.Val == nil {
 
108
                                continue
 
109
                        }
 
110
 
 
111
                        switch c.Name {
 
112
                        case "error":
 
113
                                val := c.Val.(types.LocalizedMethodFault)
 
114
                                lease.Error = &val
 
115
                                done = true
 
116
                        case "info":
 
117
                                val := c.Val.(types.HttpNfcLeaseInfo)
 
118
                                lease.Info = &val
 
119
                        case "state":
 
120
                                lease.State = c.Val.(types.HttpNfcLeaseState)
 
121
                                if lease.State != types.HttpNfcLeaseStateInitializing {
 
122
                                        done = true
 
123
                                }
 
124
                        }
 
125
                }
 
126
 
 
127
                return done
 
128
        })
 
129
 
 
130
        if err != nil {
 
131
                return nil, err
 
132
        }
 
133
 
 
134
        if lease.State == types.HttpNfcLeaseStateReady {
 
135
                return lease.Info, nil
 
136
        }
 
137
 
 
138
        if lease.Error != nil {
 
139
                return nil, errors.New(lease.Error.LocalizedMessage)
 
140
        }
 
141
 
 
142
        return nil, fmt.Errorf("unexpected nfc lease state: %s", lease.State)
 
143
}