-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): cloud.Function for target tf-gcp (#4197)
Fixed #614 This adds support of cloud function resource for target tf-gcp using cdktf ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [ ] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
1 parent
f8ec82d
commit 60c61fd
Showing
10 changed files
with
939 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { resolve } from "path"; | ||
import { AssetType, TerraformAsset } from "cdktf"; | ||
import { Construct } from "constructs"; | ||
import { App } from "./app"; | ||
import { Bucket } from "./bucket"; | ||
import { CloudfunctionsFunction } from "../.gen/providers/google/cloudfunctions-function"; | ||
import { StorageBucketObject } from "../.gen/providers/google/storage-bucket-object"; | ||
import * as cloud from "../cloud"; | ||
import { createBundle } from "../shared/bundling"; | ||
import { | ||
CaseConventions, | ||
NameOptions, | ||
ResourceNames, | ||
} from "../shared/resource-names"; | ||
import { IInflightHost } from "../std"; | ||
|
||
const FUNCTION_NAME_OPTS: NameOptions = { | ||
maxLen: 32, | ||
disallowedRegex: /[^a-z0-9]+/g, | ||
case: CaseConventions.LOWERCASE, | ||
}; | ||
|
||
/** | ||
* GCP implementation of `cloud.Function`. | ||
* | ||
* @inflight `@winglang/wingsdk.cloud.IFunctionClient` | ||
*/ | ||
|
||
export class Function extends cloud.Function { | ||
private readonly function: CloudfunctionsFunction; | ||
|
||
constructor( | ||
scope: Construct, | ||
id: string, | ||
inflight: cloud.IFunctionHandler, | ||
props: cloud.FunctionProps = {} | ||
) { | ||
super(scope, id, inflight, props); | ||
|
||
// app is a property of the `cloud.Function` class | ||
const app = App.of(this) as App; | ||
|
||
// bundled code is guaranteed to be in a fresh directory | ||
const bundle = createBundle(this.entrypoint); | ||
|
||
// Create Cloud Function executable | ||
const asset = new TerraformAsset(this, "Asset", { | ||
path: resolve(bundle.directory), | ||
type: AssetType.ARCHIVE, | ||
}); | ||
|
||
// memory limits must be between 128 and 8192 MB | ||
if (props?.memory && (props.memory < 128 || props.memory > 8192)) { | ||
throw new Error( | ||
"Memory must be between 128 and 8192 MB for GCP Cloud Functions" | ||
); | ||
} | ||
|
||
// timeout must be between 1 and 540 seconds | ||
if ( | ||
props?.timeout && | ||
(props.timeout.seconds < 1 || props.timeout.seconds > 540) | ||
) { | ||
throw new Error( | ||
"Timeout must be between 1 and 540 seconds for GCP Cloud Functions" | ||
); | ||
} | ||
|
||
// create a bucket to store the function executable | ||
const FunctionBucket = new Bucket(this, "FunctionBucket"); | ||
|
||
// put the executable in the bucket as an object | ||
const FunctionObjectBucket = new StorageBucketObject( | ||
this, | ||
"FunctionObjectBucket", | ||
{ | ||
name: "objects", | ||
bucket: FunctionBucket.bucket.name, | ||
source: asset.path, | ||
} | ||
); | ||
|
||
// create the cloud function | ||
this.function = new CloudfunctionsFunction(this, "DefaultFunction", { | ||
name: ResourceNames.generateName(this, FUNCTION_NAME_OPTS), | ||
description: "This function was created by Wing", | ||
project: app.projectId, | ||
region: app.region, | ||
runtime: "nodejs16", | ||
availableMemoryMb: props.memory ?? 128, | ||
sourceArchiveBucket: FunctionBucket.bucket.name, | ||
sourceArchiveObject: FunctionObjectBucket.name, | ||
entryPoint: "handler", | ||
triggerHttp: true, | ||
timeout: props.timeout?.seconds ?? 60, | ||
environmentVariables: props.env ?? {}, | ||
}); | ||
} | ||
|
||
public get functionName(): string { | ||
return this.function.name; | ||
} | ||
|
||
// TODO: implement with https://github.com/winglang/wing/issues/1282 | ||
public _toInflight(): string { | ||
throw new Error( | ||
"cloud.Function cannot be used as an Inflight resource on GCP yet" | ||
); | ||
} | ||
|
||
public bind(_host: IInflightHost, _ops: string[]): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./app"; | ||
export * from "./bucket"; | ||
export * from "./function"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.