更改更新检查。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
public static object UpdateUptimeCheck(string configName,
string newHttpPath, string newDisplayName)
{
var client = UptimeCheckServiceClient.Create();
var config = client.GetUptimeCheckConfig(configName);
var fieldMask = new FieldMask();
if (newDisplayName != null)
{
config.DisplayName = newDisplayName;
fieldMask.Paths.Add("display_name");
}
if (newHttpPath != null)
{
config.HttpCheck.Path = newHttpPath;
fieldMask.Paths.Add("http_check.path");
}
client.UpdateUptimeCheckConfig(config);
return 0;
}
Go
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
// update is an example of updating an uptime check. resourceName should be
// of the form `projects/[PROJECT_ID]/uptimeCheckConfigs/[UPTIME_CHECK_ID]`.
func update(w io.Writer, resourceName, displayName, httpCheckPath string) (*monitoringpb.UptimeCheckConfig, error) {
ctx := context.Background()
client, err := monitoring.NewUptimeCheckClient(ctx)
if err != nil {
return nil, fmt.Errorf("NewUptimeCheckClient: %w", err)
}
defer client.Close()
getReq := &monitoringpb.GetUptimeCheckConfigRequest{
Name: resourceName,
}
config, err := client.GetUptimeCheckConfig(ctx, getReq)
if err != nil {
return nil, fmt.Errorf("GetUptimeCheckConfig: %w", err)
}
config.DisplayName = displayName
config.GetHttpCheck().Path = httpCheckPath
req := &monitoringpb.UpdateUptimeCheckConfigRequest{
UpdateMask: &field_mask.FieldMask{
Paths: []string{"display_name", "http_check.path"},
},
UptimeCheckConfig: config,
}
config, err = client.UpdateUptimeCheckConfig(ctx, req)
if err != nil {
return nil, fmt.Errorf("UpdateUptimeCheckConfig: %w", err)
}
fmt.Fprintf(w, "Successfully updated %v", resourceName)
return config, nil
}
Java
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
private static void updateUptimeCheck(String checkName, String hostName, String pathName)
throws IOException {
UpdateUptimeCheckConfigRequest request =
UpdateUptimeCheckConfigRequest.newBuilder()
.setUpdateMask(FieldMask.newBuilder().addPaths("http_check.path"))
.setUptimeCheckConfig(
UptimeCheckConfig.newBuilder()
.setName(checkName)
.setMonitoredResource(
MonitoredResource.newBuilder()
.setType("uptime_url")
.putLabels("host", hostName))
.setHttpCheck(HttpCheck.newBuilder().setPath(pathName).setPort(80))
.setTimeout(Duration.newBuilder().setSeconds(10))
.setPeriod(Duration.newBuilder().setSeconds(300)))
.build();
try (UptimeCheckServiceClient client = UptimeCheckServiceClient.create()) {
UptimeCheckConfig config = client.updateUptimeCheckConfig(request);
System.out.println("Uptime check updated: \n" + config.toString());
} catch (Exception e) {
usage("Exception updating uptime check: " + e.toString());
throw e;
}
}
Node.js
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
// const displayName = 'A New Display Name';
// const path = '/some/path';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Updating ${request.name} to ${displayName}`);
// Updates the display name and path on an uptime check config
request.uptimeCheckConfig = {
name: request.name,
displayName: displayName,
httpCheck: {
path: path,
},
};
request.updateMask = {
paths: ['display_name', 'http_check.path'],
};
const [response] = await client.updateUptimeCheckConfig(request);
console.log(`${response.name} config updated.`);
PHP
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
use Google\Cloud\Monitoring\V3\Client\UptimeCheckServiceClient;
use Google\Cloud\Monitoring\V3\GetUptimeCheckConfigRequest;
use Google\Cloud\Monitoring\V3\UpdateUptimeCheckConfigRequest;
use Google\Protobuf\FieldMask;
/**
* Example:
* ```
* update_uptime_checks($projectId);
* ```
*/
function update_uptime_checks(
string $projectId,
string $configName,
string $newDisplayName = null,
string $newHttpCheckPath = null
): void {
$uptimeCheckClient = new UptimeCheckServiceClient([
'projectId' => $projectId,
]);
$getUptimeCheckConfigRequest = (new GetUptimeCheckConfigRequest())
->setName($configName);
$uptimeCheck = $uptimeCheckClient->getUptimeCheckConfig($getUptimeCheckConfigRequest);
$fieldMask = new FieldMask();
if ($newDisplayName) {
$fieldMask->getPaths()[] = 'display_name';
$uptimeCheck->setDisplayName($newDisplayName);
}
if ($newHttpCheckPath) {
$paths = $fieldMask->getPaths()[] = 'http_check.path';
$uptimeCheck->getHttpCheck()->setPath($newHttpCheckPath);
}
$updateUptimeCheckConfigRequest = (new UpdateUptimeCheckConfigRequest())
->setUptimeCheckConfig($uptimeCheck)
->setUpdateMask($fieldMask);
$uptimeCheckClient->updateUptimeCheckConfig($updateUptimeCheckConfigRequest);
print($uptimeCheck->serializeToString() . PHP_EOL);
}
Python
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
def update_uptime_check_config(
config_name: str, new_display_name: str = None, new_http_check_path: str = None
) -> uptime.UptimeCheckConfig:
"""Creates a new uptime check configuration
Args:
config_name: Uptime check configuration identity
new_display_name: A new human friendly name of the configuration
new_http_check_path: A new HTTP endpoint of the configuration
Returns:
A structure that describes the updated uptime check
"""
client = monitoring_v3.UptimeCheckServiceClient()
config = client.get_uptime_check_config(request={"name": config_name})
field_mask = field_mask_pb2.FieldMask()
if new_display_name:
field_mask.paths.append("display_name")
config.display_name = new_display_name
if new_http_check_path:
field_mask.paths.append("http_check.path")
config.http_check.path = new_http_check_path
changed_config = client.update_uptime_check_config(
request={"uptime_check_config": config, "update_mask": field_mask}
)
pprint.pprint(changed_config)
return changed_config
Ruby
如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
def update_uptime_check_config config_name: nil,
new_display_name: nil,
new_http_check_path: nil
require "google/cloud/monitoring"
client = Google::Cloud::Monitoring.uptime_check_service
config = { name: config_name }
field_mask = { paths: [] }
unless new_display_name.to_s.empty?
field_mask[:paths].push "display_name"
config[:display_name] = new_display_name
end
unless new_http_check_path.to_s.empty?
field_mask[:paths].push "http_check.path"
config[:http_check] = { path: new_http_check_path }
end
client.update_uptime_check_config uptime_check_config: config,
update_mask: field_mask
end
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。