<commit_before># Copyright 2012-2013 Red Hat, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

module Providers
  module Ovirt
    class OvirtProviderApplication < ProviderApplication
      attr_accessor :ips, :creation_time, :cores, :memory

      def attributes
        super.merge({ 'ips' => ips,
                      'creation_time' => creation_time,
                      'memory' => memory,
                      'cores' => cores })
      end

      validates_numericality_of :cores

      def launch
        self.class.connect! do |client|
          vm = client.create_vm(:name => @name,
                                :template => @launchable.id,
                                :cores => @cores
                                )
          @id = vm.id
        end
      end

      def destroy
        self.class.connect! do |client|
          client.destroy_vm id
        end
      end

      def self.all filter=nil
        templates = Launchable.all
        connect! do |client|
          client.vms.map do |vm|
            self.map_vm_to_application(vm, templates)
          end
        end
      end

      def self.find id
        templates = Launchable.all
        connect! do |client|
          vm = client.vm(id)
          self.map_vm_to_application(vm, templates)
        end
      end

      def as_json(options={})
        super(:root => false)
      end

      private

      def self.map_vm_to_application(vm, templates)
        template_id = vm.template.id
        state = vm.status.strip
        wm_state = case state
                   when "image_locked", "powering_up" then ProviderApplication::WM_STATE_PENDING
                   when "up" then ProviderApplication::WM_STATE_RUNNING
                   when "down" then ProviderApplication::WM_STATE_STOPPED
                   when "powering_down" then ProviderApplication::WM_STATE_STOPPING
                   else ProviderApplication::WM_STATE_FAILED
                   end

        ProviderApplication.create({
                                     :id => vm.id,
                                     :launchable => templates.find{|t| t.id.to_s == template_id},
                                     :name => vm.name,
                                     :state => state,
                                     :wm_state => wm_state,
                                     :ips => vm.ips,
                                     :creation_time => vm.creation_time,
                                     :memory => vm.memory.strip,
                                     :cores => vm.cores
                                   })
      end
    end
  end
end<commit_msg>added available_actions and start/stop/pause<commit_after># Copyright 2012-2013 Red Hat, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

module Providers
  module Ovirt
    class OvirtProviderApplication < ProviderApplication
      attr_accessor :ips, :creation_time, :cores, :memory
      validates_numericality_of :cores

      def attributes
        super.merge({ 'ips' => ips,
                      'creation_time' => creation_time,
                      'memory' => memory,
                      'cores' => cores })
      end

      def available_actions
        case wm_state
        when ProviderApplication::WM_STATE_RUNNING then
          [ ProviderApplication::WM_ACTION_PAUSE, ProviderApplication::WM_ACTION_STOP ]
        when ProviderApplication::WM_STATE_STOPPED then
          [ ProviderApplication::WM_ACTION_TERMINATE, ProviderApplication::WM_ACTION_START ]
        when ProviderApplication::WM_STATE_PAUSED then
          [ ProviderApplication::WM_ACTION_START, ProviderApplication::WM_ACTION_STOP ]
        else [ WM_ACTION_TERMINATE ]
        end
      end

      def launch
        self.class.connect! do |client|
          vm = client.create_vm(:name => @name,
                                :template => @launchable.id,
                                :cores => @cores
                                )
          @id = vm.id
        end
      end

      def destroy
        self.class.connect! do |client|
          client.destroy_vm id
        end
      end

      def start
        self.class.connect! do |client|
          client.vm_action(id, :start)
        end
      end

      def pause
        self.class.connect! do |client|
          client.vm_action(id, :suspend)
        end
      end

      def stop
        self.class.connect! do |client|
          client.vm_action(id, :shutdown)
        end
      end

      def self.all filter=nil
        templates = Launchable.all
        connect! do |client|
          client.vms.map do |vm|
            self.map_vm_to_application(vm, templates)
          end
        end
      end

      def self.find id
        templates = Launchable.all
        connect! do |client|
          vm = client.vm(id)
          self.map_vm_to_application(vm, templates)
        end
      end

      def as_json(options={})
        super(:root => false)
      end

      private

      def self.map_vm_to_application(vm, templates)
        template_id = vm.template.id
        state = vm.status.strip
        wm_state = case state
                   when "image_locked", "powering_up", "saving_state" then ProviderApplication::WM_STATE_PENDING
                   when "up" then ProviderApplication::WM_STATE_RUNNING
                   when "down" then ProviderApplication::WM_STATE_STOPPED
                   when "suspended" then ProviderApplication::WM_STATE_PAUSED
                   when "powering_down" then ProviderApplication::WM_STATE_STOPPING
                   else ProviderApplication::WM_STATE_FAILED
                   end

        ProviderApplication.create({
                                     :id => vm.id,
                                     :launchable => templates.find{|t| t.id.to_s == template_id},
                                     :name => vm.name,
                                     :state => state,
                                     :wm_state => wm_state,
                                     :ips => vm.ips,
                                     :creation_time => vm.creation_time,
                                     :memory => vm.memory.strip,
                                     :cores => vm.cores
                                   })
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07363810566657542, 'fraction_numerical': 0.0032849712565015056, 'mean_word_length': 1.5144528561596697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5392486', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1791, 'n_words': 509}