|
| 1 | +/** |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * 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. |
| 4 | + */ |
| 5 | + |
| 6 | +import common = require("oci-common"); |
| 7 | +import * as identity from "oci-identity"; |
| 8 | +import * as audit from "oci-audit"; |
| 9 | + |
| 10 | +const configurationFilePath = "~/.oci/config"; |
| 11 | +const configProfile = "DEFAULT"; |
| 12 | + |
| 13 | +/* This script demostrates how to create a circuit breaker and share the circuit breaker among clients. |
| 14 | + */ |
| 15 | +const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider( |
| 16 | + configurationFilePath, |
| 17 | + configProfile |
| 18 | +); |
| 19 | +const compartmentId = provider.getTenantId(); |
| 20 | + |
| 21 | +(async () => { |
| 22 | + // Options for creating a circuit breaker |
| 23 | + const options = { |
| 24 | + timeout: 10000, // If our function takes longer than 10 seconds, trigger a failure |
| 25 | + errorThresholdPercentage: 50, // When 50% of requests fail, trip the circuit |
| 26 | + resetTimeout: 30000 // After 30 seconds, try again. |
| 27 | + }; |
| 28 | + const clientConfiguration = { |
| 29 | + circuitBreaker: new common.CircuitBreaker(options) |
| 30 | + }; |
| 31 | + |
| 32 | + // Use the circuitBreaker to create identity client |
| 33 | + const identityClient = new identity.IdentityClient( |
| 34 | + { authenticationDetailsProvider: provider }, |
| 35 | + clientConfiguration |
| 36 | + ); |
| 37 | + |
| 38 | + // Share the same circuitBreaker to create audit client. |
| 39 | + const auditClient = new audit.AuditClient( |
| 40 | + { authenticationDetailsProvider: provider }, |
| 41 | + clientConfiguration |
| 42 | + ); |
| 43 | + |
| 44 | + let listUserReq: identity.requests.ListUsersRequest; |
| 45 | + listUserReq = { |
| 46 | + compartmentId: compartmentId |
| 47 | + }; |
| 48 | + |
| 49 | + const response = await identityClient.listUsers(listUserReq); |
| 50 | + console.log("List User Repsonse: ", response); |
| 51 | + |
| 52 | + const endTime: Date = new Date(); |
| 53 | + const offset = new Date().setDate(new Date().getDate() - 5); |
| 54 | + const startTime: Date = new Date(offset); |
| 55 | + const listEventRequest: audit.requests.ListEventsRequest = { |
| 56 | + compartmentId: compartmentId, |
| 57 | + startTime: startTime, |
| 58 | + endTime: endTime |
| 59 | + }; |
| 60 | + try { |
| 61 | + const listEventResponse = await auditClient.listEvents(listEventRequest); |
| 62 | + console.log("response: ", listEventResponse); |
| 63 | + } catch (err) { |
| 64 | + console.log("what is err: ", err); |
| 65 | + } |
| 66 | +})(); |
0 commit comments