<commit_before>#
# Description: This method checks to see if the stack has been provisioned
#   and whether the refresh has completed
#

def refresh_provider(service)
  provider = service.orchestration_manager

  $evm.log("info", "Refreshing provider #{provider.name}")
  $evm.set_state_var('provider_last_refresh', provider.last_refresh_date.to_i)
  provider.refresh
end

def refresh_may_have_completed?(service)
  stack = service.orchestration_stack
  refreshed_stack = $evm.vmdb(:orchestration_stack).find_by(:name => stack.name, :ems_ref => stack.ems_ref)
  refreshed_stack && refreshed_stack.status != 'CREATE_IN_PROGRESS'
end

def check_deployed(service)
  $evm.log("info", "Check orchestration deployed")
  # check whether the stack deployment completed
  status, reason = service.orchestration_stack_status
  case status.downcase
  when 'create_complete'
    $evm.root['ae_result'] = 'ok'
  when 'rollback_complete', 'delete_complete', /failed$/, /canceled$/
    $evm.root['ae_result'] = 'error'
    $evm.root['ae_reason'] = reason
  else
    # deployment not done yet in provider
    $evm.root['ae_result']         = 'retry'
    $evm.root['ae_retry_interval'] = '1.minute'
    return
  end

  $evm.log("info", "Stack deployment finished. Status: #{$evm.root['ae_result']}, reason: #{$evm.root['ae_reason']}")
  $evm.log("info", "Please examine stack resources for more details") if $evm.root['ae_result'] == 'error'

  return unless service.orchestration_stack
  $evm.set_state_var('deploy_result', $evm.root['ae_result'])
  $evm.set_state_var('deploy_reason', $evm.root['ae_reason'])

  refresh_provider(service)

  $evm.root['ae_result']         = 'retry'
  $evm.root['ae_retry_interval'] = '30.seconds'
end

def check_refreshed(service)
  $evm.log("info", "Check refresh status of stack (#{service.stack_name})")

  if refresh_may_have_completed?(service)
    $evm.root['ae_result'] = $evm.get_state_var('deploy_result')
    $evm.root['ae_reason'] = $evm.get_state_var('deploy_reason')
    $evm.log("info", "Refresh completed.")
  else
    $evm.root['ae_result']         = 'retry'
    $evm.root['ae_retry_interval'] = '30.seconds'
  end
end

task = $evm.root["service_template_provision_task"]
service = task.destination
if $evm.state_var_exist?('provider_last_refresh')
  check_refreshed(service)
else
  check_deployed(service)
end
task.miq_request.user_message = $evm.root['ae_reason'].truncate(255) unless $evm.root['ae_reason'].blank?
<commit_msg>Refactoring the check_provisioned method for theautomate cloud orchestration.<commit_after>#
# Description: This class checks to see if the stack has been provisioned
#   and whether the refresh has completed
#
module ManageIQ
  module Automate
    module Cloud
      module Orchestration
        module Provisioning
          module StateMachines
            class CheckProvisioned
              def initialize(handle = $evm)
                @handle = handle
              end

              def main
                method1()
              end

              private

              def method1
                task = @handle.root["service_template_provision_task"]
                service = task.try(:destination)

                if @handle.state_var_exist?('provider_last_refresh')
                  check_refreshed(service)
                else
                  check_deployed(service)
                end

                task.miq_request.user_message = @handle.root['ae_reason'].truncate(255) unless @handle.root['ae_reason'].blank?
              end


              def refresh_provider(service)
                provider = service.orchestration_manager

                @handle.log("info", "Refreshing provider #{provider.name}")
                @handle.set_state_var('provider_last_refresh', provider.last_refresh_date.to_i)
                provider.refresh
              end

              def refresh_may_have_completed?(service)
                stack = service.orchestration_stack
                refreshed_stack = @handle.vmdb(:orchestration_stack).find_by(:name => stack.name, :ems_ref => stack.ems_ref)
                refreshed_stack && refreshed_stack.status != 'CREATE_IN_PROGRESS'
              end

              def check_deployed(service)
                @handle.log("info", "Check orchestration deployed")
                # check whether the stack deployment completed
                status, reason = service.orchestration_stack_status
                case status.downcase
                when 'create_complete'
                  @handle.root['ae_result'] = 'ok'
                when 'rollback_complete', 'delete_complete', /failed$/, /canceled$/
                  @handle.root['ae_result'] = 'error'
                  @handle.root['ae_reason'] = reason
                else
                  # deployment not done yet in provider
                  @handle.root['ae_result']         = 'retry'
                  @handle.root['ae_retry_interval'] = '1.minute'
                  return
                end

                @handle.log("info", "Stack deployment finished. Status: #{@handle.root['ae_result']}, reason: #{@handle.root['ae_reason']}")
                @handle.log("info", "Please examine stack resources for more details") if @handle.root['ae_result'] == 'error'

                return unless service.orchestration_stack
                @handle.set_state_var('deploy_result', @handle.root['ae_result'])
                @handle.set_state_var('deploy_reason', @handle.root['ae_reason'])

                refresh_provider(service)

                @handle.root['ae_result']         = 'retry'
                @handle.root['ae_retry_interval'] = '30.seconds'
              end

              def check_refreshed(service)
                @handle.log("info", "Check refresh status of stack (#{service.stack_name})")

                if refresh_may_have_completed?(service)
                  @handle.root['ae_result'] = @handle.get_state_var('deploy_result')
                  @handle.root['ae_reason'] = @handle.get_state_var('deploy_reason')
                  @handle.log("info", "Refresh completed.")
                else
                  @handle.root['ae_result']         = 'retry'
                  @handle.root['ae_retry_interval'] = '30.seconds'
                end
              end
            end
          end
        end
      end
    end
  end
end

if __FILE__ == $PROGRAM_NAME
    ManageIQ::Automate::Cloud::Orchestration::Provisioning::StateMachines::CheckProvisioned.new.main
end

{'fraction_non_alphanumeric': 0.11361183906274086, 'fraction_numerical': 0.0027747803298905503, 'mean_word_length': 2.522258414766558, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2660311', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1876, 'n_words': 363}