Once you become familiar with the code to call one method on one Google service, you will be able to infer how to call any method on any Google service.
First, we make a connection to the service using the credential
object instantiated in the previous example:
CloudResourceManagerService service = new CloudResourceManagerService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Our First Google API Client"
}
);
Then we can call methods provided by the service. What methods are available?
https://cloud.google.com/resource-manager/docs/apis
What is the underlying REST request for Projects.Create
?
https://cloud.google.com/resource-manager/reference/rest/v1/projects/create
OK... Let's write the code.
The code expects a string value for projectId
. Project IDs are unique identifiers. I recommend you use a system for naming your projects to help you identify them.
Projects.Create
expects a Data.Project
object. This object one mandatory property, the Project ID which is all we'll provide but we could also provide a Project Name, Labels, details of the Project's parent etc.
Data.Operation operation1 = service.Projects.Create(
new Data.Project()
{
ProjectId = projectId,
}
).Execute();
Project creation is handled asynchronously. We are given an Operation
object that we must poll to determine when the Project is created. Operations have a Name property that uniquely identifies the operation. The next section of code polls the platform "Are we done yet?". The project will be created when our new operation includes a Done
property that is True
.
Data.Operation operation2;
do
{
operation2 = service.Operations.Get(operation1.Name).Execute();
System.Threading.Thread.Sleep(1000);
} while (operation2.Done != true);
For completeness, and hopefully many years from now after much happy use of your project, you may need to delete your project. We simply call Projects.Delete and provide our Project ID. This also returns an operation and we ought really to poll this operation too until it completes definitively. Our project will then be deleted.
var operation3 = service.Projects.Delete(projectId).Execute();
That's it!