1. 程式人生 > >GCP-php(google雲)呼叫示例

GCP-php(google雲)呼叫示例

記錄一下GCP呼叫

首先需要

composer require google/apiclient:^2.0
然後直接上php程式碼
namespace App\Services;
use Google_Client;
use Google_Service_Compute_Instance;
use Google_Service_Compute;
use Google_Service_Compute_AttachedDisk;
use Google_Service_Compute_AttachedDiskInitializeParams;
use Google_Service_Compute_NetworkInterface;
use Google_Service_Compute_AccessConfig;

class GoogleService
{
    /**
     * @var Google_Client
     */
    private $client;

    /**
     * 填自己的projectID
     */
    private $project='xxxx-184506';

    public function __construct(){
        //以下主要是驗證與授權,具體查詢gcp文件
        $client = new Google_Client();
        //填json格式證書路徑,具體查詢gcp文件
        $path='xxxx-76d3a96430ed.json';
        putenv('GOOGLE_APPLICATION_CREDENTIALS='.$path);
        $client->useApplicationDefaultCredentials();
        $client->addScope('https://www.googleapis.com/auth/cloud-platform');

        $this->client=$client;
    }


    /**
     * 建立例項
     * @param string 例項名字首
     * @param string zoneID 具體查gcp
     */
    public function createInstance($instanceNamePrefix,$typeZoneId){

        $service = new Google_Service_Compute($this->client);

        $requestBody = new Google_Service_Compute_Instance();
        if(isDevelopment()){
            //設定例項名,生產環境與開發環境名稱不同,便於分辨
            $requestBody->setName($instanceNamePrefix."-dev-".date('YmdHis',time()).random_int(1000,9999));
        }else{
            //設定例項名
            $requestBody->setName($instanceNamePrefix."-prod-".date('YmdHis',time()).random_int(1000,9999));
        }
        //設定例項配置,這裡用了最便宜的配置
        $requestBody->setMachineType("zones/{$typeZoneId}/machineTypes/f1-micro");

        $diskInitializeParams=new Google_Service_Compute_AttachedDiskInitializeParams();
        //設定映象,生產環境與開發環境環境不同,從config從獲取
        $diskInitializeParams->setSourceImage(config('app.gcp_source_image'));
        //設定硬碟型別
        $diskInitializeParams->setDiskType("zones/{$typeZoneId}/diskTypes/pd-standard");
        //設定硬碟大小
        $diskInitializeParams->setDiskSizeGb(10);

        $disk=new Google_Service_Compute_AttachedDisk();
        $disk->setInitializeParams($diskInitializeParams);
        //設定為啟動盤
        $disk->setBoot(true);
        //設定刪除例項時自動刪除硬碟
        $disk->setAutoDelete(true);

        $requestBody->setDisks([$disk]);


        //以下的網路配置都是預設設定
        $accessConfig=new Google_Service_Compute_AccessConfig();
        $accessConfig->setName('External NAT');
        $accessConfig->setType('ONE_TO_ONE_NAT');

        $networkInterface=new Google_Service_Compute_NetworkInterface();
        $networkInterface->setNetwork('global/networks/default');
        $networkInterface->setAccessConfigs([$accessConfig]);

        $requestBody->setNetworkInterfaces([$networkInterface]);


        $service->instances->insert($this->project, $typeZoneId, $requestBody);
    }

    /**
     * 銷燬例項
     */
    public function destroyInstance($zoneId,$instanceName){
        $service = new Google_Service_Compute($this->client);

        $service->instances->delete($this->project, $zoneId, $instanceName);

    }

    /**
     * 重啟例項
     */
    public function reset($zoneId,$instanceName){
        $service = new Google_Service_Compute($this->client);

        $service->instances->reset($this->project, $zoneId,$instanceName);
    }

}