Module: HammerCLIForeman::Interface::InterfaceUpdate

Included in:
CreateCommand, UpdateCommand
Defined in:
lib/hammer_cli_foreman/interface.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



76
77
78
79
# File 'lib/hammer_cli_foreman/interface.rb', line 76

def self.included(base)
  base.option "--primary", :flag, _("Should this interface be used for constructing the FQDN of the host? Each managed hosts needs to have one primary interface")
  base.option "--provision", :flag, _("Should this interface be used for TFTP of PXELinux (or SSH for image-based hosts)? Each managed hosts needs to have one provision interface")
end

Instance Method Details

#get_interfaces(host_id) ⇒ Object



81
82
83
# File 'lib/hammer_cli_foreman/interface.rb', line 81

def get_interfaces(host_id)
  HammerCLIForeman.foreman_resource!(:interfaces).call(:index, {'host_id' => host_id}, request_headers, request_options)
end

#mandatory_interfaces(host_id, nic_id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hammer_cli_foreman/interface.rb', line 85

def mandatory_interfaces(host_id, nic_id)
  mandatory_options = []
  get_interfaces(host_id)['results'].each do |nic|
    if (nic['primary'] || nic['provision']) && nic['id'] != nic_id
      mandatory_options << {
        'id' => nic['id'],
        'primary' => nic['primary'],
        'provision' => nic['provision']
      }
    end
  end
  mandatory_options
end

#reset_flag(interfaces_params, flag) ⇒ Object



99
100
101
102
103
# File 'lib/hammer_cli_foreman/interface.rb', line 99

def reset_flag(interfaces_params, flag)
  interfaces_params.each do |nic|
    nic[flag] = false if nic[flag]
  end
end

#send_requestObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hammer_cli_foreman/interface.rb', line 105

def send_request
  nic_params = request_params
  interface = nic_params['interface']

  interface['id'] = nic_params['id'].to_i if nic_params['id']
  interface['compute_attributes'] = option_compute_attributes

  host_params = {}
  host_params['id'] = nic_params['host_id']
  host_params['host'] = {}
  host_params['host']['interfaces_attributes'] = mandatory_interfaces(nic_params['host_id'], interface['id'])

  reset_flag(host_params['host']['interfaces_attributes'], 'primary') if option_primary?
  reset_flag(host_params['host']['interfaces_attributes'], 'provision') if option_provision?

  host_params['host']['interfaces_attributes'] += [interface]

  HammerCLIForeman.foreman_resource!(:hosts).call(:update, host_params, request_headers, request_options)
end