Scalr\Modules\Platforms\GoogleCE\GoogleCEPlatformModule::getClient PHP Method

getClient() public method

public getClient ( Scalr_Environment $environment = null, array $config = [] ) : Google_Service_Compute
$environment Scalr_Environment Scalr Environment object
$config array optional Config array
return Google_Service_Compute
    public function getClient(\Scalr_Environment $environment = null, array $config = [])
    {
        $ccProps = null;
        if (empty($config)) {
            $ccProps = $environment->keychain(\SERVER_PLATFORMS::GCE)->properties;
            $config = $ccProps;
        }
        $client = new \Google_Client();
        $client->setApplicationName("Scalr GCE");
        $client->setScopes(array('https://www.googleapis.com/auth/compute'));
        $key = base64_decode($config[Entity\CloudCredentialsProperty::GCE_KEY]);
        // If it's not a json key we need to convert PKCS12 to PEM
        if (!$config[Entity\CloudCredentialsProperty::GCE_JSON_KEY]) {
            @openssl_pkcs12_read($key, $certs, 'notasecret');
            $key = $certs['pkey'];
        }
        $client->setAuthConfig(['type' => 'service_account', 'project_id' => $config[Entity\CloudCredentialsProperty::GCE_PROJECT_ID], 'private_key' => $key, 'client_email' => $config[Entity\CloudCredentialsProperty::GCE_SERVICE_ACCOUNT_NAME], 'client_id' => $config[Entity\CloudCredentialsProperty::GCE_CLIENT_ID]]);
        $client->setClientId($config[Entity\CloudCredentialsProperty::GCE_CLIENT_ID]);
        $gce = new \Google_Service_Compute($client);
        //**** Store access token ****//
        $jsonAccessToken = $config[Entity\CloudCredentialsProperty::GCE_ACCESS_TOKEN];
        $accessToken = @json_decode($jsonAccessToken);
        if ($accessToken && $accessToken->created + $accessToken->expires_in > time()) {
            $client->setAccessToken($jsonAccessToken);
        } else {
            $gce->zones->listZones($config[Entity\CloudCredentialsProperty::GCE_PROJECT_ID]);
            if ($ccProps) {
                $token = $client->getAccessToken();
                $ccProps[Entity\CloudCredentialsProperty::GCE_ACCESS_TOKEN] = $token;
                $ccProps->save();
            }
        }
        return $gce;
    }