Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated SDK #1

Open
tiffchros opened this issue Feb 22, 2023 · 2 comments
Open

Deprecated SDK #1

tiffchros opened this issue Feb 22, 2023 · 2 comments

Comments

@tiffchros
Copy link

When using this node, I get a console log:
"(node:7) NOTE: The AWS SDK for JavaScript (v2) will be put into maintenance mode in 2023.
Please migrate your code to use AWS SDK for JavaScript (v3).
For more information, check the migration guide at https://a.co/7PzMCcy"

@tiffchros tiffchros changed the title Deprecaded SDK Deprecated SDK Feb 22, 2023
@mlconnor
Copy link
Owner

thank you for posting this and apologize for the slow response. working on upgrading this.

@mlconnor
Copy link
Owner

mlconnor commented Feb 15, 2024

began work... lot's to do.

npm install @aws-sdk/client-s3

const AWS = require('aws-sdk')
const paginators = require('./resources/paginators.json')

async function callAwsService(service, method, options) {
    try {
        // Dynamically import the service client module
        const { [service]: ServiceClient } = await import(`@aws-sdk/client-${service.toLowerCase()}`);

        // Instantiate the service client with options (e.g., credentials, region)
        const client = new ServiceClient(options);

        if (typeof client[method] === 'function') {
            // Call the method if it exists and return the result
            const data = await client[method](options);
            console.log(data);
            return data;
        } else {
            throw new Error(`Method ${method} not found in ${service} service client.`);
        }
    } catch (error) {
        console.error(`Error calling service ${service}:`, error);
        throw error;
    }
}

// Example usage - replace 'S3' and 'listBuckets' with your service and method
callAwsService('S3', 'listBuckets', {})
    .then(data => console.log(data))
    .catch(err => console.error(err));

module.exports = function(RED) {
    function SimpleAWSNode(config) {
        //console.log("config", config)
        RED.nodes.createNode(this, config);
        var node = this;
		    let awsConfig = RED.nodes.getNode(config.aws); 
        this.region = awsConfig.region
        /*  
        this.parameterType = config.parameterType
        this.parameter = config.parameter
        this.operation = config.operation
        this.service = config.service
        this.aws = config.aws
        */
		    AWS.config.update({
			    accessKeyId: awsConfig.accessKey,
			    secretAccessKey: awsConfig.secretKey
		    });

        let serviceOptions = {}
        try {
          if ( config.serviceOptions && config.serviceOptions.trim() > 0 ) {
            serviceOptions = JSON.parse(config.serviceOptions)
          }
        } catch (e) {
          let eMsg = "service options were not valid JSON"
          node.status({ fill: "red", shape: "ring", text: sMsg })
          node.error(e, eMsg)
        }
        if ( config.apiVersion ) {
          serviceOptions.apiVersion = config.apiVersion
        }
        if ( this.region ) {
          serviceOptions.region = this.region
        }
        //console.log("creating new AWS service " + config.service + " with params", serviceOptions)
        let client = null
        try {
          client = new AWS[config.service](serviceOptions)
          let service = { [service]: ServiceClient } = await import(`@aws-sdk/client-${service.toLowerCase()}`);

          // Instantiate the service client with options (e.g., credentials, region)
          client = new ServiceClient(serviceOptions);

        } catch (e) {
          let eMsg = "error instantiating AWS service " + config.service + "(" + config.apiVersion + ":" + config.operation + " " + e.message
          node.status({ fill: "red", shape: "ring", text: eMsg})
          node.error(e, eMsg);
        }

        /* let's handle the payload parameter */
        let valid = true
        if ( config.parameterType === 'json' ) {
          try {
            // check this is parsable JSON
            JSON.parse(config.parameter);
          } catch(e) {
            this.error(RED._("change.errors.invalid-json, "));
          }
        } else if ( config.parameterType === 'jsonata') {
          try {
            node.jsonata = RED.util.prepareJSONataExpression(config.parameterType, this);
          } catch(e) {
            valid = false;
            let eMsg = "error parsing JSONata expression " + config.service + "(" + config.apiVersion + ":" + config.operation + " " + e.message
            node.status({ fill: "red", shape: "ring", text: eMsg})
            node.error(e, eMsg);
          }
        }

        /* let's deal with the paginators here */
        let paginatorsDef = paginators[config.service.toLowerCase() + '-' + config.apiVersion]
        let paginatorDef = paginatorsDef ? paginatorsDef[config.operation] : null
        //console.log("pagDefs", paginatorsDef, "pagDef", paginatorDef,"op", config.operation)
        
        /*
        if (this.awsConfig.proxyRequired){
            var proxy = require('proxy-agent');
            AWS.config.update({
                httpOptions: { agent: new proxy(this.awsConfig.proxy) }
            });
        */

        node.on('input', async function(msg, nodeSend, nodeDone) {
          //console.log("message received", msg, "node", node,"send",nodeSend,"done",nodeDone)
          let operationParam = {}
          let msgCopy = { ...msg }
          try {
            switch (config.parameterType) {
              case 'jsonata' :
                operationParam = RED.util.evaluateNodeProperty(config.parameter, config.parameterType, node, msg);
                break
              case 'msg':
                operationParam = RED.util.getMessageProperty(msg,config.parameter);
                break
              case 'json':
                operationParam = JSON.parse(config.parameter)
                break
              default:
                throw "Unexpected config parameter type " + config.parameterType

            }
          } catch (e) {
            let eMsg = "AWS parameter to " + config.service + ":" + config.operation + " was malformed. " + e.message
            msgCopy.error = e
            msgCopy.errorMsg = eMsg
            node.status({ fill: "red", shape: "ring", text: eMsg})
            node.error(eMsg);
            node.send([null, msgCopy])
            return
          }

          node.status({ fill: "green", shape: "ring", text: "calling " + config.service + ":" + config.operation });
          
          let operationParamCopy = { ... operationParam }

          try {
            let done = false

            while (!done) {
              var response = await client[config.operation](operationParamCopy).promise()
              msgCopy.payload = response
              if ( config.paging !== 'disabled' && paginatorDef && response[paginatorDef.output_token]) {
                //console.log(`paginating ${config.service}:${config.operation} on ${paginatorDef.output_token}`)
                operationParamCopy[paginatorDef.input_token] = response[paginatorDef.output_token]
                delete msgCopy.complete
                node.send([msgCopy, null])
              } else {
                done = true
                msgCopy.complete = true
                node.status({ fill: "green", shape: "ring", text: "done" });
                node.send([msgCopy, null])
                nodeDone()
              }
            }
          } catch (e) {
            let eMsg = `Error while calling ${config.service}:${config.operation} ${e.message}`
            msgCopy.error = e
            msgCopy.errorMsg = eMsg
            node.status({ fill: "red", shape: "ring", text: e });
            node.error(e, `error calling ${config.service}:${config.operation} ${e}`);
            node.send([null, msgCopy])
          }
        });
    }
    RED.nodes.registerType("simple-aws",SimpleAWSNode);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants