|
| 1 | +import * as ec2 from '@aws-cdk/aws-ec2'; |
| 2 | +import * as cdk from '@aws-cdk/core'; |
| 3 | +import * as constructs from 'constructs'; |
| 4 | +import { CfnConnection } from './glue.generated'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The type of the glue connection |
| 8 | + * |
| 9 | + * If you need to use a connection type that doesn't exist as a static member, you |
| 10 | + * can instantiate a `ConnectionType` object, e.g: `new ConnectionType('NEW_TYPE')`. |
| 11 | + */ |
| 12 | +export class ConnectionType { |
| 13 | + |
| 14 | + /** |
| 15 | + * Designates a connection to a database through Java Database Connectivity (JDBC). |
| 16 | + */ |
| 17 | + public static readonly JDBC = new ConnectionType('JDBC'); |
| 18 | + |
| 19 | + /** |
| 20 | + * Designates a connection to an Apache Kafka streaming platform. |
| 21 | + */ |
| 22 | + public static readonly KAFKA = new ConnectionType('KAFKA'); |
| 23 | + |
| 24 | + /** |
| 25 | + * Designates a connection to a MongoDB document database. |
| 26 | + */ |
| 27 | + public static readonly MONGODB = new ConnectionType('MONGODB'); |
| 28 | + |
| 29 | + /** |
| 30 | + * Designates a network connection to a data source within an Amazon Virtual Private Cloud environment (Amazon VPC). |
| 31 | + */ |
| 32 | + public static readonly NETWORK = new ConnectionType('NETWORK'); |
| 33 | + |
| 34 | + /** |
| 35 | + * The name of this ConnectionType, as expected by Connection resource. |
| 36 | + */ |
| 37 | + public readonly name: string; |
| 38 | + |
| 39 | + constructor(name: string) { |
| 40 | + this.name = name; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * The connection type name as expected by Connection resource. |
| 45 | + */ |
| 46 | + public toString(): string { |
| 47 | + return this.name; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Interface representing a created or an imported {@link Connection} |
| 53 | + */ |
| 54 | +export interface IConnection extends cdk.IResource { |
| 55 | + /** |
| 56 | + * The name of the connection |
| 57 | + * @attribute |
| 58 | + */ |
| 59 | + readonly connectionName: string; |
| 60 | + |
| 61 | + /** |
| 62 | + * The ARN of the connection |
| 63 | + * @attribute |
| 64 | + */ |
| 65 | + readonly connectionArn: string; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Base Connection Options |
| 70 | + */ |
| 71 | +export interface ConnectionOptions { |
| 72 | + /** |
| 73 | + * The name of the connection |
| 74 | + * @default cloudformation generated name |
| 75 | + */ |
| 76 | + readonly connectionName?: string; |
| 77 | + |
| 78 | + /** |
| 79 | + * The description of the connection. |
| 80 | + * @default no description |
| 81 | + */ |
| 82 | + readonly description?: string; |
| 83 | + |
| 84 | + /** |
| 85 | + * Key-Value pairs that define parameters for the connection. |
| 86 | + * @default empty properties |
| 87 | + * @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-connect.html |
| 88 | + */ |
| 89 | + readonly properties?: { [key: string]: string }; |
| 90 | + |
| 91 | + /** |
| 92 | + * A list of criteria that can be used in selecting this connection. |
| 93 | + * This is useful for filtering the results of https://awscli.amazonaws.com/v2/documentation/api/latest/reference/glue/get-connections.html |
| 94 | + * @default no match criteria |
| 95 | + */ |
| 96 | + readonly matchCriteria?: string[]; |
| 97 | + |
| 98 | + /** |
| 99 | + * The list of security groups needed to successfully make this connection e.g. to successfully connect to VPC. |
| 100 | + * @default no security group |
| 101 | + */ |
| 102 | + readonly securityGroups?: ec2.ISecurityGroup[]; |
| 103 | + |
| 104 | + /** |
| 105 | + * The VPC subnet to connect to resources within a VPC. See more at https://docs.aws.amazon.com/glue/latest/dg/start-connecting.html. |
| 106 | + * @default no subnet |
| 107 | + */ |
| 108 | + readonly subnet?: ec2.ISubnet; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Construction properties for {@link Connection} |
| 113 | + */ |
| 114 | +export interface ConnectionProps extends ConnectionOptions { |
| 115 | + /** |
| 116 | + * The type of the connection |
| 117 | + */ |
| 118 | + readonly type: ConnectionType; |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * An AWS Glue connection to a data source. |
| 123 | + */ |
| 124 | +export class Connection extends cdk.Resource implements IConnection { |
| 125 | + |
| 126 | + /** |
| 127 | + * Creates a Connection construct that represents an external connection. |
| 128 | + * |
| 129 | + * @param scope The scope creating construct (usually `this`). |
| 130 | + * @param id The construct's id. |
| 131 | + * @param connectionArn arn of external connection. |
| 132 | + */ |
| 133 | + public static fromConnectionArn(scope: constructs.Construct, id: string, connectionArn: string): IConnection { |
| 134 | + class Import extends cdk.Resource implements IConnection { |
| 135 | + public readonly connectionName = cdk.Arn.extractResourceName(connectionArn, 'connection'); |
| 136 | + public readonly connectionArn = connectionArn; |
| 137 | + } |
| 138 | + |
| 139 | + return new Import(scope, id); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Creates a Connection construct that represents an external connection. |
| 144 | + * |
| 145 | + * @param scope The scope creating construct (usually `this`). |
| 146 | + * @param id The construct's id. |
| 147 | + * @param connectionName name of external connection. |
| 148 | + */ |
| 149 | + public static fromConnectionName(scope: constructs.Construct, id: string, connectionName: string): IConnection { |
| 150 | + class Import extends cdk.Resource implements IConnection { |
| 151 | + public readonly connectionName = connectionName; |
| 152 | + public readonly connectionArn = Connection.buildConnectionArn(scope, connectionName); |
| 153 | + } |
| 154 | + |
| 155 | + return new Import(scope, id); |
| 156 | + } |
| 157 | + |
| 158 | + private static buildConnectionArn(scope: constructs.Construct, connectionName: string) : string { |
| 159 | + return cdk.Stack.of(scope).formatArn({ |
| 160 | + service: 'glue', |
| 161 | + resource: 'connection', |
| 162 | + resourceName: connectionName, |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * The ARN of the connection |
| 168 | + */ |
| 169 | + public readonly connectionArn: string; |
| 170 | + |
| 171 | + /** |
| 172 | + * The name of the connection |
| 173 | + */ |
| 174 | + public readonly connectionName: string; |
| 175 | + |
| 176 | + private readonly properties: {[key: string]: string}; |
| 177 | + |
| 178 | + constructor(scope: constructs.Construct, id: string, props: ConnectionProps) { |
| 179 | + super(scope, id, { |
| 180 | + physicalName: props.connectionName, |
| 181 | + }); |
| 182 | + |
| 183 | + this.properties = props.properties || {}; |
| 184 | + |
| 185 | + const physicalConnectionRequirements = props.subnet || props.securityGroups ? { |
| 186 | + availabilityZone: props.subnet ? props.subnet.availabilityZone : undefined, |
| 187 | + subnetId: props.subnet ? props.subnet.subnetId : undefined, |
| 188 | + securityGroupIdList: props.securityGroups ? props.securityGroups.map(sg => sg.securityGroupId) : undefined, |
| 189 | + } : undefined; |
| 190 | + |
| 191 | + const connectionResource = new CfnConnection(this, 'Resource', { |
| 192 | + catalogId: cdk.Stack.of(this).account, |
| 193 | + connectionInput: { |
| 194 | + connectionProperties: cdk.Lazy.any({ produce: () => Object.keys(this.properties).length > 0 ? this.properties : undefined }), |
| 195 | + connectionType: props.type.name, |
| 196 | + description: props.description, |
| 197 | + matchCriteria: props.matchCriteria, |
| 198 | + name: props.connectionName, |
| 199 | + physicalConnectionRequirements, |
| 200 | + }, |
| 201 | + }); |
| 202 | + |
| 203 | + const resourceName = this.getResourceNameAttribute(connectionResource.ref); |
| 204 | + this.connectionArn = Connection.buildConnectionArn(this, resourceName); |
| 205 | + this.connectionName = resourceName; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Add additional connection parameters |
| 210 | + * @param key parameter key |
| 211 | + * @param value parameter value |
| 212 | + */ |
| 213 | + public addProperty(key: string, value: string): void { |
| 214 | + this.properties[key] = value; |
| 215 | + } |
| 216 | +} |
0 commit comments