使用 web-uri 在 gcp 计算引擎上启动虚拟机
Starting virtual machine on gcp compute engine with web-uri
我在 CE 上设置了一个虚拟机。我想通过 API 使用带有 HTTP 请求的 instances().stop 和 instances().start 方法启动和关闭此 VM。
使用 API 资源管理器 (https://cloud.google.com/compute/docs/reference/rest/v1/instances/start) 并输入项目名称、区域和实例名称一切正常,我可以启动和停止虚拟机。我被转发到 google 登录 -> 我授权 --> 它有效。
但是,当我尝试通过浏览器中提供的 html 执行此操作时:https://www.googleapis.com/compute/v1/projects/{my_projekt}/zones/{my_zone}/instances/ {my_instance}/start”,它不起作用。错误:未找到。我认为缺少某种授权,所以我也尝试添加 ?key={my_key}。
在文档中我发现:
需要以下 OAuth 范围之一:
https://www.googleapis.com/auth/compute
https://www.googleapis.com/auth/cloud-platform
但我不知道如何设置。有人可以帮我吗?我想做的事情有可能吗?
在下一步中,我想允许其他人通过授予他们 IAM 角色来启动和停止此虚拟机。他们也可以使用 http post 请求吗?
我在使用 GCP 方面还很陌生,自动化过程让我很头疼...
提前致谢。
问候,
奥利
好的,您有 3 种方法可以实现此目的,它们在 here 中有详细记录,我将从最简单到最难的列出它们:
A.- Google 云控制台。
B.- Google Cloud SDK CLI 工具 "gcloud".
C.- Google 云 HTTP API 调用。
帐户执行实例 Stop/start 所需的权限为:
compute.instances.stop
compute.instances.start
重置:
compute.instances.reset
具有这些权限的角色是 "compute.instanceAdmin",但是您始终可以创建具有所需权限的客户角色。
A.- Google 云控制台
是对用户最友好的方式,因为它使用 GUI。转到 Compute Engine Instances 上的 Cloud Console。如果您没有在列表中看到您的实例,请确保您选择了正确的项目。
点击您想要stop/start的实例,然后根据您想要执行的操作点击上面的按钮。
B.- Google Cloud SDK CLI 工具 "gcloud"
Install the CLI tool "gcloud", authenticate 使用:
gcloud auth login [ACCOUNT]
gcloud compute instances stop example-instance-1 example-instance-2
gcloud compute instances start example-instance
gcloud compute instances reset example-instance
C.- Google 云 HTTP API 调用
这是您当前尝试使用的方法,您必须向 Google 云 API 发出 HTTP 请求:Start , Stop , Reset。
您需要将 "access token" 添加到请求 header 的 "authentication" 字段中。使用 "Authorization: bearer here-your-long-token"。有关它的更多信息 here。
如何获得 "access token" 可能会因您使用的语言而异,这里是 javascript 中的示例:
var {google} = require("googleapis");
// Load the service account key JSON file.
var serviceAccount = require("path/to/serviceAccountKey.json");
// Define the required scopes.
var scopes = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute"
];
// Authenticate a JWT client with the service account.
var jwtClient = new google.auth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
scopes
);
// Use the JWT client to generate an access token.
jwtClient.authorize(function(error, tokens) {
if (error) {
console.log("Error making request to generate access token:", error);
} else if (tokens.access_token === null) {
console.log("Provided service account does not have permission to generate access tokens");
} else {
var accessToken = tokens.access_token;
// here you have the token, you can use it on your API request.
}
});
我在 CE 上设置了一个虚拟机。我想通过 API 使用带有 HTTP 请求的 instances().stop 和 instances().start 方法启动和关闭此 VM。
使用 API 资源管理器 (https://cloud.google.com/compute/docs/reference/rest/v1/instances/start) 并输入项目名称、区域和实例名称一切正常,我可以启动和停止虚拟机。我被转发到 google 登录 -> 我授权 --> 它有效。
但是,当我尝试通过浏览器中提供的 html 执行此操作时:https://www.googleapis.com/compute/v1/projects/{my_projekt}/zones/{my_zone}/instances/ {my_instance}/start”,它不起作用。错误:未找到。我认为缺少某种授权,所以我也尝试添加 ?key={my_key}。
在文档中我发现: 需要以下 OAuth 范围之一: https://www.googleapis.com/auth/compute https://www.googleapis.com/auth/cloud-platform
但我不知道如何设置。有人可以帮我吗?我想做的事情有可能吗?
在下一步中,我想允许其他人通过授予他们 IAM 角色来启动和停止此虚拟机。他们也可以使用 http post 请求吗?
我在使用 GCP 方面还很陌生,自动化过程让我很头疼...
提前致谢。 问候, 奥利
好的,您有 3 种方法可以实现此目的,它们在 here 中有详细记录,我将从最简单到最难的列出它们:
A.- Google 云控制台。
B.- Google Cloud SDK CLI 工具 "gcloud".
C.- Google 云 HTTP API 调用。
帐户执行实例 Stop/start 所需的权限为:
compute.instances.stop
compute.instances.start
重置:
compute.instances.reset
具有这些权限的角色是 "compute.instanceAdmin",但是您始终可以创建具有所需权限的客户角色。
A.- Google 云控制台
是对用户最友好的方式,因为它使用 GUI。转到 Compute Engine Instances 上的 Cloud Console。如果您没有在列表中看到您的实例,请确保您选择了正确的项目。
点击您想要stop/start的实例,然后根据您想要执行的操作点击上面的按钮。
B.- Google Cloud SDK CLI 工具 "gcloud"
Install the CLI tool "gcloud", authenticate 使用:
gcloud auth login [ACCOUNT]
gcloud compute instances stop example-instance-1 example-instance-2
gcloud compute instances start example-instance
gcloud compute instances reset example-instance
C.- Google 云 HTTP API 调用
这是您当前尝试使用的方法,您必须向 Google 云 API 发出 HTTP 请求:Start , Stop , Reset。
您需要将 "access token" 添加到请求 header 的 "authentication" 字段中。使用 "Authorization: bearer here-your-long-token"。有关它的更多信息 here。
如何获得 "access token" 可能会因您使用的语言而异,这里是 javascript 中的示例:
var {google} = require("googleapis");
// Load the service account key JSON file.
var serviceAccount = require("path/to/serviceAccountKey.json");
// Define the required scopes.
var scopes = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute"
];
// Authenticate a JWT client with the service account.
var jwtClient = new google.auth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
scopes
);
// Use the JWT client to generate an access token.
jwtClient.authorize(function(error, tokens) {
if (error) {
console.log("Error making request to generate access token:", error);
} else if (tokens.access_token === null) {
console.log("Provided service account does not have permission to generate access tokens");
} else {
var accessToken = tokens.access_token;
// here you have the token, you can use it on your API request.
}
});