|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright 2012 Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Text.RegularExpressions; |
| 19 | +using System.Threading; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using Azure; |
| 22 | +using Azure.Data.Tables; |
| 23 | +using Azure.Data.Tables.Models; |
| 24 | +using Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel; |
| 25 | +using Microsoft.WindowsAzure.Commands.Storage.Model.Contract; |
| 26 | + |
| 27 | +namespace Microsoft.WindowsAzure.Commands.Storage.Test.Service |
| 28 | +{ |
| 29 | + public partial class MockStorageTableManagement : IStorageTableManagement |
| 30 | + { |
| 31 | + private List<TableItem> v2Tables = new List<TableItem>(); |
| 32 | + |
| 33 | + private Dictionary<string, TableSignedIdentifier> signedIdentifiers = new Dictionary<string, TableSignedIdentifier>(); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets an instance of a AzureStorageTable wrapping TableClient configured with the current TableServiceClient options, |
| 37 | + /// affinitized to the specified tableName. |
| 38 | + /// </summary> |
| 39 | + /// <param name="tableName"></param> |
| 40 | + /// <returns></returns> |
| 41 | + public AzureStorageTable GetAzureStorageTable(string tableName) |
| 42 | + { |
| 43 | + return new AzureStorageTable(new TableClient(new Uri($"{MockStorageTableManagement.TableEndPoint}{tableName}"))); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets a list of tables from the storage account. |
| 48 | + /// </summary> |
| 49 | + /// <param name="filter">Table name filter expression.</param> |
| 50 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 51 | + /// <returns></returns> |
| 52 | + public IEnumerable<TableItem> QueryTables(string filter, CancellationToken cancellationToken) |
| 53 | + { |
| 54 | + if (string.IsNullOrEmpty(filter)) |
| 55 | + { |
| 56 | + return this.v2Tables; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Match match = Regex.Match(filter, "^TableName eq '(.+?)'"); |
| 61 | + if (match.Success) |
| 62 | + { |
| 63 | + string tableName = match.Groups[1].Value; |
| 64 | + return this.v2Tables.Where(t => t.Name.Equals(tableName, StringComparison.OrdinalIgnoreCase)); |
| 65 | + } |
| 66 | + |
| 67 | + match = Regex.Match(filter, "^TableName ge '(.+?)'"); |
| 68 | + if (match.Success) |
| 69 | + { |
| 70 | + string tableNamePrefix = match.Groups[1].Value; |
| 71 | + return this.v2Tables.Where(t => t.Name.StartsWith(tableNamePrefix, StringComparison.OrdinalIgnoreCase)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + Console.WriteLine($"Unsupported table query filter string: {filter}"); |
| 76 | + throw new NotImplementedException(); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Gets the properties of an account's Table service, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. |
| 81 | + /// </summary> |
| 82 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 83 | + /// <returns></returns> |
| 84 | + public Response<TableServiceProperties> GetProperties(CancellationToken cancellationToken) |
| 85 | + { |
| 86 | + throw new NotImplementedException(); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Sets properties for an account's Table service endpoint, including properties for Analytics and CORS (Cross-Origin Resource Sharing) rules. |
| 91 | + /// </summary> |
| 92 | + /// <param name="properties">The Table Service properties.</param> |
| 93 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 94 | + /// <returns></returns> |
| 95 | + public Response SetProperties(TableServiceProperties properties, CancellationToken cancellationToken) |
| 96 | + { |
| 97 | + throw new NotImplementedException(); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Creates a table on the service. |
| 102 | + /// </summary> |
| 103 | + /// <param name="tableName"></param> |
| 104 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 105 | + /// <returns>True if table was created; otherwise, false.</returns> |
| 106 | + public bool CreateTableIfNotExists(string tableName, CancellationToken cancellationToken) |
| 107 | + { |
| 108 | + if (this.v2Tables.Any(t => t.Name.Equals(tableName, StringComparison.OrdinalIgnoreCase))) |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + this.v2Tables.Add(new TableItem(tableName)); |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Deletes the table on the service. |
| 119 | + /// </summary> |
| 120 | + /// <param name="tableName"></param> |
| 121 | + /// <param name="cancellationToken"></param> |
| 122 | + /// <returns>True if table was deleted; otherwise, false.</returns> |
| 123 | + public bool DeleteTable(string tableName, CancellationToken cancellationToken) |
| 124 | + { |
| 125 | + TableItem table = this.v2Tables |
| 126 | + .Where(t => t.Name.Equals(tableName, StringComparison.OrdinalIgnoreCase)) |
| 127 | + .FirstOrDefault(); |
| 128 | + |
| 129 | + if (table == null) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + this.v2Tables.Remove(table); |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Retrieves details about any stored access policies specified on the table that may be used with Shared Access Signatures. |
| 140 | + /// </summary> |
| 141 | + /// <param name="tableName"></param> |
| 142 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 143 | + /// <returns></returns> |
| 144 | + public IEnumerable<TableSignedIdentifier> GetAccessPolicies(string tableName, CancellationToken cancellationToken) |
| 145 | + { |
| 146 | + return this.signedIdentifiers.Values.ToList(); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Retrieves details about any stored access policies specified on the table that may be used with Shared Access Signatures. |
| 151 | + /// </summary> |
| 152 | + /// <param name="tableName"></param> |
| 153 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 154 | + /// <returns></returns> |
| 155 | + public Task<IEnumerable<TableSignedIdentifier>> GetAccessPoliciesAsync(string tableName, CancellationToken cancellationToken) |
| 156 | + { |
| 157 | + return Task.FromResult(this.GetAccessPolicies(tableName, cancellationToken)); |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Sets stored access policies for the table that may be used with Shared Access Signatures. |
| 162 | + /// </summary> |
| 163 | + /// <param name="tableName"></param> |
| 164 | + /// <param name="identifiers">The access policies for the table.</param> |
| 165 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 166 | + /// <returns></returns> |
| 167 | + public Response SetAccessPolicies(string tableName, IEnumerable<TableSignedIdentifier> identifiers, CancellationToken cancellationToken) |
| 168 | + { |
| 169 | + throw new NotImplementedException(); |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Queries entities in the table. |
| 174 | + /// </summary> |
| 175 | + /// <typeparam name="T">A custom model type that implements ITableEntity or an instance of TableEntity.</typeparam> |
| 176 | + /// <param name="tableName"></param> |
| 177 | + /// <param name="filter">Returns only entities that satisfy the specified OData filter. For example, "PartitionKey eq 'foo'".</param> |
| 178 | + /// <param name="maxPerPage">The maximum number of entities that will be returned per page.</param> |
| 179 | + /// <param name="selects">An IEnumerable<T> of entity property names that selects which set of entity properties to return in the result set.</param> |
| 180 | + /// <param name="cancellationToken">A CancellationToken controlling the request lifetime.</param> |
| 181 | + /// <returns></returns> |
| 182 | + public IEnumerable<T> QueryTableEntities<T>(string tableName, string filter, int maxPerPage, IEnumerable<string> selects, CancellationToken cancellationToken) |
| 183 | + where T : class, ITableEntity, new() |
| 184 | + { |
| 185 | + throw new NotImplementedException(); |
| 186 | + } |
| 187 | + |
| 188 | + public void ClearAndAddTestTableV2(params string[] tableNames) |
| 189 | + { |
| 190 | + this.v2Tables.Clear(); |
| 191 | + |
| 192 | + foreach (string tableName in tableNames) |
| 193 | + { |
| 194 | + this.v2Tables.Add(new TableItem(tableName)); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public void ClearTestSignedIdentifiers() |
| 199 | + { |
| 200 | + this.signedIdentifiers.Clear(); |
| 201 | + } |
| 202 | + |
| 203 | + public void ClearAndAddTestSignedIdentifiers(params Tuple<string, TableAccessPolicy>[] accessPolicies) |
| 204 | + { |
| 205 | + this.signedIdentifiers.Clear(); |
| 206 | + |
| 207 | + foreach (Tuple<string, TableAccessPolicy> accessPolicy in accessPolicies) |
| 208 | + { |
| 209 | + this.signedIdentifiers.Add( |
| 210 | + accessPolicy.Item1, |
| 211 | + new TableSignedIdentifier(accessPolicy.Item1, accessPolicy.Item2)); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments