|
| 1 | +require 'jobs/reoccurring_job' |
| 2 | + |
| 3 | +module VCAP::CloudController |
| 4 | + module V3 |
| 5 | + class CreateServiceInstanceJob < VCAP::CloudController::Jobs::ReoccurringJob |
| 6 | + attr_reader :warnings |
| 7 | + |
| 8 | + def initialize(service_instance_guid, arbitrary_parameters: {}, user_audit_info:) |
| 9 | + super() |
| 10 | + @service_instance_guid = service_instance_guid |
| 11 | + @arbitrary_parameters = arbitrary_parameters |
| 12 | + @user_audit_info = user_audit_info |
| 13 | + @start_time = Time.now |
| 14 | + @first_time = true |
| 15 | + @warnings = [] |
| 16 | + end |
| 17 | + |
| 18 | + def perform |
| 19 | + client = VCAP::Services::ServiceClientProvider.provide({ instance: service_instance }) |
| 20 | + |
| 21 | + if first_time |
| 22 | + compute_maximum_duration |
| 23 | + send_provision_request(client) |
| 24 | + compatibility_checks |
| 25 | + @first_time = false |
| 26 | + end |
| 27 | + |
| 28 | + gone! if service_instance.nil? |
| 29 | + |
| 30 | + operation_in_progress = service_instance.last_operation.type |
| 31 | + aborted! if operation_in_progress != 'create' |
| 32 | + |
| 33 | + if service_instance.operation_in_progress? |
| 34 | + fetch_last_operation(client) |
| 35 | + end |
| 36 | + |
| 37 | + if service_instance.last_operation.state == 'succeeded' |
| 38 | + record_event(service_instance, @arbitrary_parameters) |
| 39 | + finish |
| 40 | + elsif service_instance.last_operation.state == 'failed' |
| 41 | + operation_failed!(service_instance.last_operation.description) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + def handle_timeout |
| 46 | + service_instance.save_and_update_operation( |
| 47 | + last_operation: { |
| 48 | + state: 'failed', |
| 49 | + description: 'Service Broker failed to provision within the required time.', |
| 50 | + } |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + def job_name_in_configuration |
| 55 | + :service_instance_create |
| 56 | + end |
| 57 | + |
| 58 | + def max_attempts |
| 59 | + 1 |
| 60 | + end |
| 61 | + |
| 62 | + def resource_type |
| 63 | + 'service_instances' |
| 64 | + end |
| 65 | + |
| 66 | + def resource_guid |
| 67 | + service_instance_guid |
| 68 | + end |
| 69 | + |
| 70 | + def display_name |
| 71 | + 'service_instance.create' |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + attr_reader :service_instance_guid, :arbitrary_parameters, :first_time |
| 77 | + |
| 78 | + def compute_maximum_duration |
| 79 | + max_poll_duration_on_plan = service_instance.service_plan.try(:maximum_polling_duration) |
| 80 | + self.maximum_duration_seconds = max_poll_duration_on_plan if max_poll_duration_on_plan |
| 81 | + end |
| 82 | + |
| 83 | + def send_provision_request(client) |
| 84 | + broker_response = client.provision( |
| 85 | + service_instance, |
| 86 | + accepts_incomplete: true, |
| 87 | + arbitrary_parameters: arbitrary_parameters, |
| 88 | + maintenance_info: service_instance.service_plan.maintenance_info |
| 89 | + ) |
| 90 | + |
| 91 | + service_instance.save_with_new_operation(broker_response[:instance], broker_response[:last_operation]) |
| 92 | + rescue => e |
| 93 | + service_instance.save_with_new_operation({}, { |
| 94 | + type: 'create', |
| 95 | + state: 'failed', |
| 96 | + description: e.message, |
| 97 | + }) |
| 98 | + raise e |
| 99 | + end |
| 100 | + |
| 101 | + def fetch_last_operation(client) |
| 102 | + last_operation_result = client.fetch_service_instance_last_operation(service_instance) |
| 103 | + self.polling_interval_seconds = last_operation_result[:retry_after] if last_operation_result[:retry_after] |
| 104 | + |
| 105 | + service_instance.save_and_update_operation( |
| 106 | + last_operation: last_operation_result[:last_operation].slice(:state, :description) |
| 107 | + ) |
| 108 | + rescue HttpRequestError, HttpResponseError, Sequel::Error => e |
| 109 | + logger = Steno.logger('cc-background') |
| 110 | + logger.error("There was an error while fetching the service instance operation state: #{e}") |
| 111 | + end |
| 112 | + |
| 113 | + def record_event(service_instance, request_attrs) |
| 114 | + Repositories::ServiceEventRepository.new(@user_audit_info). |
| 115 | + record_service_instance_event(:create, service_instance, request_attrs) |
| 116 | + end |
| 117 | + |
| 118 | + def service_instance |
| 119 | + ManagedServiceInstance.first(guid: service_instance_guid) |
| 120 | + end |
| 121 | + |
| 122 | + def compatibility_checks |
| 123 | + if service_instance.service_plan.service.volume_service? && volume_services_disabled? |
| 124 | + @warnings.push({ detail: ServiceInstance::VOLUME_SERVICE_WARNING }) |
| 125 | + end |
| 126 | + |
| 127 | + if service_instance.service_plan.service.route_service? && route_services_disabled? |
| 128 | + @warnings.push({ detail: ServiceInstance::ROUTE_SERVICE_WARNING }) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def volume_services_disabled? |
| 133 | + !VCAP::CloudController::Config.config.get(:volume_services_enabled) |
| 134 | + end |
| 135 | + |
| 136 | + def route_services_disabled? |
| 137 | + !VCAP::CloudController::Config.config.get(:route_services_enabled) |
| 138 | + end |
| 139 | + |
| 140 | + def gone! |
| 141 | + raise CloudController::Errors::ApiError.new_from_details('ServiceInstanceNotFound', service_instance_guid) |
| 142 | + end |
| 143 | + |
| 144 | + def aborted! |
| 145 | + raise CloudController::Errors::ApiError.new_from_details('UnableToPerform', 'Create', 'delete in progress') |
| 146 | + end |
| 147 | + |
| 148 | + def operation_failed!(msg) |
| 149 | + raise CloudController::Errors::ApiError.new_from_details('ServiceInstanceProvisionFailed', msg) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments