guideCreating an environment in Node.js

This article presents an example of using the Cloud Services Management REST API to create an environment in Node.js.

This example works only with the On-Premises version.

# Dependencies

This example uses request-promise-native and core dependencies from Node.js: crypto, url.

# Example

The following simple example presents how to create an environment using the Cloud Services Management REST API.
The ENVIRONMENTS_MANAGEMENT_SECRET_KEY and APPLICATION_ENDPOINT variables should be set to proper values from the configuration.

const url = require( 'url' );
const crypto = require( 'crypto' );
const requestPromise = require( 'request-promise-native' );

const ENVIRONMENTS_MANAGEMENT_SECRET_KEY = 'secret';
const APPLICATION_ENDPOINT = 'http://localhost:8000';

( async () => {
    try {
        const newEnvironment = {
            id: _randomString( 20 ), // required length 20
            name: 'Production',
            organizationId: _randomString( 60 ), // required length 10-60
            accessKeys: [
                {
                    value: _randomString( 100 ) // required length 10-120
                }
            ],
            services: [
                {
                    id: _randomString( 24 ), // required length 24
                    type: 'easy-image'
                },
                {
                    id: _randomString( 24 ), // required length 24
                    type: 'collaboration'
                }
            ] // all these services types recommended
        };

        const timestamp = Date.now();

        const uri = `${ APPLICATION_ENDPOINT }/environments`;
        const method = 'POST';

        const signature = _generateSignature(
            ENVIRONMENTS_MANAGEMENT_SECRET_KEY,
            method,
            uri,
            timestamp,
            newEnvironment
        );

        const options = {
            uri,
            method,
            headers: {
                'X-CS-Signature': signature,
                'X-CS-Timestamp': timestamp
            },
            body: newEnvironment,
            json: true,
            rejectUnauthorized: false // required for domains with self signed certificate
        };

        await requestPromise( options );

        console.log( 'New Environment created.' );
        console.log( `EnvironmentId: ${ newEnvironment.id } AccessKey: ${ newEnvironment.accessKeys[ 0 ].value }` );
    } catch ( error ) {
        console.log( 'error:', error.message );
    }
} )();

function _generateSignature( apiSecret, method, uri, timestamp, body ) {
    const path = url.parse( uri ).path;

    const hmac = crypto.createHmac( 'SHA256', apiSecret );

    hmac.update( `${ method.toUpperCase() }${ path }${ timestamp }` );

    if ( body ) {
        hmac.update( Buffer.from( JSON.stringify( body ) ) );
    }

    return hmac.digest( 'hex' );
}

function _randomString( length ) {
    return crypto.randomBytes( length / 2 ).toString( 'hex' );
}

# Usage

Run:

node index.js