1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\server\models\traits; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\server\models\AssignSwitchInterface; |
6
|
|
|
use hipanel\modules\server\models\Binding; |
7
|
|
|
use hipanel\modules\server\models\Server; |
8
|
|
|
use Yii; |
9
|
|
|
use yii\base\InvalidConfigException; |
10
|
|
|
|
11
|
|
|
trait AssignSwitchTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* List of switch types |
15
|
|
|
* Example: ['net', 'kvm', 'pdu', 'rack', 'console'] |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public $switchVariants = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param AssignSwitchInterface $originalModel |
23
|
|
|
* |
24
|
|
|
* @return AssignSwitchInterface |
25
|
|
|
*/ |
26
|
|
|
public static function fromOriginalModel(AssignSwitchInterface $originalModel): AssignSwitchInterface |
27
|
|
|
{ |
28
|
|
|
$attributes = array_merge($originalModel->getAttributes(), []); |
|
|
|
|
29
|
|
|
$model = new static(['scenario' => 'default']); |
|
|
|
|
30
|
|
|
foreach ($originalModel->bindings as $binding) { |
|
|
|
|
31
|
|
|
$attribute = $binding->typeWithNo . '_id'; |
32
|
|
|
if ($model->hasAttribute($attribute)) { |
|
|
|
|
33
|
|
|
$attributes[$binding->typeWithNo . '_id'] = $binding->switch_id; |
34
|
|
|
$attributes[$binding->typeWithNo . '_port'] = $binding->port; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
$model->setAttributes($attributes); |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $model; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function defaultSwitchRules(): array |
43
|
|
|
{ |
44
|
|
|
$variantIds = []; |
45
|
|
|
$variantPorts = []; |
46
|
|
|
foreach ($this->switchVariants as $variant) { |
47
|
|
|
$variantIds[] = $variant . '_id'; |
48
|
|
|
$variantPorts[] = $variant . '_port'; |
49
|
|
|
} |
50
|
|
|
return [ |
51
|
|
|
[['id'], 'required'], |
52
|
|
|
[$variantIds, 'integer'], |
53
|
|
|
[$variantPorts, 'string'], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* For compatibility with [[hiqdev\hiart\Collection]] |
59
|
|
|
* |
60
|
|
|
* @param $defaultScenario |
61
|
|
|
* @param array $data |
62
|
|
|
* @param array $options |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function batchQuery($defaultScenario, $data = [], array $options = []) |
67
|
|
|
{ |
68
|
|
|
$map = [ |
69
|
|
|
'update' => 'assign-hubs', |
70
|
|
|
]; |
71
|
|
|
$scenario = isset($map[$defaultScenario]) ? $map[$defaultScenario] : $defaultScenario; |
72
|
|
|
|
73
|
|
|
return parent::batchQuery($scenario, $data, $options); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Added to model's rules list of switch pairs |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
* @throws InvalidConfigException |
81
|
|
|
*/ |
82
|
|
|
protected function generateUniqueValidators(): array |
83
|
|
|
{ |
84
|
|
|
if (empty($this->switchVariants)) { |
85
|
|
|
throw new InvalidConfigException('Please specify `switchVariants` array to use AssignSwitchTrait::generateUniqueValidators()'); |
86
|
|
|
} |
87
|
|
|
$rules = []; |
88
|
|
|
|
89
|
|
|
foreach ($this->switchVariants as $variant) { |
90
|
|
|
$rules[] = [ |
91
|
|
|
[$variant . '_port'], |
92
|
|
|
function ($attribute, $params, $validator) use ($variant) { |
|
|
|
|
93
|
|
|
if ($this->{$attribute} && $this->{$variant . '_id'}) { |
94
|
|
|
$query = Binding::find(); |
95
|
|
|
$query->andWhere(['port' => $this->{$attribute}]); |
96
|
|
|
$query->andWhere(['switch_id' => $this->{$variant . '_id'}]); |
97
|
|
|
$query->andWhere(['ne', 'base_device_id', $this->id]); |
98
|
|
|
/** @var Binding[] $bindings */ |
99
|
|
|
$bindings = $query->all(); |
100
|
|
|
if (!empty($bindings)) { |
101
|
|
|
$binding = reset($bindings); |
102
|
|
|
$this->addError($attribute, Yii::t('hipanel:server', '{switch}::{port} already taken by {device}', [ |
|
|
|
|
103
|
|
|
'switch' => $binding->switch_name, |
104
|
|
|
'port' => $binding->port, |
105
|
|
|
'device' => $binding->device_name, |
106
|
|
|
])); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
}, |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $rules; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|