public void TestStagePackageResponse() { string json = @"{ ""guid"": ""whatuuid"", ""state"": ""PENDING"", ""hash"": null, ""buildpack_git_url"": ""http://github.com/myorg/awesome-buildpack"", ""failure_reason"": null, ""detected_start_command"": null, ""procfile"": null, ""environment_variables"": { ""VCAP_APPLICATION"": { ""limits"": { ""mem"": 1024, ""disk"": 4096, ""fds"": 16384 }, ""application_version"": ""whatuuid"", ""application_name"": ""name-1978"", ""application_uris"": [ ], ""version"": ""whatuuid"", ""name"": ""name-1978"", ""space_name"": ""name-1975"", ""space_id"": ""e1425c4a-02c4-4848-bc4e-86c21af44516"", ""uris"": [ ], ""users"": null }, ""CF_STACK"": ""trusty64"" }, ""created_at"": ""2015-06-30T07:10:53Z"", ""updated_at"": null, ""_links"": { ""self"": { ""href"": ""/v3/droplets/whatuuid"" }, ""package"": { ""href"": ""/v3/packages/guid-3be487b8-1212-4192-a3c6-c1b936e7b6ab"" }, ""app"": { ""href"": ""/v3/apps/guid-63f11614-907e-4f1e-bb85-1713bfadc92a"" } } }"; StagePackageResponse obj = Utilities.DeserializeJson <StagePackageResponse>(json); Assert.AreEqual("whatuuid", TestUtil.ToTestableString(obj.Guid), true); Assert.AreEqual("PENDING", TestUtil.ToTestableString(obj.State), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.Hash), true); Assert.AreEqual("http://github.com/myorg/awesome-buildpack", TestUtil.ToTestableString(obj.BuildpackGitUrl), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.FailureReason), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.DetectedStartCommand), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.Procfile), true); Assert.AreEqual("2015-06-30T07:10:53Z", TestUtil.ToTestableString(obj.CreatedAt), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.UpdatedAt), true); }
/// <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> /// <param name="diskLimit">Memory limit used to stage package</param> /// <param name="memoryLimit">Disk limit used to stage package</param> public async Task Push(Guid appGuid, string appPath, string stack, string buildpackGitUrl, bool startApplication, int memoryLimit, int diskLimit) { 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.AppsExperimental.GetApp(appGuid); usedSteps += 1; // Step 2 - Create package CreatePackageRequest createPackage = new CreatePackageRequest(); createPackage.Type = "bits"; CreatePackageResponse packageResponse = await this.Client.PackagesExperimental.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.PackagesExperimental.UploadBits(packageId, zippedPayload); bool uploadProcessed = false; while (!uploadProcessed) { GetPackageResponse getPackage = await this.Client.PackagesExperimental.GetPackage(packageId); switch (getPackage.State) { case "FAILED": { throw new Exception(string.Format(CultureInfo.InvariantCulture, "Upload failed: {0}", getPackage.Data["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.Lifecycle = new Dictionary <string, dynamic>(); Dictionary <string, string> data = new Dictionary <string, string>(); data["buildpack"] = buildpackGitUrl; data["stack"] = stack; stagePackage.Lifecycle["data"] = data; stagePackage.Lifecycle["type"] = "buildpack"; stagePackage.MemoryLimit = memoryLimit; stagePackage.DiskLimit = diskLimit; StagePackageResponse stageResponse = await this.Client.PackagesExperimental.StagePackage(packageId, stagePackage); if (this.CheckCancellation()) { return; } usedSteps += 1; if (startApplication) { bool staged = false; while (!staged) { GetDropletResponse getDroplet = await this.Client.DropletsExperimental.GetDroplet(new Guid(stageResponse.Guid.ToString())); switch (getDroplet.State) { case "FAILED": { throw new Exception(string.Format(CultureInfo.InvariantCulture, "Staging failed: {0}", getDroplet.Error)); } case "STAGED": { staged = true; break; } default: continue; } if (this.CheckCancellation()) { return; } Task.Delay(500).Wait(); } // Step 6 - Assign droplet AssignDropletAsAppsCurrentDropletRequest assignRequest = new AssignDropletAsAppsCurrentDropletRequest(); assignRequest.DropletGuid = stageResponse.Guid; AssignDropletAsAppsCurrentDropletResponse assignDroplet = await this.AssignDropletAsAppsCurrentDroplet(appGuid, assignRequest); if (this.CheckCancellation()) { return; } usedSteps += 1; // Step 7 - Start Application StartingAppResponse response = await this.Client.AppsExperimental.StartingApp(appGuid); if (this.CheckCancellation()) { return; } usedSteps += 1; } // Step 8 - Done this.TriggerPushProgressEvent(usedSteps, "Application {0} pushed successfully", app.Name); }
public void TestStagePackageResponse() { string json = @"{ ""guid"": ""9f486b79-1c04-47f2-976f-ec4a020ff378"", ""state"": ""PENDING"", ""error"": null, ""lifecycle"": { ""type"": ""buildpack"", ""data"": { ""buildpack"": ""http://github.com/myorg/awesome-buildpack"", ""stack"": ""cflinuxfs2"" } }, ""memory_limit"": 1024, ""disk_limit"": 4096, ""result"": { ""buildpack"": null, ""stack"": ""cflinuxfs2"", ""process_types"": null, ""hash"": { ""type"": ""sha1"", ""value"": null }, ""execution_metadata"": null }, ""environment_variables"": { ""CUSTOM_ENV_VAR"": ""hello"", ""VCAP_APPLICATION"": { ""limits"": { ""mem"": 1024, ""disk"": 4096, ""fds"": 16384 }, ""application_id"": ""1f9169b7-6640-4fc0-b135-917a5ba40b97"", ""application_version"": ""whatuuid"", ""application_name"": ""name-2404"", ""application_uris"": [ ], ""version"": ""whatuuid"", ""name"": ""name-2404"", ""space_name"": ""name-2401"", ""space_id"": ""06bc564d-ae95-46d7-be69-91079fa1952f"", ""uris"": [ ], ""users"": null }, ""CF_STACK"": ""cflinuxfs2"", ""MEMORY_LIMIT"": 1024, ""VCAP_SERVICES"": { } }, ""created_at"": ""2016-07-07T09:17:16Z"", ""updated_at"": null, ""links"": { ""self"": { ""href"": ""/v3/droplets/whatuuid"" }, ""package"": { ""href"": ""/v3/packages/092c3d83-1000-4924-a2ac-62eb6c2dfa9e"" }, ""app"": { ""href"": ""/v3/apps/1f9169b7-6640-4fc0-b135-917a5ba40b97"" }, ""assign_current_droplet"": { ""href"": ""/v3/apps/1f9169b7-6640-4fc0-b135-917a5ba40b97/current_droplet"", ""method"": ""PUT"" } } }"; StagePackageResponse obj = Utilities.DeserializeJson <StagePackageResponse>(json); Assert.AreEqual("9f486b79-1c04-47f2-976f-ec4a020ff378", TestUtil.ToTestableString(obj.Guid), true); Assert.AreEqual("PENDING", TestUtil.ToTestableString(obj.State), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.Error), true); Assert.AreEqual("1024", TestUtil.ToTestableString(obj.MemoryLimit), true); Assert.AreEqual("4096", TestUtil.ToTestableString(obj.DiskLimit), true); Assert.AreEqual("2016-07-07T09:17:16Z", TestUtil.ToTestableString(obj.CreatedAt), true); Assert.AreEqual("", TestUtil.ToTestableString(obj.UpdatedAt), true); }