<commit_before>module API
  module V1
    class EnvironmentAPI < API::V1::Base
      resource :environments do
        desc 'List environments'
        get '/' do
          authorize!(:read, ::Environment)
          ::Environment.all.select do |environment|
            can?(:read, environment)
          end
        end

        desc 'Show environment'
        params do
          requires :id, type: Integer, desc: 'Environment id'
        end
        get '/:id' do
          environment = ::Environment.find(params[:id])
          authorize!(:read, environment)
          environment
        end

        desc 'Create environment'
        params do
          requires :system_id, type: Integer, desc: 'System id'
          requires :blueprint_id, type: Integer, desc: 'Blueprint id'
          requires :name, type: String, desc: 'Environment name'
          optional :description, type: String, desc: 'Environment description'
          requires :candidates_attributes, type: Array, desc: 'Cloud ids to build environment. First cloud is primary.' do
            requires :cloud_id, type: String, desc: 'Cloud id'
            optional :priority, type: Integer, desc: 'Cloud priority(prefer cloud that has higher value)'
          end
          optional :stacks_attributes, type: Array, desc: 'Parameters for individual stack' do
            requires :name, type: String, desc: 'Stack name'
            optional :template_parameters, type: String, desc: 'Parameters for cloudformation', default: '{}'
            optional :parameters, type: String, desc: 'Parameters is used when some event in created stack'
          end
        end
        post '/' do
          authorize!(:create, ::Environment)
          environment = Environment.create!(declared_params)

          Thread.new do
            CloudConductor::SystemBuilder.new(environment).build
          end

          environment
        end

        desc 'Update environment'
        params do
          requires :id, type: Integer, desc: 'Environment id'
          optional :name, type: String, desc: 'Environment name'
          optional :clouds, type: Array, desc: 'Cloud ids to build environment. First cloud is primary.'
          optional :stacks, type: Array, desc: 'Pattern parameters to build environment'
        end
        put '/:id' do
          environment = ::Environment.find(params[:id])
          authorize!(:update, environment)<commit_msg>Update primary_environment when have created first environment<commit_after>module API
  module V1
    class EnvironmentAPI < API::V1::Base
      resource :environments do
        desc 'List environments'
        get '/' do
          authorize!(:read, ::Environment)
          ::Environment.all.select do |environment|
            can?(:read, environment)
          end
        end

        desc 'Show environment'
        params do
          requires :id, type: Integer, desc: 'Environment id'
        end
        get '/:id' do
          environment = ::Environment.find(params[:id])
          authorize!(:read, environment)
          environment
        end

        desc 'Create environment'
        params do
          requires :system_id, type: Integer, desc: 'System id'
          requires :blueprint_id, type: Integer, desc: 'Blueprint id'
          requires :name, type: String, desc: 'Environment name'
          optional :description, type: String, desc: 'Environment description'
          requires :candidates_attributes, type: Array, desc: 'Cloud ids to build environment. First cloud is primary.' do
            requires :cloud_id, type: String, desc: 'Cloud id'
            optional :priority, type: Integer, desc: 'Cloud priority(prefer cloud that has higher value)'
          end
          optional :stacks_attributes, type: Array, desc: 'Parameters for individual stack' do
            requires :name, type: String, desc: 'Stack name'
            optional :template_parameters, type: String, desc: 'Parameters for cloudformation', default: '{}'
            optional :parameters, type: String, desc: 'Parameters is used when some event in created stack'
          end
        end
        post '/' do
          authorize!(:create, ::Environment)
          environment = Environment.create!(declared_params)

          Thread.new do
            CloudConductor::SystemBuilder.new(environment).build
            system = environment.system
            system.update_attributes!(primary_environment: environment) unless system.primary_environment
          end

          environment
        end

        desc 'Update environment'
        params do
          requires :id, type: Integer, desc: 'Environment id'
          optional :name, type: String, desc: 'Environment name'
          optional :clouds, type: Array, desc: 'Cloud ids to build environment. First cloud is primary.'
          optional :stacks, type: Array, desc: 'Pattern parameters to build environment'
        end
        put '/:id' do
          environment = ::Environment.find(params[:id])
          authorize!(:update, environment)
{'fraction_non_alphanumeric': 0.0890125173852573, 'fraction_numerical': 0.0007947546195112259, 'mean_word_length': 2.3183915622940012, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6527984', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1192, 'n_words': 449}