Delete VM from api


#1

Hello,

how delete VM from disk with API?
I use delete action,but the vm delete only VMDB.

thank you so much


#2

@json I have not looked at the API lately, but in our implementation we would likely invoke a retirement on the machine and that state machine could be configured to do the deletion.


#3

Thank you for reply…

In the old API version,action “retire” was present for the “/api/vms/”.
How can i retire VM form new version of API 2.0.0?

Thanks…


#4

@abellotti could you reply to this?


#5

Currently, only delete VMs from Vmdb is available (v2.0.0 of API). I will create a Trello card for adding support for the vm retire action.


#6

Thank you from Italy


#7

We have built our retirement system to extend the default MIQ behavior. All of our VMs have the Lifecycle tag set to “Retire Full”. During a retirement request, we check for our Soft Retirement boolean flag. If present and true, we implement a soft-retirement period that just powers down the VM for X number of days before deleting completely (release IPs, delete from vCenter, remove from VMDB). As part of that effort, we created a button to override the soft-retirement and to delete immediately. We are able to call the API and execute the retire immediately button which does perform a complete retirement. Until the feature is available in v2, you could use the follow code snippets create your own button.

Our Retire Immediatley button code is similar to the following:

  begin
  @method = 'cai_retire_immediately'
  $evm.log("info", "#{@method} - EVM Automate Method Started")
  # Turn off verbose logging
  @debug = true

  obj = $evm.object("process")
  if obj.nil?
    vm_name = $evm.root["vm_name"]
    vm = $evm.vmdb('vm').find_by_name(vm_name)
  else
    vm = obj["vm"] || $evm.root["vm"]
  end
  #$evm.log("info","#{@method} --CAI-- Inspecting object: vm = #{vm}")

## Unretire and do not set a retirement date or warning
  vm.retires_on = nil
  vm.retirement_warn = nil

##  Remove the soft-retire flag so it will immediately retire
  tag_category = "cai_soft_retire_flag"
  vm.tags(tag_category).each do |tag_name|
    vm.tag_unassign("#{tag_category}/#{tag_name}")
  end
  $evm.log("info","#{@method} --CAI-- Inspecting object: vm.tags = #{vm.tags.inspect}")

## The soft retire flag should have been removed.  Now we retire/delete the VM from vCenter and CF
  vm.retire_now


#
# Exit method
#
  $evm.log("info", "#{@method} - EVM Automate Method Ended")
  exit MIQ_OK

    #
    # Set Ruby rescue behavior
    #
rescue => err
  $evm.log("error", "#{@method} - [#{err}]\n#{err.backtrace.join("\n")}")
  exit MIQ_ABORT
end

Ruby Script to make the API call:

require 'rest-client'
require 'json'

@user_id = "admin"
@password = "THIS_NEEDS_TO_BE_UPDATED_WITH_PASSWORD"
server_url = "miq.work.com"
server_name = "myserver0123"

retire_url = "https://#{@user_id}:#{@password}@#{server_url}/api/automation_requests"
puts " * retire_url = #{retire_url}"

result=RestClient.post "#{retire_url}",
                        {
                            "action" => "create",
                            "resource" => {
                                "version" => "1.1",
                                "uri_parts" => {
                                    "namespace" => "System",
                                    "class" => "Request",
                                    "instance" => "cai_retire_immediately",
                                    "message" => "create"
                                },
                                "parameters" => {
                                    :vm_name => "#{server_name}"
                                },
                                "requester" => {
                                    "user_name" => "admin",
                                    "auto_approve" => true
                                }
                            }
                        }.to_json
items=JSON.parse(result)
puts "Response Object: #{items.inspect}"

#8

Has there been any movement on this since 2015? I’d really like a delete from disk REST API call that doesn’t involve changing or tweaking our currently retirement process.

Thanks,