mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-27 21:01:57 +00:00
Adding changes to find host address of write region.
This commit is contained in:
committed by
Craig Boger (from Dev Box)
parent
70635e426f
commit
d720c56d48
@@ -148,6 +148,57 @@ export function client(): Cosmos.CosmosClient {
|
||||
_defaultHeaders["x-ms-cosmos-priority-level"] = PriorityLevel.Default;
|
||||
}
|
||||
|
||||
// find the ip address associated with the endpoint
|
||||
function findHostAddress(endpoint: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Extract hostname from endpoint
|
||||
const hostname = new URL(endpoint).hostname;
|
||||
|
||||
// Use dns.lookup to find the IP address
|
||||
dns.lookup(hostname, (err, address) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(address);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Parsing out endpoint from diagnostics. Used to find address I need to add to firewall rule.
|
||||
function parseEndpointFromDiag(json: string): string {
|
||||
const suffix: string = ".documents.azure.com";
|
||||
const start: number = json.indexOf("//") + "//".length;
|
||||
const end: number = json.indexOf(suffix) + suffix.length;
|
||||
const endpoint: string = json.substring(start, end);
|
||||
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
async function fetchConnectedRegions(client: Cosmos.CosmosClient) {
|
||||
// Check currently connected regions.
|
||||
try {
|
||||
const someMoreThings = await client.databases.readAll().fetchAll();
|
||||
console.log(`Current list of databases: ${JSON.stringify(someMoreThings)}`);
|
||||
const currentReadRegion = await client.getReadEndpoint();
|
||||
console.log(`Current read endpoint: ${JSON.stringify(currentReadRegion)}`);
|
||||
const currentReadRegions = await client.getReadEndpoints();
|
||||
console.log(`Current account endpoints: ${JSON.stringify(currentReadRegions)}`);
|
||||
// Getting primary region IP that needs to be blocked.
|
||||
// retrieve the regional endpoint of the account
|
||||
const regionalWriteEndpoint = await client.getWriteEndpoint();
|
||||
console.log(`Current write endpoint: ${JSON.stringify(regionalWriteEndpoint)}`);
|
||||
const parsedWriteEndpoint = parseEndpointFromDiag(JSON.stringify(regionalWriteEndpoint));
|
||||
const writeHostAddress = await findHostAddress(parsedWriteEndpoint);
|
||||
console.log(`Current write host address: ${JSON.stringify(writeHostAddress)}`);
|
||||
} catch (error) {
|
||||
console.error("Error getting read endpoints:", error);
|
||||
}
|
||||
|
||||
const currentWriteRegion = await client.getWriteEndpoint();
|
||||
console.log(`Current write endpoint: ${JSON.stringify(currentWriteRegion)}`);
|
||||
}
|
||||
|
||||
const options: Cosmos.CosmosClientOptions = {
|
||||
endpoint: endpoint() || "https://cosmos.azure.com", // CosmosClient gets upset if we pass a bad URL. This should never actually get called
|
||||
key: userContext.masterKey,
|
||||
@@ -174,5 +225,9 @@ export function client(): Cosmos.CosmosClient {
|
||||
}
|
||||
|
||||
_client = new Cosmos.CosmosClient(options);
|
||||
|
||||
// Log debug vals
|
||||
fetchConnectedRegions(_client).catch((error) => console.error(error));
|
||||
|
||||
return _client;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user