Skip to content

Commit

Permalink
Merge pull request #379 from dougm/nas
Browse files Browse the repository at this point in the history
Add commands to add and remove datastores
  • Loading branch information
dougm committed Nov 19, 2015
2 parents b1e9438 + 42d899d commit 161f04e
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 0 deletions.
2 changes: 2 additions & 0 deletions govc/CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### (unreleased)

* Add datastore.remove and datastore.nas.create commands

* Add dvs.{create,add} and dvs.portgroup.add commands

* Add host.vnic.{service,info} commands
Expand Down
124 changes: 124 additions & 0 deletions govc/datastore/nas/create.go
@@ -0,0 +1,124 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package nas

import (
"errors"
"flag"
"fmt"
"os"
"strings"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)

type create struct {
*flags.HostSystemFlag

Force bool

types.HostNasVolumeSpec
}

func init() {
cli.Register("datastore.nas.create", &create{})
}

func (cmd *create) Register(f *flag.FlagSet) {
fsTypes := []string{
string(types.HostFileSystemVolumeFileSystemTypeNFS),
string(types.HostFileSystemVolumeFileSystemTypeNFS41),
string(types.HostFileSystemVolumeFileSystemTypeCIFS),
}

modes := []string{
string(types.HostMountModeReadOnly),
string(types.HostMountModeReadWrite),
}

f.BoolVar(&cmd.Force, "force", false, "Ignore DuplicateName error if datastore is already mounted on a host")

f.StringVar(&cmd.LocalPath, "local-path", "", "Name of the NAS datastore")
f.StringVar(&cmd.RemoteHost, "remote-host", "", "Remote hostname of the NAS datastore")
f.StringVar(&cmd.RemotePath, "remote-path", "", "Remote path of the NFS mount point")
f.StringVar(&cmd.AccessMode, "mode", modes[0],
fmt.Sprintf("Access mode for the mount point (%s)", strings.Join(modes, "|")))
f.StringVar(&cmd.Type, "type", fsTypes[0],
fmt.Sprintf("Type of the the NAS volume (%s)", strings.Join(fsTypes, "|")))
f.StringVar(&cmd.UserName, "username", "", "Username to use when connecting (CIFS only)")
f.StringVar(&cmd.Password, "password", "", "Password to use when connecting (CIFS only)")
}

func (cmd *create) Process() error { return nil }

func (cmd *create) Usage() string {
return "HOST..."
}

func (cmd *create) Description() string {
return `Create datastore on HOST.
Example:
govc datastore.nas.create -local-path nfsDatastore -remote-host 10.143.2.232 -remote-path /share cluster1
`
}

func (cmd *create) Run(f *flag.FlagSet) error {
var ctx = context.Background()

hosts, err := cmd.HostSystems(f.Args())
if err != nil {
return err
}

object := types.ManagedObjectReference{
Type: "Datastore",
Value: fmt.Sprintf("%s:%s", cmd.RemoteHost, cmd.RemotePath),
}

for _, host := range hosts {
ds, err := host.ConfigManager().DatastoreSystem(ctx)
if err != nil {
return err
}

_, err = ds.CreateNasDatastore(ctx, cmd.HostNasVolumeSpec)
if err != nil {
if soap.IsSoapFault(err) {
switch fault := soap.ToSoapFault(err).VimFault().(type) {
case types.PlatformConfigFault:
if len(fault.FaultMessage) != 0 {
return errors.New(fault.FaultMessage[0].Message)
}
case types.DuplicateName:
if cmd.Force && fault.Object == object {
fmt.Fprintf(os.Stderr, "%s: '%s' already mounted\n",
host.InventoryPath, cmd.LocalPath)
continue
}
}
}

return fmt.Errorf("%s: %s", host.InventoryPath, err)
}
}

return nil
}
78 changes: 78 additions & 0 deletions govc/datastore/remove.go
@@ -0,0 +1,78 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package datastore

import (
"flag"

"golang.org/x/net/context"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
)

type remove struct {
*flags.HostSystemFlag
*flags.DatastoreFlag
}

func init() {
cli.Register("datastore.remove", &remove{})
}

func (cmd *remove) Register(f *flag.FlagSet) {}

func (cmd *remove) Process() error { return nil }

func (cmd *remove) Usage() string {
return "HOST..."
}

func (cmd *remove) Description() string {
return `Remove datastore from HOST.
Example:
govc datastore.remove -ds nfsDatastore cluster1
`
}

func (cmd *remove) Run(f *flag.FlagSet) error {
ctx := context.TODO()

ds, err := cmd.Datastore()
if err != nil {
return err
}

hosts, err := cmd.HostSystems(f.Args())
if err != nil {
return err
}

for _, host := range hosts {
hds, err := host.ConfigManager().DatastoreSystem(ctx)
if err != nil {
return err
}

err = hds.Remove(ctx, ds)
if err != nil {
return err
}
}

return nil
}
1 change: 1 addition & 0 deletions govc/main.go
Expand Up @@ -25,6 +25,7 @@ import (
_ "github.com/vmware/govmomi/govc/cluster"
_ "github.com/vmware/govmomi/govc/datacenter"
_ "github.com/vmware/govmomi/govc/datastore"
_ "github.com/vmware/govmomi/govc/datastore/nas"
_ "github.com/vmware/govmomi/govc/device"
_ "github.com/vmware/govmomi/govc/device/cdrom"
_ "github.com/vmware/govmomi/govc/device/floppy"
Expand Down
11 changes: 11 additions & 0 deletions object/host_config_manager.go
Expand Up @@ -33,6 +33,17 @@ func NewHostConfigManager(c *vim25.Client, ref types.ManagedObjectReference) *Ho
}
}

func (m HostConfigManager) DatastoreSystem(ctx context.Context) (*HostDatastoreSystem, error) {
var h mo.HostSystem

err := m.Properties(ctx, m.Reference(), []string{"configManager.datastoreSystem"}, &h)
if err != nil {
return nil, err
}

return NewHostDatastoreSystem(m.c, *h.ConfigManager.DatastoreSystem), nil
}

func (m HostConfigManager) NetworkSystem(ctx context.Context) (*HostNetworkSystem, error) {
var h mo.HostSystem

Expand Down
62 changes: 62 additions & 0 deletions object/host_datastore_system.go
@@ -0,0 +1,62 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package object

import (
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)

type HostDatastoreSystem struct {
Common
}

func NewHostDatastoreSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostDatastoreSystem {
return &HostDatastoreSystem{
Common: NewCommon(c, ref),
}
}

func (s HostDatastoreSystem) CreateNasDatastore(ctx context.Context, spec types.HostNasVolumeSpec) (*Datastore, error) {
req := types.CreateNasDatastore{
This: s.Reference(),
Spec: spec,
}

res, err := methods.CreateNasDatastore(ctx, s.Client(), &req)
if err != nil {
return nil, err
}

return NewDatastore(s.Client(), res.Returnval), nil
}

func (s HostDatastoreSystem) Remove(ctx context.Context, ds *Datastore) error {
req := types.RemoveDatastore{
This: s.Reference(),
Datastore: ds.Reference(),
}

_, err := methods.RemoveDatastore(ctx, s.Client(), &req)
if err != nil {
return err
}

return nil
}

0 comments on commit 161f04e

Please sign in to comment.