Skip to content

Commit ef72600

Browse files
bart-vmwareTimHess
andauthored
Update extensibility docs for connectors (#439)
* Update extensibility docs for connectors * Review feedback * Apply suggestions from code review Co-authored-by: Tim Hess <tim.hess@broadcom.com> * Add line break --------- Co-authored-by: Tim Hess <tim.hess@broadcom.com>
1 parent 2282a20 commit ef72600

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,96 @@
11
# Extensibility
22

3-
Connectors were redesigned in Steeltoe v4, and extensibility is not yet available.
4-
We intend to address this gap in the near future. Please follow along and add any input you may have on [this issue](https://github.com/SteeltoeOSS/Steeltoe/issues/1154).
3+
Steeltoe Connectors cover a fixed set of supported data stores and messaging systems (PostgreSQL, MySQL, SQL Server, MongoDB, Cosmos DB, RabbitMQ, and Redis/Valkey).
4+
They are not open-ended plug-ins; extensibility means shaping platform credentials into the connection strings external drivers for those built-in connectors already understand.
55

6-
See [Advanced settings](usage.md#advanced-settings) to customize the built-in Connectors.
6+
Connectors map credentials from [Cloud Foundry service bindings](https://techdocs.broadcom.com/us/en/vmware-tanzu/platform/elastic-application-runtime/10-3/eart/binding-credentials.html) and
7+
[Service Binding Spec for Kubernetes](https://github.com/servicebinding/spec#well-known-secret-entries) into configuration keys starting with `steeltoe:service-bindings` and merge them with local settings from `Steeltoe:Client`.
8+
Each connector runs the binding logic for its own service type.
9+
10+
> [!NOTE]
11+
> The remainder of this page covers Cloud Foundry. Equivalent support for custom Kubernetes service bindings is not yet implemented.
12+
> If you need it, [open an issue](https://github.com/SteeltoeOSS/Steeltoe/issues/new/choose) describing your scenario.
13+
14+
To use a third-party `VCAP_SERVICES` structure, populate the `steeltoe:service-bindings` keys yourself.
15+
It is recommended to turn off the built-in binding logic to prevent conflicts by setting `SkipDefaultServiceBindings` to `true`.
16+
Doing so will still merge with local settings from `Steeltoe:Client`.
17+
18+
> [!TIP]
19+
> See the [Cloud Foundry configuration provider](../configuration/cloud-foundry-provider.md) for `vcap:*` keys and [`VCAP_SERVICES`](https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES) in general.
20+
21+
For example, to use a third-party Cloud Foundry service broker for PostgreSQL that sets the `VCAP_SERVICES` environment variable to:
22+
23+
```json
24+
{
25+
"custom-postgres-broker": [
26+
{
27+
"name": "products-db",
28+
"credentials": {
29+
"custom-hostname-key": "example.cloud.com",
30+
"custom-port-key": 2345,
31+
"custom-username-key": "products-user",
32+
"custom-password-key": "products-secret",
33+
"custom-database-name-key": "product-database"
34+
}
35+
},
36+
{
37+
"name": "orders-db",
38+
"credentials": {
39+
"custom-hostname-key": "example.cloud.com",
40+
"custom-port-key": 2345,
41+
"custom-username-key": "orders-user",
42+
"custom-password-key": "orders-secret",
43+
"custom-database-name-key": "order-database"
44+
}
45+
}
46+
]
47+
}
48+
```
49+
50+
The following code can be used to map the PostgreSQL credentials to the format that [`NpgsqlConnectionStringBuilder`](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnectionStringBuilder.html) expects:
51+
52+
```c#
53+
using Npgsql;
54+
using Steeltoe.Configuration.CloudFoundry;
55+
using Steeltoe.Connectors;
56+
using Steeltoe.Connectors.PostgreSql;
57+
58+
var builder = WebApplication.CreateBuilder();
59+
builder.AddCloudFoundryConfiguration();
60+
MapCustomServiceBindings("custom-postgres-broker");
61+
builder.AddPostgreSql(configure => configure.SkipDefaultServiceBindings = true, null);
62+
var app = builder.Build();
63+
64+
var factory = app.Services.GetRequiredService<ConnectorFactory<PostgreSqlOptions, NpgsqlConnection>>();
65+
66+
PostgreSqlOptions productsDbOptions = factory.Get("products-db").Options;
67+
Console.WriteLine(productsDbOptions.ConnectionString);
68+
// Database=product-database;Host=example.cloud.com;Password=products-secret;Port=2345;Username=products-user
69+
70+
PostgreSqlOptions ordersDbOptions = factory.Get("orders-db").Options;
71+
Console.WriteLine(ordersDbOptions.ConnectionString);
72+
// Database=order-database;Host=example.cloud.com;Password=orders-secret;Port=2345;Username=orders-user
73+
74+
void MapCustomServiceBindings(string brokerName)
75+
{
76+
var options = builder.Configuration.GetSection("vcap").Get<CloudFoundryServicesOptions>();
77+
78+
foreach (CloudFoundryService service in options?.Services
79+
.Where(pair => pair.Key == brokerName)
80+
.SelectMany(pair => pair.Value) ?? [])
81+
{
82+
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
83+
{
84+
// Map credentials into the property names expected by NpgsqlConnectionStringBuilder.
85+
[$"steeltoe:service-bindings:postgresql:{service.Name}:host"] = service.Credentials["custom-hostname-key"].Value,
86+
[$"steeltoe:service-bindings:postgresql:{service.Name}:port"] = service.Credentials["custom-port-key"].Value,
87+
[$"steeltoe:service-bindings:postgresql:{service.Name}:username"] = service.Credentials["custom-username-key"].Value,
88+
[$"steeltoe:service-bindings:postgresql:{service.Name}:password"] = service.Credentials["custom-password-key"].Value,
89+
[$"steeltoe:service-bindings:postgresql:{service.Name}:database"] = service.Credentials["custom-database-name-key"].Value
90+
});
91+
}
92+
}
93+
```
94+
95+
> [!TIP]
96+
> See [Advanced settings](usage.md#advanced-settings) to customize the built-in Connectors.

docs/docs/v4/connectors/usage.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,6 @@ builder.Services.AddDbContext<AppDbContext>((serviceProvider, options) =>
347347
sqlServerOptions.EnableRetryOnFailure();
348348
}));
349349
```
350+
351+
> [!TIP]
352+
> See [Extensibility](extensibility.md) to use third-party service brokers.

0 commit comments

Comments
 (0)