/// <summary> /// Starting an App /// <para>For detailed information, see online documentation at: "http://apidocs.cloudfoundry.org/195/apps__experimental_/starting_an_app.html"</para> /// </summary> public async Task <StartingAppResponse> StartingApp(Guid?guid, StartingAppRequest value) { UriBuilder uriBuilder = new UriBuilder(this.Client.CloudTarget); uriBuilder.Path = string.Format(CultureInfo.InvariantCulture, "/v3/apps/{0}/start", guid); var client = this.GetHttpClient(); client.Uri = uriBuilder.Uri; client.Method = HttpMethod.Put; var authHeader = await BuildAuthenticationHeader(); if (!string.IsNullOrWhiteSpace(authHeader.Key)) { client.Headers.Add(authHeader); } client.ContentType = "application/x-www-form-urlencoded"; client.Content = JsonConvert.SerializeObject(value).ConvertToStream(); var expectedReturnStatus = 200; var response = await this.SendAsync(client, expectedReturnStatus); return(Utilities.DeserializeJson <StartingAppResponse>(await response.ReadContentAsStringAsync())); }
/// <summary> /// Pushes an application to the cloud. /// <remarks> /// This method is only available on the .NET 4.5 framework. /// Calling this method from a Windows Phone App or a Windows App will throw a <see cref="NotImplementedException"/>. /// </remarks> /// </summary> /// <exception cref="NotImplementedException"></exception> /// <param name="appGuid">Application guid</param> /// <param name="appPath">Path of origin from which the application will be deployed</param> /// <param name="stack">The name of the stack the app will be running on</param> /// <param name="buildpackGitUrl">Git URL of the buildpack</param> /// <param name="startApplication">True if the app should be started after upload is complete, false otherwise</param> public async Task Push(Guid appGuid, string appPath, string stack, string buildpackGitUrl, bool startApplication) { if (appPath == null) { throw new ArgumentNullException("appPath"); } IAppPushTools pushTools = new AppPushTools(appPath); int usedSteps = 1; // Step 1 - Check if application exists this.TriggerPushProgressEvent(usedSteps, "Checking if application exists"); GetAppResponse app = await this.Client.Apps.GetApp(appGuid); usedSteps += 1; // Step 2 - Create package CreatePackageRequest createPackage = new CreatePackageRequest(); createPackage.Type = "bits"; CreatePackageResponse packageResponse = await this.Client.Packages.CreatePackage(appGuid, createPackage); Guid packageId = new Guid(packageResponse.Guid.ToString()); if (this.CheckCancellation()) { return; } usedSteps += 1; // Step 3 - Zip all needed files and get a stream back from the PushTools this.TriggerPushProgressEvent(usedSteps, "Creating zip package ..."); using (Stream zippedPayload = await pushTools.GetZippedPayload(this.Client.CancellationToken)) { if (this.CheckCancellation()) { return; } usedSteps += 1; // Step 4 - Upload zip to CloudFoundry ... this.TriggerPushProgressEvent(usedSteps, "Uploading zip package ..."); await this.Client.Packages.UploadBits(packageId, zippedPayload); bool uploadProcessed = false; while (!uploadProcessed) { GetPackageResponse getPackage = await this.Client.Packages.GetPackage(packageId); switch (getPackage.State) { case "FAILED": { throw new Exception(string.Format(CultureInfo.InvariantCulture, "Upload failed: {0}", getPackage.Error)); } case "READY": { uploadProcessed = true; break; } default: continue; } if (this.CheckCancellation()) { return; } Task.Delay(500).Wait(); } usedSteps += 1; } // Step 5 - Stage application StagePackageRequest stagePackage = new StagePackageRequest(); stagePackage.Stack = stack; stagePackage.BuildpackGitUrl = buildpackGitUrl; StagePackageResponse stageResponse = await this.Client.Packages.StagePackage(packageId, stagePackage); if (this.CheckCancellation()) { return; } usedSteps += 1; if (startApplication) { bool staged = false; while (!staged) { GetDropletResponse getDroplet = await this.Client.Droplets.GetDroplet(new Guid(stageResponse.Guid.ToString())); switch (getDroplet.State) { case "FAILED": { throw new Exception(string.Format(CultureInfo.InvariantCulture, "Staging failed: {0}", getDroplet.FailureReason)); } case "STAGED": { staged = true; break; } default: continue; } if (this.CheckCancellation()) { return; } Task.Delay(500).Wait(); } // Step 6 - Assign droplet AssignDropletAsAppsCurrentDropletRequest assignRequest = new AssignDropletAsAppsCurrentDropletRequest(); assignRequest.DesiredDropletGuid = stageResponse.Guid.ToString(); AssignDropletAsAppsCurrentDropletResponse assignDroplet = await this.AssignDropletAsAppsCurrentDroplet(appGuid, assignRequest); if (this.CheckCancellation()) { return; } usedSteps += 1; // Step 7 - Start Application StartingAppRequest startApp = new StartingAppRequest(); StartingAppResponse response = await this.Client.Apps.StartingApp(appGuid, startApp); if (this.CheckCancellation()) { return; } usedSteps += 1; } // Step 8 - Done this.TriggerPushProgressEvent(usedSteps, "Application {0} pushed successfully", app.Name); }
public void StartingAppTest() { using (ShimsContext.Create()) { MockClients clients = new MockClients(); string json = @"{ ""guid"": ""guid-169519a4-8744-43e3-ad05-f0bf762e936d"", ""name"": ""original_name"", ""desired_state"": ""STARTED"", ""total_desired_instances"": 0, ""created_at"": ""2015-06-30T07:10:42Z"", ""updated_at"": ""2015-06-30T07:10:43Z"", ""environment_variables"": { }, ""_links"": { ""self"": { ""href"": ""/v3/apps/guid-169519a4-8744-43e3-ad05-f0bf762e936d"" }, ""processes"": { ""href"": ""/v3/apps/guid-169519a4-8744-43e3-ad05-f0bf762e936d/processes"" }, ""packages"": { ""href"": ""/v3/apps/guid-169519a4-8744-43e3-ad05-f0bf762e936d/packages"" }, ""space"": { ""href"": ""/v2/spaces/0a7e65bd-1486-4445-aef7-4e7e700f0bf8"" }, ""desired_droplet"": { ""href"": ""/v3/droplets/guid-a4fa09f0-9bae-4a53-846d-a3c22a661cf4"" }, ""start"": { ""href"": ""/v3/apps/guid-169519a4-8744-43e3-ad05-f0bf762e936d/start"", ""method"": ""PUT"" }, ""stop"": { ""href"": ""/v3/apps/guid-169519a4-8744-43e3-ad05-f0bf762e936d/stop"", ""method"": ""PUT"" }, ""assign_current_droplet"": { ""href"": ""/v3/apps/guid-169519a4-8744-43e3-ad05-f0bf762e936d/current_droplet"", ""method"": ""PUT"" } } }"; clients.JsonResponse = json; clients.ExpectedStatusCode = (HttpStatusCode)200; var cfClient = clients.CreateCloudFoundryClient(); Guid?guid = Guid.NewGuid(); StartingAppRequest value = null; var obj = cfClient.Apps.StartingApp(guid, value).Result; Assert.AreEqual("guid-169519a4-8744-43e3-ad05-f0bf762e936d", TestUtil.ToTestableString(obj.Guid), true); Assert.AreEqual("original_name", TestUtil.ToTestableString(obj.Name), true); Assert.AreEqual("STARTED", TestUtil.ToTestableString(obj.DesiredState), true); Assert.AreEqual("0", TestUtil.ToTestableString(obj.TotalDesiredInstances), true); Assert.AreEqual("2015-06-30T07:10:42Z", TestUtil.ToTestableString(obj.CreatedAt), true); Assert.AreEqual("2015-06-30T07:10:43Z", TestUtil.ToTestableString(obj.UpdatedAt), true); } }