본문 바로가기

Hadoop Ecosystem

cloud : kt ucloud vm - openAPI test 01

kt ucloud vm - openAPI test  -- php version

https://ucloudbiz.olleh.com/ ]


*************************************************************************************

           ucloudbiz r개발자센터 Open API     http://developer.ucloudbiz.olleh.com

*************************************************************************************

api key45xxxxxxxxxxxxxxxxx your api key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyQ

secret key97xxxxxxxxxxxxxxx your secret key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7Q


prerequisite : 

   kt ucloud account

   check your both api key and secret key, they are required to go proceed

   vmware server                  


my test environment : 

    os : centos 

    2 vm [ vm-1 : free pack , vm-2 : 1 core, 1g ram, 100 disk ] 



1. yum install php     # required php

2. vi callAPI.php       # auth key generation

3. vi testAPI.php       # put your api key and secret key and specify command you want

3.  ./testAPI.php      # execute


* Java sample source is also available to do same thing.



[root@f6706ff4 api]# ./testAPI.php 


Array

(

    [virtualmachine] => Array

        (

            [0] => Array

                (

                    [id] => a359ab40-497d-4b49-84ec-14bf56dee4c5

                    [name] => a359ab40-497d-4b49-84ec-14bf56dee4c5

                    [displayname] => j_ucloud

                    [account] => EPC_M149409_S1820

                    [domainid] => 849e8028-dafc-4ad6-a649-800221fdf834

                    [domain] => EPC_USER

                    [created] => 2013-07-10T12:34:40+0900

                    [state] => Running

                    [haenable] => true

                    [zoneid] => eceb5d65-6571-4696-875f-5a17949f3317

                    [zonename] => kr-1

                    [templateid] => dcc1a0b3-9f47-49af-ab66-a41c6825801c

                    [templatename] => CentOS 5.4 64bit kernel 0105

                    [templatedisplaytext] => CentOS 5.4 64bit

                    [passwordenabled] => true

                    [serviceofferingid] => 647ea205-6bc3-435d-aaf5-15f6c6356d19

                    [serviceofferingname] => micro instance 2

                    [cpunumber] => 1

                    [cpuspeed] => 1000

                    [memory] => 512

                    [cpuused] => 0.09%

                    [networkkbsread] => 201

                    [networkkbswrite] => 8

                    [guestosid] => df724ef6-9f43-4214-b3af-dd5af75c266c

                    [rootdeviceid] => 0

                    [rootdevicetype] => IscsiLUN

                    [nic] => Array

                        (

                            [id] => b5042122-8694-4e35-bc0f-33e06b249e85

                            [networkid] => 92f04e5e-3aeb-4dae-b9e3-d4b426d92724

                            [netmask] => 255.255.0.0

                            [gateway] => 172.27.0.1

                            [ipaddress] => 172.27.37.130

                            [traffictype] => Guest

                            [type] => Isolated

                            [isdefault] => true

                            [macaddress] => 02:00:03:ad:00:01

                        )


                    [hypervisor] => XenServer

                )


            [1] => Array

                (

                    [id] => f6706ff4-5992-4f1a-97e1-d8e593f59228

                    [name] => f6706ff4-5992-4f1a-97e1-d8e593f59228

                    [displayname] => h001

                    [account] => EPC_M149409_S1820

                    [domainid] => 849e8028-dafc-4ad6-a649-800221fdf834

                    [domain] => EPC_USER

                    [created] => 2013-07-10T14:27:21+0900

                    [state] => Running

                    [haenable] => true

                    [zoneid] => eceb5d65-6571-4696-875f-5a17949f3317

                    [zonename] => kr-1

                    [templateid] => c2e4c0f2-2bc7-444c-a280-59f5b42dbf11

                    [templatename] => CentOS_6.3_64bit(20120927)

                    [templatedisplaytext] => CentOS 6.3 64bit

                    [passwordenabled] => true

                    [serviceofferingid] => c504e367-20d6-47c6-a82c-183b12d357f2

                    [serviceofferingname] => tier2 1core 1GB Instance. Virtual Networking

                    [cpunumber] => 1

                    [cpuspeed] => 2000

                    [memory] => 1024

                    [cpuused] => 0.15%

                    [networkkbsread] => 933

                    [networkkbswrite] => 577

                    [guestosid] => 9d889194-9ec6-4902-92fd-ba3d3d69ff8f

                    [rootdeviceid] => 0

                    [rootdevicetype] => NetworkFilesystem

                    [nic] => Array

                        (

                            [id] => bf36550c-fcaa-4e17-816c-7c7cad8d8249

                            [networkid] => 92f04e5e-3aeb-4dae-b9e3-d4b426d92724

                            [netmask] => 255.255.0.0

                            [gateway] => 172.27.0.1

                            [ipaddress] => 172.27.37.231

                            [traffictype] => Guest

                            [type] => Isolated

                            [isdefault] => true

                            [macaddress] => 02:00:1b:26:00:05

                        )


                    [hypervisor] => XenServer

                )


        )


    [count] => 2

)



