| 1 |
/* |
| 2 |
* Copyright (C) 2023 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import client from "../client.js"; |
| 8 |
|
| 9 |
import { dialog_open, TextInput, PassInput, SelectRow } from "../dialog.jsx"; |
| 10 |
|
| 11 |
const _ = cockpit.gettext; |
| 12 |
|
| 13 |
export function iscsi_discover() { |
| 14 |
dialog_open({ |
| 15 |
Title: _("Add iSCSI portal"), |
| 16 |
Fields: [ |
| 17 |
TextInput("address", _("Server address"), |
| 18 |
{ validate: val => val === "" ? _("Server address cannot be empty.") : null, }), |
| 19 |
TextInput("username", _("Username"), { }), |
| 20 |
PassInput("password", _("Password"), { }) |
| 21 |
], |
| 22 |
Action: { |
| 23 |
Title: _("Next"), |
| 24 |
action: (vals, progress_callback) => new Promise((resolve, reject) => { |
| 25 |
const options = { }; |
| 26 |
if (vals.username || vals.password) { |
| 27 |
options.username = { t: 's', v: vals.username }; |
| 28 |
options.password = { t: 's', v: vals.password }; |
| 29 |
} |
| 30 |
|
| 31 |
let cancelled = false; |
| 32 |
client.manager_iscsi.call('DiscoverSendTargets', |
| 33 |
[vals.address, |
| 34 |
0, |
| 35 |
options |
| 36 |
]) |
| 37 |
.then(function (results) { |
| 38 |
if (!cancelled) { |
| 39 |
resolve(); |
| 40 |
iscsi_add(vals, results[0]); |
| 41 |
} |
| 42 |
}) |
| 43 |
.catch(function (error) { |
| 44 |
if (cancelled) |
| 45 |
return; |
| 46 |
|
| 47 |
// HACK - https://github.com/storaged-project/udisks/issues/26 |
| 48 |
if (error.message.indexOf("initiator failed authorization") != -1) |
| 49 |
error = { |
| 50 |
username: true, // make it red without text below |
| 51 |
password: _("Invalid username or password") |
| 52 |
}; |
| 53 |
else if (error.message.indexOf("cannot resolve host name") != -1) |
| 54 |
error = { |
| 55 |
address: _("Unknown host name") |
| 56 |
}; |
| 57 |
else if (error.message.indexOf("connection login retries") != -1) |
| 58 |
error = { |
| 59 |
address: _("Unable to reach server") |
| 60 |
}; |
| 61 |
|
| 62 |
reject(error); |
| 63 |
}); |
| 64 |
|
| 65 |
progress_callback(null, function () { |
| 66 |
cancelled = true; |
| 67 |
reject(); |
| 68 |
}); |
| 69 |
}), |
| 70 |
} |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
function iscsi_login(target, cred_vals) { |
| 75 |
const options = { |
| 76 |
'node.startup': { t: 's', v: "automatic" } |
| 77 |
}; |
| 78 |
if (cred_vals.username || cred_vals.password) { |
| 79 |
options.username = { t: 's', v: cred_vals.username }; |
| 80 |
options.password = { t: 's', v: cred_vals.password }; |
| 81 |
} |
| 82 |
return client.manager_iscsi.call('Login', |
| 83 |
[target[0], |
| 84 |
target[1], |
| 85 |
target[2], |
| 86 |
target[3], |
| 87 |
target[4], |
| 88 |
options |
| 89 |
]); |
| 90 |
} |
| 91 |
|
| 92 |
function iscsi_add(discover_vals, nodes) { |
| 93 |
dialog_open({ |
| 94 |
Title: cockpit.format(_("Available targets on $0"), |
| 95 |
discover_vals.address), |
| 96 |
Fields: [ |
| 97 |
SelectRow("target", [_("Name"), _("Address"), _("Port")], |
| 98 |
{ |
| 99 |
choices: nodes.map(n => ({ |
| 100 |
columns: [n[0], n[2], n[3]], |
| 101 |
value: n |
| 102 |
})) |
| 103 |
}) |
| 104 |
], |
| 105 |
Action: { |
| 106 |
Title: _("Add"), |
| 107 |
action: function (vals) { |
| 108 |
return iscsi_login(vals.target, discover_vals) |
| 109 |
.catch(err => { |
| 110 |
if (err.message.indexOf("authorization") != -1) |
| 111 |
iscsi_add_with_creds(discover_vals, vals); |
| 112 |
else |
| 113 |
return Promise.reject(err); |
| 114 |
}); |
| 115 |
} |
| 116 |
} |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
function iscsi_add_with_creds(discover_vals, login_vals) { |
| 121 |
dialog_open({ |
| 122 |
Title: _("Authentication required"), |
| 123 |
Fields: [ |
| 124 |
TextInput("username", _("Username"), |
| 125 |
{ value: discover_vals.username }), |
| 126 |
PassInput("password", _("Password"), |
| 127 |
{ value: discover_vals.password }) |
| 128 |
], |
| 129 |
Action: { |
| 130 |
Title: _("Add"), |
| 131 |
action: function (vals) { |
| 132 |
return iscsi_login(login_vals.target, vals) |
| 133 |
.catch(err => { |
| 134 |
// HACK - https://github.com/storaged-project/udisks/issues/26 |
| 135 |
if (err.message.indexOf("authorization") != -1) |
| 136 |
err = { |
| 137 |
username: true, // makes it red without text below |
| 138 |
password: _("Invalid username or password") |
| 139 |
}; |
| 140 |
return Promise.reject(err); |
| 141 |
}); |
| 142 |
} |
| 143 |
} |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
export function iscsi_change_name() { |
| 148 |
return client.manager_iscsi.call('GetInitiatorName') |
| 149 |
.then(function (results) { |
| 150 |
const name = results[0]; |
| 151 |
dialog_open({ |
| 152 |
Title: _("Change iSCSI initiator name"), |
| 153 |
Fields: [ |
| 154 |
TextInput("name", _("Name"), { value: name }) |
| 155 |
], |
| 156 |
Action: { |
| 157 |
Title: _("Change"), |
| 158 |
action: function (vals) { |
| 159 |
return client.manager_iscsi.call('SetInitiatorName', |
| 160 |
[vals.name, |
| 161 |
{ } |
| 162 |
]); |
| 163 |
} |
| 164 |
} |
| 165 |
}); |
| 166 |
}); |
| 167 |
} |