@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
408 lines (406 loc) • 18.4 kB
JavaScript
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { Cim } from './cim';
import { headerConstants } from './http-constants';
import { Net } from './net';
import { NodeCimOutput } from './node-connection';
import { PowerShellAlternate } from './powershell-alternate';
/**
* The CIM Connection class.
*/
export class CimConnection {
nodeConnection;
batchConnection;
/**
* Initializes a new instance of the CimService class.
*
* @param nodeConnection the NodeConnection class instance injected.
* @param batchConnection the BatchConnection class instance injected.
*/
constructor(nodeConnection, batchConnection) {
this.nodeConnection = nodeConnection;
this.batchConnection = batchConnection;
}
/**
* CIM GET MultipleInstances
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
getInstanceMultiple(nodeName, namespace, className, options) {
this.setPowerShellParameters(options, NodeCimOutput.Multiple);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlMultipleInstances(namespace, className);
return this.nodeConnection.get(nodeName, cimUrl, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM GET SingleInstance
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
getInstanceSingle(nodeName, namespace, className, keyProperties, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
return this.nodeConnection.get(nodeName, cimUrl, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM POST InstanceMethod
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param methodName the method name.
* @param keyProperties the key properties object.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
invokeMethodInstance(nodeName, namespace, className, methodName, keyProperties, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Result, keyProperties, data && data.parameters);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlInstanceMethod(namespace, className, methodName, keyProperties);
return this.nodeConnection.post(nodeName, cimUrl, data, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM POST StaticMethod
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param methodName the method name.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
invokeMethodStatic(nodeName, namespace, className, methodName, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Result, data && data.parameters);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlStaticMethod(namespace, className, methodName);
return this.nodeConnection.post(nodeName, cimUrl, data, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM PUT SingleInstance
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
setInstance(nodeName, namespace, className, keyProperties, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties, data && data.properties);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
return this.nodeConnection.put(nodeName, cimUrl, data, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM PATCH SingleInstance
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
modifyInstance(nodeName, namespace, className, keyProperties, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties, data && data.properties);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
return this.nodeConnection.patch(nodeName, cimUrl, data, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM DELETE SingleInstance
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
deleteInstance(nodeName, namespace, className, keyProperties, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
return this.nodeConnection.delete(nodeName, cimUrl, null, options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* CIM POST WqlQuery
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param query the WQL string.
* @param options the options for this request.
* @return Observable<any> the query observable.
*/
getInstanceQuery(nodeName, namespace, query, options) {
this.setPowerShellParameters(options, NodeCimOutput.Query, options && options.powerShellParameters);
const observable = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlWqlQuery(namespace);
return this.nodeConnection.post(nodeName, cimUrl, JSON.stringify({ query: query }), options)
.pipe(catchError(error => this.handleJeaFallback(error, nodeName, options)));
}
/**
* Handle the fallback from CIM to powershell with JEA, if applicable
* @param error The error to handle
* @param nodeName The node name
* @param options The request options
*/
handleJeaFallback(error, nodeName, options) {
const authError = Net.isUnauthorized(error);
const responseEndpoint = error && error.xhr && error.xhr.getResponseHeader(headerConstants.POWERSHELL_ENDPOINT);
const requestEndpoint = options && options.powerShellEndpoint;
if (authError && responseEndpoint && requestEndpoint !== responseEndpoint) {
options.powerShellEndpoint = responseEndpoint;
const powershellRequest = PowerShellAlternate.create(this.nodeConnection, nodeName, options);
if (powershellRequest) {
return powershellRequest
.pipe(tap(() => {
// The JEA request went through - persist this context in authorization manager.
this.nodeConnection.saveJeaContext(nodeName, responseEndpoint);
}));
}
}
return throwError(() => error);
}
/**********************************
* Cim Batch Section
**********************************/
/**
* CIM GET MultipleInstances for list of nodes
*
* @param nodeNamesList the Nodes to use for this request.
* @param namespace the cim namespace.
* @param className the class name.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
getBatchInstanceMultiple(nodeNamesList, namespace, className, options) {
this.setPowerShellParameters(options, NodeCimOutput.Multiple);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlMultipleInstances(namespace, className);
const urlList = [];
for (let index = 0; index < nodeNamesList.length; index++) {
urlList.push(cimUrl);
}
return this.batchConnection.get(nodeNamesList, urlList, options);
}
/**
* CIM GET SingleInstance for list of nodes
*
* @param nodeNamesList the Nodes to use for this request.
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
getBatchInstanceSingle(nodeNamesList, namespace, className, keyProperties, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
const urlList = [];
for (let index = 0; index < nodeNamesList.length; index++) {
urlList.push(cimUrl);
}
return this.batchConnection.get(nodeNamesList, urlList, options);
}
/**
* CIM POST InstanceMethod for list of nodes
*
* @param nodeNamesList the Nodes to use for this request.
* @param namespace the cim namespace.
* @param className the class name.
* @param methodName the method name.
* @param keyProperties the key properties object.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
invokeBatchMethodInstance(nodeNamesList, namespace, className, methodName, keyProperties, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Result, keyProperties, data && data.parameters);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlInstanceMethod(namespace, className, methodName, keyProperties);
const body = data ? JSON.stringify(data) : null;
return this.cimBatchPost(nodeNamesList, cimUrl, body, options);
}
/**
* CIM POST StaticMethod for list of nodes
*
* @param nodeNamesList the Nodes to use for this request.
* @param namespace the cim namespace.
* @param className the class name.
* @param methodName the method name.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
invokeBatchMethodStatic(nodeNamesList, namespace, className, methodName, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Result, data && data.parameters);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlStaticMethod(namespace, className, methodName);
const body = data ? JSON.stringify(data) : null;
return this.cimBatchPost(nodeNamesList, cimUrl, body, options);
}
/**
* CIM PUT SingleInstance for list of nodes
*
* @param nodeName the name of the node to use for this request
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param data the method input data.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
setBatchInstance(nodeNamesList, namespace, className, keyProperties, data, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties, data && data.properties);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
const urlList = [];
const dataList = [];
for (let index = 0; index < nodeNamesList.length; index++) {
urlList.push(cimUrl);
dataList.push(JSON.stringify(data));
}
return this.batchConnection.put(nodeNamesList, urlList, dataList, options);
}
/**
* CIM DELETE SingleInstance for list of nodes
*
* @param nodeNamesList the Nodes to use for this request.
* @param namespace the cim namespace.
* @param className the class name.
* @param keyProperties the key properties object.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
deleteBatchInstance(nodeNamesList, namespace, className, keyProperties, options) {
this.setPowerShellParameters(options, NodeCimOutput.Single, keyProperties);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlSingleInstance(namespace, className, keyProperties);
const urlList = [];
for (let index = 0; index < nodeNamesList.length; index++) {
urlList.push(cimUrl);
}
return this.batchConnection.delete(nodeNamesList, urlList, options);
}
/**
* CIM POST WqlQuery for list of nodes
*
* @param nodeNamesList the Nodes to use for this request.
* @param namespace the cim namespace.
* @param query the WQL string.
* @param options the options for this request.
* @return Observable<BatchResponseItem[]> the query observable.
*/
getBatchInstanceQuery(nodeNamesList, namespace, query, options) {
this.setPowerShellParameters(options, NodeCimOutput.Query, options && options.powerShellParameters);
const observable = PowerShellAlternate.createBatch(this.nodeConnection, this.batchConnection, nodeNamesList, options);
if (observable) {
return observable;
}
const cimUrl = Cim.cimUrlWqlQuery(namespace);
const body = JSON.stringify({ query: query });
return this.cimBatchPost(nodeNamesList, cimUrl, body, options);
}
/**
* Cim batch post helper
*
* @param nodeNamesList The list of Nodes to run the call against
* @param cimUrl The CIM end point to call
* @param jsonBody The body of Post in json format.
* @param options the request options.
*/
cimBatchPost(nodeNamesList, cimUrl, jsonBody, options) {
const urlList = [];
const bodyList = [];
for (let index = 0; index < nodeNamesList.length; index++) {
urlList.push(cimUrl);
if (jsonBody) {
bodyList.push(jsonBody);
}
}
return this.batchConnection.post(nodeNamesList, urlList, bodyList, options);
}
/**
* Set PowerShell parameters to the options object.
*
* @param options The node request options.
* @param outputType The output data type.
* @param keys The key data.
* @param data The arguments data.
*/
setPowerShellParameters(options, cimOutput, keys, data) {
if (options == null || options.powerShell == null) {
return;
}
options.powerShellContext = {
cimOutput,
parameters: { ...(keys || {}), ...(data || {}) }
};
}
}
//# sourceMappingURL=cim-connection.js.map