本文整理汇总了Golang中github.com/vmware/vic/pkg/errors.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: inventoryPathToComputePath
func (v *Validator) inventoryPathToComputePath(path string) string {
defer trace.End(trace.Begin(path))
// sanity check datacenter
if !strings.HasPrefix(path, v.DatacenterPath) {
log.Debugf("Expected path to be within target datacenter %q: %q", v.DatacenterPath, path)
v.NoteIssue(errors.New("inventory path was not in datacenter scope"))
return ""
}
// inventory path is always /dc/host/computeResource/Resources/path/to/pool
// NOTE: all of the indexes are +1 because the leading / means we have an empty string for [0]
pElems := strings.Split(path, "/")
if len(pElems) < 4 {
log.Debugf("Expected path to be fully qualified, e.g. /dcName/host/clusterName/Resources/poolName: %s", path)
v.NoteIssue(errors.New("inventory path format was not recognised"))
return ""
}
if len(pElems) == 4 || len(pElems) == 5 {
// cluster only or cluster/Resources
return pElems[3]
}
// messy but avoid reallocation - overwrite Resources with cluster name
pElems[4] = pElems[3]
// /dc/host/cluster/Resources/path/to/pool
return strings.Join(pElems[4:], "/")
}
示例2: dpgMorefHelper
func (v *Validator) dpgMorefHelper(ctx context.Context, ref string) (string, error) {
defer trace.End(trace.Begin(ref))
moref := new(types.ManagedObjectReference)
ok := moref.FromString(ref)
if !ok {
// TODO: error message about no such match and how to get a network list
return "", errors.New("could not restore serialized managed object reference: " + ref)
}
net, err := v.Session.Finder.ObjectReference(ctx, *moref)
if err != nil {
// TODO: error message about no such match and how to get a network list
return "", errors.New("unable to locate network from moref: " + ref)
}
// ensure that the type of the network is a Distributed Port Group if the target is a vCenter
// if it's not then any network suffices
if v.IsVC() {
_, dpg := net.(*object.DistributedVirtualPortgroup)
if !dpg {
return "", fmt.Errorf("%q is not a Distributed Port Group", ref)
}
}
return ref, nil
}
示例3: CreateVCH
func (d *Dispatcher) CreateVCH(conf *config.VirtualContainerHostConfigSpec, settings *data.InstallerData) error {
defer trace.End(trace.Begin(conf.Name))
var err error
if err = d.checkExistence(conf, settings); err != nil {
return err
}
if d.isVC && !settings.UseRP {
if d.vchVapp, err = d.createVApp(conf, settings); err != nil {
detail := fmt.Sprintf("Creating virtual app failed: %s", err)
if !d.force {
return errors.New(detail)
}
log.Error(detail)
log.Errorf("Deploying vch under parent pool %q, (--force=true)", settings.ResourcePoolPath)
d.vchPool = d.session.Pool
conf.ComputeResources = append(conf.ComputeResources, d.vchPool.Reference())
}
} else {
if d.vchPool, err = d.createResourcePool(conf, settings); err != nil {
detail := fmt.Sprintf("Creating resource pool failed: %s", err)
if !d.force {
return errors.New(detail)
}
log.Error(detail)
log.Errorf("Deploying vch under parent pool %q, (--force=true)", settings.ResourcePoolPath)
d.vchPool = d.session.Pool
conf.ComputeResources = append(conf.ComputeResources, d.vchPool.Reference())
}
}
if err = d.createBridgeNetwork(conf); err != nil {
return err
}
if err = d.createVolumeStores(conf); err != nil {
return errors.Errorf("Exiting because we could not create volume stores due to error: %s", err)
}
if err = d.createAppliance(conf, settings); err != nil {
return errors.Errorf("Creating the appliance failed with %s. Exiting...", err)
}
if err = d.uploadImages(settings.ImageFiles); err != nil {
return errors.Errorf("Uploading images failed with %s. Exiting...", err)
}
if d.session.IsVC() {
if err = d.RegisterExtension(conf, settings.Extension); err != nil {
return errors.Errorf("Error registering VCH vSphere extension: %s", err)
}
}
return d.startAppliance(conf)
}
示例4: Run
func (l *List) Run(cli *cli.Context) (err error) {
if err = l.processParams(); err != nil {
return err
}
if l.Debug.Debug > 0 {
log.SetLevel(log.DebugLevel)
trace.Logger.Level = log.DebugLevel
}
if len(cli.Args()) > 0 {
log.Errorf("Unknown argument: %s", cli.Args()[0])
return errors.New("invalid CLI arguments")
}
log.Infof("### Listing VCHs ####")
ctx, cancel := context.WithTimeout(context.Background(), l.Timeout)
defer cancel()
defer func() {
if ctx.Err() != nil && ctx.Err() == context.DeadlineExceeded {
//context deadline exceeded, replace returned error message
err = errors.Errorf("List timed out: use --timeout to add more time")
}
}()
var validator *validate.Validator
if l.Data.ComputeResourcePath == "" {
validator, err = validate.CreateNoDCCheck(ctx, l.Data)
} else {
validator, err = validate.NewValidator(ctx, l.Data)
}
if err != nil {
log.Errorf("List cannot continue - failed to create validator: %s", err)
return errors.New("list failed")
}
_, err = validator.ValidateTarget(ctx, l.Data)
if err != nil {
log.Errorf("List cannot continue - target validation failed: %s", err)
return err
}
_, err = validator.ValidateCompute(ctx, l.Data)
if err != nil {
log.Errorf("List cannot continue - compute resource validation failed: %s", err)
return err
}
executor := management.NewDispatcher(validator.Context, validator.Session, nil, false)
vchs, err := executor.SearchVCHs(validator.ResourcePoolPath)
if err != nil {
log.Errorf("List cannot continue - failed to search VCHs in %s: %s", validator.ResourcePoolPath, err)
}
l.prettyPrint(cli, ctx, vchs, executor)
return nil
}
示例5: assertTarget
func (v *Validator) assertTarget(conf *config.VirtualContainerHostConfigSpec) {
defer trace.End(trace.Begin(""))
if conf.Target.User != nil {
if _, set := conf.Target.User.Password(); set {
v.NoteIssue(errors.New("Password should not be set in target URL"))
}
}
if !v.IsVC() && conf.UserPassword == "" {
v.NoteIssue(errors.New("ESX credential is not set"))
}
}
示例6: deleteDatastoreFiles
func (d *Dispatcher) deleteDatastoreFiles(ds *object.Datastore, path string, force bool) (bool, error) {
defer trace.End(trace.Begin(fmt.Sprintf("path %q, force %t", path, force)))
// refuse to delete everything on the datstore, ignore force
if path == "" {
dsn, _ := ds.ObjectName(d.ctx)
msg := fmt.Sprintf("refusing to remove datastore files for path \"\" on datastore %q", dsn)
return false, errors.New(msg)
}
var empty bool
dsPath := ds.Path(path)
res, err := d.lsFolder(ds, dsPath)
if err != nil {
if !types.IsFileNotFound(err) {
err = errors.Errorf("Failed to browse folder %q: %s", dsPath, err)
return empty, err
}
log.Debugf("Folder %q is not found", dsPath)
empty = true
return empty, nil
}
if len(res.File) > 0 && !force {
log.Debugf("Folder %q is not empty, leave it there", dsPath)
return empty, nil
}
m := object.NewFileManager(ds.Client())
if err = d.deleteFilesIteratively(m, ds, dsPath); err != nil {
return empty, err
}
return true, nil
}
示例7: checkLicense
func (v *Validator) checkLicense(ctx context.Context) error {
var invalidLic []string
client := v.Session.Client.Client
lm := license.NewManager(client)
licenses, err := lm.List(ctx)
if err != nil {
return err
}
v.checkEvalLicense(licenses)
features := []string{"serialuri"}
for _, feature := range features {
if len(licenses.WithFeature(feature)) == 0 {
msg := fmt.Sprintf("Host license missing feature %q", feature)
invalidLic = append(invalidLic, msg)
}
}
if len(invalidLic) > 0 {
log.Errorf("License check FAILED:")
for _, h := range invalidLic {
log.Errorf(" %q", h)
}
msg := "License does not meet minimum requirements to use VIC"
return errors.New(msg)
}
log.Infof("License check OK")
return nil
}
示例8: findApplianceByID
func (d *Dispatcher) findApplianceByID(conf *metadata.VirtualContainerHostConfigSpec) (*vm.VirtualMachine, error) {
defer trace.End(trace.Begin(""))
var err error
var vmm *vm.VirtualMachine
moref := new(types.ManagedObjectReference)
if ok := moref.FromString(conf.ID); !ok {
message := "Failed to get appliance VM mob reference"
log.Errorf(message)
return nil, errors.New(message)
}
ref, err := d.session.Finder.ObjectReference(d.ctx, *moref)
if err != nil {
if _, ok := err.(*find.NotFoundError); !ok {
err = errors.Errorf("Failed to query appliance (%s): %s", moref, err)
return nil, err
}
log.Debugf("Appliance is not found")
return nil, nil
}
ovm, ok := ref.(*object.VirtualMachine)
if !ok {
log.Errorf("Failed to find VM %s, %s", moref, err)
return nil, err
}
vmm = vm.NewVirtualMachine(d.ctx, d.session, ovm.Reference())
return vmm, nil
}
示例9: DeleteVCHInstances
func (d *Dispatcher) DeleteVCHInstances(vmm *vm.VirtualMachine, conf *config.VirtualContainerHostConfigSpec) error {
defer trace.End(trace.Begin(""))
log.Infof("Removing VMs")
var errs []string
var err error
var children []*vm.VirtualMachine
rpRef := conf.ComputeResources[len(conf.ComputeResources)-1]
ref, err := d.session.Finder.ObjectReference(d.ctx, rpRef)
if err != nil {
err = errors.Errorf("Failed to get VCH resource pool %q: %s", rpRef, err)
return err
}
switch ref.(type) {
case *object.VirtualApp:
case *object.ResourcePool:
// ok
default:
log.Errorf("Failed to find virtual app or resource pool %q: %s", rpRef, err)
return err
}
rp := compute.NewResourcePool(d.ctx, d.session, ref.Reference())
if children, err = rp.GetChildrenVMs(d.ctx, d.session); err != nil {
return err
}
ds, err := d.session.Finder.Datastore(d.ctx, conf.ImageStores[0].Host)
if err != nil {
err = errors.Errorf("Failed to find image datastore %q", conf.ImageStores[0].Host)
return err
}
d.session.Datastore = ds
for _, child := range children {
name, err := child.Name(d.ctx)
if err != nil {
errs = append(errs, err.Error())
continue
}
//Leave VCH appliance there until everything else is removed, cause it has VCH configuration. Then user could retry delete in case of any failure.
if name == conf.Name {
continue
}
if err = d.deleteVM(child, d.force); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
log.Debugf("Error deleting container VMs %s", errs)
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
示例10: createBridgeNetwork
func (d *Dispatcher) createBridgeNetwork(conf *config.VirtualContainerHostConfigSpec) error {
defer trace.End(trace.Begin(""))
// if the bridge network is already extant there's nothing to do
bnet := conf.ExecutorConfig.Networks[conf.BridgeNetwork]
if bnet != nil && bnet.ID != "" {
return nil
}
// network didn't exist during validation given we don't have a moref, so create it
if d.session.Client.IsVC() {
// double check
return errors.New("bridge network must already exist for vCenter environments")
}
// in this case the name to use is held in container network ID
name := bnet.Network.ID
log.Infof("Creating VirtualSwitch")
hostNetSystem, err := d.session.Host.ConfigManager().NetworkSystem(d.ctx)
if err != nil {
err = errors.Errorf("Failed to retrieve host network system: %s", err)
return err
}
if err = hostNetSystem.AddVirtualSwitch(d.ctx, name, &types.HostVirtualSwitchSpec{
NumPorts: 1024,
}); err != nil {
err = errors.Errorf("Failed to add virtual switch (%q): %s", name, err)
return err
}
log.Infof("Creating Portgroup")
if err = hostNetSystem.AddPortGroup(d.ctx, types.HostPortGroupSpec{
Name: name,
VlanId: 1, // TODO: expose this for finer grained grouping within the switch
VswitchName: name,
Policy: types.HostNetworkPolicy{},
}); err != nil {
err = errors.Errorf("Failed to add port group (%q): %s", name, err)
return err
}
net, err := d.session.Finder.Network(d.ctx, name)
if err != nil {
_, ok := err.(*find.NotFoundError)
if !ok {
err = errors.Errorf("Failed to query virtual switch (%q): %s", name, err)
return err
}
}
// assign the moref to the bridge network config on the appliance
bnet.ID = net.Reference().String()
bnet.Network.ID = net.Reference().String()
conf.CreateBridgeNetwork = true
return nil
}
示例11: DeleteVCH
func (d *Dispatcher) DeleteVCH(conf *config.VirtualContainerHostConfigSpec) error {
defer trace.End(trace.Begin(conf.Name))
var errs []string
var err error
var vmm *vm.VirtualMachine
if vmm, err = d.findApplianceByID(conf); err != nil {
return err
}
if vmm == nil {
return nil
}
if err = d.DeleteVCHInstances(vmm, conf); err != nil {
// if container delete failed, do not remove anything else
log.Infof("Specify --force to force delete")
return err
}
if err = d.deleteImages(conf); err != nil {
errs = append(errs, err.Error())
}
d.deleteVolumeStoreIfForced(conf) // logs errors but doesn't ever bail out if it has an issue
if err = d.deleteNetworkDevices(vmm, conf); err != nil {
errs = append(errs, err.Error())
}
if err = d.removeNetwork(conf); err != nil {
errs = append(errs, err.Error())
}
if len(errs) > 0 {
// stop here, leave vch appliance there for next time delete
return errors.New(strings.Join(errs, "\n"))
}
if d.isVC {
log.Infoln("Removing VCH vSphere extension")
if err = d.GenerateExtensionName(conf, vmm); err != nil {
log.Warnf("Failed to get extension name during VCH deletion: %s", err)
}
if err = d.UnregisterExtension(conf.ExtensionName); err != nil {
log.Warnf("Failed to remove extension %q: %s", conf.ExtensionName, err)
}
}
err = d.deleteVM(vmm, true)
if err != nil {
log.Debugf("Error deleting appliance VM %s", err)
return err
}
if err = d.destroyResourcePoolIfEmpty(conf); err != nil {
log.Warnf("VCH resource pool is not removed: %s", err)
}
return nil
}
示例12: checkAssignedLicenses
func (v *Validator) checkAssignedLicenses(ctx context.Context) error {
var hosts []*object.HostSystem
var invalidLic []string
var validLic []string
var err error
client := v.Session.Client.Client
if hosts, err = v.Session.Datastore.AttachedClusterHosts(ctx, v.Session.Cluster); err != nil {
log.Errorf("Unable to get the list of hosts attached to given storage: %s", err)
return err
}
lm := license.NewManager(client)
am, err := lm.AssignmentManager(ctx)
if err != nil {
return err
}
features := []string{"serialuri", "dvs"}
for _, host := range hosts {
valid := true
la, err := am.QueryAssigned(ctx, host.Reference().Value)
if err != nil {
return err
}
for _, feature := range features {
if !v.assignedLicenseHasFeature(la, feature) {
valid = false
msg := fmt.Sprintf("%q - license missing feature %q", host.InventoryPath, feature)
invalidLic = append(invalidLic, msg)
}
}
if valid == true {
validLic = append(validLic, host.InventoryPath)
}
}
if len(validLic) > 0 {
log.Infof("License check OK on hosts:")
for _, h := range validLic {
log.Infof(" %q", h)
}
}
if len(invalidLic) > 0 {
log.Errorf("License check FAILED on hosts:")
for _, h := range invalidLic {
log.Errorf(" %q", h)
}
msg := "License does not meet minimum requirements to use VIC"
return errors.New(msg)
}
return nil
}
示例13: CreateNoDCCheck
func CreateNoDCCheck(ctx context.Context, input *data.Data) (*Validator, error) {
defer trace.End(trace.Begin(""))
var err error
v := &Validator{}
v.Context = ctx
tURL := input.URL
// default to https scheme
if tURL.Scheme == "" {
tURL.Scheme = "https"
}
// if they specified only an IP address the parser for some reason considers that a path
if tURL.Host == "" {
tURL.Host = tURL.Path
tURL.Path = ""
}
sessionconfig := &session.Config{
Insecure: input.Insecure,
}
// if a datacenter was specified, set it
v.DatacenterPath = tURL.Path
if v.DatacenterPath != "" {
sessionconfig.DatacenterPath = v.DatacenterPath
// path needs to be stripped before we can use it as a service url
tURL.Path = ""
}
sessionconfig.Service = tURL.String()
v.Session = session.NewSession(sessionconfig)
v.Session, err = v.Session.Connect(v.Context)
if err != nil {
return nil, err
}
// cached here to allow a modicum of testing while session is still in use.
v.isVC = v.Session.IsVC()
finder := find.NewFinder(v.Session.Client.Client, false)
v.Session.Finder = finder
v.Session.Populate(ctx)
// only allow the datacenter to be specified in the taget url, if any
pElems := strings.Split(v.DatacenterPath, "/")
if len(pElems) > 2 {
detail := "--target should only specify datacenter in the path (e.g. https://addr/datacenter) - specify cluster, resource pool, or folder with --compute-resource"
log.Error(detail)
v.suggestDatacenter()
return nil, errors.New(detail)
}
return v, nil
}
示例14: checkVDSMembership
// checkVDSMembership verifes all hosts in the vCenter are connected to the vDS
func (v *Validator) checkVDSMembership(ctx context.Context, network types.ManagedObjectReference, netName string) error {
defer trace.End(trace.Begin(network.Value))
var dvp mo.DistributedVirtualPortgroup
var nonMembers []string
if !v.IsVC() {
return nil
}
if v.Session.Cluster == nil {
return errors.New("Invalid cluster. Check --compute-resource")
}
clusterHosts, err := v.Session.Cluster.Hosts(ctx)
if err != nil {
return err
}
r := object.NewDistributedVirtualPortgroup(v.Session.Client.Client, network)
if err := r.Properties(ctx, r.Reference(), []string{"name", "host"}, &dvp); err != nil {
return err
}
for _, h := range clusterHosts {
if !v.inDVP(h.Reference(), dvp.Host) {
nonMembers = append(nonMembers, h.InventoryPath)
}
}
if len(nonMembers) > 0 {
log.Errorf("vDS configuration incorrect on %q. All cluster hosts must be in the vDS.", netName)
log.Errorf(" %q is missing hosts:", netName)
for _, hs := range nonMembers {
log.Errorf(" %q", hs)
}
errMsg := fmt.Sprintf("All cluster hosts must be in the vDS. %q is missing hosts: %s", netName, nonMembers)
v.NoteIssue(errors.New(errMsg))
} else {
log.Infof("vDS configuration OK on %q", netName)
}
return nil
}
示例15: datacenter
func (v *Validator) datacenter() error {
if v.Session.Datacenter == nil {
detail := "Datacenter must be specified in --target (e.g. https://addr/datacenter)"
log.Error(detail)
v.suggestDatacenter()
return errors.New(detail)
}
v.DatacenterPath = v.Session.Datacenter.InventoryPath
return nil
}