UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

232 lines (230 loc) 8.09 kB
import { Net } from '../data/net'; import { EnvironmentModule } from '../manifest/environment-modules'; /** * The reserved connection property name on server. */ export class ConnectionPropertiesName { static PropertyDisplayName = 'displayName'; static PropertyName = 'name'; static PropertyNetworkName = 'networkName'; static DisplayNameLocalhost = 'localhost'; /** * Check if the display name is localhost. * * @param properties the connection properties. * @returns true if it's localhost. */ static isDisplayNameLocalhost(properties) { return !!properties && properties[ConnectionPropertiesName.PropertyDisplayName] === ConnectionPropertiesName.DisplayNameLocalhost; } /** * Gets the display name. * * @param properties the connection properties. * @returns the display name property value. */ static getDisplayName(properties) { return properties && properties[ConnectionPropertiesName.PropertyDisplayName]; } /** * Gets the name. * * @param properties the connection properties. * @returns the name property value. */ static getName(properties) { return properties && properties[ConnectionPropertiesName.PropertyName]; } /** * Gets the network name. * * @param properties the connection properties. * @returns the network name property value. */ static getNetworkName(properties) { return properties && properties[ConnectionPropertiesName.PropertyNetworkName]; } } /** * Defines connection type strings known by core * Be careful that these strings match what is defined by the manifest of @msft-sme/server-manager */ export const connectionTypeConstants = { server: 'msft.sme.connection-type.server', cluster: 'msft.sme.connection-type.cluster', windowsClient: 'msft.sme.connection-type.windows-client', eflowDevice: 'microsoft.connection-type.eflow-device', clusterNodesProperty: 'nodes' }; /** * Connection Utility class. */ export class ConnectionUtility { /** * Determines if one connection is referring to the same object as another connection * * @param a the first connection in the comparison * @param b the second connection in the comparison * @returns true if the connections are of the same type and have the same name */ static areEqual(a, b) { if (!a || !b) { return a === b; } if (a.type !== b.type) { return false; } if (a.name !== b.name) { return false; } if (a.id !== b.id) { return false; } return true; } /** * Determines if the given connection is to a server * * @param connection the connection to check */ static isServer(connection) { return connection.type === connectionTypeConstants.server; } /** * Determines if the given connection is to a cluster connection. * Currently we support: HCI and Failover Cluster. * * @param connection the connection to check */ static isCluster(connection) { return connection.type === connectionTypeConstants.cluster; } /** * Determines if the given connection is to a FailOver cluster * * @param connection the connection to check */ static isFailoverCluster(connection) { return !MsftSme.isNullOrUndefined(connection.properties) && connection.properties['connectionType'] === 'cluster' || (MsftSme.isNullOrUndefined(connection.properties['connectionType']) && connection.type === connectionTypeConstants.cluster); } /** * Determines if the given connection is to a windows client * * @param connection the connection to check */ static isWindowsClient(connection) { return connection.type === connectionTypeConstants.windowsClient; } /** * Determines if the given connection is to an EFLOW device * * @param connection the connection to check */ static isEflowDevice(connection) { return connection.type === connectionTypeConstants.eflowDevice; } /** * Determines if the given connection is to a node * * @param connection the connection to check */ static isNode(connection) { return this.isServer(connection) || this.isCluster(connection) || this.isWindowsClient(connection) || this.isEflowDevice(connection); } /** * Gets the name of a node from a connection. This assumes the connection is to a single server or cluster. * * @param connection the connection object. (should be of type server or cluster) * @param throwError throw an error if not a server or cluster. */ static getNodeName(connection, throwError = false) { if (!this.isNode(connection)) { if (throwError) { const message = MsftSme.getStrings().MsftSmeShell.Core.Error.ExpectedSingleNode.message; throw new Error(message); } return null; } return connection.name; } /** * Gets the name of a valid node from a connection. This assumes the connection is to a single server or cluster. * if activeAlias is used return it, otherwise return connection.name * * @param connection the connection object. (should be of type server or cluster) * @param throwError throw an error if not a server or cluster. */ static getValidNodeName(connection, throwError = false) { if (!this.isNode(connection)) { if (throwError) { const message = MsftSme.getStrings().MsftSmeShell.Core.Error.ExpectedSingleNode.message; throw new Error(message); } return null; } if (connection.activeAlias) { return connection.activeAlias; } else { return connection.name; } } /** * Gets nodes of a connection of type cluster * * @param connection the connection object. (should be of type cluster) * @param throwError throw an error if not a cluster. */ static getClusterNodes(connection, throwError = false) { if (!this.isCluster(connection)) { if (throwError) { const message = MsftSme.getStrings().MsftSmeShell.Core.Error.ExpectedClusterNode.message; throw new Error(message); } return []; } return connection.properties[connectionTypeConstants.clusterNodesProperty]; } /** * Gets the connection type info for a given connection * * @param connection the connection object. */ static getConnectionTypeInfo(connection) { return EnvironmentModule.getConnectionTypeInfo(connection.type); } /** * creates a connection Identifier * * @param connectionType the connection type. * @param connectionName the connection name. */ static createConnectionId(connectionType, connectionName, groupId) { let id = `${connectionType.toLocaleLowerCase()}!${connectionName.toLocaleLowerCase()}`; if (groupId) { id = id + `!${groupId.toLocaleLowerCase()}`; } return id; } /** * Ensures tat important fields in a connection are lowercase */ static forceLowercase(connection) { connection.id = connection.id.toLocaleLowerCase(); connection.name = connection.name.toLocaleLowerCase(); connection.type = connection.type.toLocaleLowerCase(); } /** * Ensures tat important fields in a connection are lowercase */ static hasTags(connection) { return connection.tags && connection.tags.length > 0; } static convertToConnectionName(inputName) { return inputName.indexOf(':') < 0 ? inputName.toLowerCase() : Net.convertIPv6ToLiteral(inputName); } } //# sourceMappingURL=connection.js.map