/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ import lb = require ( "oci-loadbalancer" ) ; import common = require ( "oci-common" ) ; const provider = new common . ConfigFileAuthenticationDetailsProvider ( ) ; param args const loadBalancerId = "<INSERT_LOAD_BALANCER_OCID>" ; const ruleSetName = "RandomRuleSet" ; const listenerPort = 8080 ; const listenerName = "newListenter" ; const backendSetName = "<INSERT_BACKEND_SET_NAME>" ; const listenerProtocol = "HTTP" ; const lbClient = new lb . LoadBalancerClient ( { authenticationDetailsProvider : provider } ) ; const lbWaiter = lbClient . createWaiters ( ) ; console . log ( "Creating new rule set " + ruleSetName + "..." ) ; ( async ( ) => { try { const ruleSet = await createRuleSetDetails ( lbClient , loadBalancerId , ruleSetName ) ; console . log ( "Rule set " + ruleSetName + " is created..." ) ; console . log ( "Creating new listener " + listenerName + " with rule set " + ruleSetName + "..." ) ; await createListener ( lbClient , loadBalancerId , listenerPort , listenerName , backendSetName , listenerProtocol , ruleSet ) ; console . log ( "Listener " + listenerName + " with rule set " + ruleSetName + " is created..." ) ; } catch ( error ) { console . log ( " Not able to run Loadbalancer Example " + error ) ; } } ) ( ) ; async function createRuleSetDetails ( client , loadBalancerId , ruleSetName ) { let rules = new Array ( ) ; const httpRequestHeaderRule = { header : "someRequestHeader" , value : "/example/1" , action : "ADD_HTTP_REQUEST_HEADER" } ; const extendedHttpRequestHeaderRule = { header : "someResponseHeader" , suffix : "Some-static-suffix-value" , prefix : "Some-static-prefix-value" , action : "EXTEND_HTTP_REQUEST_HEADER_VALUE" } ; const removeHttpResponseHeaderValue = { header : "someResponseHeader" , action : "REMOVE_HTTP_REQUEST_HEADER" } ; rules . push ( httpRequestHeaderRule ) ; rules . push ( extendedHttpRequestHeaderRule ) ; rules . push ( removeHttpResponseHeaderValue ) ; const createRuleSetDetails = { name : ruleSetName , items : rules } ; const createRuleSetRequest = { createRuleSetDetails : createRuleSetDetails , loadBalancerId : loadBalancerId } ; const response = await client . createRuleSet ( createRuleSetRequest ) ; const getWorkRequestRequest = { workRequestId : response . opcWorkRequestId } ; await lbWaiter . forWorkRequest ( getWorkRequestRequest ) ; const getRuleSetRequest = { loadBalancerId : loadBalancerId , ruleSetName : ruleSetName } ; const getRuleSetResponse = await client . getRuleSet ( getRuleSetRequest ) ; return getRuleSetResponse . ruleSet ; } async function createListener ( client , loadBalancerId , listenerPort , listenerName , backendSetName , listenerProtocol , ruleSet ) { const createListenerDetails = { name : listenerName , defaultBackendSetName : backendSetName , port : listenerPort , protocol : listenerProtocol , ruleSetNames : [ ruleSet . name ] } ; const createListenerRequest = { createListenerDetails : createListenerDetails , loadBalancerId : loadBalancerId } ; const response = await client . createListener ( createListenerRequest ) ; const getWorkRequestRequest = { workRequestId : response . opcWorkRequestId } ; await lbWaiter . forWorkRequest ( getWorkRequestRequest ) ; }