Google计算引擎.NET API示例/示例/教程 [英] Google compute engine .NET API examples/samples/tutorials

查看:70
本文介绍了Google计算引擎.NET API示例/示例/教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到任何能够清楚说明如何通过.net API(特别是C#)使用谷歌计算引擎的内容.有没有人可以指点我?

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.我了解API参考( https: //developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html )

P.S. I know about the API reference (https://developers.google.com/resources/api-libraries/documentation/compute/v1/csharp/latest/annotated.html)

推荐答案

您需要遵循的步骤列表: 具体来说,您可以修改并使用以下代码来创建Google Compute Engine的vmInstance 这是可以创建实例的c#(使用Google api SDK)功能

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

这篇关于Google计算引擎.NET API示例/示例/教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