Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image.ListImages #611

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/corelib/Authentication/ServiceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public override int GetHashCode()
/// The Networking service
/// </summary>
public static readonly ServiceType Networking = new ServiceType("network");

/// <summary>
///The Image service
/// </summary>
public static readonly ServiceType Image = new ServiceType("Image");
}

/// <summary>
Expand Down
File renamed without changes.
64 changes: 64 additions & 0 deletions src/corelib/Images/v2/ImageApiBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenStack.Serialization;
using OpenStack.Authentication;
using System.Threading;
using Flurl.Http;
using Flurl.Extensions;
using OpenStack.Images.v2.Serialization;

namespace OpenStack.Images.v2
{
/// <summary>
/// Builds requests to the Image API which can be further customized and then executed.
/// <para>Intended for custom implementations.</para>
/// </summary>
/// <seealso href="http://developer.openstack.org/api-ref-image-v2.html">OpenStack Image API v2 Reference</seealso>
public class ImageApiBuilder
{
private readonly ServiceEndpoint _endpoint;
private readonly IAuthenticationProvider _authProvider;

/// <summary>
/// Initializes a new instance of the <see cref="ImageApiBuilder"/> class.
/// </summary>
/// <param name="serviceType">The service type for the desired image provider.</param>
/// <param name="authenticationProvider">The authentication provider.</param>
/// <param name="region">The region.</param>
/// <param name="useInternalUrl">if set to <c>true</c> uses the internal URLs specified in the ServiceCatalog, otherwise the public URLs are used.</param>
public ImageApiBuilder(IServiceType serviceType, IAuthenticationProvider authenticationProvider, string region, bool useInternalUrl = false)
{
_authProvider = authenticationProvider;
_endpoint = new ServiceEndpoint(serviceType, authenticationProvider, region, useInternalUrl);
}

#region Image
/// <summary>
/// List all images associated with the account
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>
/// return the collections of resource associated with the accout
/// </returns>
public virtual async Task<PreparedRequest> BuildListImageAsync (CancellationToken cancellationToken)
{
var endpoint = await _endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false);
return endpoint
.AppendPathSegment("images")
.Authenticate(_authProvider)
.PrepareGet();
}

/// <inheritdoc cref="ImageApiBuilder.ListImageAsync(CancellationToken)" />
public async Task<IEnumerable<ImageOptions>> ListImageAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return await BuildListImageAsync(cancellationToken)
.SendAsync()
.ReceiveJson<ImageOptionsCollection>();
}
#endregion Image
}
}
134 changes: 134 additions & 0 deletions src/corelib/Images/v2/ImageOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using OpenStack.Serialization;

namespace OpenStack.Images.v2
{

/// <summary>
///Represent the image resource of <seealso cref="ImageService"/>
/// </summary>
public class ImageOptions
{
/// <summary>
///The image identifier
/// </summary>
[JsonProperty("id")]
public Identifier Id;

/// <summary>
///The name of the image.
/// </summary>
[JsonProperty("name")]
public string Name;

/// <summary>
/// The image visibility. A valid value is public or private. Default is private.
/// </summary>
[JsonProperty("visibility")]
public string Visibility;

/// <summary>
///The ID of the owner, or tenant, of the image.
///The value might be null (JSON null data type).
/// </summary>
[JsonProperty("owner")]
public string Owner;

/// <summary>
///Image protection for deletion.A valid value is True or False.Default is False.
/// </summary>
[JsonProperty("protected")]
public bool Protected;

/// <summary>
///The size of the image data, in bytes.
/// </summary>
[JsonProperty("size")]
public String Size;

/// <summary>
///The image status
/// </summary>
[JsonProperty("status")]
public ImageStatus Status;

/// <summary>
/// The date and time when the resource was updated.
/// </summary>
[JsonProperty("updated_at")]
public DateTime? UpdatedAt;

/// <summary>
///The disk format of the image.
/// </summary>
[JsonProperty("disk_format")]
public string DiskFormat;

/// <summary>
///The minimum disk size in GB that is required to boot the image.
/// </summary>
[JsonProperty("min_disk")]
public string MinDisk;

/// <summary>
///The minimum amount of RAM in MB that is required to boot the image.
/// </summary>
[JsonProperty("min_ram")]
public string MinRam;

/// <summary>
///The date and time when the resource was created.
/// </summary>
[JsonProperty("created_at")]
public DateTime? CreatedAt;

/// <summary>
///The container format of the image.
/// </summary>
[JsonProperty("container_format")]
public string ContainerFormat;

/// <summary>
///The list of tag object
/// </summary>
[JsonProperty("tags")]
public IList<string> Tags;

/// <summary>
///Hash that is used over the image data
/// </summary>
[JsonProperty("checksum")]
public string Checksum;


/// <summary>
///The URL for the virtual machine image.
/// </summary>
[JsonProperty("self")]
public string Self;

/// <summary>
///The virtual size of the image.
/// </summary>
[JsonProperty("virtual_size")]
public string VirtualSize;

/// <summary>
///The URL for the virtual machine image file.
/// </summary>
[JsonProperty("file")]
public string File;

/// <summary>
///The URL for schema of the virtual machine image.
/// </summary>
[JsonProperty("schema")]
public string Schema;

}
}
41 changes: 41 additions & 0 deletions src/corelib/Images/v2/ImageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenStack.Authentication;
using OpenStack.Serialization;
using System.Threading;
using OpenStack.Images.v2.Serialization;
using Flurl.Http;
using Flurl.Extensions;

