Module: IDRAC::Network

Included in:
Client
Defined in:
lib/idrac/network.rb

Instance Method Summary collapse

Instance Method Details

#get_bmc_networkObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/idrac/network.rb', line 41

def get_bmc_network
  # Get the iDRAC ethernet interface
  collection_response = authenticated_request(:get, "/redfish/v1/Managers/iDRAC.Embedded.1/EthernetInterfaces")
  
  if collection_response.status == 200
    collection = JSON.parse(collection_response.body)
    
    if collection["Members"] && collection["Members"].any?
      interface_path = collection["Members"][0]["@odata.id"]
      response = authenticated_request(:get, interface_path)
      
      if response.status == 200
        data = JSON.parse(response.body)
        {
          "ipv4" => data.dig("IPv4Addresses", 0, "Address"),
          "mask" => data.dig("IPv4Addresses", 0, "SubnetMask"),
          "gateway" => data.dig("IPv4Addresses", 0, "Gateway"),
          "mode" => data.dig("IPv4Addresses", 0, "AddressOrigin"), # DHCP or Static
          "mac" => data["MACAddress"],
          "hostname" => data["HostName"],
          "fqdn" => data["FQDN"],
          "dns_servers" => data["NameServers"] || [],
          "name" => data["Id"] || "iDRAC",
          "speed_mbps" => data["SpeedMbps"] || 1000,
          "status" => data.dig("Status", "Health") || "OK",
          "kind" => "ethernet"
        }
      else
        raise Error, "Failed to get interface details. Status: #{response.status}"
      end
    else
      raise Error, "No ethernet interfaces found"
    end
  else
    raise Error, "Failed to get ethernet interfaces. Status: #{collection_response.status}"
  end
end

#get_idrac_version_infoObject

Get iDRAC version information following Dell’s approach



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/idrac/network.rb', line 8

def get_idrac_version_info
  # Get iDRAC model to determine generation (following Dell's pattern)
  manager_response = authenticated_request(:get, "/redfish/v1/Managers/iDRAC.Embedded.1?$select=Model,FirmwareVersion")
  
  if manager_response.status == 200
    manager_data = JSON.parse(manager_response.body)
    model = manager_data["Model"]
    firmware_version = manager_data["FirmwareVersion"]
    
    # Determine iDRAC generation based on model (Dell's approach)
    if model.include?("12") || model.include?("13")
      idrac_generation = 8
    elsif model.include?("14") || model.include?("15") || model.include?("16")
      idrac_generation = 9
    else
      idrac_generation = 10  # iDRAC9 and newer
    end
    
    # Convert firmware version to numeric for comparison (Dell's approach)
    firmware_numeric = firmware_version.gsub(".", "").to_i if firmware_version
    
    {
      generation: idrac_generation,
      firmware_version: firmware_version,
      firmware_numeric: firmware_numeric,
      model: model
    }
  else
    # Fallback - assume newer version if we can't determine
    { generation: 9, firmware_version: "unknown", firmware_numeric: 0, model: "unknown" }
  end
end

#set_bmc_dhcpObject



228
229
230
231
# File 'lib/idrac/network.rb', line 228

def set_bmc_dhcp
  # Convenience method
  set_bmc_network(dhcp: true)
end

#set_bmc_network(ipv4: nil, mask: nil, gateway: nil, dns_primary: nil, dns_secondary: nil, hostname: nil, dhcp: false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/idrac/network.rb', line 79

