<commit_before># == Schema Information
#
# Table name: virtual_machine_templates
#
#  id                 :integer          not null, primary key
#  id_at_site         :string(255)      not null
#  name               :string(255)      not null
#  state              :string(255)      not null
#  compute_site_id    :integer          not null
#  virtual_machine_id :integer
#  appliance_type_id  :integer
#  created_at         :datetime
#  updated_at         :datetime
#

class VirtualMachineTemplate < ActiveRecord::Base
  belongs_to :source_vm, class_name: 'VirtualMachine', foreign_key: 'virtual_machine_id'
  has_many :instances, class_name: 'VirtualMachine'
  belongs_to :compute_site
  belongs_to :appliance_type
  validates_presence_of :id_at_site, :name, :state, :compute_site_id
  validates_uniqueness_of :id_at_site, :scope => :compute_site_id
  before_update :release_source_vm, if: :state_changed? 
  after_update :destroy_source_vm, if: :state_changed?

  def uuid
    "#{compute_site.site_id}-tmpl-#{id_at_site}"
  end

  def VirtualMachineTemplate.create_from_vm(virtual_machine, name=nil)
    vm_template = VirtualMachineTemplate.new(source_vm: virtual_machine, name: name|| virtual_machine.name)
    logger.info "Saving template #{vm_template}"
    cs = vm_template.source_vm.compute_site
    cloud_client = cs.cloud_client
    id_at_site = cloud_client.save_template(vm_template.source_vm.id_at_site, vm_template.name)
    logger.info "Created template #{id_at_site}"
    vm_template.id_at_site = id_at_site
    vm_template.compute_site = cs
    vm_template.state = :saving
    vm_template.save
    vm_template
  end

  def destroy(delete_in_cloud = true)
    delete_in_cloud if delete_in_cloud
    super()
  end

  private

  def release_source_vm
    if state != 'saving' and state != :saving
      self.source_vm = nil<commit_msg>added states validations for template; valid states are based on OpenStack states<commit_after># == Schema Information
#
# Table name: virtual_machine_templates
#
#  id                 :integer          not null, primary key
#  id_at_site         :string(255)      not null
#  name               :string(255)      not null
#  state              :string(255)      not null
#  compute_site_id    :integer          not null
#  virtual_machine_id :integer
#  appliance_type_id  :integer
#  created_at         :datetime
#  updated_at         :datetime
#

class VirtualMachineTemplate < ActiveRecord::Base
  extend Enumerize

  belongs_to :source_vm, class_name: 'VirtualMachine', foreign_key: 'virtual_machine_id'
  has_many :instances, class_name: 'VirtualMachine'
  belongs_to :compute_site
  belongs_to :appliance_type
  validates_presence_of :id_at_site, :name, :state, :compute_site_id
  validates_uniqueness_of :id_at_site, :scope => :compute_site_id
  enumerize :state, in: ['active', 'deleted', 'error', 'saving', 'queued', 'killed', 'pending_delete']
  validates :state, inclusion: %w(active deleted error saving queued killed pending_delete)
  before_update :release_source_vm, if: :state_changed? 
  after_update :destroy_source_vm, if: :state_changed?

  def uuid
    "#{compute_site.site_id}-tmpl-#{id_at_site}"
  end

  def VirtualMachineTemplate.create_from_vm(virtual_machine, name=nil)
    vm_template = VirtualMachineTemplate.new(source_vm: virtual_machine, name: name|| virtual_machine.name)
    logger.info "Saving template #{vm_template}"
    cs = vm_template.source_vm.compute_site
    cloud_client = cs.cloud_client
    id_at_site = cloud_client.save_template(vm_template.source_vm.id_at_site, vm_template.name)
    logger.info "Created template #{id_at_site}"
    vm_template.id_at_site = id_at_site
    vm_template.compute_site = cs
    vm_template.state = :saving
    vm_template.save
    vm_template
  end

  def destroy(delete_in_cloud = true)
    delete_in_cloud if delete_in_cloud
    super()
  end

  private

  def release_source_vm
    if state != 'saving' and state != :saving
      self.source_vm = nil
{'fraction_non_alphanumeric': 0.08389851796031148, 'fraction_numerical': 0.00452147701582517, 'mean_word_length': 3.8501827040194883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5921608', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1283, 'n_words': 284}