Commit fc8291a2 authored by Arthur Del Esposte's avatar Arthur Del Esposte 🤘🏻
Browse files

Split task to create and register components

parent 7440f872
Pipeline #2974283 passed with stage
......@@ -39,6 +39,10 @@ To set the three required URL, edit the [services config file](config/services.y
### Resource and Components
In order to automatically populate the database with resource and components data we recommend that you use the [resource config file](config/resource.yml). See the [file](config/resource.yml) to undestand how to add data.
In order to automatically populate the database with resource and components data we recommend that you use the [resource config file](config/resource.yml). See the [file](config/resource.yml) to undestand how to add data. After this, run the task to create components in database:
```$ bundle exec rake component:create```
You can also use alternative ways to populate the database with your informantion. You could add new fields or table by adding new migrations or create your own scripts to populate the database, for instance.
......@@ -2,7 +2,7 @@ require 'json'
module Platform
module ResourceManager
def self.register_resources
def self.create_all
return if BasicResource.count == RESOURCE_CONFIG["resources"].count
#TODO: Logger
......@@ -13,12 +13,14 @@ module Platform
resource.maker = resource_data["maker"]
resource.save!
resource.create_components(resource_data["components"])
Platform::ResourceManager.register(resource.meta_data)
end
end
def self.register(json)
"HTTP POST #{SERVICES_CONFIG['services']['catalog']}/resource/registry/" + json.to_json
def self.register_all
Component.find_each do |component|
data = component.meta_data
"HTTP POST #{SERVICES_CONFIG['services']['catalog']}/resource/registry/" + data.to_json
end
end
end
end
namespace :platform do
namespace :component do
desc "Create all resources and components in local database"
task :create => :environment do
begin
Platform::ResourceManager.create_all
puts "#{BasicResource.count} resources and #{Component.count} components created!"
rescue
puts "An error has occurred while trying to create Components"
puts "Please, verify the resource configuration file"
end
end
desc "Register all resources and components in the Platform"
task :register => :environment do
begin
Platform::ResourceManager.register_resources
puts "#{BasicResource.count} resources and #{Component.count} components registered!"
Platform::ResourceManager.register_all
puts "#{Component.where.not(uuid: nil).count} components registered!"
rescue
puts "An error has occurred while trying to register in the platform"
puts "Please, verify the services configuration file"
......
......@@ -8,14 +8,14 @@ describe Platform::ResourceManager do
@previous_number_of_resources = BasicResource.count
@previous_number_of_component = Component.count
described_class.register_resources
described_class.create_all
end
context "when there aren't new resources data" do
before do
@previous_number_of_resources = BasicResource.count
@previous_number_of_component = Component.count
described_class.register_resources
described_class.create_all
end
it "does't create new resources" do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment