Fixes to instance type handling, syntax, ordering

Changes are properly persisted and re-read from the database.
So, a modify will be saved, and after failover/upgrade/restart
be re-read correctly.

Along with this persisting of changes using the UI is fixed.

The name of the operations are now fixed to be InstanceType
rather than VmType.  Corresponding adjust made in euca2ools
and included here are for the console and autoscaling.

Type ordering restriction is relaxed in the following way:
- Instance types are not /required/ to be partial ordered
- Instances types which are /not/ partially ordered are
  treated as a degenerate case.
- The system determines the set of partially ordered types
  and as long as run-instance requests during a duty cycle
  only refer to ordered types the system behaves as it did
  before.
- Should an unordered type be requested, or be inflight when
  any other run-instance request arrives, the request will
  be preceded by and immediately followed by a CC downcall
  to update resource state information.  Note that only the
  preceding resource state update happens synchronously.
- The state updates must happen whenever there is an as-yet-
  unfulfilled request that references an unordered type.
  Otherwise, the ordered type resource accounting is skewed.
- Below is a script for computing the set of conflicting or
  unordered instance types and could serve as the basis for
  future enhancement to tighten up resource accounting and
  perhaps allow removing the synchronous special case update.

This functionality supercedes the previous approach using
'euca-describe-availability-zones verbose' which will be
deprecated in a subsequent release.

The following tickets are addressed
- EUCA-442  instance types can be set out of order
- EUCA-6018 persisting changes to instance types
- EUCA-5915 IAM policy checking
- EUCA-5968 modification of instance types in the admin ui
- EUCA-6041 upgrading of instances types using old values
- EUCA-6042 adjust operation and parameter naming to prefer instance to vm
- EUCA-6095 changes in admin ui are saved properly
- EUCA-4529 api for retrieving instance type information
- EUCA-3635 non-ui method for modifying instance types
- EUCA-3056 instance types can be set out of order

import org.apache.log4j.Logger
import com.eucalyptus.scripting.Groovyness
import com.eucalyptus.vmtypes.VmType
import com.eucalyptus.vmtypes.VmTypes

Logger LOG = Logger.getLogger( "TESTING" );

def conflictVms=[:], res = [:]
def comparator = { compVm, vm ->
  boolean gtcpu = vm.cpu > compVm.cpu, eqcpu = vm.cpu == compVm.cpu, ltcpu = vm.cpu < compVm.cpu;
  boolean gtdisk = vm.disk > compVm.disk, eqdisk = vm.disk == compVm.disk, ltdisk = vm.disk < compVm.disk;
  boolean gtmemory = vm.memory > compVm.memory, eqmem = vm.memory == compVm.memory, ltmem = vm.memory < compVm.memory;

  boolean singleOrder = ( gtcpu && gtdisk && ltmem ) ||
      ( gtcpu && gtmemory && ltdisk ) ||
      ( gtdisk && gtmemory && ltcpu );

  boolean doubleOrder = ( gtmemory && ltcpu && ltdisk ) ||
      ( gtdisk && ltcpu && ltmem ) ||
      ( gtcpu && ltdisk && ltmem );

  !compVm.equals(vm) && ( singleOrder || doubleOrder )
}
vmTypes.each {
  conflictVms[it.name]=[]
}
vmTypes.each{ compVm ->
  def c=compVm.cpu,m=compVm.memory;
  vmTypes.findAll{ comparator(compVm,it) }.each{ vm ->
    c = vm.cpu >= c && vm.memory < m ? vm.cpu : c;
    m = vm.memory >= m && vm.cpu < c ? vm.memory : m;
    conflictVms[compVm.name].add(vm.name)
  }
  res[compVm.name]= VmType.create(compVm.name,c,compVm.disk,m);
}
return (".\n"+
res.collect{ dimension ->
  dimension.collect{ "\n${dimension.key}\t${VmTypes.lookup(dimension.key)}\n${dimension.key}\t${it.value} (${conflictVms[dimension.key].sort()})" }
}
+ "\n."
).toString().replace("[","").replace("]","").replace( ") ,",')')