|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { DataZone, ListConnectionsCommandOutput } from '@aws-sdk/client-datazone' |
| 7 | +import { getLogger } from '../../../shared/logger/logger' |
| 8 | + |
| 9 | +/** |
| 10 | + * Represents a DataZone connection |
| 11 | + */ |
| 12 | +export interface DataZoneConnection { |
| 13 | + connectionId: string |
| 14 | + name: string |
| 15 | + type: string |
| 16 | + props?: Record<string, any> |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * DataZone client for use in a SageMaker Unified Studio connected space |
| 21 | + * Uses the user's current AWS credentials (project role credentials) |
| 22 | + */ |
| 23 | +export class ConnectedSpaceDataZoneClient { |
| 24 | + private datazoneClient: DataZone | undefined |
| 25 | + private readonly logger = getLogger() |
| 26 | + |
| 27 | + constructor( |
| 28 | + private readonly region: string, |
| 29 | + private readonly customEndpoint?: string |
| 30 | + ) {} |
| 31 | + |
| 32 | + /** |
| 33 | + * Gets the DataZone client, initializing it if necessary |
| 34 | + * Uses default AWS credentials from the environment (project role) |
| 35 | + * Supports custom endpoints for non-production environments |
| 36 | + */ |
| 37 | + private getDataZoneClient(): DataZone { |
| 38 | + if (!this.datazoneClient) { |
| 39 | + try { |
| 40 | + const clientConfig: any = { |
| 41 | + region: this.region, |
| 42 | + } |
| 43 | + |
| 44 | + // Use custom endpoint if provided (for non-prod environments) |
| 45 | + if (this.customEndpoint) { |
| 46 | + clientConfig.endpoint = this.customEndpoint |
| 47 | + this.logger.debug( |
| 48 | + `ConnectedSpaceDataZoneClient: Using custom DataZone endpoint: ${this.customEndpoint}` |
| 49 | + ) |
| 50 | + } else { |
| 51 | + this.logger.debug( |
| 52 | + `ConnectedSpaceDataZoneClient: Using default AWS DataZone endpoint for region: ${this.region}` |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + this.logger.debug('ConnectedSpaceDataZoneClient: Creating DataZone client with default credentials') |
| 57 | + this.datazoneClient = new DataZone(clientConfig) |
| 58 | + this.logger.debug('ConnectedSpaceDataZoneClient: Successfully created DataZone client') |
| 59 | + } catch (err) { |
| 60 | + this.logger.error('ConnectedSpaceDataZoneClient: Failed to create DataZone client: %s', err as Error) |
| 61 | + throw err |
| 62 | + } |
| 63 | + } |
| 64 | + return this.datazoneClient |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Lists the connections in a DataZone domain and project |
| 69 | + * @param domainId The DataZone domain identifier |
| 70 | + * @param projectId The DataZone project identifier |
| 71 | + * @returns List of connections |
| 72 | + */ |
| 73 | + public async listConnections(domainId: string, projectId: string): Promise<DataZoneConnection[]> { |
| 74 | + try { |
| 75 | + this.logger.info( |
| 76 | + `ConnectedSpaceDataZoneClient: Listing connections for domain ${domainId}, project ${projectId}` |
| 77 | + ) |
| 78 | + |
| 79 | + const datazoneClient = this.getDataZoneClient() |
| 80 | + |
| 81 | + const response: ListConnectionsCommandOutput = await datazoneClient.listConnections({ |
| 82 | + domainIdentifier: domainId, |
| 83 | + projectIdentifier: projectId, |
| 84 | + }) |
| 85 | + |
| 86 | + if (!response.items || response.items.length === 0) { |
| 87 | + this.logger.info( |
| 88 | + `ConnectedSpaceDataZoneClient: No connections found for domain ${domainId}, project ${projectId}` |
| 89 | + ) |
| 90 | + return [] |
| 91 | + } |
| 92 | + |
| 93 | + const connections: DataZoneConnection[] = response.items.map((connection) => ({ |
| 94 | + connectionId: connection.connectionId || '', |
| 95 | + name: connection.name || '', |
| 96 | + type: connection.type || '', |
| 97 | + props: connection.props || {}, |
| 98 | + })) |
| 99 | + |
| 100 | + this.logger.info( |
| 101 | + `ConnectedSpaceDataZoneClient: Found ${connections.length} connections for domain ${domainId}, project ${projectId}` |
| 102 | + ) |
| 103 | + return connections |
| 104 | + } catch (err) { |
| 105 | + this.logger.error('ConnectedSpaceDataZoneClient: Failed to list connections: %s', err as Error) |
| 106 | + throw err |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments