Google compute engine API examples / examples / tutorials

I haven't been able to find anything that clearly explains how to use Google's compute engine through API.net (specifically C #). Is there anyone who can point me to something?

PS I am aware of the API link ( https://developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html )

+3


source to share


2 answers


I have not been able to find a detailed tutorial with sample code, but official documentation is available in [1], including sample code.

There is a tutorial with a Google Drive-specific C # sample in [2].



API documentation is available in [3], and a link to the annotated library is available in [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

+1


source


List of steps you need to follow: Specifically, you can modify and use the following code to create vmInstance Google Compute Engine Here is C # (using Google api SDK) that can instantiate



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");
        }
    }

      

0


source







All Articles