Example #1
0
        public void TestStartingAppResponse()
        {
            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""
    }
  }
}";

            StartingAppResponse obj = Utilities.DeserializeJson <StartingAppResponse>(json);

            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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        public void TestStartingAppResponse()
        {
            string json = @"{
  ""guid"": ""8528cbdb-12e7-4c1e-aaa6-9387e37a88ab"",
  ""name"": ""original_name"",
  ""desired_state"": ""STARTED"",
  ""total_desired_instances"": 0,
  ""created_at"": ""2016-07-07T09:16:55Z"",
  ""updated_at"": ""2016-07-07T09:16:55Z"",
  ""lifecycle"": {
    ""type"": ""buildpack"",
    ""data"": {
      ""buildpack"": ""name-1201"",
      ""stack"": ""name-1202""
    }
  },
  ""environment_variables"": {

  },
  ""links"": {
    ""self"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee""
    },
    ""space"": {
      ""href"": ""/v2/spaces/5cb0207a-b120-477b-a478-38c0f211ab76""
    },
    ""processes"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/processes""
    },
    ""routes"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/routes""
    },
    ""packages"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/packages""
    },
    ""droplet"": {
      ""href"": ""/v3/droplets/90d050d0-39c8-46bf-ba02-3f8b03263b24""
    },
    ""droplets"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/droplets""
    },
    ""start"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/start"",
      ""method"": ""PUT""
    },
    ""stop"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/stop"",
      ""method"": ""PUT""
    },
    ""assign_current_droplet"": {
      ""href"": ""/v3/apps/e26ddfa6-4667-4127-9671-a831d767a2ee/current_droplet"",
      ""method"": ""PUT""
    }
  }
}";

            StartingAppResponse obj = Utilities.DeserializeJson <StartingAppResponse>(json);

            Assert.AreEqual("8528cbdb-12e7-4c1e-aaa6-9387e37a88ab", 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("2016-07-07T09:16:55Z", TestUtil.ToTestableString(obj.CreatedAt), true);
            Assert.AreEqual("2016-07-07T09:16:55Z", TestUtil.ToTestableString(obj.UpdatedAt), true);
        }