1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\server\forms; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\server\models\Server; |
6
|
|
|
use Yii; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AssignHubsForm |
10
|
|
|
*/ |
11
|
|
|
class AssignHubsForm extends Server |
12
|
|
|
{ |
13
|
|
|
use \hipanel\base\ModelTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
|
|
public static function tableName() |
19
|
|
|
{ |
20
|
|
|
return 'server'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create AttachHubsForm model from Server model |
25
|
|
|
* |
26
|
|
|
* @param Server $server |
27
|
|
|
* @return AssignHubsForm |
28
|
|
|
*/ |
29
|
|
|
public static function fromServer(Server $server): AssignHubsForm |
30
|
|
|
{ |
31
|
|
|
$attributes = array_merge($server->getAttributes(), []); |
32
|
|
|
foreach ($server->bindings as $binding) { |
|
|
|
|
33
|
|
|
$attributes[$binding->type . '_id'] = $binding->switch_id; |
34
|
|
|
$attributes[$binding->type . '_port'] = $binding->port; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return new self(array_merge($attributes, ['scenario' => 'default'])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function rules() |
44
|
|
|
{ |
45
|
|
|
return array_merge(parent::rules(), [ |
46
|
|
|
[['id'], 'required'], |
47
|
|
|
[['net_id', 'kvm_id', 'pdu_id', 'rack_id', 'pdu2_id', 'nic2_id', 'ipmi_id'], 'integer'], |
48
|
|
|
[['net_port', 'kvm_port', 'pdu_port', 'rack_port', 'pdu2_port', 'nic2_port', 'ipmi_port'], 'string'], |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function attributeLabels() |
53
|
|
|
{ |
54
|
|
|
return array_merge(parent::attributeLabels(), [ |
55
|
|
|
'pdu2' => Yii::t('hipanel:server', 'APC 2'), |
56
|
|
|
'nic2' => Yii::t('hipanel:server', 'Switch 2'), |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* For compatibility with [[hiqdev\hiart\Collection]] |
62
|
|
|
* |
63
|
|
|
* @param $defaultScenario |
64
|
|
|
* @param array $data |
65
|
|
|
* @param array $options |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function batchQuery($defaultScenario, $data = [], array $options = []) |
69
|
|
|
{ |
70
|
|
|
$map = [ |
71
|
|
|
'update' => 'assign-hubs', |
72
|
|
|
]; |
73
|
|
|
$scenario = isset($map[$defaultScenario]) ? $map[$defaultScenario] : $defaultScenario; |
74
|
|
|
|
75
|
|
|
return (new Server)->batchQuery($scenario, $data, $options); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|