private async Task <string> GetInstance()
        {
            try
            {
                ServiceAccountCredential credential = new ServiceAccountCredential(
                    new ServiceAccountCredential.Initializer(ServiceAccountEmail)
                {
                    Scopes = new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform }
                }.FromPrivateKey(PrivateKey));

                var cs = new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName       = "GCloud Get Instance"
                };

                var t = new ComputeService(cs);

                var zoneRegion = Region + "-" + Zone;

                InstancesResource.GetRequest request = t.Instances.Get(Project, zoneRegion, InstanceName);

                Google.Apis.Compute.v1.Data.Instance response = await request.ExecuteAsync();

                return(JsonConvert.SerializeObject(response));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 /// <summary>
 /// Gets the Cloud SQL instances for the given project and name.
 /// </summary>
 /// <returns>The Cloud SQL instance.</returns>
 public async Task <DatabaseInstance> GetInstanceAsync(string name)
 {
     try
     {
         InstancesResource.GetRequest request = Service.Instances.Get(ProjectId, name);
         return(await request.ExecuteAsync());
     }
     catch (GoogleApiException ex)
     {
         Debug.WriteLine($"Failed to get database instance: {ex.Message}");
         throw new DataSourceException(ex.Message, ex);
     }
 }
Esempio n. 3
0
        private async Task RemoveInstanceTask()
        {
            //credentials for certificate-based service accounts
            var certificate = new X509Certificate2(CertificateFile, "notasecret", X509KeyStorageFlags.Exportable);
            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(ServiceAccountEmail)
            {
                Scopes = new[] { ComputeService.Scope.Compute, ComputeService.Scope.CloudPlatform, ComputeService.Scope.DevstorageFullControl, "https://www.googleapis.com/auth/logging.write" }
            }.FromCertificate(certificate));

            var service = new ComputeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "Apprenda Addon",
            });
            var startTime = DateTime.UtcNow;

            //   while(DateTime.UtcNow - startTime < TimeSpan.FromSeconds(60))
            while (true)
            {
                var instance = new InstancesResource.GetRequest(service, ProjectId, Zone, InstanceName).Execute();
                if (instance.Status == "RUNNING")
                {
                    break;
                }
                if (DateTime.UtcNow - startTime < TimeSpan.FromMinutes(2))
                {
                    throw new Exception("Remove instance timed out\n");
                }
            }

            try
            {
                var removeInstanceQuery = await new InstancesResource.DeleteRequest(service, ProjectId, Zone, InstanceName).ExecuteAsync();
            }
            catch (AggregateException e)
            {
                throw new AggregateException(e);
            }
        }