|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Security.Cryptography; |
| 7 | +using System.Text; |
| 8 | +using JSIStudios.SimpleRESTServices.Client; |
| 9 | +using JSIStudios.SimpleRESTServices.Client.Json; |
| 10 | +using net.openstack.Core; |
| 11 | +using net.openstack.Core.Domain; |
| 12 | +using net.openstack.Core.Exceptions; |
| 13 | +using net.openstack.Core.Exceptions.Response; |
| 14 | +using net.openstack.Providers.Rackspace.Exceptions; |
| 15 | +using net.openstack.Providers.Rackspace.Objects.Request; |
| 16 | +using net.openstack.Providers.Rackspace.Objects.Response; |
| 17 | + |
| 18 | +namespace net.openstack.Providers.Rackspace |
| 19 | +{ |
| 20 | + public class CloudNetworksProvider : ProviderBase, ICloudNetworksProvider |
| 21 | + { |
| 22 | + private readonly int[] _validResponseCode = new[] { 200, 201, 202 }; |
| 23 | + |
| 24 | + #region Constructors |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Creates a new instance of the Rackspace <see cref="net.openstack.Providers.Rackspace.CloudNetworksProvider"/> class. |
| 28 | + /// </summary> |
| 29 | + public CloudNetworksProvider() |
| 30 | + : this(null) { } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Creates a new instance of the Rackspace <see cref="net.openstack.Providers.Rackspace.CloudNetworksProvider"/> class. |
| 34 | + /// </summary> |
| 35 | + /// <param name="identity">An instance of a <see cref="net.openstack.Core.Domain.CloudIdentity"/> object.<remarks>[Optional]: If not provided, the user will be required to pass a <see cref="net.openstack.Core.Domain.CloudIdentity"/> object to each method individually.</remarks></param> |
| 36 | + public CloudNetworksProvider(CloudIdentity identity) |
| 37 | + : base(identity, new CloudIdentityProvider(), new JsonRestServices()) { } |
| 38 | + |
| 39 | + #endregion |
| 40 | + |
| 41 | + |
| 42 | + #region Networks |
| 43 | + |
| 44 | + public IEnumerable<CloudNetwork> ListNetworks(string region = null, CloudIdentity identity = null) |
| 45 | + { |
| 46 | + var urlPath = new Uri(string.Format("{0}/os-networksv2", GetServiceEndpoint(identity, region))); |
| 47 | + var response = ExecuteRESTRequest<ListCloudNetworksResponse>(identity, urlPath, HttpMethod.GET); |
| 48 | + |
| 49 | + if (response == null || response.Data == null) |
| 50 | + return null; |
| 51 | + |
| 52 | + return response.Data.Networks; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + public CloudNetwork CreateNetwork(string cidr, string label, string region = null, CloudIdentity identity = null) |
| 57 | + { |
| 58 | + var urlPath = new Uri(string.Format("{0}/os-networksv2", GetServiceEndpoint(identity, region))); |
| 59 | + var cloudNetworkRequest = new CreateCloudNetworkRequest { Details = new CreateCloudNetworksDetails { Cidr = cidr, Label = label } }; |
| 60 | + |
| 61 | + var response = ExecuteRESTRequest<CloudNetworkResponse>(identity, urlPath, HttpMethod.POST, cloudNetworkRequest); |
| 62 | + |
| 63 | + if (response == null || response.Data == null) |
| 64 | + return null; |
| 65 | + |
| 66 | + return response.Data.Network; |
| 67 | + } |
| 68 | + |
| 69 | + public CloudNetwork ShowNetwork(string network_id, string region = null, CloudIdentity identity = null) |
| 70 | + { |
| 71 | + var urlPath = new Uri(string.Format("{0}/os-networksv2/{1}", GetServiceEndpoint(identity, region), network_id)); |
| 72 | + var response = ExecuteRESTRequest<CloudNetworkResponse>(identity, urlPath, HttpMethod.GET); |
| 73 | + |
| 74 | + if (response == null || response.Data == null) |
| 75 | + return null; |
| 76 | + |
| 77 | + return response.Data.Network; |
| 78 | + } |
| 79 | + |
| 80 | + public bool DeleteNetwork(string network_id, string region = null, CloudIdentity identity = null) |
| 81 | + { |
| 82 | + var urlPath = new Uri(string.Format("{0}/os-networksv2/{1}", GetServiceEndpoint(identity, region), network_id)); |
| 83 | + |
| 84 | + Response response = null; |
| 85 | + try |
| 86 | + { |
| 87 | + response = ExecuteRESTRequest(identity, urlPath, HttpMethod.DELETE); |
| 88 | + } |
| 89 | + catch(UserNotAuthorizedException ex) |
| 90 | + { |
| 91 | + if(ex.Response.StatusCode == 403) |
| 92 | + throw new UserAuthorizationException("ERROR: Cannot delete network. Ensure that all servers are removed from this network first."); |
| 93 | + } |
| 94 | + |
| 95 | + return response != null && _validResponseCode.Contains(response.StatusCode); |
| 96 | + } |
| 97 | + |
| 98 | + #endregion |
| 99 | + |
| 100 | + |
| 101 | + #region Private methods |
| 102 | + |
| 103 | + protected string GetServiceEndpoint(CloudIdentity identity = null, string region = null) |
| 104 | + { |
| 105 | + return base.GetPublicServiceEndpoint(identity, "cloudServersOpenStack", region); |
| 106 | + } |
| 107 | + |
| 108 | + #endregion |
| 109 | + |
| 110 | + } |
| 111 | +} |
0 commit comments