【问题标题】:PHP Google Api Client SQL Admin not enable Auth IPPHP Google Api Client SQL Admin 未启用 Auth IP
【发布时间】:2018-07-05 13:11:51
【问题描述】:

我有更新我的谷歌 MySQL 实例授权 IP 的代码,连接正常,代码打印我当前的 IP,但它无法将新 IP 添加到设置中,我尝试了很多方法,但它仍然无法正常工作。 t 对实例配置进行任何更改。

$client = new Google_Client();
    $client->setAuthConfig('../config/service-account.json');
    $client->setApplicationName(env("APP_NAME"));
    $projectName = env("GOOGLE_PROJECT_NAME");
    $instanceName = env("SQL_INSTANCE_NAME");
    $scopes = [
        "https://www.googleapis.com/auth/sqlservice.admin",
        "https://www.googleapis.com/auth/compute",
    ];
    $client->addScope($scopes);
    $sql = new Google_Service_SQLAdmin($client);
    $sqlAdmin = new Google_Service_SQLAdmin_Settings($client);
    $instanceSettings = $sql->instances->get($projectName, $instanceName)->getSettings();
    $authNetworks = $instanceSettings->getIpConfiguration();
    $newAuthNetwork = new Google_Service_SQLAdmin_AclEntry($client);
    $newAuthNetwork->setName("tmp_ip_connection");
    $newAuthNetwork->setKind("sql#aclEntry");
    $authNetworks->setAuthorizedNetworks($newAuthNetwork);
    $ipv4 = file_get_contents('https://api.ipify.org');
    $newAuthNetwork->setValue($ipv4);
    $ipConfiguration = new Google_Service_SQLAdmin_IpConfiguration($client);
    $ipConfiguration->setIpv4Enabled(true);
    $ipConfiguration->setAuthorizedNetworks([$newAuthNetwork]);
    $instanceSettings->setIpConfiguration($ipConfiguration);
    $sql->instances->get($projectName, $instanceName)->setSettings($instanceSettings);
    //TODO why it is not working??
    print_r($sql->instances->get($projectName, $instanceName)->getSettings()->getIpConfiguration()->getAuthorizedNetworks());

【问题讨论】:

    标签: php mysql google-api google-cloud-platform google-api-php-client


    【解决方案1】:

    您缺少的主要内容是您在进行更改后从未更新过实例。

    工作示例:

    $client = new Google_Client();
    $client->setAuthConfig('../config/service-account.json');
    $client->setApplicationName(env("APP_NAME"));
    $projectName = env("GOOGLE_PROJECT_NAME");
    $instanceName = env("SQL_INSTANCE_NAME");
    $scopes = [
        "https://www.googleapis.com/auth/sqlservice.admin",
        "https://www.googleapis.com/auth/compute",
    ];
    $client->addScope($scopes);
    $sql = new Google_Service_SQLAdmin($client);
    
    $instances = $sql->instances;
    $instance = $instances->get('projectId', 'instanceId');
    $networks = $instance->getSettings()->getIpConfiguration()->getAuthorizedNetworks();
    
    $values = [];
    foreach ($networks as $network) {
        $values[$network->getName()] = $network;
    }
    
    $values['1.production'] = array_get($values, '1.production', clone head($values));
    $external_ip = @file_get_contents('http://ipecho.net/plain');
    $values['1.production']->setValue("$external_ip/32");
    $values['1.production']->setName('1.production');
    
    $instance->getSettings()->getIpConfiguration()->setAuthorizedNetworks(array_values($values));
    
    $instances->update('projectId', 'instanceId', $instance);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2017-07-30
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 2013-07-25
      相关资源
      最近更新 更多