Example RestAPI code

RESTful knowledge exchange

Example RestAPI code

Veeam Logoby headcloudmonkey » Tue Oct 27, 2015 1:43 pm

Hi

Does anyone have any example code for putting a web page in front of the cloud connect service? Looking for any examples of using the API to stand up tenants, reset password, allocate repositories and quotas, etc

If not, I will have a try to develop my own and will look to contribute it back for the greater good :)

Thanks
Dan
Daniel Gould
CTO
w: www.stratogen.net
headcloudmonkey
Service Provider
 
Posts: 13
Liked: 3 times
Joined: Thu Jul 09, 2015 9:14 am
Location: Brighton, East Sussex, UK
Full Name: Dan Gould

Re: Example RestAPI code

Veeam Logoby v.Eremin » Tue Nov 03, 2015 4:25 pm

There is a small example written by our Solutions Architect Preben Berg. I believe it should be a good starting point. Thanks.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Example RestAPI code

Veeam Logoby headcloudmonkey » Tue Nov 03, 2015 5:34 pm

Hi,

Thanks for that - I had found that this week. When I try it I seem to get an issue with the lookups against the B&R server:

Code: Select all
[Tue Nov 03 17:59:25.572811 2015] [:error] [pid 2790] [client 192.168.159.1:54485] PHP Fatal error:  Uncaught exception 'GuzzleHttp\\Exception\\ClientException' with message 'Client error response [url] http://192.168.159.128:9399/api/backupServers/Not%20Found!/repositories [status code] 400 [reason phrase] BadRequest' in /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:89\nStack trace:\n#0 /var/www/html/vendor/guzzlehttp/guzzle/src/Subscriber/HttpError.php(33): GuzzleHttp\\Exception\\RequestException::create(Object(GuzzleHttp\\Message\\Request), Object(GuzzleHttp\\Message\\Response))\n#1 /var/www/html/vendor/guzzlehttp/guzzle/src/Event/Emitter.php(109): GuzzleHttp\\Subscriber\\HttpError->onComplete(Object(GuzzleHttp\\Event\\CompleteEvent), 'complete')\n#2 /var/www/html/vendor/guzzlehttp/guzzle/src/RequestFsm.php(91): GuzzleHttp\\Event\\Emitter->emit('complete', Object(GuzzleHttp\\Event\\CompleteEvent))\n#3 /var/www/html/vendor/guzzlehttp/guzzle/src/RequestFsm.php(132): GuzzleHttp\\RequestFsm->__invoke(Object(GuzzleHttp\\Transaction))\n#4 /var/www/html/vendor/react/promise/src/FulfilledPromise.php(25): Guzzle in /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 89, referer: http://192.168.159.131/index.php


From what I can tell, it looks like it's not properly interrogating the API for the available backup servers. Any thoughts on this?

Thanks
Dan
Daniel Gould
CTO
w: www.stratogen.net
headcloudmonkey
Service Provider
 
Posts: 13
Liked: 3 times
Joined: Thu Jul 09, 2015 9:14 am
Location: Brighton, East Sussex, UK
Full Name: Dan Gould

Re: Example RestAPI code

Veeam Logoby poulpreben » Wed Nov 04, 2015 9:35 am

Hi Dan,

Thanks for posting here. I had one service provider last week mentioning they had some issues with the code on the most recent version of PHP. Can you please clarify your version of GuzzleHttp and PHP? I will verify it in my lab ASAP, as I need to also update the code for v9 and Cloud Connect Replication functionality.

- Preben
poulpreben
Expert
 
Posts: 935
Liked: 398 times
Joined: Mon Jul 23, 2012 8:16 am
Full Name: Preben Berg

Re: Example RestAPI code

Veeam Logoby headcloudmonkey » Thu Nov 05, 2015 12:00 pm

Hi Preben,

This is on guzzlehttp 4.2.3 and php 5.4.16

Thanks
Dan
Daniel Gould
CTO
w: www.stratogen.net
headcloudmonkey
Service Provider
 
Posts: 13
Liked: 3 times
Joined: Thu Jul 09, 2015 9:14 am
Location: Brighton, East Sussex, UK
Full Name: Dan Gould

Re: Example RestAPI code

Veeam Logoby poulpreben » Thu Nov 05, 2015 12:52 pm

Thank you. I will verify if something else is required.
poulpreben
Expert
 
Posts: 935
Liked: 398 times
Joined: Mon Jul 23, 2012 8:16 am
Full Name: Preben Berg

Re: Example RestAPI code

Veeam Logoby headcloudmonkey » Sun Nov 08, 2015 11:44 am

Hi Preben,

I rebuilt my cloud connect server from scratch and it's now working correctly! I think it was down to a certificate error in the end.

Looking forward to the updated example code for v9 as soon as it's available.

Thanks for the great foundation work

Regards
Dan
Daniel Gould
CTO
w: www.stratogen.net
headcloudmonkey
Service Provider
 
Posts: 13
Liked: 3 times
Joined: Thu Jul 09, 2015 9:14 am
Location: Brighton, East Sussex, UK
Full Name: Dan Gould

Re: Example RestAPI code

