Forked from AparnaKarve/Service that has GO instance associated to it
Last active
July 23, 2021 17:46
-
-
Save martinpovolny/4d960e550a7838a9b04cb4d8396514b0 to your computer and use it in GitHub Desktop.
Greate a GO linked to a service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using this GIST | |
# wget https://gist.githubusercontent.com/martinpovolny/4d960e550a7838a9b04cb4d8396514b0/raw/dd5825a169953275f16a604bc62e8e482c36b4df/create_a_go_linked_to_a_service.rb | |
# bundle exec rails runner create_a_go_linked_to_a_service.rb | |
number = sprintf('%06d', rand(1_000_000)) | |
# 1. Create the Generic Object Definition | |
go_def = GenericObjectDefinition.create( | |
:name => "GO example #{number}", | |
:description => "Go example with linked service", | |
:properties => { | |
:attributes => {:max_vms => "integer", :location => "string", :active => "boolean"}, | |
:associations => {:vms => "Vm", :abc => "Service"}, | |
:methods => [:add_vm, :remove_vm] | |
} | |
) | |
# 2. Create a Generic Object Instance | |
go = GenericObject.create(:name => "GO With a Service #{number}", :generic_object_definition_id => go_def.id) | |
# 3. Create a Service | |
service = Service.create(:display => true, :name => "Service w/ GO #{number}", :description => "Service with GO #{number}") | |
# Instead of creating a brand-new Service we can take an existing Service from the DB and assigning it to `service` | |
# service = Service.first | |
# 4. Assign the Service to the GO | |
go.abc = [service] | |
go.save | |
# 5. Add GO Resource to the Service Object | |
service.add_resource(go) | |
service.save! | |
# Like `Service`, you can associate any other Reportable model with the GO object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment