This section provides an overview of what google-cloud-platform is, and why a developer might want to use it.
It should also mention any large subjects within google-cloud-platform, and link out to the related topics. Since the Documentation for google-cloud-platform is new, you may need to create initial versions of those related topics.
Putting it all together...
You should have two files. The first file is either called packages.config
or project.json
.
Let's name the second file Program.cs
. All the code that was included in the sections above may be pasted into a single main method:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.CloudResourceManager.v1;
using Google.Apis.Services;
using Data = Google.Apis.CloudResourceManager.v1.Data;
namespace OurFirstProject
{
public class Program
{
private const string projectId = [YOUR-PROJECT-ID];
private const string applicationName = "Test";
public static void Main(string[] args)
{
var scopes = new String[] {
CloudResourceManagerService.Scope.CloudPlatform
};
GoogleCredential credential = Task.Run(
() => GoogleCredential.GetApplicationDefaultAsync()
).Result;
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped(scopes);
}
CloudResourceManagerService service = new CloudResourceManagerService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName
}
);
Console.WriteLine("1. Create Project");
Data.Operation operation1 = service.Projects.Create(
new Data.Project()
{
ProjectId = projectId,
}
).Execute();
Console.Write("2. Awaiting Operation Completion");
Data.Operation operation2;
do
{
operation2 = service.Operations.Get(operation1.Name).Execute();
Console.WriteLine(operation2.Done.ToString());
System.Threading.Thread.Sleep(1000);
} while (operation2.Done != true);
Console.WriteLine();
Console.WriteLine("Enter to continue");
Console.ReadLine();
Console.WriteLine("3. Deleting Project");
var operation3 = service.Projects.Delete(projectId).Execute();
}
}
}
If you're using Windows and Visual Studio, "Start"
If you're using Linux, you should first restore the packages, then run the app
dotnet restore
dotnet run
The output should be similar to:
Compiling Projects for .NETCoreApp,Version=v1.1
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:01.4161926
1. Create Project
2. Awaiting Operation Completion
True
Enter to continue
3. Deleting Project
The "Awaiting" step will contain blank lines (hopefully) ending in "True".
Putting it all together...
If you followed the examples above, you should in a directory called my_project_folder
and it hopefully contains a subdirectory called venv
.
Ensure your virtualenv is activated.
All the code can be placed in one file, let's call it create_project.py
.
Create the file create_project.py
in my_project_folder
.
import json
import time
from apiclient.discovery import build
from oauth2client.client import GoogleCredentials
SERVICE_NAME = "cloudresourcemanager"
SERVICE_VERSION = "v1"
# Don't forget to replace YOUR-PROJECT-ID with your choice of Project ID
# Even though the code deletes the Project, change this value each time
PROJECT_ID = "YOUR-PROJECT-ID"
credentials = GoogleCredentials.get_application_default()
service = build(
SERVICE_NAME,
SERVICE_VERSION,
credentials=credentials)
operation1 = service.projects().create(
body={
"project_id": PROJECT_ID
}
).execute()
print(json.dumps(
operation1,
sort_keys=True,
indent=3))
name = operation1["name"]
while True:
operation2 = service.operations().get(
name=name
).execute()
print(json.dumps(
operation2,
sort_keys=True,
indent=3))
if "done" in operation2:
if (operation2["done"]):
break
time.sleep(1)
raw_input("Press Enter to delete the Project...")
operation3 = service.projects().delete(
projectId=PROJECT_ID
).execute()
Save the file and, from the command-prompt type:
python create_project.py
The output should be similar to:
{
"metadata": {
"@type": "type.googleapis.com/google.cloudresourcemanager.v1.ProjectCreationStatus",
"createTime": "2017-12-31T00:00:00.000Z"
},
"name": "operations/pc.1234567890123456789"
}
...
{
"done": true,
"metadata": {
"@type": "type.googleapis.com/google.cloudresourcemanager.v1.ProjectCreationStatus",
"createTime": "2017-12-31T00:00:00.000Z"
"gettable": true,
"ready": true
},
"name": "operations/pc.1234567890123456789",
"response": {
"@type": "type.googleapis.com/google.cloudresourcemanager.v1.Project",
"createTime": "2017-12-31T00:00:00.000Z",
"lifecycleState": "ACTIVE",
"projectId": "your-project-id",
"projectNumber": "123456789012"
}
}
...
Press Enter to delete the Project...