PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/manageiq/providers/cloud_manager.rb

https://gitlab.com/miq-chargeback/manageiq
Ruby | 182 lines | 134 code | 32 blank | 16 comment | 13 complexity | 327416567014210c20eaf009e9c3b770 MD5 | raw file
  1. module ManageIQ::Providers
  2. class CloudManager < BaseManager
  3. require_nested :AuthKeyPair
  4. require_nested :Template
  5. require_nested :MetricsCapture
  6. require_nested :Provision
  7. require_nested :ProvisionWorkflow
  8. require_nested :Vm
  9. require_nested :OrchestrationStack
  10. require_nested :VmOrTemplate
  11. class << model_name
  12. define_method(:route_key) { "ems_clouds" }
  13. define_method(:singular_route_key) { "ems_cloud" }
  14. end
  15. has_many :availability_zones, :foreign_key => :ems_id, :dependent => :destroy
  16. has_many :flavors, :foreign_key => :ems_id, :dependent => :destroy
  17. has_many :cloud_database_flavors, :foreign_key => :ems_id, :dependent => :destroy
  18. has_many :cloud_tenants, :foreign_key => :ems_id, :dependent => :destroy
  19. has_many :cloud_resource_quotas, :foreign_key => :ems_id, :dependent => :destroy
  20. has_many :cloud_volumes, :foreign_key => :ems_id, :dependent => :destroy
  21. has_many :cloud_volume_types, :foreign_key => :ems_id, :dependent => :destroy
  22. has_many :cloud_volume_backups, :foreign_key => :ems_id, :dependent => :destroy
  23. has_many :cloud_volume_snapshots, :foreign_key => :ems_id, :dependent => :destroy
  24. has_many :cloud_object_store_containers, :foreign_key => :ems_id, :dependent => :destroy
  25. has_many :cloud_object_store_objects, :foreign_key => :ems_id, :dependent => :destroy
  26. has_many :cloud_services, :foreign_key => :ems_id, :dependent => :destroy
  27. has_many :cloud_databases, :foreign_key => :ems_id, :dependent => :destroy
  28. has_many :key_pairs, :class_name => "AuthKeyPair", :as => :resource, :dependent => :destroy
  29. has_many :host_aggregates, :foreign_key => :ems_id, :dependent => :destroy
  30. has_many :resource_groups, :foreign_key => :ems_id, :dependent => :destroy, :inverse_of => :ext_management_system
  31. has_one :source_tenant, :as => :source, :class_name => 'Tenant'
  32. has_many :vm_and_template_labels, :through => :vms_and_templates, :source => :labels
  33. # Only taggings mapped from labels, excluding user-assigned tags.
  34. has_many :vm_and_template_taggings,
  35. -> { joins(:tag).merge(Tag.controlled_by_mapping) },
  36. :through => :vms_and_templates,
  37. :source => :taggings
  38. virtual_has_many :volume_availability_zones, :class_name => "AvailabilityZone", :uses => :availability_zones
  39. supports_not :auth_key_pair_create
  40. supports_not :cinder_service
  41. supports_not :cloud_tenants
  42. supports_not :cloud_volume
  43. supports_not :cloud_tenant_mapping
  44. supports_not :create_flavor
  45. validates_presence_of :zone
  46. # TODO: remove and have each manager include this
  47. include HasNetworkManagerMixin
  48. include HasManyOrchestrationStackMixin
  49. after_destroy :destroy_mapped_tenants
  50. # These are availability zones that are available to be chosen
  51. # when creating a new cloud volume for providers that support it.
  52. # By default this is all AZs, individual cloud managers
  53. # can override this.
  54. def volume_availability_zones
  55. availability_zones
  56. end
  57. # Development helper method for Rails console for opening a browser to the EMS.
  58. #
  59. # This method is NOT meant to be called from production code.
  60. def open_browser
  61. raise NotImplementedError unless Rails.env.development?
  62. require 'util/miq-system'
  63. MiqSystem.open_browser(browser_url)
  64. end
  65. def validate_authentication_status
  66. {:available => true, :message => nil}
  67. end
  68. def stop_event_monitor_queue_on_credential_change
  69. if event_monitor_class && !self.new_record? && self.credentials_changed?
  70. _log.info("EMS: [#{name}], Credentials have changed, stopping Event Monitor. It will be restarted by the WorkerMonitor.")
  71. stop_event_monitor_queue
  72. network_manager.stop_event_monitor_queue if respond_to?(:network_manager) && network_manager
  73. end
  74. end
  75. def sync_cloud_tenants_with_tenants
  76. return unless supports?(:cloud_tenant_mapping)
  77. sync_root_tenant
  78. sync_tenants
  79. sync_deleted_cloud_tenants
  80. end
  81. def sync_tenants
  82. reload
  83. _log.info("Syncing CloudTenant with Tenants...")
  84. CloudTenant.with_ext_management_system(id).walk_tree do |cloud_tenant, _|
  85. cloud_tenant_description = cloud_tenant.description.blank? ? cloud_tenant.name : cloud_tenant.description
  86. tenant_params = {:name => cloud_tenant.name, :description => cloud_tenant_description, :source => cloud_tenant}
  87. tenant_parent = cloud_tenant.parent.try(:source_tenant) || source_tenant
  88. if cloud_tenant.source_tenant
  89. cloud_tenant.update_source_tenant(tenant_params)
  90. elsif existing_source_tenant = Tenant.descendants_of(tenant_parent).find_by(:name => tenant_params[:name])
  91. _log.info("CloudTenant #{cloud_tenant.name} has orphaned tenant #{existing_source_tenant.name}")
  92. cloud_tenant.source_tenant = existing_source_tenant
  93. tenant_params[:parent] = tenant_parent
  94. cloud_tenant.update_source_tenant(tenant_params)
  95. else
  96. _log.info("CloudTenant #{cloud_tenant.name} has no tenant")
  97. _log.info("Creating Tenant with parameters: #{tenant_params.inspect}")
  98. # first level of CloudTenants does not have parents - in that case
  99. # source_tenant from EmsCloud is used - this is tenant which is representing
  100. # provider (EmsCloud)
  101. # if it is not first level of cloud tenant
  102. # there is existing parent of CloudTenant and his related tenant is taken
  103. _log.info("and with parent #{tenant_parent.name}")
  104. tenant_params[:parent] = tenant_parent
  105. cloud_tenant.source_tenant = Tenant.new(tenant_params)
  106. _log.info("New Tenant #{cloud_tenant.source_tenant.name} created")
  107. end
  108. cloud_tenant.update_source_tenant_associations
  109. cloud_tenant.save!
  110. _log.info("CloudTenant #{cloud_tenant.name} saved")
  111. end
  112. end
  113. def sync_deleted_cloud_tenants
  114. return unless source_tenant
  115. source_tenant.descendants.each do |tenant|
  116. next if tenant.source
  117. if tenant.parent == source_tenant # tenant is already under provider's tenant
  118. _log.info("Setting source_id and source_type to nil for #{tenant.name} under provider's tenant #{source_tenant.name}")
  119. tenant.update(:source_id => nil, :source_type => nil)
  120. next
  121. end
  122. # move tenant under the provider's tenant
  123. _log.info("Moving out #{tenant.name} under provider's tenant #{source_tenant.name}")
  124. tenant.update(:parent => source_tenant, :source_id => nil, :source_type => nil)
  125. end
  126. end
  127. def sync_root_tenant
  128. ems_tenant = source_tenant || Tenant.new(:parent => tenant, :source => self)
  129. ems_tenant_name = "#{self.class.description} Cloud Provider #{name}"
  130. ems_tenant.update!(:name => ems_tenant_name, :description => ems_tenant_name)
  131. end
  132. def create_cloud_tenant(options)
  133. CloudTenant.create_cloud_tenant(self, options)
  134. end
  135. def valid_service_orchestration_resource
  136. true
  137. end
  138. def self.display_name(number = 1)
  139. n_('Cloud Manager', 'Cloud Managers', number)
  140. end
  141. def destroy_mapped_tenants
  142. if source_tenant
  143. # We just destroyed ourself, reload the source_tenant association
  144. source_tenant.reload
  145. source_tenant.destroy_with_subtree
  146. end
  147. end
  148. define_method(:allow_duplicate_endpoint_url?) { true }
  149. end
  150. end