Exploriment / hcloud-php

PHP library for the Hetzner Cloud API by Exploriment
MIT License
28 stars 8 forks source link

Servers::create issue #1

Closed Vladdy88 closed 6 years ago

Vladdy88 commented 6 years ago

I have tested the script and I found I little problem with it. When I use the $creation = HetznerCloud\Servers::create($config); it returns an object with null members like in the example bellow :

object(Exploriment\HetznerCloud\Objects\ServerCreation)#105 (3) {
  ["action"]=>
  NULL
  ["server"]=>
  NULL
  ["root_password"]=>
  NULL
}

I saw that the response is returned after the command is given, and the action is still in progress. It should be modified to wait until the action is finished and send the response only after that. Otherwise I have to use after the creation the command to get the server details, but it`s not the best way in my opinion.

Vladdy88 commented 6 years ago

I modified the Servers::create method to

public static function create($config)
    {
        if(!is_a($config, Configs\Server::class))
            throw new InvalidInput('config must be a ' . Configs\Server::class);

        $config->verify();

        $response = self::request('POST', '', $config->toArray());
        $serialized_response = $response->jsonSerialize();

        $status = $serialized_response->action->status;
        $progress = $serialized_response->action->progress;
        $action_id = $serialized_response->action->id;

        while ($progress != 100 && $status != 'success') {
            $actionResponse = \Exploriment\HetznerCloud\Actions::find($action_id);
            $progress = $actionResponse->progress;
            $status = $actionResponse->status;
            sleep(1);
        }

        return new ServerCreation($serialized_response);
    }

and in this way I can get at the end of the creation API the desired details. But maybe there is a better way.

dyhli commented 6 years ago

Pushed out an update, response should no longer be empty. Sorry about that! The action doesn't have to be completed for you to get your server information.

Let me know if it still doesn't work for you.

Vladdy88 commented 6 years ago

Yes, now it is ok. Thanks a lot. Great work!