99namespace LKDev \HetznerCloud \Models \Images ;
1010
1111use LKDev \HetznerCloud \HetznerAPIClient ;
12+ use LKDev \HetznerCloud \Models \Actions \Action ;
1213use LKDev \HetznerCloud \Models \Model ;
14+ use LKDev \HetznerCloud \Models \Protection ;
1315use LKDev \HetznerCloud \Models \Servers \Server ;
1416
1517/**
@@ -82,6 +84,11 @@ class Image extends Model
8284 */
8385 public $ rapidDeploy ;
8486
87+ /**
88+ * @var array|\LKDev\HetznerCloud\Models\Protection
89+ */
90+ public $ protection ;
91+
8592 /**
8693 * Image constructor.
8794 *
@@ -98,6 +105,7 @@ class Image extends Model
98105 * @param string $osFlavor
99106 * @param string $osVersion
100107 * @param bool $rapidDeploy
108+ * @param Protection $protection
101109 */
102110 public function __construct (
103111 int $ id ,
@@ -112,7 +120,8 @@ public function __construct(
112120 int $ boundTo = null ,
113121 string $ osFlavor = null ,
114122 string $ osVersion = null ,
115- bool $ rapidDeploy = null
123+ bool $ rapidDeploy = null ,
124+ Protection $ protection = null
116125 ) {
117126 $ this ->id = $ id ;
118127 $ this ->type = $ type ;
@@ -127,6 +136,7 @@ public function __construct(
127136 $ this ->osFlavor = $ osFlavor ;
128137 $ this ->osVersion = $ osVersion ;
129138 $ this ->rapidDeploy = $ rapidDeploy ;
139+ $ this ->protection = $ protection ;
130140 parent ::__construct ();
131141 }
132142
@@ -152,6 +162,26 @@ public function update(string $description, string $type): Image
152162 }
153163 }
154164
165+ /**
166+ * Changes the protection configuration of the image. Can only be used on snapshots.
167+ *
168+ * @see https://docs.hetzner.cloud/#resources-image-actions-post
169+ * @param bool $delete
170+ * @return \LKDev\HetznerCloud\Models\Actions\Action
171+ * @throws \LKDev\HetznerCloud\APIException
172+ */
173+ public function changeProtection (bool $ delete = true ): Action
174+ {
175+ $ response = $ this ->httpClient ->post ('images/ ' .$ this ->id .'/change_protection ' , [
176+ 'json ' => [
177+ 'delete ' => $ delete ,
178+ ],
179+ ]);
180+ if (! HetznerAPIClient::hasError ($ response )) {
181+ return Action::parse (json_decode ((string ) $ response ->getBody ())->action );
182+ }
183+ }
184+
155185 /**
156186 * Deletes an Image. Only images of type snapshot and backup can be deleted.
157187 *
@@ -176,6 +206,7 @@ public static function parse($input)
176206 if ($ input == null ) {
177207 return null ;
178208 }
179- return new self ($ input ->id , $ input ->type , $ input ->status , $ input ->name , $ input ->description , $ input ->image_size , $ input ->disk_size , $ input ->created , $ input ->created_from , $ input ->bound_to , $ input ->os_flavor , $ input ->os_version , $ input ->rapid_deploy );
209+
210+ return new self ($ input ->id , $ input ->type , $ input ->status , $ input ->name , $ input ->description , $ input ->image_size , $ input ->disk_size , $ input ->created , $ input ->created_from , $ input ->bound_to , $ input ->os_flavor , $ input ->os_version , $ input ->rapid_deploy , Protection::parse ($ input ->protection ));
180211 }
181212}
0 commit comments