~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/govc/device/connect.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 device
 
18
 
 
19
import (
 
20
        "flag"
 
21
        "fmt"
 
22
 
 
23
        "github.com/juju/govmomi/govc/cli"
 
24
        "github.com/juju/govmomi/govc/flags"
 
25
        "golang.org/x/net/context"
 
26
)
 
27
 
 
28
type connect struct {
 
29
        *flags.VirtualMachineFlag
 
30
}
 
31
 
 
32
func init() {
 
33
        cli.Register("device.connect", &connect{})
 
34
}
 
35
 
 
36
func (cmd *connect) Register(f *flag.FlagSet) {}
 
37
 
 
38
func (cmd *connect) Process() error { return nil }
 
39
 
 
40
func (cmd *connect) Usage() string {
 
41
        return "DEVICE..."
 
42
}
 
43
 
 
44
func (cmd *connect) Run(f *flag.FlagSet) error {
 
45
        vm, err := cmd.VirtualMachine()
 
46
        if err != nil {
 
47
                return err
 
48
        }
 
49
 
 
50
        if vm == nil {
 
51
                return flag.ErrHelp
 
52
        }
 
53
 
 
54
        devices, err := vm.Device(context.TODO())
 
55
        if err != nil {
 
56
                return err
 
57
        }
 
58
 
 
59
        for _, name := range f.Args() {
 
60
                device := devices.Find(name)
 
61
                if device == nil {
 
62
                        return fmt.Errorf("device '%s' not found", name)
 
63
                }
 
64
 
 
65
                if err = devices.Connect(device); err != nil {
 
66
                        return err
 
67
                }
 
68
 
 
69
                if err = vm.EditDevice(context.TODO(), device); err != nil {
 
70
                        return err
 
71
                }
 
72
        }
 
73
 
 
74
        return nil
 
75
}