/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using OpenSearch.Net.VirtualizedCluster.Providers; using OpenSearch.Net.VirtualizedCluster.Rules; namespace OpenSearch.Net.VirtualizedCluster { public class VirtualCluster { private readonly List _nodes; public VirtualCluster(IEnumerable nodes) => _nodes = nodes.ToList(); public List ClientCallRules { get; } = new List(); public TestableDateTimeProvider DateTimeProvider { get; } = new TestableDateTimeProvider(); public IReadOnlyList Nodes => _nodes; public List PingingRules { get; } = new List(); public List SniffingRules { get; } = new List(); internal string PublishAddressOverride { get; private set; } internal bool SniffShouldReturnFqnd { get; private set; } internal string OpenSearchVersion { get; private set; } = "1.0.0"; public VirtualCluster SniffShouldReturnFqdn() { SniffShouldReturnFqnd = true; return this; } public VirtualCluster SniffOpenSearchVersionNumber(string version) { OpenSearchVersion = version; return this; } public VirtualCluster PublishAddress(string publishHost) { PublishAddressOverride = publishHost; return this; } public VirtualCluster ClusterManagerEligible(params int[] ports) { foreach (var node in _nodes.Where(n => !ports.Contains(n.Uri.Port))) node.ClusterManagerEligible = false; return this; } [Obsolete("Use ClusterManagerEligible instead", false)] public VirtualCluster MasterEligible(params int[] ports) { return ClusterManagerEligible(ports); } public VirtualCluster StoresNoData(params int[] ports) { foreach (var node in _nodes.Where(n => ports.Contains(n.Uri.Port))) node.HoldsData = false; return this; } public VirtualCluster HasSetting(string key, string value, params int[] ports) { foreach (var node in _nodes.Where(n => ports.Contains(n.Uri.Port))) node.Settings = new ReadOnlyDictionary(new Dictionary { { key, value } }); return this; } public VirtualCluster HttpDisabled(params int[] ports) { foreach (var node in _nodes.Where(n => ports.Contains(n.Uri.Port))) node.HttpEnabled = false; return this; } public VirtualCluster Ping(Func selector) { PingingRules.Add(selector(new PingRule())); return this; } public VirtualCluster Sniff(Func selector) { SniffingRules.Add(selector(new SniffRule())); return this; } public VirtualCluster ClientCalls(Func selector) { ClientCallRules.Add(selector(new ClientCallRule())); return this; } public SealedVirtualCluster SingleNodeConnection(Func, IEnumerable> seedNodesSelector = null) { var nodes = seedNodesSelector?.Invoke(_nodes) ?? _nodes; return new SealedVirtualCluster(this, new SingleNodeConnectionPool(nodes.First().Uri), DateTimeProvider); } public SealedVirtualCluster StaticConnectionPool(Func, IEnumerable> seedNodesSelector = null) { var nodes = seedNodesSelector?.Invoke(_nodes) ?? _nodes; return new SealedVirtualCluster(this, new StaticConnectionPool(nodes, false, DateTimeProvider), DateTimeProvider); } public SealedVirtualCluster SniffingConnectionPool(Func, IEnumerable> seedNodesSelector = null) { var nodes = seedNodesSelector?.Invoke(_nodes) ?? _nodes; return new SealedVirtualCluster(this, new SniffingConnectionPool(nodes, false, DateTimeProvider), DateTimeProvider); } public SealedVirtualCluster StickyConnectionPool(Func, IEnumerable> seedNodesSelector = null) { var nodes = seedNodesSelector?.Invoke(_nodes) ?? _nodes; return new SealedVirtualCluster(this, new StickyConnectionPool(nodes, DateTimeProvider), DateTimeProvider); } public SealedVirtualCluster StickySniffingConnectionPool(Func sorter = null, Func, IEnumerable> seedNodesSelector = null ) { var nodes = seedNodesSelector?.Invoke(_nodes) ?? _nodes; return new SealedVirtualCluster(this, new StickySniffingConnectionPool(nodes, sorter, DateTimeProvider), DateTimeProvider); } } }