Пример #1
0
        public void TestAssignDropletAsAppsCurrentDropletResponse()
        {
            string json = @"{
  ""guid"": ""guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb"",
  ""name"": ""name1"",
  ""desired_state"": ""STOPPED"",
  ""total_desired_instances"": 1,
  ""created_at"": ""2015-06-30T07:10:43Z"",
  ""updated_at"": ""2015-06-30T07:10:43Z"",
  ""environment_variables"": {

  },
  ""_links"": {
    ""self"": {
      ""href"": ""/v3/apps/guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb""
    },
    ""processes"": {
      ""href"": ""/v3/apps/guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb/processes""
    },
    ""packages"": {
      ""href"": ""/v3/apps/guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb/packages""
    },
    ""space"": {
      ""href"": ""/v2/spaces/835ae441-34f0-4442-b02e-55e73e7b7582""
    },
    ""desired_droplet"": {
      ""href"": ""/v3/droplets/guid-71f0b415-c533-499f-b2e6-5b23458f0d94""
    },
    ""start"": {
      ""href"": ""/v3/apps/guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb/start"",
      ""method"": ""PUT""
    },
    ""stop"": {
      ""href"": ""/v3/apps/guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb/stop"",
      ""method"": ""PUT""
    },
    ""assign_current_droplet"": {
      ""href"": ""/v3/apps/guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb/current_droplet"",
      ""method"": ""PUT""
    }
  }
}";

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

            Assert.AreEqual("guid-d140a6a4-ff94-4351-b3f4-7cf23541e8cb", TestUtil.ToTestableString(obj.Guid), true);
            Assert.AreEqual("name1", TestUtil.ToTestableString(obj.Name), true);
            Assert.AreEqual("STOPPED", TestUtil.ToTestableString(obj.DesiredState), true);
            Assert.AreEqual("1", TestUtil.ToTestableString(obj.TotalDesiredInstances), true);
            Assert.AreEqual("2015-06-30T07:10:43Z", TestUtil.ToTestableString(obj.CreatedAt), true);
            Assert.AreEqual("2015-06-30T07:10:43Z", TestUtil.ToTestableString(obj.UpdatedAt), true);
        }
Пример #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);
        }
Пример #3
0
        public void TestAssignDropletAsAppsCurrentDropletResponse()
        {
            string json = @"{
  ""guid"": ""de0acb8f-500e-4103-8deb-626a9a6882c5"",
  ""name"": ""name1"",
  ""desired_state"": ""STOPPED"",
  ""total_desired_instances"": 1,
  ""created_at"": ""2016-07-07T09:16:55Z"",
  ""updated_at"": ""2016-07-07T09:16:55Z"",
  ""lifecycle"": {
    ""type"": ""buildpack"",
    ""data"": {
      ""buildpack"": ""name-1220"",
      ""stack"": ""name-1221""
    }
  },
  ""environment_variables"": {

  },
  ""links"": {
    ""self"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879""
    },
    ""space"": {
      ""href"": ""/v2/spaces/2da51a33-3eb1-42ec-bf9d-f6faf98ba0b5""
    },
    ""processes"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/processes""
    },
    ""routes"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/routes""
    },
    ""packages"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/packages""
    },
    ""droplet"": {
      ""href"": ""/v3/droplets/0ca264d9-7a86-494e-99c6-9bd81bf445df""
    },
    ""droplets"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/droplets""
    },
    ""start"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/start"",
      ""method"": ""PUT""
    },
    ""stop"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/stop"",
      ""method"": ""PUT""
    },
    ""assign_current_droplet"": {
      ""href"": ""/v3/apps/cef98070-2afe-4272-9cf1-69a9f657f879/current_droplet"",
      ""method"": ""PUT""
    }
  }
}";

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

            Assert.AreEqual("de0acb8f-500e-4103-8deb-626a9a6882c5", TestUtil.ToTestableString(obj.Guid), true);
            Assert.AreEqual("name1", TestUtil.ToTestableString(obj.Name), true);
            Assert.AreEqual("STOPPED", TestUtil.ToTestableString(obj.DesiredState), true);
            Assert.AreEqual("1", 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);
        }