Reputation: 339
I haven't been able to find anything that will clearly explain how to use google compute engine through the .net API (specifically c#). Is there anyone that can point me to anything?
P.S. I know about the API reference (https://developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html)
Upvotes: 4
Views: 1443
Reputation: 33
A list of steps you need to follow: Specifically you can modify and use the following code to Create the vmInstance of Google Compute Engine Here is the c# (using Google api SDK) functions that can create instances
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "ClientId",
ClientSecret = "ClientSecret"
},
new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform },
"user",
CancellationToken.None, null);
`ComputeService service = new ComputeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "ApplicationName",
ApiKey = "ApiKey"
});
public IEnumerable<CreateInstanceResult> CreateInstances(params CreateInstanceRequest[] instances)
{
IList<Instance> vmInstances = new List<Instance>();
ComputeService service = assign GoogleComputeServiceObject;
if (instances != null)
{
foreach (CreateInstanceRequest requestInstance in instances)
{
#region Meatadata Setting
Metadata metaData = new Metadata();
metaData.Items = new List<Metadata.ItemsData>();
Metadata.ItemsData itemData = new Metadata.ItemsData();
itemData.Key = "Expiration";
itemData.Value = requestInstance.Expiration.ToString();
metaData.Items.Add(itemData);
itemData = new Metadata.ItemsData();
itemData.Key = "AccountId";
itemData.Value = requestInstance.AccountId;
metaData.Items.Add(itemData);
if (requestInstance.Data != null)
{
foreach (KeyValuePair<string, string> keyValue in requestInstance.Data)
{
Metadata.ItemsData otherItemData = new Metadata.ItemsData();
otherItemData.Key = keyValue.Key;
otherItemData.Value = keyValue.Value;
metaData.Items.Add(otherItemData);
}
}
#endregion Meatadata Setting
#region DiskSetting
IList<AttachedDisk> attachedDisks = new List<AttachedDisk>();
AttachedDisk attachedDisk = new AttachedDisk();
AttachedDiskInitializeParams attachedDiskInitializeParams = new AttachedDiskInitializeParams();
attachedDiskInitializeParams.DiskSizeGb = googleCloudServerSetting.DiskSize;
attachedDiskInitializeParams.DiskType = service.BaseUri + "Your_ProjectId" + "/zones/" + "specifyZone" + "/diskTypes/" + "specify_DiskType";
// for example
attachedDiskInitializeParams.SourceImage = service.BaseUri + "/debian-cloud/global/images/specify_imagesourceImage";
attachedDisk.AutoDelete = true;
attachedDisk.Boot = true;
attachedDisk.Interface__ = "SCSI";//for example
attachedDisk.InitializeParams = attachedDiskInitializeParams;
attachedDisks.Add(attachedDisk);
IList<NetworkInterface> networkInterfaces = new List<NetworkInterface>();
NetworkInterface networkInterface = new NetworkInterface();
networkInterface.Network = service.BaseUri + ProjectId + "/global/networks/default";
networkInterfaces.Add(networkInterface);
Tags tags = new Tags();
IList<string> stringList = new List<string>();
tags.Items = new List<string>();
tags.Items.Add("http-server");
tags.Items.Add("https-server");
#endregion DiskSetting
#region Creating Instance object
Instance instance = new Instance()
{
MachineType = requestInstance.SizeId ?? service.BaseUri + "ProjectId" + "/zones/" + "specify_Zone" + "/machineTypes/" + "specify_machineType",
Metadata = metaData,
Name = "InstanceName",
Tags = tags,
NetworkInterfaces = networkInterfaces,
Disks = attachedDisks
};
#endregion Creating Instance object
vmInstances.Add(instance);
}
var batchRequest = new BatchRequest(service);
foreach (Instance instance in instances)
{
batchRequest.Queue<Instance>(service.Instances.Insert(instance, ProjectId, Zone),
(content, error, i, message) =>
{
});
}
await batchRequest.ExecuteAsync();
}
else
{
throw new Exception("null");
}
}
Upvotes: 1
Reputation: 886
I could not find any detailed tutorial with code samples, but official documentation is available at [1] includes a code sample.
There is a tutorial with C# sample specific for Google Drive at [2].
For your reference APIs documentation is available at [3] and the annotated library reference is available at [4].
Link:
[1] - https://developers.google.com/api-client-library/dotnet/get_started#examples
[2] - http://conficient.wordpress.com/2014/06/18/using-google-drive-api-with-c-part-1/
[3] - https://developers.google.com/compute/docs/reference/latest/
[4] - https://developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html
Upvotes: 1