RESTful knowledge exchange
Post Reply
headcloudmonkey
Influencer
Posts: 13
Liked: 3 times
Joined: Jul 09, 2015 9:14 am
Full Name: Dan Gould
Location: Brighton, East Sussex, UK
Contact:

Example RestAPI code

Post by headcloudmonkey »

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
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Example RestAPI code

Post by veremin »

There is a small example written by our Solutions Architect Preben Berg. I believe it should be a good starting point. Thanks.
headcloudmonkey
Influencer
Posts: 13
Liked: 3 times
Joined: Jul 09, 2015 9:14 am
Full Name: Dan Gould
Location: Brighton, East Sussex, UK
Contact:

Re: Example RestAPI code

Post by headcloudmonkey »

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
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

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
headcloudmonkey
Influencer
Posts: 13
Liked: 3 times
Joined: Jul 09, 2015 9:14 am
Full Name: Dan Gould
Location: Brighton, East Sussex, UK
Contact:

Re: Example RestAPI code

Post by headcloudmonkey »

Hi Preben,

This is on guzzlehttp 4.2.3 and php 5.4.16

Thanks
Dan
Daniel Gould
CTO
w: www.stratogen.net
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

Thank you. I will verify if something else is required.
headcloudmonkey
Influencer
Posts: 13
Liked: 3 times
Joined: Jul 09, 2015 9:14 am
Full Name: Dan Gould
Location: Brighton, East Sussex, UK
Contact:

Re: Example RestAPI code

Post by headcloudmonkey »

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
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto »

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
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

[MERGED] : Missing Auth header

Post by peasoupcto »

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
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

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
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto »

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
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben » 1 person likes this post

Sorry Harold, please delete the vendor folder and run "composer update", not "install". My bad...
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto »

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
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

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
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto »

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
headcloudmonkey
Influencer
Posts: 13
Liked: 3 times
Joined: Jul 09, 2015 9:14 am
Full Name: Dan Gould
Location: Brighton, East Sussex, UK
Contact:

Re: Example RestAPI code

Post by headcloudmonkey »

Hi Harold/Preben,

How are you getting on with the API code for Veeam v9? Did you manage to get it working correctly? I'm getting a cURL error 7 Failed to connect Permission denied error when I try with the modifications above (Guzzle v4, and base_url).

Thanks
Dan
Daniel Gould
CTO
w: www.stratogen.net
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

Hi Dan,

Sorry, I have been on the road for customer engagements for the past couple of days.

I'm not sure why that is not working for you. You may just revert to the last version I have tested extensively. Remove all the files, and use this:

Code: Select all

git clone https://github.com/poulpreben/veeam-cloudconnect.git
cd veeam-cloudconnect
git checkout d1dc554b5525e5b5ba383a2659e3659a3be02eea
That's using the commit I made on GuzzleHTTP 4, which definitely worked.
headcloudmonkey
Influencer
Posts: 13
Liked: 3 times
Joined: Jul 09, 2015 9:14 am
Full Name: Dan Gould
Location: Brighton, East Sussex, UK
Contact:

Re: Example RestAPI code

Post by headcloudmonkey » 1 person likes this post

All fixed - I forgot about disbaling selinux protection in my linux VM running the code!
Daniel Gould
CTO
w: www.stratogen.net
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

Hi all. I have had to revert to using GuzzleHTTP 5.x, as 6.x implements PSR-7, which would require me to completely refactor my code.

I have not tested which SELinux settings are required, but here is my configuration:

Code: Select all

$ vagrant ssh -c "uname -a; php --version; apachectl -v"
Linux restful-demo 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
PHP 5.5.9-1ubuntu4.14 (cli) (built: Oct 28 2015 01:34:46)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Oct 14 2015 14:20:21
Thanks for your feedback!
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto »

Thanks Preben,

I'm using guzzle 4.x currently which is working good for me so far. any benefits of using 5.x?

my config is:

Code: Select all

Linux vm-dc01-www02 4.2.0-25-generic #30-Ubuntu SMP Mon Jan 18 12:31:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
PHP 5.6.11-1ubuntu3.1 (cli)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
Server version: Apache/2.4.12 (Ubuntu)
Server built:   Jul 24 2015 15:59:00
Do you have any tips on username check? i.e. check if user exist in API and return to form if it does .. Am breaking my head around that part

Thanks
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

Hi Harold,

The only reason I changed to 5.x is because it is actively maintanined, whereas 4.x is end of life. For the example specifically, I do not see any differences.

We are all at the annual sales kickoff this week, so I do not have access to my development environment, but I can definitely write such a check for you once I get back.

Thanks,
Preben
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto »

Hi Preben,

that would make sense, I will upgrade to version 5.x a bit later as I still develop some other parts at the moment . Any help with the check would be very much appreciated, programming using the API is relative new to me

Thanks again for your help mate

and have fun at the kick off :)

cheers,
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
poulpreben
Certified Trainer
Posts: 1024
Liked: 448 times
Joined: Jul 23, 2012 8:16 am
Full Name: Preben Berg
Contact:

Re: Example RestAPI code

Post by poulpreben »

Hi Harold,

I have added the veeam_check_username() method here > https://github.com/poulpreben/veeam-clo ... s.php#L152

Thanks,
Preben
peasoupcto
Influencer
Posts: 11
Liked: 3 times
Joined: Jan 22, 2016 11:49 am
Full Name: Harold Buter
Location: weybridge
Contact:

Re: Example RestAPI code

Post by peasoupcto » 1 person likes this post

Brilliant mate,

many thanks for that! will incorporate that into the our site now :)

cheers,
Harold Buter
CTO PeaSoup Hosting
w: http://peasoup.net
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests