本文整理汇总了Golang中github.com/vmware/govmomi/vim25/types.ManagedObjectReference类的典型用法代码示例。如果您正苦于以下问题:Golang ManagedObjectReference类的具体用法?Golang ManagedObjectReference怎么用?Golang ManagedObjectReference使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ManagedObjectReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: blacklisted
func (mgr *vSphereManager) blacklisted(ref types.ManagedObjectReference) bool {
if _, ok := mgr.blacklist[ref]; ok {
log.Debugf("EventManager skipping blacklisted object (%s)", ref.String())
return true
}
return false
}
示例3: registeredVMCallback
// registeredVMCallback will process registeredVMEvent
func registeredVMCallback(sess *session.Session, ie events.Event) {
// check container registered event if this container is not found in container cache
// grab the container from the cache
container := Containers.Container(ie.Reference())
if container != nil {
// if container exists, ingore it
return
}
switch ie.String() {
case events.ContainerRegistered:
moref := new(types.ManagedObjectReference)
if ok := moref.FromString(ie.Reference()); !ok {
log.Errorf("Failed to get event VM mobref: %s", ie.Reference())
return
}
if !isManagedbyVCH(sess, *moref) {
return
}
log.Debugf("Register container VM %s", moref)
ctx := context.Background()
vms, err := populateVMAttributes(ctx, sess, []types.ManagedObjectReference{*moref})
if err != nil {
log.Error(err)
return
}
registeredContainers := convertInfraContainers(ctx, sess, vms)
for i := range registeredContainers {
Containers.put(registeredContainers[i])
log.Debugf("Registered container %q", registeredContainers[i].Config.Name)
}
}
return
}
示例4: registerVM
func (vm *VirtualMachine) registerVM(ctx context.Context, path, name string,
vapp, pool, host *types.ManagedObjectReference, vmfolder *object.Folder) (*object.Task, error) {
log.Debugf("Register VM %s", name)
if vapp == nil {
var hostObject *object.HostSystem
if host != nil {
hostObject = object.NewHostSystem(vm.Vim25(), *host)
}
poolObject := object.NewResourcePool(vm.Vim25(), *pool)
return vmfolder.RegisterVM(ctx, path, name, false, poolObject, hostObject)
}
req := types.RegisterChildVM_Task{
This: vapp.Reference(),
Path: path,
Host: host,
}
if name != "" {
req.Name = name
}
res, err := methods.RegisterChildVM_Task(ctx, vm.Vim25(), &req)
if err != nil {
return nil, err
}
return object.NewTask(vm.Vim25(), res.Returnval), nil
}
示例5: printEvents
func (cmd *events) printEvents(ctx context.Context, obj *types.ManagedObjectReference, page []types.BaseEvent, m *event.Manager) error {
event.Sort(page)
if obj != nil {
// print the object reference
fmt.Fprintf(os.Stdout, "\n==> %s <==\n", obj.String())
}
for _, e := range page {
cat, err := m.EventCategory(ctx, e)
if err != nil {
return err
}
event := e.GetEvent()
msg := strings.TrimSpace(event.FullFormattedMessage)
// if this is a TaskEvent gather a little more information
if t, ok := e.(*types.TaskEvent); ok {
// some tasks won't have this information, so just use the event message
if t.Info.Entity != nil {
msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName)
}
}
fmt.Fprintf(os.Stdout, "[%s] [%s] %s\n",
event.CreatedTime.Local().Format(time.ANSIC),
cat,
msg)
}
return nil
}
示例6: 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
}
示例7: Init
func Init(ctx context.Context, sess *session.Session, source extraconfig.DataSource, sink extraconfig.DataSink) error {
trace.End(trace.Begin(""))
initializer.once.Do(func() {
var err error
defer func() {
initializer.err = err
}()
f := find.NewFinder(sess.Vim25(), false)
var config Configuration
config.sink = sink
config.source = source
config.Decode()
config.PortGroups = make(map[string]object.NetworkReference)
log.Debugf("Decoded VCH config for network: %#v", config)
for nn, n := range config.ContainerNetworks {
pgref := new(types.ManagedObjectReference)
if !pgref.FromString(n.ID) {
log.Warnf("Could not reacquire object reference from id for network %s: %s", nn, n.ID)
}
var r object.Reference
if r, err = f.ObjectReference(ctx, *pgref); err != nil {
log.Warnf("could not get network reference for %s network: %s", nn, err)
err = nil
continue
}
config.PortGroups[nn] = r.(object.NetworkReference)
}
// make sure a NIC attached to the bridge network exists
config.BridgeLink, err = getBridgeLink(&config)
if err != nil {
return
}
var kv kvstore.KeyValueStore
kv, err = store.NewDatastoreKeyValue(ctx, sess, "network.contexts.default")
if err != nil {
return
}
var netctx *Context
if netctx, err = NewContext(&config, kv); err != nil {
return
}
if err = engageContext(ctx, netctx, exec.Config.EventManager); err == nil {
DefaultContext = netctx
log.Infof("Default network context allocated")
}
})
return initializer.err
}
示例8: Blacklist
// Blacklist object from callbacks
func (mgr *vSphereManager) Blacklist(ref string) error {
moRef := types.ManagedObjectReference{}
if !moRef.FromString(ref) {
return fmt.Errorf("Invalid Managed Object provided to Blacklist(%s)", ref)
}
mgr.blacklist[moRef] = ref
return nil
}
示例9: AddMonitoredObject
func (mgr *vSphereManager) AddMonitoredObject(ref string) error {
log.Debugf("EventManager added (%s) to monitor", ref)
moRef := types.ManagedObjectReference{}
if !moRef.FromString(ref) {
return fmt.Errorf("Invalid Managed Object provided for Montioring(%s)", ref)
}
mgr.objects[ref] = moRef
return nil
}
示例10: addNetworkDevices
func (d *Dispatcher) addNetworkDevices(conf *metadata.VirtualContainerHostConfigSpec, cspec *spec.VirtualMachineConfigSpec, devices object.VirtualDeviceList) (object.VirtualDeviceList, error) {
defer trace.End(trace.Begin(""))
// network name:alias, to avoid create multiple devices for same network
slots := make(map[int32]bool)
nets := make(map[string]*metadata.NetworkEndpoint)
for name, endpoint := range conf.ExecutorConfig.Networks {
if pnic, ok := nets[endpoint.Network.Common.ID]; ok {
// there's already a NIC on this network
endpoint.Common.ID = pnic.Common.ID
log.Infof("Network role %s is sharing NIC with %s", name, pnic.Network.Common.Name)
continue
}
moref := new(types.ManagedObjectReference)
if ok := moref.FromString(endpoint.Network.ID); !ok {
return nil, fmt.Errorf("serialized managed object reference in unexpected format: %s", endpoint.Network.ID)
}
obj, err := d.session.Finder.ObjectReference(d.ctx, *moref)
if err != nil {
return nil, fmt.Errorf("unable to reacquire reference for network %s from serialized form: %s", endpoint.Network.Name, endpoint.Network.ID)
}
network, ok := obj.(object.NetworkReference)
if !ok {
return nil, fmt.Errorf("reacquired reference for network %s, from serialized form %s, was not a network: %T", endpoint.Network.Name, endpoint.Network.ID, obj)
}
backing, err := network.EthernetCardBackingInfo(d.ctx)
if err != nil {
err = errors.Errorf("Failed to get network backing info for %s: %s", network, err)
return nil, err
}
nic, err := devices.CreateEthernetCard("vmxnet3", backing)
if err != nil {
err = errors.Errorf("Failed to create Ethernet Card spec for %s", err)
return nil, err
}
slot := cspec.AssignSlotNumber(nic, slots)
if slot == spec.NilSlot {
err = errors.Errorf("Failed to assign stable PCI slot for %s network card", name)
}
endpoint.Common.ID = strconv.Itoa(int(slot))
slots[slot] = true
log.Debugf("Setting %s to slot %d", name, slot)
devices = append(devices, nic)
nets[endpoint.Network.Common.ID] = endpoint
}
return devices, nil
}
示例11: AddMonitoredObject
func (ec *EventCollector) AddMonitoredObject(ref string) error {
ec.mos.mu.Lock()
defer ec.mos.mu.Unlock()
moRef := types.ManagedObjectReference{}
if !moRef.FromString(ref) {
return fmt.Errorf("%s received an invalid Object to monitor(%s)", name, ref)
}
ec.mos.mos[ref] = moRef
return nil
}
示例12: ManagedObjects
func (f *PermissionFlag) ManagedObjects(ctx context.Context, args []string) ([]types.ManagedObjectReference, error) {
if !f.asRef {
return f.DatacenterFlag.ManagedObjects(ctx, args)
}
var refs []types.ManagedObjectReference
for _, arg := range args {
var ref types.ManagedObjectReference
if ref.FromString(arg) {
refs = append(refs, ref)
} else {
return nil, fmt.Errorf("invalid moref: %s", arg)
}
}
return refs, nil
}
示例13: isManagedbyVCH
func isManagedbyVCH(sess *session.Session, moref types.ManagedObjectReference) bool {
var vm mo.VirtualMachine
// current attributes we care about
attrib := []string{"resourcePool"}
// populate the vm properties
ctx := context.Background()
if err := sess.RetrieveOne(ctx, moref, attrib, &vm); err != nil {
log.Errorf("Failed to query registered vm object %s: %s", moref.String(), err)
return false
}
if *vm.ResourcePool != Config.ResourcePool.Reference() {
log.Debugf("container vm %q does not belong to this VCH, ignoring", vm.Config.Name)
return false
}
return true
}
示例14: Run
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
finder, err := cmd.Finder()
if err != nil {
return err
}
lr := listResult{
ls: cmd,
Elements: nil,
}
args := f.Args()
if len(args) == 0 {
args = []string{"."}
}
var ref = new(types.ManagedObjectReference)
for _, arg := range args {
if cmd.DeRef && ref.FromString(arg) {
e, err := finder.Element(ctx, *ref)
if err == nil {
if cmd.typeMatch(*ref) {
lr.Elements = append(lr.Elements, *e)
}
continue
}
}
es, err := finder.ManagedObjectListChildren(ctx, arg)
if err != nil {
return err
}
for _, e := range es {
if cmd.typeMatch(e.Object.Reference()) {
lr.Elements = append(lr.Elements, e)
}
}
}
return cmd.WriteResult(lr)
}
示例15: NewVCHFromID
func (d *Dispatcher) NewVCHFromID(id string) (*vm.VirtualMachine, error) {
defer trace.End(trace.Begin(id))
var err error
var vmm *vm.VirtualMachine
moref := new(types.ManagedObjectReference)
if ok := moref.FromString(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())
// check if it's VCH
if ok, err = d.isVCH(vmm); err != nil {
log.Errorf("%s", err)
return nil, err
}
if !ok {
err = errors.Errorf("Not a VCH")
log.Errorf("%s", err)
return nil, err
}
return vmm, nil
}