Skip to content

Commit

Permalink
removing zone/projectid reqs for vsphere; adding in delete {instance,…
Browse files Browse the repository at this point in the history
…vm} ops; (#554)

Co-authored-by: Ian Eyberg <ian@deferpanic.com>
  • Loading branch information
eyberg and Ian Eyberg authored Jul 11, 2020
1 parent 707e9c5 commit 7cf5d1d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
11 changes: 6 additions & 5 deletions cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func instanceCreateCommandHandler(cmd *cobra.Command, args []string) {
}

projectID, _ := cmd.Flags().GetString("projectid")
if projectID != "" {
if projectID == "" && provider == "gcp" {
c.CloudConfig.ProjectID = projectID
}

zone, _ := cmd.Flags().GetString("zone")
if zone != "" {
if zone == "" && (provider == "gcp" || provider == "aws") {
c.CloudConfig.Zone = zone
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func instanceListCommandHandler(cmd *cobra.Command, args []string) {
}

zone, _ := cmd.Flags().GetString("zone")
if zone == "" {
if zone == "" && (provider == "gcp" || provider == "aws") {
exitForCmd(cmd, "zone argument missing")
}

Expand Down Expand Up @@ -139,9 +139,10 @@ func instanceDeleteCommandHandler(cmd *cobra.Command, args []string) {
}

zone, _ := cmd.Flags().GetString("zone")
if zone == "" {
if zone == "" && (provider == "gcp" || provider == "aws") {
exitForCmd(cmd, "zone argument missing")
}

c.CloudConfig.ProjectID = projectID
c.CloudConfig.Zone = zone
ctx := api.NewContext(&c, &p)
Expand All @@ -163,7 +164,7 @@ func instanceStartCommandHandler(cmd *cobra.Command, args []string) {
}

zone, _ := cmd.Flags().GetString("zone")
if zone == "" {
if zone == "" && (provider == "gcp" || provider == "aws") {
exitForCmd(cmd, "zone argument missing")
}

Expand Down
41 changes: 40 additions & 1 deletion lepton/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,46 @@ func (v *Vsphere) ListInstances(ctx *Context) error {

// DeleteInstance deletes instance from VSphere
func (v *Vsphere) DeleteInstance(ctx *Context, instancename string) error {
fmt.Println("un-implemented")
f := find.NewFinder(v.client, true)

dc, err := f.DatacenterOrDefault(context.TODO(), v.datacenter)
if err != nil {
fmt.Println(err)
return err
}

f.SetDatacenter(dc)

vms, err := f.VirtualMachineList(context.TODO(), instancename)
if err != nil {
if _, ok := err.(*find.NotFoundError); ok {
fmt.Println("can't find vm " + instancename)
}
fmt.Println(err)
}

vm := vms[0]

task, err := vm.PowerOff(context.TODO())
if err != nil {
fmt.Println(err)
}

// Ignore error since the VM may already been in powered off
// state.
// vm.Destroy will fail if the VM is still powered on.
_ = task.Wait(context.TODO())

task, err = vm.Destroy(context.TODO())
if err != nil {
return err
}

err = task.Wait(context.TODO())
if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 7cf5d1d

Please sign in to comment.