1: <?php
2:
3: namespace Saklient\Cloud\Resources;
4:
5: require_once __DIR__ . "/../../../Saklient/Errors/SaklientException.php";
6: use \Saklient\Errors\SaklientException;
7: require_once __DIR__ . "/../../../Saklient/Errors/HttpException.php";
8: use \Saklient\Errors\HttpException;
9: require_once __DIR__ . "/../../../Saklient/Cloud/Client.php";
10: use \Saklient\Cloud\Client;
11: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Resource.php";
12: use \Saklient\Cloud\Resources\Resource;
13: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Icon.php";
14: use \Saklient\Cloud\Resources\Icon;
15: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/DiskPlan.php";
16: use \Saklient\Cloud\Resources\DiskPlan;
17: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Server.php";
18: use \Saklient\Cloud\Resources\Server;
19: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/DiskConfig.php";
20: use \Saklient\Cloud\Resources\DiskConfig;
21: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/DiskActivity.php";
22: use \Saklient\Cloud\Resources\DiskActivity;
23: require_once __DIR__ . "/../../../Saklient/Cloud/Enums/EAvailability.php";
24: use \Saklient\Cloud\Enums\EAvailability;
25: require_once __DIR__ . "/../../../Saklient/Cloud/Enums/EDiskConnection.php";
26: use \Saklient\Cloud\Enums\EDiskConnection;
27: require_once __DIR__ . "/../../../Saklient/Cloud/Enums/EStorageClass.php";
28: use \Saklient\Cloud\Enums\EStorageClass;
29: require_once __DIR__ . "/../../../Saklient/Util.php";
30: use \Saklient\Util;
31:
32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
50: class Disk extends Resource {
51:
52: 53: 54: 55: 56: 57: 58:
59: protected $m_id;
60:
61: 62: 63: 64: 65: 66: 67:
68: protected $m_name;
69:
70: 71: 72: 73: 74: 75: 76:
77: protected $m_description;
78:
79: 80: 81: 82: 83: 84: 85:
86: protected $m_tags;
87:
88: 89: 90: 91: 92: 93: 94:
95: protected $m_icon;
96:
97: 98: 99: 100: 101: 102: 103:
104: protected $m_sizeMib;
105:
106: 107: 108: 109: 110: 111: 112:
113: protected $m_serviceClass;
114:
115: 116: 117: 118: 119: 120: 121:
122: protected $m_plan;
123:
124: 125: 126: 127: 128: 129: 130:
131: protected $m_server;
132:
133: 134: 135: 136: 137: 138: 139:
140: protected $m_availability;
141:
142: 143: 144: 145: 146: 147:
148: protected function _apiPath()
149: {
150: return "/disk";
151: }
152:
153: 154: 155: 156: 157: 158:
159: protected function _rootKey()
160: {
161: return "Disk";
162: }
163:
164: 165: 166: 167: 168: 169:
170: protected function _rootKeyM()
171: {
172: return "Disks";
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function _className()
182: {
183: return "Disk";
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function _id()
193: {
194: return $this->get_id();
195: }
196:
197: 198: 199: 200: 201: 202:
203: public function save()
204: {
205: return $this->_save();
206: }
207:
208: 209: 210: 211: 212: 213:
214: public function reload()
215: {
216: return $this->_reload();
217: }
218:
219: 220: 221: 222: 223: 224:
225: protected $_activity;
226:
227: 228: 229: 230: 231:
232: public function get_activity()
233: {
234: return $this->_activity;
235: }
236:
237:
238:
239: 240: 241: 242: 243: 244: 245:
246: public function __construct(\Saklient\Cloud\Client $client, $obj, $wrapped=false)
247: {
248: parent::__construct($client);
249: Util::validateArgCount(func_num_args(), 2);
250: Util::validateType($client, "\\Saklient\\Cloud\\Client");
251: Util::validateType($wrapped, "boolean");
252: $this->_activity = new DiskActivity($client);
253: $this->apiDeserialize($obj, $wrapped);
254: }
255:
256: 257: 258: 259: 260:
261: protected function get_isAvailable()
262: {
263: return $this->get_availability() == EAvailability::available;
264: }
265:
266:
267:
268: 269: 270: 271: 272:
273: protected function get_sizeGib()
274: {
275: $sizeMib = $this->get_sizeMib();
276: return $sizeMib == null ? null : $sizeMib >> 10;
277: }
278:
279: 280: 281: 282: 283: 284:
285: protected function set_sizeGib($sizeGib)
286: {
287: Util::validateArgCount(func_num_args(), 1);
288: Util::validateType($sizeGib, "int");
289: $this->set_sizeMib($sizeGib == null ? null : $sizeGib * 1024);
290: return $sizeGib;
291: }
292:
293:
294:
295: 296: 297: 298: 299: 300:
301: private $_source;
302:
303: 304: 305: 306: 307:
308: public function get_source()
309: {
310: return $this->_source;
311: }
312:
313: 314: 315: 316: 317: 318:
319: public function set_source(\Saklient\Cloud\Resources\Resource $source=null)
320: {
321: Util::validateArgCount(func_num_args(), 1);
322: Util::validateType($source, "\\Saklient\\Cloud\\Resources\\Resource");
323: $this->_source = $source;
324: return $source;
325: }
326:
327:
328:
329: 330: 331: 332: 333: 334: 335: 336:
337: protected function _onAfterApiDeserialize($r, $root)
338: {
339: Util::validateArgCount(func_num_args(), 2);
340: if ($r != null) {
341: $this->_activity->setSourceId($this->_id());
342: if (array_key_exists("SourceDisk", (array)($r))) {
343: $s = $r->{"SourceDisk"};
344: if ($s != null) {
345: $id = $s->{"ID"};
346: if ($id != null) {
347: $this->_source = new Disk($this->_client, $s);
348: }
349: }
350: }
351: if (array_key_exists("SourceArchive", (array)($r))) {
352: $s = $r->{"SourceArchive"};
353: if ($s != null) {
354: $id = $s->{"ID"};
355: if ($id != null) {
356: $this->_source = Resource::createWith("Archive", $this->_client, $s);
357: }
358: }
359: }
360: }
361: }
362:
363: 364: 365: 366: 367: 368: 369: 370:
371: protected function _onAfterApiSerialize($r, $withClean)
372: {
373: Util::validateArgCount(func_num_args(), 2);
374: Util::validateType($withClean, "boolean");
375: if ($r == null) {
376: return;
377: }
378: if ($this->_source != null) {
379: if ($this->_source->_className() == "Disk") {
380: $s = $withClean ? $this->_source->apiSerialize(true) : (object)['ID' => $this->_source->_id()];
381: $r->{"SourceDisk"} = $s;
382: }
383: else {
384: if ($this->_source->_className() == "Archive") {
385: $s = $withClean ? $this->_source->apiSerialize(true) : (object)['ID' => $this->_source->_id()];
386: $r->{"SourceArchive"} = $s;
387: }
388: else {
389: $this->_source = null;
390: Util::validateType($this->_source, "Disk or Archive", true);
391: }
392: }
393: }
394: }
395:
396: 397: 398: 399: 400: 401: 402:
403: public function connectTo(\Saklient\Cloud\Resources\Server $server)
404: {
405: Util::validateArgCount(func_num_args(), 1);
406: Util::validateType($server, "\\Saklient\\Cloud\\Resources\\Server");
407: $this->_client->request("PUT", "/disk/" . $this->_id() . "/to/server/" . $server->_id());
408: return $this;
409: }
410:
411: 412: 413: 414: 415: 416:
417: public function disconnect()
418: {
419: $this->_client->request("DELETE", "/disk/" . $this->_id() . "/to/server");
420: return $this;
421: }
422:
423: 424: 425: 426: 427: 428: 429: 430:
431: public function createConfig()
432: {
433: return new DiskConfig($this->_client, $this->_id());
434: }
435:
436: 437: 438: 439: 440: 441: 442:
443: public function sleepWhileCopying($timeoutSec=3600)
444: {
445: Util::validateType($timeoutSec, "int");
446: $step = 10;
447: while (0 < $timeoutSec) {
448: try {
449: $this->reload();
450: }
451: catch (HttpException $ex) {
452: }
453: $a = $this->get_availability();
454: if ($a == EAvailability::available) {
455: return true;
456: }
457: if ($a != EAvailability::migrating) {
458: $timeoutSec = 0;
459: }
460: $timeoutSec -= $step;
461: if (0 < $timeoutSec) {
462: Util::sleep($step);
463: }
464: }
465: return false;
466: }
467:
468: 469: 470: 471: 472:
473: private $n_id = false;
474:
475: 476: 477: 478: 479: 480: 481:
482: private function get_id()
483: {
484: return $this->m_id;
485: }
486:
487:
488:
489: 490: 491: 492: 493:
494: private $n_name = false;
495:
496: 497: 498: 499: 500: 501: 502:
503: private function get_name()
504: {
505: return $this->m_name;
506: }
507:
508: 509: 510: 511: 512: 513: 514: 515:
516: private function set_name($v)
517: {
518: Util::validateArgCount(func_num_args(), 1);
519: Util::validateType($v, "string");
520: $this->m_name = $v;
521: $this->n_name = true;
522: return $this->m_name;
523: }
524:
525:
526:
527: 528: 529: 530: 531:
532: private $n_description = false;
533:
534: 535: 536: 537: 538: 539: 540:
541: private function get_description()
542: {
543: return $this->m_description;
544: }
545:
546: 547: 548: 549: 550: 551: 552: 553:
554: private function set_description($v)
555: {
556: Util::validateArgCount(func_num_args(), 1);
557: Util::validateType($v, "string");
558: $this->m_description = $v;
559: $this->n_description = true;
560: return $this->m_description;
561: }
562:
563:
564:
565: 566: 567: 568: 569:
570: private $n_tags = false;
571:
572: 573: 574: 575: 576: 577: 578:
579: private function get_tags()
580: {
581: $this->n_tags = true;
582: return $this->m_tags;
583: }
584:
585: 586: 587: 588: 589: 590: 591: 592:
593: private function set_tags($v)
594: {
595: Util::validateArgCount(func_num_args(), 1);
596: Util::validateType($v, "\\ArrayObject");
597: if (is_array($v)) $v = Client::array2ArrayObject($v);
598: $this->m_tags = $v;
599: $this->n_tags = true;
600: return $this->m_tags;
601: }
602:
603:
604:
605: 606: 607: 608: 609:
610: private $n_icon = false;
611:
612: 613: 614: 615: 616: 617: 618:
619: private function get_icon()
620: {
621: return $this->m_icon;
622: }
623:
624: 625: 626: 627: 628: 629: 630: 631:
632: private function set_icon(\Saklient\Cloud\Resources\Icon $v=null)
633: {
634: Util::validateArgCount(func_num_args(), 1);
635: Util::validateType($v, "\\Saklient\\Cloud\\Resources\\Icon");
636: $this->m_icon = $v;
637: $this->n_icon = true;
638: return $this->m_icon;
639: }
640:
641:
642:
643: 644: 645: 646: 647:
648: private $n_sizeMib = false;
649:
650: 651: 652: 653: 654: 655: 656:
657: private function get_sizeMib()
658: {
659: return $this->m_sizeMib;
660: }
661:
662: 663: 664: 665: 666: 667: 668: 669:
670: private function set_sizeMib($v)
671: {
672: Util::validateArgCount(func_num_args(), 1);
673: Util::validateType($v, "int");
674: if (!$this->isNew) {
675: throw new SaklientException("immutable_field", "Immutable fields cannot be modified after the resource creation: " . "Saklient\\Cloud\\Resources\\Disk#sizeMib");
676: }
677: $this->m_sizeMib = $v;
678: $this->n_sizeMib = true;
679: return $this->m_sizeMib;
680: }
681:
682:
683:
684: 685: 686: 687: 688:
689: private $n_serviceClass = false;
690:
691: 692: 693: 694: 695: 696: 697:
698: private function get_serviceClass()
699: {
700: return $this->m_serviceClass;
701: }
702:
703:
704:
705: 706: 707: 708: 709:
710: private $n_plan = false;
711:
712: 713: 714: 715: 716: 717: 718:
719: private function get_plan()
720: {
721: return $this->m_plan;
722: }
723:
724: 725: 726: 727: 728: 729: 730: 731:
732: private function set_plan(\Saklient\Cloud\Resources\DiskPlan $v)
733: {
734: Util::validateArgCount(func_num_args(), 1);
735: Util::validateType($v, "\\Saklient\\Cloud\\Resources\\DiskPlan");
736: if (!$this->isNew) {
737: throw new SaklientException("immutable_field", "Immutable fields cannot be modified after the resource creation: " . "Saklient\\Cloud\\Resources\\Disk#plan");
738: }
739: $this->m_plan = $v;
740: $this->n_plan = true;
741: return $this->m_plan;
742: }
743:
744:
745:
746: 747: 748: 749: 750:
751: private $n_server = false;
752:
753: 754: 755: 756: 757: 758: 759:
760: private function get_server()
761: {
762: return $this->m_server;
763: }
764:
765:
766:
767: 768: 769: 770: 771:
772: private $n_availability = false;
773:
774: 775: 776: 777: 778: 779: 780:
781: private function get_availability()
782: {
783: return $this->m_availability;
784: }
785:
786:
787:
788: 789: 790: 791: 792: 793: 794:
795: protected function apiDeserializeImpl($r)
796: {
797: Util::validateArgCount(func_num_args(), 1);
798: $this->isNew = $r == null;
799: if ($this->isNew) {
800: $r = (object)[];
801: }
802: $this->isIncomplete = false;
803: if (Util::existsPath($r, "ID")) {
804: $this->m_id = Util::getByPath($r, "ID") == null ? null : "" . Util::getByPath($r, "ID");
805: }
806: else {
807: $this->m_id = null;
808: $this->isIncomplete = true;
809: }
810: $this->n_id = false;
811: if (Util::existsPath($r, "Name")) {
812: $this->m_name = Util::getByPath($r, "Name") == null ? null : "" . Util::getByPath($r, "Name");
813: }
814: else {
815: $this->m_name = null;
816: $this->isIncomplete = true;
817: }
818: $this->n_name = false;
819: if (Util::existsPath($r, "Description")) {
820: $this->m_description = Util::getByPath($r, "Description") == null ? null : "" . Util::getByPath($r, "Description");
821: }
822: else {
823: $this->m_description = null;
824: $this->isIncomplete = true;
825: }
826: $this->n_description = false;
827: if (Util::existsPath($r, "Tags")) {
828: if (Util::getByPath($r, "Tags") == null) {
829: $this->m_tags = new \ArrayObject([]);
830: }
831: else {
832: $this->m_tags = new \ArrayObject([]);
833: foreach (Util::getByPath($r, "Tags") as $t) {
834: $v1 = null;
835: $v1 = $t == null ? null : "" . $t;
836: $this->m_tags->append($v1);
837: }
838: }
839: }
840: else {
841: $this->m_tags = null;
842: $this->isIncomplete = true;
843: }
844: $this->n_tags = false;
845: if (Util::existsPath($r, "Icon")) {
846: $this->m_icon = Util::getByPath($r, "Icon") == null ? null : new Icon($this->_client, Util::getByPath($r, "Icon"));
847: }
848: else {
849: $this->m_icon = null;
850: $this->isIncomplete = true;
851: }
852: $this->n_icon = false;
853: if (Util::existsPath($r, "SizeMB")) {
854: $this->m_sizeMib = Util::getByPath($r, "SizeMB") == null ? null : intval("" . Util::getByPath($r, "SizeMB"));
855: }
856: else {
857: $this->m_sizeMib = null;
858: $this->isIncomplete = true;
859: }
860: $this->n_sizeMib = false;
861: if (Util::existsPath($r, "ServiceClass")) {
862: $this->m_serviceClass = Util::getByPath($r, "ServiceClass") == null ? null : "" . Util::getByPath($r, "ServiceClass");
863: }
864: else {
865: $this->m_serviceClass = null;
866: $this->isIncomplete = true;
867: }
868: $this->n_serviceClass = false;
869: if (Util::existsPath($r, "Plan")) {
870: $this->m_plan = Util::getByPath($r, "Plan") == null ? null : new DiskPlan($this->_client, Util::getByPath($r, "Plan"));
871: }
872: else {
873: $this->m_plan = null;
874: $this->isIncomplete = true;
875: }
876: $this->n_plan = false;
877: if (Util::existsPath($r, "Server")) {
878: $this->m_server = Util::getByPath($r, "Server") == null ? null : new Server($this->_client, Util::getByPath($r, "Server"));
879: }
880: else {
881: $this->m_server = null;
882: $this->isIncomplete = true;
883: }
884: $this->n_server = false;
885: if (Util::existsPath($r, "Availability")) {
886: $this->m_availability = Util::getByPath($r, "Availability") == null ? null : "" . Util::getByPath($r, "Availability");
887: }
888: else {
889: $this->m_availability = null;
890: $this->isIncomplete = true;
891: }
892: $this->n_availability = false;
893: }
894:
895: 896: 897: 898: 899: 900:
901: protected function apiSerializeImpl($withClean=false)
902: {
903: Util::validateType($withClean, "boolean");
904: $missing = new \ArrayObject([]);
905: $ret = (object)[];
906: if ($withClean || $this->n_id) {
907: Util::setByPath($ret, "ID", $this->m_id);
908: }
909: if ($withClean || $this->n_name) {
910: Util::setByPath($ret, "Name", $this->m_name);
911: }
912: else {
913: if ($this->isNew) {
914: $missing->append("name");
915: }
916: }
917: if ($withClean || $this->n_description) {
918: Util::setByPath($ret, "Description", $this->m_description);
919: }
920: if ($withClean || $this->n_tags) {
921: Util::setByPath($ret, "Tags", new \ArrayObject([]));
922: foreach ($this->m_tags as $r1) {
923: $v = null;
924: $v = $r1;
925: $ret->{"Tags"}->append($v);
926: }
927: }
928: if ($withClean || $this->n_icon) {
929: Util::setByPath($ret, "Icon", $withClean ? ($this->m_icon == null ? null : $this->m_icon->apiSerialize($withClean)) : ($this->m_icon == null ? (object)['ID' => "0"] : $this->m_icon->apiSerializeID()));
930: }
931: if ($withClean || $this->n_sizeMib) {
932: Util::setByPath($ret, "SizeMB", $this->m_sizeMib);
933: }
934: if ($withClean || $this->n_serviceClass) {
935: Util::setByPath($ret, "ServiceClass", $this->m_serviceClass);
936: }
937: if ($withClean || $this->n_plan) {
938: Util::setByPath($ret, "Plan", $withClean ? ($this->m_plan == null ? null : $this->m_plan->apiSerialize($withClean)) : ($this->m_plan == null ? (object)['ID' => "0"] : $this->m_plan->apiSerializeID()));
939: }
940: else {
941: if ($this->isNew) {
942: $missing->append("plan");
943: }
944: }
945: if ($withClean || $this->n_server) {
946: Util::setByPath($ret, "Server", $withClean ? ($this->m_server == null ? null : $this->m_server->apiSerialize($withClean)) : ($this->m_server == null ? (object)['ID' => "0"] : $this->m_server->apiSerializeID()));
947: }
948: if ($withClean || $this->n_availability) {
949: Util::setByPath($ret, "Availability", $this->m_availability);
950: }
951: if ($missing->count() > 0) {
952: throw new SaklientException("required_field", "Required fields must be set before the Disk creation: " . implode(", ", (array)($missing)));
953: }
954: return $ret;
955: }
956:
957: 958: 959:
960: public function __get($key) {
961: switch ($key) {
962: case "activity": return $this->get_activity();
963: case "isAvailable": return $this->get_isAvailable();
964: case "sizeGib": return $this->get_sizeGib();
965: case "source": return $this->get_source();
966: case "id": return $this->get_id();
967: case "name": return $this->get_name();
968: case "description": return $this->get_description();
969: case "tags": return $this->get_tags();
970: case "icon": return $this->get_icon();
971: case "sizeMib": return $this->get_sizeMib();
972: case "serviceClass": return $this->get_serviceClass();
973: case "plan": return $this->get_plan();
974: case "server": return $this->get_server();
975: case "availability": return $this->get_availability();
976: default: return parent::__get($key);
977: }
978: }
979:
980: 981: 982:
983: public function __set($key, $v) {
984: switch ($key) {
985: case "sizeGib": return $this->set_sizeGib($v);
986: case "source": return $this->set_source($v);
987: case "name": return $this->set_name($v);
988: case "description": return $this->set_description($v);
989: case "tags": return $this->set_tags($v);
990: case "icon": return $this->set_icon($v);
991: case "sizeMib": return $this->set_sizeMib($v);
992: case "plan": return $this->set_plan($v);
993: default: return parent::__set($key, $v);
994: }
995: }
996:
997: }
998:
999: