internal string GenerateCreateServeResponse(ComputeServer server)
        {
            var payloadFixture = @"{{
                ""server"": {{
                    ""security_groups"": [
                        {{
                            ""name"": ""default""
                        }},
                        {{
                            ""name"": ""MyGroup""
                        }}
                    ],
                    ""OS-DCF:diskConfig"": ""MANUAL"",
                    ""id"": ""{0}"",
                    ""links"": [
                        {{
                            ""href"": ""{2}"",
                            ""rel"": ""self""
                        }},
                        {{
                            ""href"": ""{3}"",
                            ""rel"": ""bookmark""
                        }}
                    ],
                    ""adminPass"": ""{1}""
                }}
            }}";

            return string.Format(payloadFixture, server.Id, server.AdminPassword,
                server.PublicUri.AbsoluteUri, server.PermanentUri.AbsoluteUri);
        }
 private string GenerateServerPayload(ComputeServer server)
 {
     var payloadFixture = @"{{
         ""server"": {{
             ""status"": ""{2}"",
             ""updated"": ""2014-06-11T18:04:46Z"",
             ""hostId"": ""bd5417ccb076908f6e0d639c37c053b0b6b9681db3464d19908dd4d9"",
             ""addresses"": {{
                 ""private"": [
                     {{
                         ""OS-EXT-IPS-MAC:mac_addr"": ""fa:16:3e:34:da:44"",
                         ""version"": 4,
                         ""addr"": ""10.0.0.2"",
                         ""OS-EXT-IPS:type"": ""fixed""
                     }},
                     {{
                         ""OS-EXT-IPS-MAC:mac_addr"": ""fa:16:3e:34:da:44"",
                         ""version"": 4,
                         ""addr"": ""172.24.4.3"",
                         ""OS-EXT-IPS:type"": ""floating""
                     }}
                 ]
             }},
             ""links"": [
                 {{
                     ""href"": ""{4}"",
                     ""rel"": ""self""
                 }},
                 {{
                     ""href"": ""{5}"",
                     ""rel"": ""bookmark""
                 }}
             ],
             ""key_name"": null,
             ""image"": {{
                 ""id"": ""c650e788-3c46-4efc-bfa6-1d94a14d6405"",
                 ""links"": [
                     {{
                         ""href"": ""http://15.125.87.81:8774/ffe683d1060449d09dac0bf9d7a371cd/images/c650e788-3c46-4efc-bfa6-1d94a14d6405"",
                         ""rel"": ""bookmark""
                     }}
                 ]
             }},
             ""OS-EXT-STS:task_state"": null,
             ""OS-EXT-STS:vm_state"": ""active"",
             ""OS-SRV-USG:launched_at"": ""2014-06-11T18:04:45.000000"",
             ""flavor"": {{
                 ""id"": ""1"",
                 ""links"": [
                     {{
                         ""href"": ""http://15.125.87.81:8774/ffe683d1060449d09dac0bf9d7a371cd/flavors/1"",
                         ""rel"": ""bookmark""
                     }}
                 ]
             }},
             ""id"": ""{0}"",
             ""security_groups"": [
                 {{
                     ""name"": ""MyGroup""
                 }},
                 {{
                     ""name"": ""default""
                 }}
             ],
             ""OS-SRV-USG:terminated_at"": null,
             ""OS-EXT-AZ:availability_zone"": ""nova"",
             ""user_id"": ""70d48d344b494a1cbe8adbf7c02be7b5"",
             ""name"": ""{1}"",
             ""created"": ""2014-06-11T18:04:25Z"",
             ""tenant_id"": ""ffe683d1060449d09dac0bf9d7a371cd"",
             ""OS-DCF:diskConfig"": ""AUTO"",
             ""os-extended-volumes:volumes_attached"": [],
             ""accessIPv4"": """",
             ""accessIPv6"": """",
             ""progress"": {3},
             ""OS-EXT-STS:power_state"": 1,
             ""config_drive"": """",
             ""metadata"": {{}}
         }}
     }}";
     return string.Format(payloadFixture, server.Id, server.Name, server.Status, server.Progress, server.PublicUri.AbsoluteUri, server.PermanentUri.AbsoluteUri);
 }
        public async Task CanGetServer()
        {
            var serverId = "12345";
            var expServer = new ComputeServer(serverId, "tiny",
                new Uri("http://testcomputeendpoint.com/v2/1234567890/servers/1"),
                new Uri("http://testcomputeendpoint.com/1234567890/servers/1"), new Dictionary<string, string>());

            this.ServicePocoClient.GetServerDelegate = (id) =>
            {
                Assert.AreEqual(serverId, id);
                return Task.Factory.StartNew(() => expServer);
            };

            var client = new ComputeServiceClient(GetValidCreds(), "Nova", CancellationToken.None, this.ServiceLocator);
            var server = await client.GetServer(serverId);

            Assert.IsNotNull(server);
            Assert.AreEqual(expServer, server);
        }
        internal IHttpResponseAbstraction HandlePostNewServer()
        {
            var payload = TestHelper.GetStringFromStream(this.Content);
            var obj = JObject.Parse(payload);
            var name = (string)obj["server"]["name"];
            var srv = new ComputeServer(Guid.NewGuid().ToString(), name, "12345", new Uri("http://test.com"), new Uri("http://test.com"), new Dictionary<string, string>());
            this.Servers.Add(srv);
            var responsePayload = GenerateCreateServeResponse(srv);

            return TestHelper.CreateResponse(HttpStatusCode.Accepted, new Dictionary<string, string>(), responsePayload.ConvertToStream());
        }
        public async Task CanGetServers()
        {
            var expServer1 = new ComputeServer("1", "srv1",
                new Uri("http://testcomputeendpoint.com/v2/1234567890/servers/1"),
                new Uri("http://testcomputeendpoint.com/1234567890/servers/1"), new Dictionary<string, string>());

            var expServer2 = new ComputeServer("2", "srv2",
                new Uri("http://testcomputeendpoint.com/v2/1234567890/servers/1"),
                new Uri("http://testcomputeendpoint.com/1234567890/servers/1"), new Dictionary<string, string>());
            var servers = new List<ComputeServer>() { expServer1, expServer2 };

            this.ServicePocoClient.GetServersDelegate = () => Task.Factory.StartNew(() => (IEnumerable<ComputeServer>)servers);

            var client = new ComputeServiceClient(GetValidCreds(), "Nova", CancellationToken.None, this.ServiceLocator);
            var resp = await client.GetServers();
            Assert.IsNotNull(resp);

            var respFlavors = resp.ToList();
            Assert.AreEqual(2, respFlavors.Count());
            Assert.AreEqual(expServer1, respFlavors[0]);
            Assert.AreEqual(expServer2, respFlavors[1]);
        }
        public async Task CanCreateServerWithoutKeyName()
        {
            var serverName = "MyServer";
            var imageId = "56789";
            var flavorId = "2";
            var networkId = "98765";
            var adminPassword = "******";
            var expServer = new ComputeServer("1235", serverName, adminPassword, new Uri("http://someuri.com/v2/servers/12345"),
                new Uri("http://someuri.com/servers/12345"), new Dictionary<string, string>());

            this.ServicePocoClient.CreateServerDelegate = (name, imgId, flvId, ntwId, key, groups) =>
            {
                Assert.AreEqual(serverName, name);
                Assert.AreEqual(imageId, imgId);
                Assert.AreEqual(flavorId, flvId);
                Assert.AreEqual(networkId, ntwId);
                Assert.AreEqual(string.Empty, key);
                Assert.IsTrue(groups.Any(g => g == "default"));
                return Task.Factory.StartNew(() => expServer);
            };

            var client = new ComputeServiceClient(GetValidCreds(), "Nova", CancellationToken.None, this.ServiceLocator);
            var server = await client.CreateServer(serverName, imageId, flavorId, networkId, new List<string>() { "default" });

            Assert.IsNotNull(server);
            Assert.AreEqual("1235", server.Id);
            Assert.AreEqual(adminPassword, server.AdminPassword);
        }
        public async Task CanDeleteServerMetadata()
        {
            var metadata = new Dictionary<string, string>() { { "item1", "value1" }, { "item2", "value2" } };
            var serverId = "1";
            var server = new ComputeServer(serverId, "tiny",
                new Uri("http://testcomputeendpoint.com/v2/1234567890/servers/1"),
                new Uri("http://testcomputeendpoint.com/1234567890/servers/1"), metadata);
            this.simulator.Servers.Add(server);

            var client = new ComputeServiceRestClient(GetValidContext(), this.ServiceLocator);

            var resp = await client.DeleteServerMetadata(serverId, "item1");

            Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);

            Assert.AreEqual(1, server.Metadata.Count);
            Assert.AreEqual("value2", server.Metadata["item2"]);
        }
        public async Task CanDeleteServer()
        {
            var serverId = "12345";
            var server = new ComputeServer(serverId, "tiny",
                new Uri("http://testcomputeendpoint.com/v2/1234567890/servers/1"),
                new Uri("http://testcomputeendpoint.com/1234567890/servers/1"), new Dictionary<string, string>());
            this.simulator.Servers.Add(server);

            var client = new ComputeServiceRestClient(GetValidContext(), this.ServiceLocator);

            var resp = await client.DeleteServer(serverId);

            Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);
            Assert.AreEqual(0, this.simulator.Servers.Count);
        }
        public async Task CanGetServers()
        {
            var serverId = "12345";
            var server = new ComputeServer(serverId, "tiny",
                new Uri("http://testcomputeendpoint.com/v2/1234567890/servers/1"),
                new Uri("http://testcomputeendpoint.com/1234567890/servers/1"), new Dictionary<string, string>());
            this.simulator.Servers.Add(server);
            
            var client = new ComputeServiceRestClient(GetValidContext(), this.ServiceLocator);

            var resp = await client.GetServers();

            Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);

            var respContent = TestHelper.GetStringFromStream(resp.Content);
            Assert.IsTrue(respContent.Length > 0);
        }