The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as ds from "@distilled.cloud/gcp/datastream_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | collectPages, |
| 14 | emptyMessage, |
| 15 | fieldMask, |
| 16 | fingerprint, |
| 17 | hasAlchemyLabelMap, |
| 18 | listAtLocation, |
| 19 | locationParent, |
| 20 | normalizeLocation, |
| 21 | parseName, |
| 22 | privateConnectionOf, |
| 23 | replaceOnIdentity, |
| 24 | ResourceNotResolved, |
| 25 | toPhysicalId, |
| 26 | userLabels, |
| 27 | settleOperation, |
| 28 | waitUntilExists, |
| 29 | waitUntilGone, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | export type MysqlProfile = ds.MysqlProfile; |
| 33 | export type PostgresqlProfile = ds.PostgresqlProfile; |
| 34 | export type OracleProfile = ds.OracleProfile; |
| 35 | export type SqlServerProfile = ds.SqlServerProfile; |
| 36 | export type MongodbProfile = ds.MongodbProfile; |
| 37 | export type SpannerProfile = ds.SpannerProfile; |
| 38 | export type GcsProfile = ds.GcsProfile; |
| 39 | export type BigQueryProfile = ds.BigQueryProfile; |
| 40 | export type SalesforceProfile = ds.SalesforceProfile; |
| 41 | export type SalesforceMarketingCloudProfile = ds.SalesforceMarketingCloudProfile; |
| 42 | export type ServiceNowProfile = ds.ServiceNowProfile; |
| 43 | export type DataverseProfile = ds.DataverseProfile; |
| 44 | export type ForwardSshTunnelConnectivity = ds.ForwardSshTunnelConnectivity; |
| 45 | export type PrivateConnectivity = ds.PrivateConnectivity; |
| 46 | export type MysqlSslConfig = ds.MysqlSslConfig; |
| 47 | export type PostgresqlSslConfig = ds.PostgresqlSslConfig; |
| 48 | export type OracleSslConfig = ds.OracleSslConfig; |
| 49 | export type SqlServerSslConfig = ds.SqlServerSslConfig; |
| 50 | export type MongodbSslConfig = ds.MongodbSslConfig; |
| 51 | export type OracleAsmConfig = ds.OracleAsmConfig; |
| 52 | export type Secret = ds.Secret; |
| 53 | |
| 54 | export type ConnectionProfileProps = { |
| 55 | /** |
| 56 | * Connection profile id (the `{connectionProfile}` segment of |
| 57 | * `projects/{project}/locations/{location}/connectionProfiles/{connectionProfile}`). |
| 58 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 59 | * it replaces the profile. |
| 60 | */ |
| 61 | connectionProfileId?: string; |
| 62 | /** |
| 63 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 64 | * profile. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 65 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 66 | */ |
| 67 | location?: string; |
| 68 | /** |
| 69 | * User-friendly display name. |
| 70 | */ |
| 71 | displayName?: string; |
| 72 | /** |
| 73 | * User labels. Alchemy ownership labels are merged in automatically. |
| 74 | */ |
| 75 | labels?: Record<string, string>; |
| 76 | /** |
| 77 | * MySQL source profile. Mutually exclusive with the other engine |
| 78 | * blocks. Switching engines replaces the profile. |
| 79 | */ |
| 80 | mysqlProfile?: MysqlProfile; |
| 81 | /** |
| 82 | * PostgreSQL source profile. Mutually exclusive with the other engine |
| 83 | * blocks. Switching engines replaces the profile. |
| 84 | */ |
| 85 | postgresqlProfile?: PostgresqlProfile; |
| 86 | /** |
| 87 | * Oracle source profile. Mutually exclusive with the other engine |
| 88 | * blocks. Switching engines replaces the profile. |
| 89 | */ |
| 90 | oracleProfile?: OracleProfile; |
| 91 | /** |
| 92 | * SQL Server source profile. Mutually exclusive with the other engine |
| 93 | * blocks. Switching engines replaces the profile. |
| 94 | */ |
| 95 | sqlServerProfile?: SqlServerProfile; |
| 96 | /** |
| 97 | * MongoDB source profile. Mutually exclusive with the other engine |
| 98 | * blocks. Switching engines replaces the profile. |
| 99 | */ |
| 100 | mongodbProfile?: MongodbProfile; |
| 101 | /** |
| 102 | * Spanner source profile. `database` is immutable — changing it |
| 103 | * replaces the profile. |
| 104 | */ |
| 105 | spannerProfile?: SpannerProfile; |
| 106 | /** |
| 107 | * Cloud Storage destination profile. Mutually exclusive with the other |
| 108 | * engine blocks. Switching engines replaces the profile. |
| 109 | */ |
| 110 | gcsProfile?: GcsProfile; |
| 111 | /** |
| 112 | * BigQuery destination profile (empty object). Mutually exclusive with |
| 113 | * the other engine blocks. Switching engines replaces the profile. |
| 114 | */ |
| 115 | bigqueryProfile?: BigQueryProfile; |
| 116 | /** |
| 117 | * Salesforce source profile. Mutually exclusive with the other engine |
| 118 | * blocks. Switching engines replaces the profile. |
| 119 | */ |
| 120 | salesforceProfile?: SalesforceProfile; |
| 121 | /** |
| 122 | * Salesforce Marketing Cloud source profile. Mutually exclusive with |
| 123 | * the other engine blocks. Switching engines replaces the profile. |
| 124 | */ |
| 125 | salesforceMarketingCloudProfile?: SalesforceMarketingCloudProfile; |
| 126 | /** |
| 127 | * ServiceNow source profile. Mutually exclusive with the other engine |
| 128 | * blocks. Switching engines replaces the profile. |
| 129 | */ |
| 130 | serviceNowProfile?: ServiceNowProfile; |
| 131 | /** |
| 132 | * Dataverse source profile. Mutually exclusive with the other engine |
| 133 | * blocks. Switching engines replaces the profile. |
| 134 | */ |
| 135 | dataverseProfile?: DataverseProfile; |
| 136 | /** |
| 137 | * Static service IP connectivity (Datastream public IPs). Mutually |
| 138 | * exclusive with SSH tunnel and private connectivity. |
| 139 | */ |
| 140 | staticServiceIpConnectivity?: Record<string, never>; |
| 141 | /** |
| 142 | * Forward SSH tunnel connectivity. Mutually exclusive with static IP |
| 143 | * and private connectivity. Passwords and private keys are input-only. |
| 144 | */ |
| 145 | forwardSshConnectivity?: ForwardSshTunnelConnectivity; |
| 146 | /** |
| 147 | * Private connectivity via a Datastream private connection. Full name |
| 148 | * or id (combined with `location`). Mutually exclusive with static IP |
| 149 | * and SSH tunnel. |
| 150 | */ |
| 151 | privateConnectivity?: { |
| 152 | privateConnection: string; |
| 153 | }; |
| 154 | /** |
| 155 | * Create or update without validating connectivity. |
| 156 | */ |
| 157 | force?: boolean; |
| 158 | /** |
| 159 | * Validate without creating or updating resources. |
| 160 | */ |
| 161 | validateOnly?: boolean; |
| 162 | }; |
| 163 | |
| 164 | export type ConnectionProfile = Resource< |
| 165 | "GCP.Datastream.ConnectionProfile", |
| 166 | ConnectionProfileProps, |
| 167 | { |
| 168 | /** Full resource name. */ |
| 169 | name: string; |
| 170 | /** Connection profile id (last path segment). */ |
| 171 | connectionProfileId: string; |
| 172 | /** Project id. */ |
| 173 | project: string; |
| 174 | /** Location id. */ |
| 175 | location: string; |
| 176 | /** User-friendly display name. */ |
| 177 | displayName: string | undefined; |
| 178 | /** User labels (Alchemy ownership labels stripped). */ |
| 179 | labels: Record<string, string>; |
| 180 | /** MySQL profile (password omitted). */ |
| 181 | mysqlProfile: MysqlProfile | undefined; |
| 182 | /** PostgreSQL profile (password omitted). */ |
| 183 | postgresqlProfile: PostgresqlProfile | undefined; |
| 184 | /** Oracle profile (password omitted). */ |
| 185 | oracleProfile: OracleProfile | undefined; |
| 186 | /** SQL Server profile (password omitted). */ |
| 187 | sqlServerProfile: SqlServerProfile | undefined; |
| 188 | /** MongoDB profile (password omitted). */ |
| 189 | mongodbProfile: MongodbProfile | undefined; |
| 190 | /** Spanner profile. */ |
| 191 | spannerProfile: SpannerProfile | undefined; |
| 192 | /** Cloud Storage destination profile. */ |
| 193 | gcsProfile: GcsProfile | undefined; |
| 194 | /** BigQuery destination profile. */ |
| 195 | bigqueryProfile: BigQueryProfile | undefined; |
| 196 | /** Salesforce profile (secrets omitted). */ |
| 197 | salesforceProfile: SalesforceProfile | undefined; |
| 198 | /** Salesforce Marketing Cloud profile (secrets omitted). */ |
| 199 | salesforceMarketingCloudProfile: SalesforceMarketingCloudProfile | undefined; |
| 200 | /** ServiceNow profile (secrets omitted). */ |
| 201 | serviceNowProfile: ServiceNowProfile | undefined; |
| 202 | /** Dataverse profile (secrets omitted). */ |
| 203 | dataverseProfile: DataverseProfile | undefined; |
| 204 | /** Static service IP connectivity. */ |
| 205 | staticServiceIpConnectivity: Record<string, never> | undefined; |
| 206 | /** Forward SSH connectivity (password and key omitted). */ |
| 207 | forwardSshConnectivity: ForwardSshTunnelConnectivity | undefined; |
| 208 | /** Private connectivity. */ |
| 209 | privateConnectivity: PrivateConnectivity | undefined; |
| 210 | /** Whether the profile satisfies physical zone isolation. */ |
| 211 | satisfiesPzi: boolean | undefined; |
| 212 | /** Whether the profile satisfies physical zone separation. */ |
| 213 | satisfiesPzs: boolean | undefined; |
| 214 | /** RFC3339 creation timestamp. */ |
| 215 | createTime: string | undefined; |
| 216 | /** RFC3339 last-update timestamp. */ |
| 217 | updateTime: string | undefined; |
| 218 | }, |
| 219 | never, |
| 220 | Providers |
| 221 | >; |
| 222 | |
| 223 | /** |
| 224 | * A Datastream connection profile: credentials and connectivity for a |
| 225 | * source database or a BigQuery / Cloud Storage destination. |
| 226 | * |
| 227 | * `connectionProfileId`, `location`, and the engine kind |
| 228 | * (`mysqlProfile` / `postgresqlProfile` / `oracleProfile` / |
| 229 | * `sqlServerProfile` / `mongodbProfile` / `spannerProfile` / |
| 230 | * `gcsProfile` / `bigqueryProfile` / `salesforceProfile` / |
| 231 | * `salesforceMarketingCloudProfile` / `serviceNowProfile` / |
| 232 | * `dataverseProfile`) plus Spanner `database` are replacement triggers. |
| 233 | * Display name, labels, credentials, and connectivity update in place. |
| 234 | * Passwords, SSL keys, and OAuth secrets are input-only and never |
| 235 | * returned. |
| 236 | * |
| 237 | * ### Creating a Connection Profile |
| 238 | * **Example:** BigQuery destination |
| 239 | * ```typescript |
| 240 | * const dest = yield* GCP.Datastream.ConnectionProfile("BqDest", { |
| 241 | * bigqueryProfile: {}, |
| 242 | * labels: { env: "test" }, |
| 243 | * }); |
| 244 | * ``` |
| 245 | * |
| 246 | * **Example:** MySQL source |
| 247 | * ```typescript |
| 248 | * const source = yield* GCP.Datastream.ConnectionProfile("MysqlSrc", { |
| 249 | * mysqlProfile: { |
| 250 | * hostname: "10.0.0.8", |
| 251 | * port: 3306, |
| 252 | * username: "datastream", |
| 253 | * password: process.env.MYSQL_PASSWORD, |
| 254 | * }, |
| 255 | * staticServiceIpConnectivity: {}, |
| 256 | * force: true, |
| 257 | * }); |
| 258 | * ``` |
| 259 | * |
| 260 | * **Example:** Cloud Storage destination |
| 261 | * ```typescript |
| 262 | * const gcs = yield* GCP.Datastream.ConnectionProfile("GcsDest", { |
| 263 | * gcsProfile: { bucket: bucket.bucketName, rootPath: "/data" }, |
| 264 | * }); |
| 265 | * ``` |
| 266 | * |
| 267 | * @resource |
| 268 | * @category Datastream |
| 269 | */ |
| 270 | export const ConnectionProfile = Resource<ConnectionProfile>("GCP.Datastream.ConnectionProfile"); |
| 271 | |
| 272 | const resourceName = (project: string, location: string, connectionProfileId: string) => |
| 273 | `${locationParent(project, location)}/connectionProfiles/${connectionProfileId}`; |
| 274 | |
| 275 | const kindOf = (value: { |
| 276 | mysqlProfile?: unknown; |
| 277 | postgresqlProfile?: unknown; |
| 278 | oracleProfile?: unknown; |
| 279 | sqlServerProfile?: unknown; |
| 280 | mongodbProfile?: unknown; |
| 281 | spannerProfile?: unknown; |
| 282 | gcsProfile?: unknown; |
| 283 | bigqueryProfile?: unknown; |
| 284 | salesforceProfile?: unknown; |
| 285 | salesforceMarketingCloudProfile?: unknown; |
| 286 | serviceNowProfile?: unknown; |
| 287 | dataverseProfile?: unknown; |
| 288 | }) => |
| 289 | value.mysqlProfile |
| 290 | ? "mysql" |
| 291 | : value.postgresqlProfile |
| 292 | ? "postgresql" |
| 293 | : value.oracleProfile |
| 294 | ? "oracle" |
| 295 | : value.sqlServerProfile |
| 296 | ? "sqlserver" |
| 297 | : value.mongodbProfile |
| 298 | ? "mongodb" |
| 299 | : value.spannerProfile |
| 300 | ? "spanner" |
| 301 | : value.gcsProfile |
| 302 | ? "gcs" |
| 303 | : value.bigqueryProfile |
| 304 | ? "bigquery" |
| 305 | : value.salesforceProfile |
| 306 | ? "salesforce" |
| 307 | : value.salesforceMarketingCloudProfile |
| 308 | ? "sfmc" |
| 309 | : value.serviceNowProfile |
| 310 | ? "servicenow" |
| 311 | : value.dataverseProfile |
| 312 | ? "dataverse" |
| 313 | : ""; |
| 314 | |
| 315 | const publicSecret = (secret: Secret | undefined) => |
| 316 | secret === undefined |
| 317 | ? undefined |
| 318 | : { |
| 319 | secretVersion: secret.secretVersion, |
| 320 | }; |
| 321 | |
| 322 | const publicMysqlSsl = (ssl: MysqlSslConfig | undefined) => |
| 323 | ssl === undefined |
| 324 | ? undefined |
| 325 | : { |
| 326 | clientCertificateSet: ssl.clientCertificateSet, |
| 327 | caCertificateSet: ssl.caCertificateSet, |
| 328 | clientKeySet: ssl.clientKeySet, |
| 329 | }; |
| 330 | |
| 331 | const publicPostgresSsl = (ssl: PostgresqlSslConfig | undefined) => |
| 332 | ssl === undefined |
| 333 | ? undefined |
| 334 | : { |
| 335 | serverVerification: ssl.serverVerification |
| 336 | ? { |
| 337 | serverCertificateHostname: ssl.serverVerification.serverCertificateHostname, |
| 338 | } |
| 339 | : undefined, |
| 340 | serverAndClientVerification: ssl.serverAndClientVerification |
| 341 | ? { |
| 342 | serverCertificateHostname: ssl.serverAndClientVerification.serverCertificateHostname, |
| 343 | } |
| 344 | : undefined, |
| 345 | }; |
| 346 | |
| 347 | const publicOracleSsl = (ssl: OracleSslConfig | undefined) => |
| 348 | ssl === undefined |
| 349 | ? undefined |
| 350 | : { |
| 351 | caCertificateSet: ssl.caCertificateSet, |
| 352 | serverCertificateDistinguishedName: ssl.serverCertificateDistinguishedName, |
| 353 | }; |
| 354 | |
| 355 | const publicSqlServerSsl = (ssl: SqlServerSslConfig | undefined) => |
| 356 | ssl === undefined |
| 357 | ? undefined |
| 358 | : { |
| 359 | encryptionNotEnforced: emptyMessage(ssl.encryptionNotEnforced), |
| 360 | basicEncryption: emptyMessage(ssl.basicEncryption), |
| 361 | encryptionAndServerValidation: ssl.encryptionAndServerValidation |
| 362 | ? { |
| 363 | serverCertificateHostname: |
| 364 | ssl.encryptionAndServerValidation.serverCertificateHostname, |
| 365 | } |
| 366 | : undefined, |
| 367 | }; |
| 368 | |
| 369 | const publicMongodbSsl = (ssl: MongodbSslConfig | undefined) => |
| 370 | ssl === undefined |
| 371 | ? undefined |
| 372 | : { |
| 373 | clientCertificateSet: ssl.clientCertificateSet, |
| 374 | clientKeySet: ssl.clientKeySet, |
| 375 | caCertificateSet: ssl.caCertificateSet, |
| 376 | secretManagerStoredClientKey: ssl.secretManagerStoredClientKey, |
| 377 | }; |
| 378 | |
| 379 | const publicMysql = (mysql: MysqlProfile | undefined) => |
| 380 | mysql === undefined |
| 381 | ? undefined |
| 382 | : { |
| 383 | username: mysql.username, |
| 384 | port: mysql.port, |
| 385 | hostname: mysql.hostname, |
| 386 | secretManagerStoredPassword: mysql.secretManagerStoredPassword, |
| 387 | sslConfig: publicMysqlSsl(mysql.sslConfig), |
| 388 | }; |
| 389 | |
| 390 | const publicPostgresql = (postgresql: PostgresqlProfile | undefined) => |
| 391 | postgresql === undefined |
| 392 | ? undefined |
| 393 | : { |
| 394 | hostname: postgresql.hostname, |
| 395 | database: postgresql.database, |
| 396 | username: postgresql.username, |
| 397 | port: postgresql.port, |
| 398 | secretManagerStoredPassword: postgresql.secretManagerStoredPassword, |
| 399 | sslConfig: publicPostgresSsl(postgresql.sslConfig), |
| 400 | }; |
| 401 | |
| 402 | const publicOracleAsm = (asm: OracleAsmConfig | undefined) => |
| 403 | asm === undefined |
| 404 | ? undefined |
| 405 | : { |
| 406 | port: asm.port, |
| 407 | connectionAttributes: asm.connectionAttributes, |
| 408 | username: asm.username, |
| 409 | secretManagerStoredPassword: asm.secretManagerStoredPassword, |
| 410 | oracleSslConfig: publicOracleSsl(asm.oracleSslConfig), |
| 411 | hostname: asm.hostname, |
| 412 | asmService: asm.asmService, |
| 413 | }; |
| 414 | |
| 415 | const publicOracle = (oracle: OracleProfile | undefined) => |
| 416 | oracle === undefined |
| 417 | ? undefined |
| 418 | : { |
| 419 | connectionAttributes: oracle.connectionAttributes, |
| 420 | databaseService: oracle.databaseService, |
| 421 | hostname: oracle.hostname, |
| 422 | oracleSslConfig: publicOracleSsl(oracle.oracleSslConfig), |
| 423 | oracleAsmConfig: publicOracleAsm(oracle.oracleAsmConfig), |
| 424 | username: oracle.username, |
| 425 | secretManagerStoredPassword: oracle.secretManagerStoredPassword, |
| 426 | port: oracle.port, |
| 427 | }; |
| 428 | |
| 429 | const publicSqlServer = (sqlserver: SqlServerProfile | undefined) => |
| 430 | sqlserver === undefined |
| 431 | ? undefined |
| 432 | : { |
| 433 | hostname: sqlserver.hostname, |
| 434 | sslConfig: publicSqlServerSsl(sqlserver.sslConfig), |
| 435 | database: sqlserver.database, |
| 436 | username: sqlserver.username, |
| 437 | secretManagerStoredPassword: sqlserver.secretManagerStoredPassword, |
| 438 | port: sqlserver.port, |
| 439 | }; |
| 440 | |
| 441 | const publicMongodb = (mongodb: MongodbProfile | undefined) => |
| 442 | mongodb === undefined |
| 443 | ? undefined |
| 444 | : { |
| 445 | hostAddresses: mongodb.hostAddresses, |
| 446 | username: mongodb.username, |
| 447 | secretManagerStoredPassword: mongodb.secretManagerStoredPassword, |
| 448 | additionalOptions: mongodb.additionalOptions, |
| 449 | replicaSet: mongodb.replicaSet, |
| 450 | srvConnectionFormat: emptyMessage(mongodb.srvConnectionFormat), |
| 451 | standardConnectionFormat: mongodb.standardConnectionFormat, |
| 452 | sslConfig: publicMongodbSsl(mongodb.sslConfig), |
| 453 | }; |
| 454 | |
| 455 | const publicSalesforce = (salesforce: SalesforceProfile | undefined) => |
| 456 | salesforce === undefined |
| 457 | ? undefined |
| 458 | : { |
| 459 | domain: salesforce.domain, |
| 460 | userCredentials: salesforce.userCredentials |
| 461 | ? { |
| 462 | username: salesforce.userCredentials.username, |
| 463 | secretManagerStoredPassword: salesforce.userCredentials.secretManagerStoredPassword, |
| 464 | secretManagerStoredSecurityToken: |
| 465 | salesforce.userCredentials.secretManagerStoredSecurityToken, |
| 466 | } |
| 467 | : undefined, |
| 468 | oauth2ClientCredentials: salesforce.oauth2ClientCredentials |
| 469 | ? { |
| 470 | clientId: salesforce.oauth2ClientCredentials.clientId, |
| 471 | secretManagerStoredClientSecret: |
| 472 | salesforce.oauth2ClientCredentials.secretManagerStoredClientSecret, |
| 473 | } |
| 474 | : undefined, |
| 475 | }; |
| 476 | |
| 477 | const publicSfmc = (profile: SalesforceMarketingCloudProfile | undefined) => |
| 478 | profile === undefined |
| 479 | ? undefined |
| 480 | : { |
| 481 | subdomain: profile.subdomain, |
| 482 | oauthClientCredentials: profile.oauthClientCredentials |
| 483 | ? { |
| 484 | clientId: profile.oauthClientCredentials.clientId, |
| 485 | clientSecret: publicSecret(profile.oauthClientCredentials.clientSecret), |
| 486 | } |
| 487 | : undefined, |
| 488 | }; |
| 489 | |
| 490 | const publicServiceNow = (profile: ServiceNowProfile | undefined) => |
| 491 | profile === undefined |
| 492 | ? undefined |
| 493 | : { |
| 494 | instance: profile.instance, |
| 495 | userPasswordCredentials: profile.userPasswordCredentials |
| 496 | ? { |
| 497 | username: profile.userPasswordCredentials.username, |
| 498 | password: publicSecret(profile.userPasswordCredentials.password), |
| 499 | } |
| 500 | : undefined, |
| 501 | oauthClientCredentials: profile.oauthClientCredentials |
| 502 | ? { |
| 503 | clientId: profile.oauthClientCredentials.clientId, |
| 504 | clientSecret: publicSecret(profile.oauthClientCredentials.clientSecret), |
| 505 | } |
| 506 | : undefined, |
| 507 | }; |
| 508 | |
| 509 | const publicDataverse = (profile: DataverseProfile | undefined) => |
| 510 | profile === undefined |
| 511 | ? undefined |
| 512 | : { |
| 513 | tenantId: profile.tenantId, |
| 514 | environmentUrl: profile.environmentUrl, |
| 515 | oauthClientCredentials: profile.oauthClientCredentials |
| 516 | ? { |
| 517 | clientId: profile.oauthClientCredentials.clientId, |
| 518 | clientSecret: publicSecret(profile.oauthClientCredentials.clientSecret), |
| 519 | } |
| 520 | : undefined, |
| 521 | }; |
| 522 | |
| 523 | const publicForwardSsh = (ssh: ForwardSshTunnelConnectivity | undefined) => |
| 524 | ssh === undefined |
| 525 | ? undefined |
| 526 | : { |
| 527 | hostname: ssh.hostname, |
| 528 | username: ssh.username, |
| 529 | port: ssh.port, |
| 530 | }; |
| 531 | |
| 532 | const publicPrivateConnectivity = ( |
| 533 | value: PrivateConnectivity | ConnectionProfileProps["privateConnectivity"], |
| 534 | project: string, |
| 535 | location: string, |
| 536 | ) => |
| 537 | value?.privateConnection === undefined |
| 538 | ? undefined |
| 539 | : { |
| 540 | privateConnection: privateConnectionOf(value.privateConnection, project, location), |
| 541 | }; |
| 542 | |
| 543 | const secretChanged = (newsHasSecret: boolean, publicPrevious: unknown, publicNext: unknown) => |
| 544 | newsHasSecret || fingerprint(publicPrevious) !== fingerprint(publicNext); |
| 545 | |
| 546 | const mysqlHasSecret = (mysql: MysqlProfile | undefined) => |
| 547 | mysql?.password !== undefined || |
| 548 | mysql?.sslConfig?.clientCertificate !== undefined || |
| 549 | mysql?.sslConfig?.clientKey !== undefined || |
| 550 | mysql?.sslConfig?.caCertificate !== undefined; |
| 551 | |
| 552 | const postgresqlHasSecret = (postgresql: PostgresqlProfile | undefined) => |
| 553 | postgresql?.password !== undefined || |
| 554 | postgresql?.sslConfig?.serverVerification?.caCertificate !== undefined || |
| 555 | postgresql?.sslConfig?.serverAndClientVerification?.caCertificate !== undefined || |
| 556 | postgresql?.sslConfig?.serverAndClientVerification?.clientCertificate !== undefined || |
| 557 | postgresql?.sslConfig?.serverAndClientVerification?.clientKey !== undefined; |
| 558 | |
| 559 | const oracleHasSecret = (oracle: OracleProfile | undefined) => |
| 560 | oracle?.password !== undefined || |
| 561 | oracle?.oracleSslConfig?.caCertificate !== undefined || |
| 562 | oracle?.oracleAsmConfig?.password !== undefined || |
| 563 | oracle?.oracleAsmConfig?.oracleSslConfig?.caCertificate !== undefined; |
| 564 | |
| 565 | const sqlServerHasSecret = (sqlserver: SqlServerProfile | undefined) => |
| 566 | sqlserver?.password !== undefined || |
| 567 | sqlserver?.sslConfig?.encryptionAndServerValidation?.caCertificate !== undefined; |
| 568 | |
| 569 | const mongodbHasSecret = (mongodb: MongodbProfile | undefined) => |
| 570 | mongodb?.password !== undefined || |
| 571 | mongodb?.sslConfig?.clientCertificate !== undefined || |
| 572 | mongodb?.sslConfig?.clientKey !== undefined || |
| 573 | mongodb?.sslConfig?.caCertificate !== undefined; |
| 574 | |
| 575 | const salesforceHasSecret = (salesforce: SalesforceProfile | undefined) => |
| 576 | salesforce?.userCredentials?.password !== undefined || |
| 577 | salesforce?.userCredentials?.securityToken !== undefined || |
| 578 | salesforce?.oauth2ClientCredentials?.clientSecret !== undefined; |
| 579 | |
| 580 | const sfmcHasSecret = (profile: SalesforceMarketingCloudProfile | undefined) => |
| 581 | profile?.oauthClientCredentials?.clientSecret?.rawValue !== undefined; |
| 582 | |
| 583 | const serviceNowHasSecret = (profile: ServiceNowProfile | undefined) => |
| 584 | profile?.userPasswordCredentials?.password?.rawValue !== undefined || |
| 585 | profile?.oauthClientCredentials?.clientSecret?.rawValue !== undefined; |
| 586 | |
| 587 | const dataverseHasSecret = (profile: DataverseProfile | undefined) => |
| 588 | profile?.oauthClientCredentials?.clientSecret?.rawValue !== undefined; |
| 589 | |
| 590 | const sshHasSecret = (ssh: ForwardSshTunnelConnectivity | undefined) => |
| 591 | ssh?.password !== undefined || ssh?.privateKey !== undefined; |
| 592 | |
| 593 | const toAttrs = (profile: ds.ConnectionProfile, project: string, locationHint: string) => { |
| 594 | const name = profile.name ?? ""; |
| 595 | const parsed = parseName(name, "connectionProfiles", locationHint); |
| 596 | return { |
| 597 | name, |
| 598 | connectionProfileId: parsed.id, |
| 599 | project: parsed.project || project, |
| 600 | location: parsed.location || locationHint, |
| 601 | displayName: profile.displayName, |
| 602 | labels: userLabels(profile.labels), |
| 603 | mysqlProfile: publicMysql(profile.mysqlProfile), |
| 604 | postgresqlProfile: publicPostgresql(profile.postgresqlProfile), |
| 605 | oracleProfile: publicOracle(profile.oracleProfile), |
| 606 | sqlServerProfile: publicSqlServer(profile.sqlServerProfile), |
| 607 | mongodbProfile: publicMongodb(profile.mongodbProfile), |
| 608 | spannerProfile: profile.spannerProfile, |
| 609 | gcsProfile: profile.gcsProfile, |
| 610 | bigqueryProfile: emptyMessage(profile.bigqueryProfile), |
| 611 | salesforceProfile: publicSalesforce(profile.salesforceProfile), |
| 612 | salesforceMarketingCloudProfile: publicSfmc(profile.salesforceMarketingCloudProfile), |
| 613 | serviceNowProfile: publicServiceNow(profile.serviceNowProfile), |
| 614 | dataverseProfile: publicDataverse(profile.dataverseProfile), |
| 615 | staticServiceIpConnectivity: emptyMessage(profile.staticServiceIpConnectivity), |
| 616 | forwardSshConnectivity: publicForwardSsh(profile.forwardSshConnectivity), |
| 617 | privateConnectivity: profile.privateConnectivity, |
| 618 | satisfiesPzi: profile.satisfiesPzi, |
| 619 | satisfiesPzs: profile.satisfiesPzs, |
| 620 | createTime: profile.createTime, |
| 621 | updateTime: profile.updateTime, |
| 622 | }; |
| 623 | }; |
| 624 | |
| 625 | const getByName = (name: string) => |
| 626 | name.length === 0 |
| 627 | ? Effect.undefined |
| 628 | : ds |
| 629 | .getProjectsLocationsConnectionProfiles({ name }) |
| 630 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 631 | |
| 632 | const listOwned = (project: string, region: string) => |
| 633 | listAtLocation(project, region, (parent) => |
| 634 | collectPages( |
| 635 | ds.listProjectsLocationsConnectionProfiles.pages({ |
| 636 | parent, |
| 637 | pageSize: 1000, |
| 638 | }), |
| 639 | (page) => page.connectionProfiles, |
| 640 | ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))), |
| 641 | ); |
| 642 | |
| 643 | const connectivityOf = (news: ConnectionProfileProps, project: string, location: string) => ({ |
| 644 | staticServiceIpConnectivity: emptyMessage(news.staticServiceIpConnectivity), |
| 645 | forwardSshConnectivity: news.forwardSshConnectivity, |
| 646 | privateConnectivity: publicPrivateConnectivity(news.privateConnectivity, project, location), |
| 647 | }); |
| 648 | |
| 649 | export const ConnectionProfileProvider = () => |
| 650 | Provider.succeed(ConnectionProfile, { |
| 651 | stables: ["name", "connectionProfileId", "project", "location", "createTime"], |
| 652 | |
| 653 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 654 | if (!isResolved(news)) return undefined; |
| 655 | const env = yield* GcpEnvironment.current; |
| 656 | const previousKind = kindOf({ |
| 657 | mysqlProfile: olds?.mysqlProfile ?? output?.mysqlProfile, |
| 658 | postgresqlProfile: olds?.postgresqlProfile ?? output?.postgresqlProfile, |
| 659 | oracleProfile: olds?.oracleProfile ?? output?.oracleProfile, |
| 660 | sqlServerProfile: olds?.sqlServerProfile ?? output?.sqlServerProfile, |
| 661 | mongodbProfile: olds?.mongodbProfile ?? output?.mongodbProfile, |
| 662 | spannerProfile: olds?.spannerProfile ?? output?.spannerProfile, |
| 663 | gcsProfile: olds?.gcsProfile ?? output?.gcsProfile, |
| 664 | bigqueryProfile: olds?.bigqueryProfile ?? output?.bigqueryProfile, |
| 665 | salesforceProfile: olds?.salesforceProfile ?? output?.salesforceProfile, |
| 666 | salesforceMarketingCloudProfile: |
| 667 | olds?.salesforceMarketingCloudProfile ?? output?.salesforceMarketingCloudProfile, |
| 668 | serviceNowProfile: olds?.serviceNowProfile ?? output?.serviceNowProfile, |
| 669 | dataverseProfile: olds?.dataverseProfile ?? output?.dataverseProfile, |
| 670 | }); |
| 671 | const nextKind = kindOf(news) || previousKind; |
| 672 | const previousSpanner = olds?.spannerProfile?.database ?? output?.spannerProfile?.database; |
| 673 | const nextSpanner = news.spannerProfile?.database ?? previousSpanner; |
| 674 | return replaceOnIdentity({ |
| 675 | previousId: olds?.connectionProfileId ?? output?.connectionProfileId, |
| 676 | nextId: |
| 677 | news.connectionProfileId ?? olds?.connectionProfileId ?? output?.connectionProfileId, |
| 678 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 679 | nextLocation: normalizeLocation( |
| 680 | news.location ?? olds?.location ?? output?.location, |
| 681 | env.region, |
| 682 | ), |
| 683 | extra: |
| 684 | previousKind !== nextKind || |
| 685 | (nextKind === "spanner" && |
| 686 | previousSpanner !== undefined && |
| 687 | nextSpanner !== undefined && |
| 688 | previousSpanner !== nextSpanner), |
| 689 | }); |
| 690 | }), |
| 691 | |
| 692 | read: Effect.fn(function* ({ id, olds, output }) { |
| 693 | const env = yield* GcpEnvironment.current; |
| 694 | const connectionProfileId = yield* toPhysicalId( |
| 695 | id, |
| 696 | olds?.connectionProfileId, |
| 697 | output?.connectionProfileId, |
| 698 | "profile", |
| 699 | ); |
| 700 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 701 | const name = output?.name ?? resourceName(env.project, location, connectionProfileId); |
| 702 | const existing = yield* getByName(name); |
| 703 | if (existing === undefined) return undefined; |
| 704 | const attrs = toAttrs(existing, env.project, location); |
| 705 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 706 | }), |
| 707 | |
| 708 | list: () => |
| 709 | Effect.gen(function* () { |
| 710 | const env = yield* GcpEnvironment.current; |
| 711 | const items = yield* listOwned(env.project, env.region); |
| 712 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 713 | }), |
| 714 | |
| 715 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 716 | const env = yield* GcpEnvironment.current; |
| 717 | const connectionProfileId = yield* toPhysicalId( |
| 718 | id, |
| 719 | news.connectionProfileId, |
| 720 | output?.connectionProfileId, |
| 721 | "profile", |
| 722 | ); |
| 723 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 724 | const name = resourceName(env.project, location, connectionProfileId); |
| 725 | const desiredLabels = { |
| 726 | ...toLabels(news.labels), |
| 727 | ...(yield* createInternalLabels(id)), |
| 728 | }; |
| 729 | const displayName = news.displayName ?? connectionProfileId; |
| 730 | const connectivity = connectivityOf(news, env.project, location); |
| 731 | const body: ds.ConnectionProfile = { |
| 732 | displayName, |
| 733 | labels: desiredLabels, |
| 734 | mysqlProfile: news.mysqlProfile, |
| 735 | postgresqlProfile: news.postgresqlProfile, |
| 736 | oracleProfile: news.oracleProfile, |
| 737 | sqlServerProfile: news.sqlServerProfile, |
| 738 | mongodbProfile: news.mongodbProfile, |
| 739 | spannerProfile: news.spannerProfile, |
| 740 | gcsProfile: news.gcsProfile, |
| 741 | bigqueryProfile: emptyMessage(news.bigqueryProfile), |
| 742 | salesforceProfile: news.salesforceProfile, |
| 743 | salesforceMarketingCloudProfile: news.salesforceMarketingCloudProfile, |
| 744 | serviceNowProfile: news.serviceNowProfile, |
| 745 | dataverseProfile: news.dataverseProfile, |
| 746 | ...connectivity, |
| 747 | }; |
| 748 | |
| 749 | let current = yield* getByName(output?.name ?? name); |
| 750 | |
| 751 | if (current === undefined) { |
| 752 | yield* ds |
| 753 | .createProjectsLocationsConnectionProfiles({ |
| 754 | parent: locationParent(env.project, location), |
| 755 | connectionProfileId, |
| 756 | force: news.force, |
| 757 | validateOnly: news.validateOnly, |
| 758 | body, |
| 759 | }) |
| 760 | .pipe( |
| 761 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 762 | Effect.flatMap((created) => settleOperation(created)), |
| 763 | // Datastream intermittently fails the create operation with |
| 764 | // INTERNAL ("an internal error has occurred") under load. |
| 765 | Effect.retry({ |
| 766 | while: (error) => error._tag === "GCP.OperationFailed" && error.code === 13, |
| 767 | times: 4, |
| 768 | schedule: Schedule.exponential("5 seconds"), |
| 769 | }), |
| 770 | ); |
| 771 | current = yield* waitUntilExists(getByName(name), name); |
| 772 | } |
| 773 | |
| 774 | if (current === undefined) { |
| 775 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 776 | } |
| 777 | |
| 778 | const observedLabels = tagRecord(current.labels); |
| 779 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 780 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 781 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 782 | const mysqlChanged = |
| 783 | news.mysqlProfile !== undefined && |
| 784 | secretChanged( |
| 785 | mysqlHasSecret(news.mysqlProfile), |
| 786 | publicMysql(current.mysqlProfile), |
| 787 | publicMysql(news.mysqlProfile), |
| 788 | ); |
| 789 | const postgresqlChanged = |
| 790 | news.postgresqlProfile !== undefined && |
| 791 | secretChanged( |
| 792 | postgresqlHasSecret(news.postgresqlProfile), |
| 793 | publicPostgresql(current.postgresqlProfile), |
| 794 | publicPostgresql(news.postgresqlProfile), |
| 795 | ); |
| 796 | const oracleChanged = |
| 797 | news.oracleProfile !== undefined && |
| 798 | secretChanged( |
| 799 | oracleHasSecret(news.oracleProfile), |
| 800 | publicOracle(current.oracleProfile), |
| 801 | publicOracle(news.oracleProfile), |
| 802 | ); |
| 803 | const sqlServerChanged = |
| 804 | news.sqlServerProfile !== undefined && |
| 805 | secretChanged( |
| 806 | sqlServerHasSecret(news.sqlServerProfile), |
| 807 | publicSqlServer(current.sqlServerProfile), |
| 808 | publicSqlServer(news.sqlServerProfile), |
| 809 | ); |
| 810 | const mongodbChanged = |
| 811 | news.mongodbProfile !== undefined && |
| 812 | secretChanged( |
| 813 | mongodbHasSecret(news.mongodbProfile), |
| 814 | publicMongodb(current.mongodbProfile), |
| 815 | publicMongodb(news.mongodbProfile), |
| 816 | ); |
| 817 | const gcsChanged = |
| 818 | news.gcsProfile !== undefined && |
| 819 | fingerprint(current.gcsProfile) !== fingerprint(news.gcsProfile); |
| 820 | const salesforceChanged = |
| 821 | news.salesforceProfile !== undefined && |
| 822 | secretChanged( |
| 823 | salesforceHasSecret(news.salesforceProfile), |
| 824 | publicSalesforce(current.salesforceProfile), |
| 825 | publicSalesforce(news.salesforceProfile), |
| 826 | ); |
| 827 | const sfmcChanged = |
| 828 | news.salesforceMarketingCloudProfile !== undefined && |
| 829 | secretChanged( |
| 830 | sfmcHasSecret(news.salesforceMarketingCloudProfile), |
| 831 | publicSfmc(current.salesforceMarketingCloudProfile), |
| 832 | publicSfmc(news.salesforceMarketingCloudProfile), |
| 833 | ); |
| 834 | const serviceNowChanged = |
| 835 | news.serviceNowProfile !== undefined && |
| 836 | secretChanged( |
| 837 | serviceNowHasSecret(news.serviceNowProfile), |
| 838 | publicServiceNow(current.serviceNowProfile), |
| 839 | publicServiceNow(news.serviceNowProfile), |
| 840 | ); |
| 841 | const dataverseChanged = |
| 842 | news.dataverseProfile !== undefined && |
| 843 | secretChanged( |
| 844 | dataverseHasSecret(news.dataverseProfile), |
| 845 | publicDataverse(current.dataverseProfile), |
| 846 | publicDataverse(news.dataverseProfile), |
| 847 | ); |
| 848 | const desiredPrivate = publicPrivateConnectivity( |
| 849 | news.privateConnectivity, |
| 850 | env.project, |
| 851 | location, |
| 852 | ); |
| 853 | const staticChanged = |
| 854 | news.staticServiceIpConnectivity !== undefined && |
| 855 | fingerprint(emptyMessage(current.staticServiceIpConnectivity)) !== |
| 856 | fingerprint(emptyMessage(news.staticServiceIpConnectivity)); |
| 857 | const sshChanged = |
| 858 | news.forwardSshConnectivity !== undefined && |
| 859 | secretChanged( |
| 860 | sshHasSecret(news.forwardSshConnectivity), |
| 861 | publicForwardSsh(current.forwardSshConnectivity), |
| 862 | publicForwardSsh(news.forwardSshConnectivity), |
| 863 | ); |
| 864 | const privateChanged = |
| 865 | news.privateConnectivity !== undefined && |
| 866 | fingerprint(current.privateConnectivity) !== fingerprint(desiredPrivate); |
| 867 | const mask = fieldMask([ |
| 868 | labelsChanged && "labels", |
| 869 | displayNameChanged && "displayName", |
| 870 | mysqlChanged && "mysqlProfile", |
| 871 | postgresqlChanged && "postgresqlProfile", |
| 872 | oracleChanged && "oracleProfile", |
| 873 | sqlServerChanged && "sqlServerProfile", |
| 874 | mongodbChanged && "mongodbProfile", |
| 875 | gcsChanged && "gcsProfile", |
| 876 | salesforceChanged && "salesforceProfile", |
| 877 | sfmcChanged && "salesforceMarketingCloudProfile", |
| 878 | serviceNowChanged && "serviceNowProfile", |
| 879 | dataverseChanged && "dataverseProfile", |
| 880 | staticChanged && "staticServiceIpConnectivity", |
| 881 | sshChanged && "forwardSshConnectivity", |
| 882 | privateChanged && "privateConnectivity", |
| 883 | ]); |
| 884 | |
| 885 | if (mask.length > 0) { |
| 886 | const patch: ds.ConnectionProfile = { name: current.name ?? name }; |
| 887 | if (labelsChanged) patch.labels = desiredLabels; |
| 888 | if (displayNameChanged) patch.displayName = displayName; |
| 889 | if (mysqlChanged) patch.mysqlProfile = news.mysqlProfile; |
| 890 | if (postgresqlChanged) patch.postgresqlProfile = news.postgresqlProfile; |
| 891 | if (oracleChanged) patch.oracleProfile = news.oracleProfile; |
| 892 | if (sqlServerChanged) patch.sqlServerProfile = news.sqlServerProfile; |
| 893 | if (mongodbChanged) patch.mongodbProfile = news.mongodbProfile; |
| 894 | if (gcsChanged) patch.gcsProfile = news.gcsProfile; |
| 895 | if (salesforceChanged) patch.salesforceProfile = news.salesforceProfile; |
| 896 | if (sfmcChanged) { |
| 897 | patch.salesforceMarketingCloudProfile = news.salesforceMarketingCloudProfile; |
| 898 | } |
| 899 | if (serviceNowChanged) patch.serviceNowProfile = news.serviceNowProfile; |
| 900 | if (dataverseChanged) patch.dataverseProfile = news.dataverseProfile; |
| 901 | if (staticChanged) { |
| 902 | patch.staticServiceIpConnectivity = connectivity.staticServiceIpConnectivity; |
| 903 | } |
| 904 | if (sshChanged) { |
| 905 | patch.forwardSshConnectivity = connectivity.forwardSshConnectivity; |
| 906 | } |
| 907 | if (privateChanged) { |
| 908 | patch.privateConnectivity = connectivity.privateConnectivity; |
| 909 | } |
| 910 | const operation = yield* ds.patchProjectsLocationsConnectionProfiles({ |
| 911 | name: current.name ?? name, |
| 912 | updateMask: mask, |
| 913 | force: news.force, |
| 914 | validateOnly: news.validateOnly, |
| 915 | body: patch, |
| 916 | }); |
| 917 | yield* settleOperation(operation); |
| 918 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 919 | } |
| 920 | |
| 921 | return toAttrs(current, env.project, location); |
| 922 | }), |
| 923 | |
| 924 | delete: Effect.fn(function* ({ output }) { |
| 925 | const operation = yield* ds |
| 926 | .deleteProjectsLocationsConnectionProfiles({ |
| 927 | name: output.name, |
| 928 | }) |
| 929 | .pipe( |
| 930 | Effect.retry({ |
| 931 | while: (error) => error._tag === "Conflict", |
| 932 | times: 8, |
| 933 | schedule: Schedule.spaced("2 seconds"), |
| 934 | }), |
| 935 | Effect.catchTag(["NotFound", "Conflict"], () => Effect.undefined), |
| 936 | ); |
| 937 | yield* settleOperation(operation, { notFoundOk: true }); |
| 938 | yield* waitUntilGone(getByName(output.name), output.name, { |
| 939 | times: 15, |
| 940 | interval: "2 seconds", |
| 941 | }).pipe(Effect.catchTag("GCP.Datastream.ResourceStillExists", () => Effect.void)); |
| 942 | }), |
| 943 | }); |