Module: ForemanOpentofu::OpentofuVMCommands
- Includes:
- VMCommandCollectionNormalization
- Included in:
- Tofu
- Defined in:
- app/models/foreman_opentofu/opentofu_vm_commands.rb
Instance Method Summary collapse
- #create_vm(args = {}) ⇒ Object
- #destroy_vm(uuid) ⇒ Object
- #fetch_resource(resource_name = '', options = {}) ⇒ Object
- #find_vm_by_uuid(uuid) ⇒ Object
- #new_vm(args = {}) ⇒ Object
- #save_vm(uuid, attrs) ⇒ Object
- #start_vm(name) ⇒ Object
- #stop_vm(name) ⇒ Object
- #test_connection(options = {}) ⇒ Object
Instance Method Details
#create_vm(args = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 25 def create_vm(args = {}) vm_command_errors('create vm') do args = default_attributes.merge(args).to_h.symbolize_keys normalize_vm_args_collections!(args) executor = client(args) output = executor.run_create ComputeVM.new(self, output) end end |
#destroy_vm(uuid) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 35 def destroy_vm(uuid) tf_state = ForemanOpentofu::TfState.find_by(uuid: uuid) client({ 'name' => tf_state&.name }).run_destroy return unless tf_state Rails.logger.info "Deleting tfstate for #{tf_state&.name}" tf_state.destroy end |
#fetch_resource(resource_name = '', options = {}) ⇒ Object
67 68 69 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 67 def fetch_resource(resource_name = '', = {}) client({ 'resource' => { name: resource_name, options: } }).run_fetch end |
#find_vm_by_uuid(uuid) ⇒ Object
5 6 7 8 9 10 11 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 5 def find_vm_by_uuid(uuid) vm_command_errors('find vm') do tf_state = ForemanOpentofu::TfState.find_by(uuid: uuid) data = client({ 'name' => tf_state&.name }).run_output ComputeVM.new(self, data) end end |
#new_vm(args = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 13 def new_vm(args = {}) vm_command_errors('new vm') do args = default_attributes.merge(args).to_h.symbolize_keys normalize_vm_args_collections!(args) args = prefill_mandatory_attributes(args).merge(args) executor = client(args) data = executor.run_new attrs = data['resource_changes'].first['change']['after'] || {} OpenStruct.new(attrs) end end |
#save_vm(uuid, attrs) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 54 def save_vm(uuid, attrs) old_attrs = vm_compute_attributes_for(uuid).to_h.deep_stringify_keys tf_state = TfState.find_by(uuid: uuid) raise StandardError, "VM with UUID #{uuid} does not exist" unless tf_state vm_command_errors('update vm') do new_attrs = attrs.to_h.deep_stringify_keys merged_attrs = old_attrs.merge(new_attrs).deep_symbolize_keys normalize_vm_args_collections!(merged_attrs) data = client({ 'name' => tf_state.name }.merge(merged_attrs)).run_create(raise_if_recreate: true) ComputeVM.new(self, data) end end |
#start_vm(name) ⇒ Object
44 45 46 47 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 44 def start_vm(name) output = client({ 'name' => name, 'power_state' => 'on' }).run_create output['vm']['power_state'] == 'on' end |
#stop_vm(name) ⇒ Object
49 50 51 52 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 49 def stop_vm(name) output = client({ 'name' => name, 'power_state' => 'off' }).run_create output['vm']['power_state'] == 'off' end |
#test_connection(options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'app/models/foreman_opentofu/opentofu_vm_commands.rb', line 71 def test_connection( = {}) super begin client.run_test_connection rescue StandardError => e Rails.logger.error("OpenTofu test connection failed: #{e.}") errors.add(:base, e.) end end |