<commit_before>require_dependency "block_storage/application_controller"

module BlockStorage
  class VolumesController < ApplicationController
    before_action :set_volume, only: [:show, :edit, :update, :destroy, :new_snapshot, :attach, :edit_attach, :detach, :edit_detach]
    protect_from_forgery except: [:attach, :detach]

    SERVER_STATES_NEEDED_FOR_ATTACH = ['ACTIVE', 'PAUSED', 'SHUTOFF', 'VERIFY_RESIZE', 'SOFT_DELETED']
    SLEEP = 1

    # GET /volumes
    def index
      @servers = services.compute.servers()
      @action_id = params[:id]
      if @scoped_project_id
        @volumes = services.block_storage.volumes

        # # quota for block_storage is not implemented yet        
        # @quota_data = services.resource_management.quota_data([
        #   {service_name: :block_storage, resource_name: :volumes, usage: @volumes.length},
        #   {service_name: :block_storage, resource_name: :capacity}
        # ])
        
      end
    end

    # GET /volumes/1
    def show
      @servers = services.compute.servers() if @volume.status == "in-use"
    end

    # GET /volumes/new
    def new
      @volume = services.block_storage_.new_volume
      @availability_zones = services.compute.availability_zones

    end

    # POST /volumes
    def create
      @volume = services.block_storage.new_volume
      @volume.attributes = params[@volume.model_name.param_key].delete_if{ |key,value| value.blank?} # delete blank attributes from hash. If they are passed as empty strings the API doesn't recognize them as blank and throws a fit ...

      if @volume.save
        if @volume.snapshot_id.blank?
          @volume = services.block_storage.get_volume(@volume.id)
          @volume.status = 'creating'
          @target_state = target_state_for_action 'create'
          sleep(SLEEP)
          audit_logger.info(current_user, "has created", @volume)
          render template: 'block_storage/volumes/create.js'
        else
          redirect_to volumes_path
        end
      else
        @availability_zones = services.compute.availability_zones
        @volume.errors[:base] << "Volume creation failed!"
        render :new
      end
    end

    # GET /volumes/1/edit
    def edit
    end

    # PATCH/PUT /volumes/1
    def update
      if @volume.update(volume_params)
        audit_logger.info(current_user, "has updated", @volume)
        redirect_to @volume, notice: 'Volume was successfully updated.'
      else
        render :edit
      end
    end

    # GET /volumes/1/snapshot
    def new_snapshot
      @snapshot = services.block_storage.new_snapshot
      @snapshot.name = "snap-#{@volume.name}"
      @snapshot.description = "snap-#{@volume.description}"
    end

    # POST /volumes/1/snapshot
    def snapshot
      @snapshot = services.block_storage.new_snapshot
      @snapshot.attributes=params[@snapshot.model_name.param_key]
      @snapshot.attributes['volume_id'] = params[:id]

      if @snapshot.save
        flash[:notice] = "Snapshot successfully created."
        audit_logger.info(current_user, "has created", @snapshot)<commit_msg>assign attributes via methods<commit_after>require_dependency "block_storage/application_controller"

module BlockStorage
  class VolumesController < ApplicationController
    before_action :set_volume, only: [:show, :edit, :update, :destroy, :new_snapshot, :attach, :edit_attach, :detach, :edit_detach]
    protect_from_forgery except: [:attach, :detach]

    SERVER_STATES_NEEDED_FOR_ATTACH = ['ACTIVE', 'PAUSED', 'SHUTOFF', 'VERIFY_RESIZE', 'SOFT_DELETED']
    SLEEP = 1

    # GET /volumes
    def index
      @servers = services.compute.servers()
      @action_id = params[:id]
      if @scoped_project_id
        @volumes = services.block_storage.volumes

        # # quota for block_storage is not implemented yet        
        # @quota_data = services.resource_management.quota_data([
        #   {service_name: :block_storage, resource_name: :volumes, usage: @volumes.length},
        #   {service_name: :block_storage, resource_name: :capacity}
        # ])
        
      end
    end

    # GET /volumes/1
    def show
      @servers = services.compute.servers() if @volume.status == "in-use"
    end

    # GET /volumes/new
    def new
      @volume = services.block_storage_.new_volume
      @availability_zones = services.compute.availability_zones

    end

    # POST /volumes
    def create
      @volume = services.block_storage.new_volume
      @volume.attributes = params[@volume.model_name.param_key].delete_if{ |key,value| value.blank?} # delete blank attributes from hash. If they are passed as empty strings the API doesn't recognize them as blank and throws a fit ...

      if @volume.save
        if @volume.snapshot_id.blank?
          @volume = services.block_storage.get_volume(@volume.id)
          @volume.status = 'creating'
          @target_state = target_state_for_action 'create'
          sleep(SLEEP)
          audit_logger.info(current_user, "has created", @volume)
          render template: 'block_storage/volumes/create.js'
        else
          redirect_to volumes_path
        end
      else
        @availability_zones = services.compute.availability_zones
        @volume.errors[:base] << "Volume creation failed!"
        render :new
      end
    end

    # GET /volumes/1/edit
    def edit
    end

    # PATCH/PUT /volumes/1
    def update
      if @volume.update(volume_params)
        audit_logger.info(current_user, "has updated", @volume)
        redirect_to @volume, notice: 'Volume was successfully updated.'
      else
        render :edit
      end
    end

    # GET /volumes/1/snapshot
    def new_snapshot
      @snapshot = services.block_storage.new_snapshot
      @snapshot.name = "snap-#{@volume.name}"
      @snapshot.description = "snap-#{@volume.description}"
    end

    # POST /volumes/1/snapshot
    def snapshot
      @snapshot = services.block_storage.new_snapshot
      @snapshot.attributes=params[@snapshot.model_name.param_key]
      @snapshot.volume_id = params[:id]

      if @snapshot.save
        flash[:notice] = "Snapshot successfully created."
        audit_logger.info(current_user, "has created", @snapshot)
{'fraction_non_alphanumeric': 0.10048465266558966, 'fraction_numerical': 0.0019386106623586429, 'mean_word_length': 2.816892725030826, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2017333', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1863, 'n_words': 416}