SMB - Dynamically Manipulate Settings

Last modified by Erik Bakker on 2024/09/03 09:17

In a previous microlearning on this subject, we discussed how to configure the various variants of SMB connectivity. We already mentioned that more dynamic options, such as automatically creating a "multi-level" directory (i.e.,/root/sub), are complex to configure and require a different approach.
 
In this microlearning module, we’ll delve into dynamically manipulating settings on the SMB Session Factory. This approach is particularly useful when you need to create directories on-the-fly or manage file shares across multiple hosts. We’ll explore how to configure dynamic settings such as host, share, and directory using the Standard Service Activator, and discuss additional dynamic options to enhance flexibility in your SMB configurations.

If you have any questions, don't hesitate to contact academy@emagiz.com.

1. Prerequisites

  • Advanced knowledge of the eMagiz platform

2. Key concepts

This microlearning centers around dynamically manipulating settings on the SMB Session Factory.

  • This option is the correct approach when you need to create a directory dynamically or have multiple hosts that host file shares.

3. Dynamically manipulate SMB Session Settings

In a previous microlearning we learned that we need an SMB Session Factory to set up the initial connection to the SMB drive. In this component, five main settings are relevant.

  • Host
  • Port
  • Username
  • Password
  • Share and directory

In theory, each of these settings can be overwritten every time a message is consumed and processed by the flow. In practice, we see two main use cases that are relevant to mention in this microlearning as we see them currently being applied in our community.
  

3.1 Dynamic Host

Suppose you have a multitude of SMB directories (i.e., shares) span across many different host servers. In that case, you need a solution to manipulate pre-existing information on the SMB session factory dynamically. In other words, you need to be able to override the default host configuration in the component with one that is determined based on information in the headers or message.

To do so, we need to add a component to the flow that will override the host setting. This component is called the "Standard service activator."

advanced-file-connectivity-dynamically-manipulate-smb-settings--standard-service-activator.png

Here, we must define an expression to set the host based on a supplied value. Important in this expression is that we correctly reference the complete name of the SMB Session Factory support object in the flow and that once the host is set, the actual message is passed along. An example of such an expression is:

@'smb.gtwydynm.exit.support.smb-session'.setHost(headers.server) ?: #root

This expression will set the host based on the value in the header called "server" and, once done, will pass the full message to the output channel.

advanced-file-connectivity-dynamically-manipulate-smb-settings--standard-service-activator-filled-in.png

3.2 Dynamic Share and Directory

If you have a "multi-level" path that eMagiz needs to create automatically, you must dynamically set the Share and Directory settings on the SMB Session Factory support object.

To do so, we need to add a component to the flow that will override the host setting. This component is called the "Standard service activator."

advanced-file-connectivity-dynamically-manipulate-smb-settings--standard-service-activator.png

Here, we must define an expression to set the host based on a supplied value. Important in this expression is that we correctly reference the complete name of the SMB Session Factory support object in the flow and that once the host is set, the actual message is passed along. An example of such an expression is: 

@'smb.gtwydynm.exit.support.smb-session'.setShareAndDir(headers.share) ?: #root

This expression will set the host based on the value in the header called "server" and, once done, will pass the full message to the output channel.

advanced-file-connectivity-dynamically-manipulate-smb-settings--standard-service-activator-filled-in-share-and-dir.png

Note that you can combine multiple dynamic settings into one expression to keep the number of components efficient. In that case the code will look something as follows @'smb.gtwydynm.exit.support.smb-session'.setShareAndDir(headers.share) ?: @'smb.gtwydynm.exit.support.smb-session'.setHost(headers.server) ?: #root

3.3 Additional Dynamic Options

Although we don't think these options will be used much we define them here for the sake of completeness and give you examples on how to make these additional values dynamically.

3.3.1 Port

@'smb.gtwydynm.exit.support.smb-session'.setPort(headers.port) ?: #root

3.3.2 Username

@'smb.gtwydynm.exit.support.smb-session'.setUsername(headers.username) ?: #root

3.3.3 Password

@'smb.gtwydynm.exit.support.smb-session'.setPassword(headers.password) ?: #root

4. Key takeaways

  • This option is the correct approach when you need to create a directory dynamically or have multiple hosts that host file shares.

5. Suggested Additional Readings