Discussion
· 7 hr ago

Picking up an Interop Business Host setting programmatically

Hi all,

I needed to pick up the username from a set of credentials configured for a business operation in an interop production, and it turned out to take a few steps, so I'm sharing my method here, in the dual hope that someone has an easier way to do it, or failing that, that it is useful to someone.

Best,

Otto

/// Fetch the user name from the credentials configured for a production item.
/// Parameters:
///  productionName - The name of the production where the config iterm lives.
///  configItemName - The name of the config item that holds the credential setting.
///  settingName - Defaults to "Credentials". Provide if different.
///  userName - Output parameter.
/// Sample call, omitting 'settingName' to use the default:
///  set status = ##(MyClass).GetUserNameFromCredentials("Acme.Production", "My Operation", , .name)
ClassMethod GetUserNameFromCredentials(productionName As %String, configItemName As %String, settingName As %String = "Credentials", Output userName As %String) As %Status
{
   try {
      set userName = ""
      
// Find the id of the requested config item.
      &sql(select %ID into :configItemId
         from Ens_Config.Item
         where Production = :productionName
            and Name = :configItemName
      )
      if SQLCODE throw ##class(%Exception.SQL).CreateFromSQLCODE(SQLCODE, "Config item '" _ configItemName _ "' not found in production '" _ productionName _ ".")
      
// Open the config item.
      set configItem = ##class(Ens.Config.Item).%OpenId(configItemId)
      if (configItem = "") throw ##class(%Exception.General).%New(,5001,,"Failed to open config item '" _ configItemName _ "' in production '" _ productionName _ ".")
      
// Try to fetch the name of the credentials.
      if ('configItem.GetSetting(settingName, .credentials)) throw ##class(%Exception.General).%New(,5001,,"Setting '" _ settingName _ "' not found for config item '" _ configItemName _ "' in production '" _ productionName _ ".")
      if (credentials = "") throw ##class(%Exception.General).%New(,5001,,"Setting '" _ settingName _ "' not configured for config item '" _ configItemName _ "' in production '" _ productionName _ ".")
      
// Look up the corresponding user name, setting the output parameter 'userName'.
      &sql(select Username into :userName
         from Ens_Config.Credentials
         where %ID = :credentials
      )
      if SQLCODE throw ##class(%Exception.SQL).CreateFromSQLCODE(SQLCODE, "'" _ credentials _ "' not found in credentials table.")
   
catch ex {
      set status = ex.AsStatus()
   }
   return $get(status, $$$OK)
}
Discussion (2)2
Log in or sign up to continue

Looks a bit complex... For my purposes, I just took 2 lines from the ISC code. If I got it correct, and it's just a reading of Credentials.

If '$ISOBJECT(..%CredentialsObj) Do ..CredentialsSet(..Credentials) If '$ISOBJECT(..%CredentialsObj) { Set tSC=$$$EnsError($$$EnsErrNoCredentials,..Credentials) Quit }

w ..%CredentialsObj.Username

Update: Oh, your function works outside of the business host, okay 🙂

Ens.Director classmethods GetAdapterSettingValue  & GetHostSettingValue could be used - they account for System Default Settings as well. Need to know if the setting is from the Adapter or the Host class (see also GetItemSettingValue that takes Adapter or Host as the type parameter).

set hostitem="SFTPToSomewhere",username=##class(Ens.Config.Credentials).%OpenId(##class(Ens.Director).GetAdapterSettingValue(hostitem,"Credentials",.sc)).Username
write
hostitem="SFTPToSomewhere"
sc=1
username="mlm"

Going off on a tangent one can use the Ens.Setting.Reporter class that is the basis for the Port Authority to find all Credentials in the production:

set reporter=##class(Ens.Setting.Reporter).%New()
set reporter.SettingNamePattern="Credentials"
set reporter.SettingReportClass="Ens.Setting.Report.base"
set sc=reporter.SearchSettings()

The results are in multidimensional property reporter.LocalResults with setting name being lowercase:
LocalResults("MYNAMESPACE","items",1,"categories") = "rest"
LocalResults("MYNAMESPACE","items",1,"credentials") = "JGM"
LocalResults("MYNAMESPACE","items",1,"document") = "primary"
LocalResults("MYNAMESPACE","items",1,"enabled") = 0
LocalResults("MYNAMESPACE","items",1,"itemName") = "EnsLib.REST.GenericOperation"
LocalResults("MYNAMESPACE","items",1,"partner") = ""
LocalResults("MYNAMESPACE","items",2,"categories") = ""
LocalResults("MYNAMESPACE","items",2,"credentials") = "MLM"
LocalResults("MYNAMESPACE","items",2,"document") = "primary"
LocalResults("MYNAMESPACE","items",2,"enabled") = 0
LocalResults("MYNAMESPACE","items",2,"itemName") = "SFTPToSomewhere"
LocalResults("MYNAMESPACE","items",2,"partner") = ""