|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Couchbase; |
| 7 | +using Couchbase.Management.Buckets; |
| 8 | + |
| 9 | +namespace CodingWithCalvin.CouchbaseExplorer.Services |
| 10 | +{ |
| 11 | + public class ClusterConnection : IDisposable |
| 12 | + { |
| 13 | + public ICluster Cluster { get; } |
| 14 | + public bool HasQueryService { get; private set; } |
| 15 | + public bool HasKvService { get; private set; } |
| 16 | + public List<string> AvailableServices { get; private set; } = new List<string>(); |
| 17 | + |
| 18 | + public ClusterConnection(ICluster cluster) |
| 19 | + { |
| 20 | + Cluster = cluster; |
| 21 | + } |
| 22 | + |
| 23 | + public async Task DetectServicesAsync() |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + await Cluster.WaitUntilReadyAsync(TimeSpan.FromSeconds(10)); |
| 28 | + |
| 29 | + // Try to detect services by attempting operations |
| 30 | + HasKvService = true; // KV is always available if connected |
| 31 | + |
| 32 | + // Check for query service by trying to get buckets (uses management API) |
| 33 | + try |
| 34 | + { |
| 35 | + await Cluster.Buckets.GetAllBucketsAsync(); |
| 36 | + HasQueryService = true; |
| 37 | + AvailableServices.Add("Query"); |
| 38 | + } |
| 39 | + catch |
| 40 | + { |
| 41 | + HasQueryService = false; |
| 42 | + } |
| 43 | + |
| 44 | + AvailableServices.Add("KV"); |
| 45 | + } |
| 46 | + catch (Exception) |
| 47 | + { |
| 48 | + // Service detection failed, assume basic services |
| 49 | + HasKvService = true; |
| 50 | + AvailableServices.Add("KV"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void Dispose() |
| 55 | + { |
| 56 | + Cluster?.Dispose(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public class BucketInfo |
| 61 | + { |
| 62 | + public string Name { get; set; } |
| 63 | + public BucketType BucketType { get; set; } |
| 64 | + public long RamQuotaMB { get; set; } |
| 65 | + public int NumReplicas { get; set; } |
| 66 | + } |
| 67 | + |
| 68 | + public class ScopeInfo |
| 69 | + { |
| 70 | + public string Name { get; set; } |
| 71 | + public List<CollectionInfo> Collections { get; set; } = new List<CollectionInfo>(); |
| 72 | + } |
| 73 | + |
| 74 | + public class CollectionInfo |
| 75 | + { |
| 76 | + public string Name { get; set; } |
| 77 | + public string ScopeName { get; set; } |
| 78 | + } |
| 79 | + |
| 80 | + public static class CouchbaseService |
| 81 | + { |
| 82 | + private static readonly Dictionary<string, ClusterConnection> _connections = new Dictionary<string, ClusterConnection>(); |
| 83 | + |
| 84 | + public static async Task<ClusterConnection> ConnectAsync(string connectionId, string connectionString, string username, string password, bool useSsl) |
| 85 | + { |
| 86 | + // Enable TLS 1.2/1.3 explicitly for .NET Framework |
| 87 | + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13; |
| 88 | + |
| 89 | + if (_connections.TryGetValue(connectionId, out var existing)) |
| 90 | + { |
| 91 | + return existing; |
| 92 | + } |
| 93 | + |
| 94 | + var options = new ClusterOptions |
| 95 | + { |
| 96 | + UserName = username, |
| 97 | + Password = password, |
| 98 | + KvTimeout = TimeSpan.FromSeconds(30), |
| 99 | + ManagementTimeout = TimeSpan.FromSeconds(30), |
| 100 | + QueryTimeout = TimeSpan.FromSeconds(30) |
| 101 | + }; |
| 102 | + |
| 103 | + // Check if this is a Capella connection |
| 104 | + var isCapella = connectionString.Contains(".cloud.couchbase.com"); |
| 105 | + |
| 106 | + if (useSsl || isCapella) |
| 107 | + { |
| 108 | + options.EnableTls = true; |
| 109 | + } |
| 110 | + |
| 111 | + // Capella-specific configuration |
| 112 | + if (isCapella) |
| 113 | + { |
| 114 | + options.EnableDnsSrvResolution = true; |
| 115 | + options.KvIgnoreRemoteCertificateNameMismatch = true; |
| 116 | + options.HttpIgnoreRemoteCertificateMismatch = true; |
| 117 | + options.ForceIPv4 = true; |
| 118 | + } |
| 119 | + |
| 120 | + // Build connection string with protocol |
| 121 | + var fullConnectionString = connectionString; |
| 122 | + if (!connectionString.StartsWith("couchbase://") && !connectionString.StartsWith("couchbases://")) |
| 123 | + { |
| 124 | + fullConnectionString = useSsl ? $"couchbases://{connectionString}" : $"couchbase://{connectionString}"; |
| 125 | + } |
| 126 | + |
| 127 | + // Connect on background thread to avoid UI blocking |
| 128 | + var cluster = await Task.Run(async () => |
| 129 | + { |
| 130 | + return await Cluster.ConnectAsync(fullConnectionString, options); |
| 131 | + }).ConfigureAwait(true); |
| 132 | + |
| 133 | + var connection = new ClusterConnection(cluster); |
| 134 | + |
| 135 | + await Task.Run(async () => |
| 136 | + { |
| 137 | + await connection.DetectServicesAsync(); |
| 138 | + }).ConfigureAwait(true); |
| 139 | + |
| 140 | + _connections[connectionId] = connection; |
| 141 | + return connection; |
| 142 | + } |
| 143 | + |
| 144 | + public static async Task DisconnectAsync(string connectionId) |
| 145 | + { |
| 146 | + if (_connections.TryGetValue(connectionId, out var connection)) |
| 147 | + { |
| 148 | + _connections.Remove(connectionId); |
| 149 | + connection.Dispose(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public static ClusterConnection GetConnection(string connectionId) |
| 154 | + { |
| 155 | + _connections.TryGetValue(connectionId, out var connection); |
| 156 | + return connection; |
| 157 | + } |
| 158 | + |
| 159 | + public static async Task<List<BucketInfo>> GetBucketsAsync(string connectionId) |
| 160 | + { |
| 161 | + var connection = GetConnection(connectionId); |
| 162 | + if (connection == null) |
| 163 | + { |
| 164 | + throw new InvalidOperationException("Not connected to cluster"); |
| 165 | + } |
| 166 | + |
| 167 | + var buckets = await connection.Cluster.Buckets.GetAllBucketsAsync(); |
| 168 | + |
| 169 | + return buckets.Values.Select(b => new BucketInfo |
| 170 | + { |
| 171 | + Name = b.Name, |
| 172 | + BucketType = b.BucketType, |
| 173 | + RamQuotaMB = (long)(b.RamQuotaMB), |
| 174 | + NumReplicas = b.NumReplicas |
| 175 | + }).OrderBy(b => b.Name).ToList(); |
| 176 | + } |
| 177 | + |
| 178 | + public static async Task<List<ScopeInfo>> GetScopesAsync(string connectionId, string bucketName) |
| 179 | + { |
| 180 | + var connection = GetConnection(connectionId); |
| 181 | + if (connection == null) |
| 182 | + { |
| 183 | + throw new InvalidOperationException("Not connected to cluster"); |
| 184 | + } |
| 185 | + |
| 186 | + var bucket = await connection.Cluster.BucketAsync(bucketName); |
| 187 | + var scopes = await bucket.Collections.GetAllScopesAsync(); |
| 188 | + |
| 189 | + return scopes.Select(s => new ScopeInfo |
| 190 | + { |
| 191 | + Name = s.Name, |
| 192 | + Collections = s.Collections.Select(c => new CollectionInfo |
| 193 | + { |
| 194 | + Name = c.Name, |
| 195 | + ScopeName = s.Name |
| 196 | + }).OrderBy(c => c.Name).ToList() |
| 197 | + }).OrderBy(s => s.Name).ToList(); |
| 198 | + } |
| 199 | + |
| 200 | + public static async Task<List<CollectionInfo>> GetCollectionsAsync(string connectionId, string bucketName, string scopeName) |
| 201 | + { |
| 202 | + var scopes = await GetScopesAsync(connectionId, bucketName); |
| 203 | + var scope = scopes.FirstOrDefault(s => s.Name == scopeName); |
| 204 | + return scope?.Collections ?? new List<CollectionInfo>(); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments