func (cmd *vmdk) DetachDisk(vm *object.VirtualMachine) (string, error) { ctx := context.TODO() var mvm mo.VirtualMachine pc := property.DefaultCollector(cmd.Client) err := pc.RetrieveOne(ctx, vm.Reference(), []string{"config.hardware"}, &mvm) if err != nil { return "", err } spec := new(configSpec) dsFile := spec.RemoveDisk(&mvm) task, err := vm.Reconfigure(ctx, spec.ToSpec()) if err != nil { return "", err } err = task.Wait(ctx) if err != nil { return "", err } return dsFile, nil }
func (cmd *ovfx) InjectOvfEnv(vm *object.VirtualMachine) error { if !cmd.Options.InjectOvfEnv { return nil } cmd.Log("Injecting OVF environment...\n") var opts []types.BaseOptionValue a := cmd.Client.ServiceContent.About // build up Environment in order to marshal to xml var props []ovf.EnvProperty for _, p := range cmd.Options.PropertyMapping { props = append(props, ovf.EnvProperty{ Key: p.Key, Value: p.Value, }) } env := ovf.Env{ EsxID: vm.Reference().Value, Platform: &ovf.PlatformSection{ Kind: a.Name, Version: a.Version, Vendor: a.Vendor, Locale: "US", }, Property: &ovf.PropertySection{ Properties: props, }, } opts = append(opts, &types.OptionValue{ Key: "guestinfo.ovfEnv", Value: env.MarshalManual(), }) ctx := context.Background() task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{ ExtraConfig: opts, }) if err != nil { return err } return task.Wait(ctx) }
func (cmd *ovfx) InjectOvfEnv(vm *object.VirtualMachine) error { ctx := context.TODO() if !cmd.Options.PowerOn || !cmd.Options.InjectOvfEnv { return nil } a := cmd.Client.ServiceContent.About if strings.EqualFold(a.ProductLineId, "esx") || strings.EqualFold(a.ProductLineId, "embeddedEsx") || strings.EqualFold(a.ProductLineId, "vpx") { cmd.Log("Injecting OVF environment...\n") // build up Environment in order to marshal to xml var epa []ovf.EnvProperty for _, p := range cmd.Options.PropertyMapping { epa = append(epa, ovf.EnvProperty{ Key: p.Key, Value: p.Value}) } env := ovf.Env{ EsxID: vm.Reference().Value, Platform: &ovf.PlatformSection{ Kind: a.Name, Version: a.Version, Vendor: a.Vendor, Locale: "US", }, Property: &ovf.PropertySection{ Properties: epa}, } xenv := env.MarshalManual() vmConfigSpec := types.VirtualMachineConfigSpec{ ExtraConfig: []types.BaseOptionValue{&types.OptionValue{ Key: "guestinfo.ovfEnv", Value: xenv}}} task, err := vm.Reconfigure(ctx, vmConfigSpec) if err != nil { return err } if err := task.Wait(ctx); err != nil { return err } } return nil }