public void DeserializeKeyPairCollection() { // this one is odd because even in the list, the items are wrapped with a root that needs to be unwrapped string json = JObject.Parse(@"{ 'keypairs': [ { 'keypair': { 'public_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDrBzodZLiWO6nIGGy9ZOVeFhbF6EaG8HUqrknNVKynH6+Hc5ToY71gmeQGJ7XZTAlyKKdFmPhNPCQCYqFQxjPKD3xTIAoGChlRHfkjYwjefbqxFswi9S0Fi3Lq8mawUVuPmPnuTr8KhL8ibnBbAxZnrcfTKBIoxhU+kN56CCmLnkJc5ouG/UcF+UpqUso45pYRf0YWANyyuafyCmj6NiDxMCGy/vnKUBLzMg8wQ01xGSGOfyGDIwuTFZpoPzjeqEV8oUGvxYt9Xyzh/pPKoOz1cz0wBDaVDpucTz3UEq65F9HhCmdwwjso8MP1K46LkM2JNQWQ0eTotqFvUJEoP2ff Generated-by-Nova', 'name': 'keypair-d20a3d59-9433-4b79-8726-20b431d89c78', 'fingerprint': 'ce:88:fe:6a:9e:c0:d5:91:08:8b:57:80:be:e6:ec:3d' } }, { 'keypair': { 'public_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDx8nkQv/zgGgB4rMYmIf+6A4l6Rr+o/6lHBQdW5aYd44bd8JttDCE/F/pNRr0lRE+PiqSPO8nDPHw0010JeMH9gYgnnFlyY3/OcJ02RhIPyyxYpv9FhY+2YiUkpwFOcLImyrxEsYXpD/0d3ac30bNH6Sw9JD9UZHYcpSxsIbECHw== Generated-by-Nova', 'name': 'uploaded-keypair', 'fingerprint': '1e:2c:9b:56:79:4b:45:77:f9:ca:7a:98:2c:b0:d5:3c' } } ] }").ToString(); var results = OpenStackNet.Deserialize <KeyPairSummaryCollection>(json); Assert.NotNull(results); Assert.Equal(2, results.Count()); var result = results.First(); Assert.Empty(((IHaveExtraData)result).Data); Assert.NotNull(result.PublicKey); }
public void DeserializeVolumeWithEmptyAttachment() { var json = JObject.Parse(@"{'volume': {'attachments': [{}]}}").ToString(); var result = OpenStackNet.Deserialize <Volume>(json); Assert.Empty(result.Attachments); }
/// <inheritdoc/> public override UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false) { identity = identity ?? DefaultIdentity; CloudIdentityWithProject identityWithProject = identity as CloudIdentityWithProject; if (identityWithProject == null) { return(base.GetUserAccess(identityWithProject, forceCacheRefresh)); } if (string.IsNullOrEmpty(identityWithProject.Password)) { throw new NotSupportedException(string.Format("The {0} identity must specify a password.", typeof(CloudIdentityWithProject))); } if (!string.IsNullOrEmpty(identityWithProject.APIKey)) { throw new NotSupportedException(string.Format("The {0} identity does not support API key authentication.", typeof(CloudIdentityWithProject))); } Func <UserAccess> refreshCallback = () => { var projectId = identityWithProject.ProjectId != null?JToken.FromObject(identityWithProject.ProjectId) : string.Empty; JObject requestBody = new JObject( new JProperty("auth", new JObject( new JProperty("passwordCredentials", new JObject( new JProperty("username", JValue.CreateString(identityWithProject.Username)), new JProperty("password", JValue.CreateString(identityWithProject.Password)))), new JProperty("tenantName", JToken.FromObject(identityWithProject.ProjectName)), new JProperty("tenantId", projectId)))); var response = ExecuteRESTRequest <JObject>(identity, new Uri(UrlBase, "/v2.0/tokens"), HttpMethod.POST, requestBody, isTokenRequest: true); if (response == null || response.Data == null) { return(null); } // The defalut json serialization is helpfully formatting the expires date string. Use our custom serializer for this part to prevent chaos of timezone proportions. var rawJson = response.Data["access"]?.ToString(Formatting.None); if (rawJson == null) { return(null); } UserAccess access = OpenStackNet.Deserialize <UserAccess>(rawJson); if (access == null || access.Token == null) { return(null); } return(access); }; string key = string.Format("{0}:{1}/{2}", UrlBase, identityWithProject.ProjectId, identityWithProject.Username); var userAccess = TokenCache.Get(key, refreshCallback, forceCacheRefresh); return(userAccess); }
public void DeserializeWhenNotRoot() { var json = JArray.Parse("[{'id':'thing-id'}]").ToString(); var things = OpenStackNet.Deserialize <List <Thing> >(json); Assert.Equal(1, things.Count); Assert.Equal("thing-id", things[0].Id); }
public void ShouldIgnoreUnexpectedRootProperties() { var json = JObject.Parse("{'links': [{'name': 'next', 'link': 'http://nextlink'}], 'thing': {'id': 'thing-id'}}").ToString(); var thing = OpenStackNet.Deserialize <Thing>(json); Assert.NotNull(thing); Assert.Equal("thing-id", thing.Id); }
public void DeserializeWhenNested() { var json = JObject.Parse("{'things':[{'id':'thing-id'}]}").ToString(); var things = OpenStackNet.Deserialize <ThingCollection>(json); Assert.NotNull(things); Assert.Equal(1, things.Count); Assert.Equal("thing-id", things[0].Id); }
public void Deserialize() { var json = OpenStackNet.Serialize(new Thing { Id = "thing-id" }); var thing = OpenStackNet.Deserialize <Thing>(json); Assert.Equal("thing-id", thing.Id); }
public void DeserializeTimeToLiveFromSeconds() { var cache = new ServiceCache("cache", TimeSpan.FromSeconds(60)); var json = OpenStackNet.Serialize(cache); var result = OpenStackNet.Deserialize <ServiceCache>(json); Assert.Equal(60, result.TimeToLive.TotalSeconds); }
public void SerializePageLink() { var link = new PageLink("next", "http://api.com/next"); string json = OpenStackNet.Serialize(link); var result = OpenStackNet.Deserialize <PageLink>(json); Assert.NotNull(result); Assert.True(result.IsNextPage); }
public void WhenDeserializingNullCollection_ItShouldUseAnEmptyCollection() { var thing = new ExampleThing { Messages = null }; string json = OpenStackNet.Serialize(thing); Assert.DoesNotContain("messages", json); var result = OpenStackNet.Deserialize <ExampleThing>(json); Assert.NotNull(result.Messages); Assert.Empty(result.Messages); }
public void Deserialize() { string json = JObject.Parse("{'port':{'extra_dhcp_opts':[{'opt_name':'a','opt_value':'stuff'},{'opt_name':'b','opt_value':'things'}]}}").ToString(Formatting.None); var result = OpenStackNet.Deserialize <PortCreateDefinition>(json).DHCPOptions; Assert.NotNull(result); Assert.Equal(2, result.Count); Assert.Contains("a", result.Keys); Assert.Contains("b", result.Keys); Assert.Equal("stuff", result["a"]); Assert.Equal("things", result["b"]); }
public void OpenStackNet_UsesDHCPOptionConverter() { var port = new Port { DHCPOptions = new Dictionary <string, string> { { "a", "stuff" } } }; var json = OpenStackNet.Serialize(port); var result = OpenStackNet.Deserialize <Port>(json); Assert.NotNull(result.DHCPOptions); Assert.Equal(1, result.DHCPOptions.Count); }
public void DeserializeSecurityGroupRule() { const string json = @"{ 'security_group_rule': { 'from_port': 22, 'group': {}, 'ip_protocol': 'tcp', 'to_port': 22, 'parent_group_id': 'bfa0c0ed-274b-4711-b3ee-37d35660fb06', 'ip_range': { 'cidr': '0.0.0.0 / 24' }, 'id': '55d75417-37df-48e2-96aa-20ba53a82900' }}"; var result = OpenStackNet.Deserialize <SecurityGroupRule>(JObject.Parse(json).ToString()); Assert.Equal("0.0.0.0 / 24", result.CIDR); }
public void SerializeServiceCollection() { var services = new ServiceCollection { Items = { new Service { Id = "service-id" } }, Links = { new PageLink("next", "http://api.com/next") } }; string json = OpenStackNet.Serialize(services); Assert.Contains("\"services\"", json); Assert.DoesNotContain("\"service\"", json); var result = OpenStackNet.Deserialize <ServiceCollection>(json); Assert.NotNull(result); Assert.Single(result); Assert.Single(result.Items); Assert.Single(result.Links); Assert.True(result.HasNextPage); }
public void WhenValueIsUnrecognized_AndDestinationIsNullable_UseNull() { var result = OpenStackNet.Deserialize <StuffStatus?>("\"bad-enum-value\""); Assert.Null(result); }
public void WhenValueIsUnrecognized_AndUnknownIsNotPresent_MatchToFirstValue() { var result = OpenStackNet.Deserialize <StuffStatus>("\"bad-enum-value\""); Assert.Equal(StuffStatus.Missing, result); }
public void WhenValueIsUnrecognized_MatchToUnknownValue() { var result = OpenStackNet.Deserialize <ThingStatus>("\"bad-enum-value\""); Assert.Equal(ThingStatus.Unknown, result); }
public void WhenAttributedValueIsRecognized_MatchToValue() { var result = OpenStackNet.Deserialize <ThingStatus>("\"REMOVE_FAILED\""); Assert.Equal(ThingStatus.RemoveFailed, result); }
public void WhenValueIsRecognized_MatchToValue() { var result = OpenStackNet.Deserialize <ThingStatus>("\"ACTIVE\""); Assert.Equal(ThingStatus.Active, result); }
/// <summary> /// Initializes a new instance of the <see cref="ServiceOperationFailedException"/> class. /// </summary> /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param> /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param> private ServiceOperationFailedException(SerializationInfo info, StreamingContext context) : base(info, context) { Errors = OpenStackNet.Deserialize <IEnumerable <ServiceError> >(info.GetString("service_errors")); }