[root@f6706ff4 api]# vi testAPI.php 

#!/usr/bin/php -q


<?php

include './callAPI.php';

$URL = "https://api.ucloudbiz.olleh.com/server/v1/client/api?";

$APIKEY = "45xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyQ";

$SECRET = "97xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7Q";

$cmdArr = array(

                 "command" => "listVirtualMachines",

                 "apikey"  => $APIKEY

                );

print_r(callCommand($URL, $cmdArr, $SECRET));


?>



[root@f6706ff4 api]#vi callAPI.php 


<?php 

function objectsIntoArray($arrObjData, $arrSkipIndices = array()) 

    $arrData = array(); 


    if (is_object($arrObjData)) 

        $arrObjData = get_object_vars($arrObjData); 


    if (is_array($arrObjData)) 

        { 

        foreach ($arrObjData as $index => $value) 

                { 

            if (is_object($value) || is_array($value)) 

                $value = objectsIntoArray($value, $arrSkipIndices); 

            if (in_array($index, $arrSkipIndices)) 

                continue; 

            $arrData[$index] = $value; 

        } 

    } 

    return $arrData; 



function callCommand($URL, $cmdArr, $SECRET) 

        $fArray = array_keys($cmdArr); 

        $vArray = array_values($cmdArr); 

        $f = array(); $v = array(); $cmd = array(); $cmd1 = array(); 

    

for ( $i = 0; $i < count($cmdArr); $i++ ) 

        { 

        $f[$i] = strtolower(urlencode($fArray[$i])); 

        $v[$i] = strtolower(urlencode($vArray[$i])); 

        array_push($cmd, $f[$i]."=".$v[$i]); 

        } 

  

sort($cmd); 


        for ( $i = 0; $i < count($cmdArr); $i++ ) 

                array_push($cmd1, $fArray[$i]."=".$vArray[$i]); 

        sort($cmd1); 


        $cmdStr = ""; 

     

for ( $i = 0; $i < count($cmd); $i++) 

        { 

        if ( $i == count($cmd) - 1 ) 

                $cmdStr = $cmdStr . $cmd[$i]; 

        else 

                $cmdStr = $cmdStr . $cmd[$i] . "&"; 

        } 


        $signature = urlencode(base64_encode(hash_hmac("sha1", $cmdStr, $SECRET, true))); 

        $url = $URL; 


        for ( $i = 0; $i < count($cmd1); $i++) 

                $url = $url . $cmd1[$i] . "&"; 


        $xmlUrl = $url . "signature=" . $signature; 

        $orig_error_reporting = error_reporting(); 

        error_reporting(0); 

        $arrXml =  objectsIntoArray(simplexml_load_string(file_get_contents($xmlUrl))); 


        error_reporting($orig_error_reporting); 


        print_r($arrXml); 

?>