def set_bmc_network(ipv4: nil, mask: nil, gateway: nil, 
                    dns_primary: nil, dns_secondary: nil, hostname: nil, 
                    dhcp: false)
  puts "🔧 iDRAC set_bmc_network called with: ipv4=#{ipv4}, mask=#{mask}, gateway=#{gateway}, dhcp=#{dhcp}".cyan
  
  # Get iDRAC version information first (following Dell's approach)
  begin
    puts "🔍 Getting iDRAC version information...".yellow
    version_info = get_idrac_version_info
    puts "✅ Detected iDRAC Generation #{version_info[:generation]}, Firmware: #{version_info[:firmware_version]}".green
    puts "   Model: #{version_info[:model]}, Firmware Numeric: #{version_info[:firmware_numeric]}".cyan
  rescue => e
    puts "❌ Error getting iDRAC version info: #{e.class} - #{e.message}".red
    raise e
  end
  
  # Get the interface path
  puts "🔍 Getting ethernet interfaces collection...".yellow
  collection_response = authenticated_request(:get, "/redfish/v1/Managers/iDRAC.Embedded.1/EthernetInterfaces")
  
  if collection_response.status == 200
    puts "✅ Got ethernet interfaces collection successfully".green
    collection = JSON.parse(collection_response.body)
    puts "🔍 Collection members: #{collection["Members"]&.size || 0} interfaces found".cyan
    
    if collection["Members"] && collection["Members"].any?
      interface_path = collection["Members"][0]["@odata.id"]
      puts "✅ Using interface path: #{interface_path}".green
      
      if dhcp
        puts "Setting iDRAC to DHCP mode...".yellow
        body = {
          "DHCPv4" => {
            "DHCPEnabled" => true
          }
        }
      else
        puts "Configuring iDRAC network settings...".yellow
        body = {}
        
        # Choose API approach based on iDRAC generation and firmware version
        if version_info[:generation] >= 9 && version_info[:firmware_numeric] > 0
          # iDRAC9/10 - use System Configuration Profile (SCP) approach for reliable network changes
          puts "Using iDRAC9+ SCP approach for network configuration".yellow
          puts "  Delegating to set_idrac_ip method for reliable configuration".cyan
          
          # Use the existing set_idrac_ip method which uses SCP
          return set_idrac_ip(new_ip: ipv4, new_gw: gateway, new_nm: mask)
          
        else
          # iDRAC8 or older firmware - use IPv4Addresses approach
          puts "Using legacy iDRAC8 API approach (IPv4Addresses)".yellow
          
          # Disable DHCP first for older versions
          body["DHCPv4"] = {
            "DHCPEnabled" => false
          }
          
          # Configure static IP using legacy API
          if ipv4 && mask
            body["IPv4Addresses"] = [{
              "Address" => ipv4,
              "SubnetMask" => mask,
              "Gateway" => gateway,
              "AddressOrigin" => "Static"
            }]
            puts "  IP: #{ipv4}/#{mask}".cyan
            puts "  Gateway: #{gateway}".cyan if gateway
          end
        end
        
        # Configure DNS if provided
        if dns_primary || dns_secondary
          dns_servers = []
          dns_servers << dns_primary if dns_primary
          dns_servers << dns_secondary if dns_secondary
          body["StaticNameServers"] = dns_servers
          puts "  DNS: #{dns_servers.join(', ')}".cyan
        end
        
        # Configure hostname if provided
        if hostname
          body["HostName"] = hostname
          puts "  Hostname: #{hostname}".cyan
        end
      end
      
      # Send the request using the version-specific approach
      puts "🔍 Sending PATCH request to #{interface_path}".yellow
      puts "📦 Request body: #{JSON.pretty_generate(body)}".cyan
      
      begin
        response = authenticated_request(
          :patch,
          interface_path,
          body: body.to_json,
          headers: { 'Content-Type' => 'application/json' }
        )
        puts "✅ Got response with status: #{response.status}".green
      rescue => e
        puts "❌ Error sending PATCH request: #{e.class} - #{e.message}".red
        raise e
      end
      
      if response.status.between?(200, 299)
        puts "iDRAC network configured successfully using #{version_info[:generation] >= 9 ? 'newer' : 'legacy'} API.".green
        
        # For network configuration changes, automatically restart iDRAC to apply settings
        if ipv4 && !dhcp
          puts "Initiating iDRAC restart to apply network configuration...".yellow
          restart_response = authenticated_request(
            :post,
            "/redfish/v1/Managers/iDRAC.Embedded.1/Actions/Manager.Reset",
            body: { "ResetType" => "GracefulRestart" }.to_json,
            headers: { 'Content-Type' => 'application/json' }
          )
          
          if restart_response.status == 204
            puts "✓ iDRAC restart initiated successfully.".green
            puts "Network configuration will be applied after restart (2-3 minutes).".cyan
            puts "New IP: #{ipv4}".cyan
          else
            puts "⚠ iDRAC restart failed (#{restart_response.status}). You may need to restart manually.".yellow
            puts "Configuration is saved but may not be active until restart.".yellow
          end
        else
          puts "WARNING: iDRAC may restart network services. Connection may be lost.".yellow
        end
        
        puts "✅ Returning true from set_bmc_network".green
        true
      else
        # Log the error with version context for troubleshooting
        puts "❌ Network configuration failed with status: #{response.status}".red
        puts "🔍 Response body: #{response.body}".red
        error_msg = "Failed to configure iDRAC network (Generation #{version_info[:generation]}, FW #{version_info[:firmware_version]}): #{response.status} - #{response.body}"
        raise Error, error_msg
      end
    else
      puts "❌ No ethernet interfaces found in collection".red
      raise Error, "No ethernet interfaces found"
    end
  else
    puts "❌ Failed to get ethernet interfaces collection, status: #{collection_response.status}".red
    puts "🔍 Response body: #{collection_response.body}".red
    raise Error, "Failed to get ethernet interfaces"
  end
end