farmer icon indicating copy to clipboard operation
farmer copied to clipboard

Capturing the host name of a postgresql instance.

Open kurt-mueller-osumc opened this issue 3 years ago • 1 comments

If this is not the appropriate place for questions, let me know where I should post them :).

How do I go about creating a setting my myApp that contains my database's hostname/endpoint?

I notice that this is done in the COSMOS-BACKED WEB APP tutorial.

let pgDb = postgreSQL {
    name "dbName"
    admin_username "dbAdmin"
    add_database "db"
}

let myApp = webApp {
    name "myApp"
    depends_on pgDb

    setting "DB_NAME" pgDb.Databases.Head.Name
    setting "DB_USER" pgDb.AdministratorCredentials.UserName
    setting "DB_PASSWORD" pgDb.AdministratorCredentials.Password.Value

    // I'd like to do something like the following:
    setting "DB_HOST" pgDb.Endpoint
}

Would this be possible? Thanks for any and all information.

kurt-mueller-osumc avatar Feb 17 '22 18:02 kurt-mueller-osumc

It is possible to reference properties of the newly provisioned Postgres resource and pass them to your webapp by creating a reference to that resource and then retrieving properties from that reference. We usually try to bake those into the builders themselves to simplify your code, but if one is not there, something like the following should work as a stop gap.

// ID of the database itself
let dbId = Farmer.Arm.DBforPostgreSQL.databases.resourceId(ResourceName "dbserver", ResourceName "db")
// Using that ID, generate an ARM expression for the 'name' property
let dbName = ArmExpression.create($"reference({dbId.ArmExpression.Value}).name").Eval()

// Then you can use that expression in your webApp settings:
setting "DB_NAME" dbName

Does that do what you need?

ninjarobot avatar Mar 01 '22 03:03 ninjarobot

@kurt-mueller-osumc can you please review #1020 that adds a FullyQualifiedDomainName member? This serves the same purpose as Endpoint in the other resource, but it matches with the property name used in the docs for the Postgres resource.

ninjarobot avatar Mar 01 '23 02:03 ninjarobot

@kurt-mueller-osumc the fix is available in the 1.7.19 release now available on nuget.

ninjarobot avatar Mar 17 '23 14:03 ninjarobot

Awesome, thanks for your help on this. I'll have to check this out soon :).

kurt-mueller-osumc avatar Mar 17 '23 14:03 kurt-mueller-osumc