namespace OpenStack.Images.v2
{
/// <summary>
///Ihe Openstack image service
///<seealso href="http://developer.openstack.org/api-ref-image-v2.html"/>
/// </summary>
public class ImageService
{

internal readonly ImageApiBuilder _imageApiBuilder;

/// <summary>
/// Initializes a new instance of the <see cref="ImageService"/> class.
/// </summary>
/// <param name="authenticationProvider">The authentication provider.</param>
/// <param name="region">The region.</param>
/// <param name="useInternalUrl">if set to <c>true</c> uses the internal URLs specified in the ServiceCatalog, otherwise the public URLs are used.</param>
public ImageService(IAuthenticationProvider authenticationProvider, string region, bool useInternalUrl = false)
{
_imageApiBuilder = new ImageApiBuilder(ServiceType.Image, authenticationProvider, region, useInternalUrl);
}

/// <inheritdoc cref="ImageApiBuilder.ListImageAsync(CancellationToken)" />
public async Task<IEnumerable<ImageOptions>> ListImageAsync(CancellationToken cancellationToken=default(CancellationToken))
{
return await _imageApiBuilder.ListImageAsync(cancellationToken);
}
}
}
32 changes: 32 additions & 0 deletions src/corelib/Images/v2/ImageServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenStack.Images.v2;
using Flurl.Http;
using Flurl.Extensions;
using OpenStack.Images.v2.Serialization;
using OpenStack.Synchronous.Extensions;

namespace OpenStack.Images.v2.Synchronous
{
/// <summary>
///Expose synchronous extensions methods for <seealso cref="ImageService"/>
/// </summary>
public static class ImageServiceExtension
{
#region Image
/// <summary>
///
/// </summary>
/// <param name="service"></param>
/// <returns></returns>
public static IEnumerable<ImageOptions> ListImages(this ImageService service)
{
return service._imageApiBuilder.ListImageAsync().ForceSynchronous();
}
#endregion

}
}
33 changes: 33 additions & 0 deletions src/corelib/Images/v2/Serialization/ImageOptionsCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using OpenStack.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenStack.Images.v2.Serialization
{

/// <summary>
///Represent collection of the <seealso cref="ImageOptions"/>
/// </summary>
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "images")]
public class ImageOptionsCollection : List<ImageOptions>
{
/// <summary>
///initializes the new instance of <seealso cref="ImageOptionsCollection"/>
/// </summary>
public ImageOptionsCollection()
{
}

/// <summary>
///initializes the new instance of <seealso cref="ImageOptionsCollection"/>
/// </summary>
/// <param name="networks"></param>
public ImageOptionsCollection(IEnumerable<ImageOptions> networks) : base(networks)
{
}

}
}
7 changes: 6 additions & 1 deletion src/corelib/OpenStack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@
<Compile Include="Extensions\TypeExtensions.cs" />
<Compile Include="Flurl\PreparedRequest.cs" />
<Compile Include="Identifier.cs" />
<Compile Include="Images\v2\ImageStatus.cs" />
<Compile Include="Images\v2\ImageApiBuilder.cs" />
<Compile Include="Images\v2\ImageOptions.cs" />
<Compile Include="Images\v2\ImageService.cs" />
<Compile Include="Images\ImageStatus.cs" />
<Compile Include="Images\v2\ImageServiceExtension.cs" />
<Compile Include="Images\v2\Serialization\ImageOptionsCollection.cs" />
<Compile Include="Images\v2\Serialization\ImageStatus.cs" />
<Compile Include="Networking\IPVersion.cs" />
<Compile Include="Networking\NamespaceDoc.cs" />
Expand Down
43 changes: 43 additions & 0 deletions src/testing/unit/Image/v2/ImageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenStack.Images.v2;
using Xunit;
using Flurl.Http.Testing;
using OpenStack.Compute.v2_1.Serialization;
using OpenStack.Images.v2.Serialization;
using OpenStack.Images.v2.Synchronous;

namespace OpenStack.Image.v2
{
public class ImageTests
{
private readonly ImageService _imageService;

public ImageTests()
{
_imageService = new ImageService(Stubs.AuthenticationProvider, "region");
}

[Fact]
public void ListNetworks()
{
using (var httpTest = new HttpTest())
{
Identifier imageId = Guid.NewGuid();
ImageOptions image = new ImageOptions { Id = imageId, Status = ImageStatus.Active };
httpTest.RespondWithJson(new ImageOptionsCollection { image});

var images = _imageService.ListImages();

httpTest.ShouldHaveCalled("*/images");
Assert.NotNull(images);
Assert.Equal(1, images.Count());
Assert.Equal(imageId, images.First().Id);
Assert.Equal(ImageStatus.Active, images.First().Status);
}
}
}
}
1 change: 1 addition & 0 deletions src/testing/unit/OpenStack.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<Compile Include="Extensions\EnumerableExtensionsTests.cs" />
<Compile Include="Extensions\TypeExtensionsTests.cs" />
<Compile Include="IdentifierTests.cs" />
<Compile Include="Image\v2\ImageTests.cs" />
<Compile Include="Networking\v2\DHCPOptionConverterTests.cs" />
<Compile Include="Networking\v2\Layer3\Layer3Tests.cs" />
<Compile Include="Networking\v2\PortTests.cs" />
Expand Down