Veeam Logoby peasoupcto » Fri Jan 22, 2016 11:59 am

Hi Preben,

since your update on your repository for v9 I encountered some issues with the code,

first one was that i got an error code 6 ( unable to resolve host) - which was strange as I was using IP's - to resolve this I change base_url into base_uri on line 45 in veeam.class.php which after this change i was able to contact the vcc server.

however the next issue is that i get an 401 return and in the veeam api log I see the following error:
Code: Select all
22.01.2016 11:36:47] <253> Info     [POST] request to [http://myip:9399/api/sessionMngr/?v=v1_2] deserialized. Message: []; Parameters: [].
[22.01.2016 11:36:47] <253> Info     [Authenticated] Logging on...
[22.01.2016 11:36:47] <253> Error    Login failed.. RequestUri: [http://myip:9399/api/sessionMngr/?v=v1_2].
[22.01.2016 11:36:47] <253> Error    Authorization header is missing (Veeam.Backup.Interaction.RestAPI.CRestAPICommunicationException)
[22.01.2016 11:36:47] <253> Error       at Veeam.Backup.Enterprise.RestAPIService.CRestOperationContext.GetAuthorizationHeader()
[22.01.2016 11:36:47] <253> Error       at Veeam.Backup.Enterprise.RestAPIService.CEnterpriseRestServiceStub.LogInAfterAuthentication(LoginSpecType loginSpec, String version)

ps.I change the actual IP adress into myip in this log entry

any ideas how to add the missing header into the post request?

Thanks

Harold
CTO PeaSoup
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
peasoupcto
Service Provider
 
Posts: 11
Liked: 2 times
Joined: Fri Jan 22, 2016 11:49 am
Location: weybridge
Full Name: Harold Buter

[MERGED] : Missing Auth header

Veeam Logoby peasoupcto » Fri Jan 22, 2016 12:54 pm

Hi Guys,

I'm using the new v9 code from poulpreben github for our cloud connect portal, however I ran into two issues:

- line 45 in veeam.class.php base_url has to be changed into base_uri in order to prevent PHP error 6 - host not resolved (even when using IP only, this has changed in Guzzle client 6+)

- I still have an issue of authentication using the veeam.class.php

error log in apache

Code: Select all
http://172.20.1.148:9399/api/sessionMngr/?v=v1_2` resulted in a `401 Unauthorized` response:\n<?xml version="1.0" encoding="utf-8"?><Error StatusCode="401" Message="Authorization header is missing" xmlns="http://ww (truncated...)\n' in /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107\nStack trace:\n#0 /var/www/html/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\\Exception\\RequestException::create(Object(GuzzleHttp\\Psr7\\Request), Object(GuzzleHttp\\Psr7\\Response))\n#1 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(199): GuzzleHttp\\Middleware::GuzzleHttp\\{closure}(Object(GuzzleHttp\\Psr7\\Response))\n#2 /var/www/html/vendor/guzzlehttp/promises/src/Promise.php(152): GuzzleHttp\\Promise\\Promise::callHandler(1, Object(GuzzleHttp\\Psr7\\Response), Array)\n#3 /var/www/html/vendor/guzzlehttp/promises/src/TaskQueue.php(60): GuzzleHttp\\Promise\\Promise::GuzzleHttp\\Promise\\{closure}()\n#4 /var/ in /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 107, referer: http://172.20.1.139/


error log in veeam api log

Code: Select all
22.01.2016 11:36:47] <253> Info     [POST] request to [http://172.20.1.148:9399/api/sessionMngr/?v=v1_2] deserialized. Message: []; Parameters: [].
[22.01.2016 11:36:47] <253> Info     [Authenticated] Logging on...
[22.01.2016 11:36:47] <253> Error    Login failed.. RequestUri: [http://172.20.1.148:9399/api/sessionMngr/?v=v1_2].
[22.01.2016 11:36:47] <253> Error    Authorization header is missing (Veeam.Backup.Interaction.RestAPI.CRestAPICommunicationException)
[22.01.2016 11:36:47] <253> Error       at Veeam.Backup.Enterprise.RestAPIService.CRestOperationContext.GetAuthorizationHeader()
[22.01.2016 11:36:47] <253> Error       at Veeam.Backup.Enterprise.RestAPIService.CEnterpriseRestServiceStub.LogInAfterAuthentication(LoginSpecType loginSpec, String version)



i think the issue is in this part of the code

Code: Select all
public function __construct($host, $port, $username, $password, $backup, $replication) {
    $this->client = new GuzzleHttp\Client(array(
      "base_uri" => "http://" . $host . ":" . $port . "/api/",
      "defaults" => array(
        "auth" => array(
          $username,
          $password
        ),
        "headers" => array(
          "Content-Type" => "text/xml"
        ),
      )
    ));

    $response = $this->client->post('sessionMngr/?v=v1_2');

$this->backup_create = $backup;
    $this->replication_create = $replication;

    $this->session_id = $response->getHeader('X-RestSvcSessionId');
    $this->client->setDefaultOption('headers', array('X-RestSvcSessionId' => $this->session_id));
   

    $this->tenant_password = $this->veeam_generate_password(12);
  }


any ideas?
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
peasoupcto
Service Provider
 
Posts: 11
Liked: 2 times
Joined: Fri Jan 22, 2016 11:49 am
Location: weybridge
Full Name: Harold Buter

Re: Example RestAPI code

Veeam Logoby poulpreben » Fri Jan 22, 2016 1:49 pm

Hi Harold,

Thanks for your feedback.

Can you try to revert the changes you have made in veeam.class.php and try this instead:

Change GuzzleHTTP back to 4.x in composer.json, remove "vendor" folder and re-run "composer install"?

I just want to verify whether this is due to changes in Guzzle code before I begin troubleshooting.

Thanks,
Preben
poulpreben
Expert
 
Posts: 935
Liked: 398 times
Joined: Mon Jul 23, 2012 8:16 am
Full Name: Preben Berg

Re: Example RestAPI code

Veeam Logoby peasoupcto » Fri Jan 22, 2016 2:01 pm

HI Preben,

After I changed the composer.json and when I removed the vendor folder and did a reinstall of the composer it installs version 6.1.1 again :(

when I run my task I get the error which is due to base_url setting in line 45..

Code: Select all
 PHP Fatal error:  Uncaught exception 'GuzzleHttp\\Exception\\ConnectException' with message 'cURL error 6: Could not resolve host: sessionMngr (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186\nStack trace:\n#0 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(150): GuzzleHttp\\Handler\\CurlFactory::createRejection(Object(GuzzleHttp\\Handler\\EasyHandle), Array)\n#1 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(103): GuzzleHttp\\Handler\\CurlFactory::finishError(Object(GuzzleHttp\\Handler\\CurlHandler), Object(GuzzleHttp\\Handler\\EasyHandle), Object(GuzzleHttp\\Handler\\CurlFactory))\n#2 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\\Handler\\CurlFactory::finish(Object(GuzzleHttp\\Handler\\CurlHandler), Object(GuzzleHttp\\Handler\\EasyHandle), Object(GuzzleHttp\\Handler\\CurlFactory))\n#3 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(28): GuzzleHttp\\Handler\\CurlHandler->__invoke(Objec in /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 186, referer: http://172.20.1.139/


composer install has the following:

Code: Select all
/var/www/html$ sudo composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
  - Installing symfony/process (v3.0.1)
    Loading from cache

  - Installing kriswallsmith/assetic (v1.3.2)
    Loading from cache

  - Installing robloach/component-installer (0.2.3)
    Loading from cache

  - Installing components/jquery (2.1.1)
    Loading from cache

  - Installing psr/http-message (1.0)
    Loading from cache

  - Installing guzzlehttp/psr7 (1.2.1)
    Loading from cache

  - Installing guzzlehttp/promises (1.0.3)
    Loading from cache

  - Installing guzzlehttp/guzzle (6.1.1)
    Loading from cache

  - Installing twbs/bootstrap (v3.3.6)
    Loading from cache


Thanks
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
peasoupcto
Service Provider
 
Posts: 11
Liked: 2 times
Joined: Fri Jan 22, 2016 11:49 am
Location: weybridge
Full Name: Harold Buter

Re: Example RestAPI code

Veeam Logoby poulpreben » Fri Jan 22, 2016 2:04 pm 1 person likes this post

Sorry Harold, please delete the vendor folder and run "composer update", not "install". My bad...
poulpreben
Expert
 
Posts: 935
Liked: 398 times
Joined: Mon Jul 23, 2012 8:16 am
Full Name: Preben Berg

Re: Example RestAPI code

Veeam Logoby peasoupcto » Fri Jan 22, 2016 2:17 pm

Hi Preben,

yeah that worked :)

also when using guzzle 4.* the client is created and no errors are returned
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
peasoupcto
Service Provider
 
Posts: 11
Liked: 2 times
Joined: Fri Jan 22, 2016 11:49 am
Location: weybridge
Full Name: Harold Buter

Re: Example RestAPI code

Veeam Logoby poulpreben » Fri Jan 22, 2016 4:12 pm

I will fire up my dev environment and do some further testing with Guzzle 6. Apparently that last commit was past my bedtime :)

Thank you for your feedback, and please let us know how you manage to implement this in your company.

- Preben
poulpreben
Expert
 
Posts: 935
Liked: 398 times
Joined: Mon Jul 23, 2012 8:16 am
Full Name: Preben Berg

Re: Example RestAPI code

Veeam Logoby peasoupcto » Mon Jan 25, 2016 9:42 am

Brilliant,

I've tried a few things but didn't get the header inside the post request.. If you need any testing please let me know as I got a test environment here and am more than happy to work together on this . (PM me if needed mate)

We are busy to create a trial service for our customers, one is instant trial portal from our website and secondly to create a portal for our sales team so than can create new customers quickly.
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
peasoupcto
Service Provider
 
Posts: 11
Liked: 2 times
Joined: Fri Jan 22, 2016 11:49 am
Location: weybridge
Full Name: Harold Buter

Next

Return to RESTful API



Who is online

Users browsing this forum: No registered users and 2 guests