<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2013-2015 SKYARCH NETWORKS INC.
#
# This software is released under the MIT License.
#
# http://opensource.org/licenses/mit-license.php
#

class InfrastructuresController < ApplicationController
  include Concerns::InfraLogger

  # --------------- Auth
  before_action :authenticate_user!

  # --------------- existence check
  before_action :project_exist, only: [:index]
  before_action :infrastructure_exist, only: [:show, :edit, :update, :destroy, :delete_stack, :stack_events]


  before_action :set_infrastructure, only: [:show, :edit, :update, :destroy, :delete_stack, :stack_events]

  before_action :keypair_validation, only: [:create]

  before_action do
    infra = @infrastructure || (
      project_id = params[:project_id] || params[:infrastructure][:project_id] rescue nil
      Infrastructure.new(project_id: project_id)
    )

    authorize(infra)
  end


  before_action :with_zabbix, only: [:destroy, :delete_stack]

  @@regions = AWS::Regions



  # GET /infrastructures
  # GET /infrastructures.json
  def index
    project_id = params.require(:project_id)
    session[:project_id] = project_id
    page       = params[:page] || 1

    @selected_project = Project.find(project_id)

    @infrastructures = @selected_project.infrastructures.includes(:ec2_private_key).page(page)
    @selected_client = @selected_project.client

    respond_to do |format|
      format.json
      format.html
    end
  end

  # GET /infrastructures/:id
  def show
    stack = Stack.new(@infrastructure)

    resp = {
      status:        stack.status_and_type,
      name:          @infrastructure.stack_name,
      token_invalid: stack.status[:message].eql?("The security token included in the request is invalid."),
    }

    # When stack does not exist
    unless stack.status[:available] # in many cases, it will be when stack does not exist.
      @infrastructure.status = ""
      @infrastructure.save!
      @infrastructure.resources.destroy_all

      resp[:message] = stack.status[:message]
    end

    if stack.update_complete?
      @infrastructure.resources.destroy_all
    end

    @infrastructure.status = stack.status[:status]
    @infrastructure.save!

    render json: resp and return
  end

  # GET /infrastructures/:id/stack_events
  def stack_events
    stack = Stack.new(@infrastructure)
    events = nil
    begin
      events = stack.events
      # rubocop:disable Lint/HandleExceptions
    rescue Aws::CloudFormation::Errors::ValidationError # stack does not exist
      # rubocop:enable Lint/HandleExceptions
    end

    render json: {
      stack_status: stack.status_and_type,
      stack_events: events,
    } and return
  end

  # GET /infrastructures/new
  def new
    project_id = params.require(:project_id)
    @infrastructure = Infrastructure.new(
      project_id: project_id,
    )
    @regions = @@regions
  end

  # GET /infrastructures/1/edit
  # スタック情報が取得できない場合のみ
  def edit
    if @infrastructure.status.present?
      redirect_to infrastructures_path(project_id: @infrastructure.project_id),
        alert: I18n.t('infrastructures.msg.not_necessary')
    end

    @regions = @@regions
  end

  # POST /infrastructures
  # POST /infrastructures.json
  def create
    infra = Infrastructure.create_with_ec2_private_key!(infrastructure_params)
  rescue => ex
    flash[:alert] = ex.message<commit_msg>Keep the input data when infrastructure registration failed<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2013-2015 SKYARCH NETWORKS INC.
#
# This software is released under the MIT License.
#
# http://opensource.org/licenses/mit-license.php
#

class InfrastructuresController < ApplicationController
  include Concerns::InfraLogger

  # --------------- Auth
  before_action :authenticate_user!

  # --------------- existence check
  before_action :project_exist, only: [:index]
  before_action :infrastructure_exist, only: [:show, :edit, :update, :destroy, :delete_stack, :stack_events]


  before_action :set_infrastructure, only: [:show, :edit, :update, :destroy, :delete_stack, :stack_events]

  before_action do
    infra = @infrastructure || (
      project_id = params[:project_id] || params[:infrastructure][:project_id] rescue nil
      Infrastructure.new(project_id: project_id)
    )

    authorize(infra)
  end


  before_action :with_zabbix, only: [:destroy, :delete_stack]

  @@regions = AWS::Regions



  # GET /infrastructures
  # GET /infrastructures.json
  def index
    project_id = params.require(:project_id)
    session[:project_id] = project_id
    page       = params[:page] || 1

    @selected_project = Project.find(project_id)

    @infrastructures = @selected_project.infrastructures.includes(:ec2_private_key).page(page)
    @selected_client = @selected_project.client

    respond_to do |format|
      format.json
      format.html
    end
  end

  # GET /infrastructures/:id
  def show
    stack = Stack.new(@infrastructure)

    resp = {
      status:        stack.status_and_type,
      name:          @infrastructure.stack_name,
      token_invalid: stack.status[:message].eql?("The security token included in the request is invalid."),
    }

    # When stack does not exist
    unless stack.status[:available] # in many cases, it will be when stack does not exist.
      @infrastructure.status = ""
      @infrastructure.save!
      @infrastructure.resources.destroy_all

      resp[:message] = stack.status[:message]
    end

    if stack.update_complete?
      @infrastructure.resources.destroy_all
    end

    @infrastructure.status = stack.status[:status]
    @infrastructure.save!

    render json: resp and return
  end

  # GET /infrastructures/:id/stack_events
  def stack_events
    stack = Stack.new(@infrastructure)
    events = nil
    begin
      events = stack.events
      # rubocop:disable Lint/HandleExceptions
    rescue Aws::CloudFormation::Errors::ValidationError # stack does not exist
      # rubocop:enable Lint/HandleExceptions
    end

    render json: {
      stack_status: stack.status_and_type,
      stack_events: events,
    } and return
  end

  # GET /infrastructures/new
  def new
    project_id = params.require(:project_id)
    @infrastructure = Infrastructure.new(
      project_id: project_id,
    )
    @regions = @@regions
  end

  # GET /infrastructures/1/edit
  # スタック情報が取得できない場合のみ
  def edit
    if @infrastructure.status.present?
      redirect_to infrastructures_path(project_id: @infrastructure.project_id),
        alert: I18n.t('infrastructures.msg.not_necessary')
    end

    @regions = @@regions
  end

  # POST /infrastructures
  # POST /infrastructures.json
  def create
    keypair_validation
    infra = Infrastructure.create_with_ec2_private_key!(infrastructure_params)
  rescue => ex
    flash[:alert] = ex.message
{'fraction_non_alphanumeric': 0.10649845973302038, 'fraction_numerical': 0.00440076279888514, 'mean_word_length': 3.309734513274336, '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': '1779951', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2056, 'n_words': 463}