|
| 1 | +/* |
| 2 | + * list_clusters_sdk.js |
| 3 | + * |
| 4 | + * Code sample showing basic usage of the Nutanix v4 API SDK for JS |
| 5 | + * |
| 6 | + * This demo read configuration from config.json, connects to Prism Central, |
| 7 | + * authenticates then returns a list of registered clusters. The list is formatted to show |
| 8 | + * cluster names and their extId |
| 9 | + * |
| 10 | + * Requirements: |
| 11 | + * |
| 12 | + * Prism Central pc.2023.3 or later |
| 13 | +*/ |
| 14 | + |
| 15 | +const sdk = require("@nutanix-api/clustermgmt-js-client/dist/lib/index"); |
| 16 | +let client = new sdk.ApiClient(); |
| 17 | + |
| 18 | +client.addDefaultHeader("Accept-Encoding","gzip, deflate, br"); |
| 19 | + |
| 20 | +const fs = require('fs'); |
| 21 | + |
| 22 | +// load the configuration from on-disk config.json |
| 23 | +fs.readFile('./config.json', 'utf8', (err, data) => { |
| 24 | + if (err) { |
| 25 | + console.log(`Unable to load config from ./config.json: ${err}`); |
| 26 | + } else { |
| 27 | + // parse JSON string to JSON object |
| 28 | + const config = JSON.parse(data); |
| 29 | + |
| 30 | + // setup the connection configuration |
| 31 | + // for this demo, only Prism Central IP, port, username, password and debug mode are read from on-disk configuration |
| 32 | + // Prism Central IPv4/IPv6 address or FQDN |
| 33 | + client.host = config.pc_ip; |
| 34 | + // HTTP port for connection |
| 35 | + client.port = config.pc_port; |
| 36 | + // connection credentials |
| 37 | + client.username = config.username; |
| 38 | + client.password = config.password; |
| 39 | + // don't verify SSL certificates; not recommended in production |
| 40 | + client.verifySsl = false; |
| 41 | + // show extra debug info during demo |
| 42 | + client.debug = config.debug; |
| 43 | + client.cache = false; |
| 44 | + |
| 45 | + console.log(`Prism Central IP or FQDN: ${client.host}`); |
| 46 | + console.log(`Username: ${client.username}`); |
| 47 | + |
| 48 | + let clientApi = new sdk.ClusterApi(client); |
| 49 | + |
| 50 | + // setup request options |
| 51 | + var entityListOptions = new Object(); |
| 52 | + entityListOptions.page = 0; |
| 53 | + entityListOptions.limit = 20; |
| 54 | + entityListOptions.filter = ''; |
| 55 | + entityListOptions.orderBy = ''; |
| 56 | + |
| 57 | + clientApi.getClusters(entityListOptions).then(({ data, response }) => { |
| 58 | + console.log(`API returned the following status code: ${response.status}`); |
| 59 | + data.getData().forEach(element => console.log('Cluster found with name "' + element.name + '"')) |
| 60 | + }).catch((err) => { |
| 61 | + console.log(`Error is ${err}`); |
| 62 | + }) |
| 63 | + } |
| 64 | +}) |
0 commit comments