:wave: Hey All, just starting to piece together a ...
# getting-started
f
đź‘‹ Hey All, just starting to piece together a larger stack for the first time and starting to deal with
Output<string>
types. I'm dealing with a MongoDB URI, db name, and an associated username/password. I have all 4 of these as outputs, and i am trying to form them into a proper MongoDB connection string. This involves taking the URI in the format
<mongodb+srv://mycluster.randomchars.mongodb.net>
and inserting the username/password to form this format:
mongodb+srv://<username>:<password>@mycluster.randomchars.mongodb.net/<dbName>
. The problem I have is that I need to do some sort of replace in order to retain the
mycluster.randomchars
section of the URI - this means that
pulumi.interpolate
is not sufficient for my use-case. Any suggestions where I can go from here? Just keep them separate and form them properly in the application?
b
use an
apply
- once you’re inside the
apply
you can manipulate the string as you would any other string
although: shouldn’t the
uri
have the random string in there?
f
The uri already has the random section. However, I need to insert the
username:password@
section after the
mongodb+srv://
section.
I was having an issue using the
replace
function within an apply. Let me see if I can get back to that point.
Ah, I might be able to do a split. Let me play with this a bit. Thanks for the help.
l
Here's my code to do similar for SQL Server... Given a variable
sqlServerOutput
which is of type:
Copy code
Output.Tuple<string, string, string>  // aka
Output<(string, string, string)>
Using
.Apply
looks like:
Copy code
// Server=tcp:{server-name}.<http://database.windows.net|database.windows.net>,1433;Initial Catalog={db-name};Persist Security Info=False;User ID={login-name};Password={login-password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
        return sqlServerOutput.Apply(t =>
        {
            (string server, string loginId, string loginPassword) = t;
            return sqlDatabase.Name.Apply(finalDbName =>
                $"Server=tcp:{server}.<http://database.windows.net|database.windows.net>,1433;Initial Catalog={finalDbName};Persist Security Info=False;User ID={loginId};Password={loginPassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
            );
        });
Also,
sqlDatabase.Name
is another
Output<string>
f
Thanks. I think I got it working how I need in my case.
Copy code
const mongoDbUri = pulumi
        .all([db.uri, db.user, db.password, db.name])
        .apply(([uri, user, password, name]) => {
          const [prefix, suffix] = uri.split('//')
          return `${prefix}//${user}:${password}@${suffix}/${name}`
        })
b
language primitives ftw!