Skip to content

Commit

Permalink
fix(lang-js): Add support for connection annotations to SecretDefinit…
Browse files Browse the repository at this point in the history
…ions

This will add the specified connection annotation to the generated output socket

```
function main() {
    const credential = new SecretDefinitionBuilder()
        .setName("GHCR Credential")
        .setConnectionAnnotation("Docker Hub Credential")
        .addProp(
            new PropBuilder()
            .setName("Username")
            .setKind("string")
            .setWidget(
                new PropWidgetDefinitionBuilder()
                .setKind("password")
                .build()
            ).build())
        .build();
    return new AssetBuilder()
        .defineSecret(credential)
        .build()
}
```
  • Loading branch information
stack72 committed Jun 24, 2024
1 parent 4456346 commit 647fd94
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 23 deletions.
63 changes: 46 additions & 17 deletions bin/lang-js/src/asset_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class ValueFromBuilder implements IValueFromBuilder {
*/
setSocketName(name: string): this {
if (
this.valueFrom.kind !== "inputSocket"
&& this.valueFrom.kind !== "outputSocket"
this.valueFrom.kind !== "inputSocket" &&
this.valueFrom.kind !== "outputSocket"
) {
return this;
}
Expand Down Expand Up @@ -301,7 +301,8 @@ export interface IPropWidgetDefinitionBuilder {
* .build()
*/
export class PropWidgetDefinitionBuilder
implements IPropWidgetDefinitionBuilder {
implements IPropWidgetDefinitionBuilder
{
propWidget = <PropWidgetDefinition>{};

constructor() {
Expand Down Expand Up @@ -449,7 +450,8 @@ export interface ISiPropValueFromDefinitionBuilder {
}

export class SiPropValueFromDefinitionBuilder
implements ISiPropValueFromDefinitionBuilder {
implements ISiPropValueFromDefinitionBuilder
{
definition = <SiPropValueFromDefinition>{};

constructor() {
Expand Down Expand Up @@ -895,6 +897,7 @@ export class SecretPropBuilder implements ISecretPropBuilder {
export interface SecretDefinition {
name: string;
props: PropDefinition[];
connectionAnnotations?: string;
}

export interface ISecretDefinitionBuilder {
Expand All @@ -910,7 +913,8 @@ export interface ISecretDefinitionBuilder {
*
* @example
* const secretDefinition = new SecretDefinitionBuilder()
* .setName("DigitalOcean Token")
* .setName("DigitalOcean Token")
* .setConnectionAnnotations("Registry Token")
* .addProp(
* new PropBuilder()
* .setKind("string")
Expand All @@ -931,6 +935,7 @@ export class SecretDefinitionBuilder implements ISecretDefinitionBuilder {
this.definition = <SecretDefinition>{};
this.definition.name = "";
this.definition.props = [];
this.definition.connectionAnnotations = "";
}

/**
Expand Down Expand Up @@ -967,6 +972,21 @@ export class SecretDefinitionBuilder implements ISecretDefinitionBuilder {
return this;
}

/**
* Adds the specified connection annotations to the output socket for the secret
*
* @param {string} connectionAnnotations - the connection annotations to create for the output socket.
*
* @returns this
*
* @example
* .setConnectionAnnotation("Registry Token")
*/
setConnectionAnnotation(connectionAnnotations: string): this {
this.definition.connectionAnnotations = connectionAnnotations;
return this;
}

build(): SecretDefinition {
const def = this.definition;

Expand Down Expand Up @@ -1070,18 +1090,27 @@ export class AssetBuilder implements IAssetBuilder {
.build(),
);

this.addOutputSocket(
new SocketDefinitionBuilder()
.setArity("one")
.setName(definition.name)
.setValueFrom(
new ValueFromBuilder()
.setKind("prop")
.setPropPath(["root", "secrets", definition.name])
.build(),
)
.build(),
);
const outputSocketBuilder = new SocketDefinitionBuilder()
.setArity("one")
.setName(definition.name)
.setValueFrom(
new ValueFromBuilder()
.setKind("prop")
.setPropPath(["root", "secrets", definition.name])
.build(),
);

if (
definition.connectionAnnotations &&
definition.connectionAnnotations !== ""
) {
outputSocketBuilder.setConnectionAnnotation(
definition.connectionAnnotations,
);
}

const outputSocket = outputSocketBuilder.build();
this.addOutputSocket(outputSocket);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ declare class SocketDefinitionBuilder implements ISocketDefinitionBuilder {
}

type PropWidgetDefinitionKind =
"array"
| "array"
| "checkbox"
| "codeEditor"
| "color"
Expand Down Expand Up @@ -235,7 +235,9 @@ interface IPropWidgetDefinitionBuilder {
* .setKind("text")
* .build()
*/
declare class PropWidgetDefinitionBuilder implements IPropWidgetDefinitionBuilder {
declare class PropWidgetDefinitionBuilder
implements IPropWidgetDefinitionBuilder
{
propWidget: PropWidgetDefinition;

constructor();
Expand Down Expand Up @@ -351,7 +353,9 @@ interface ISiPropValueFromDefinitionBuilder {
build(): SiPropValueFromDefinition;
}

declare class SiPropValueFromDefinitionBuilder implements ISiPropValueFromDefinitionBuilder {
declare class SiPropValueFromDefinitionBuilder
implements ISiPropValueFromDefinitionBuilder
{
definition: SiPropValueFromDefinition;

constructor();
Expand All @@ -370,7 +374,7 @@ declare class SiPropValueFromDefinitionBuilder implements ISiPropValueFromDefini
}

type PropDefinitionKind =
"array"
| "array"
| "boolean"
| "integer"
| "map"
Expand Down Expand Up @@ -686,10 +690,12 @@ declare class SecretPropBuilder implements ISecretPropBuilder {
interface SecretDefinition {
name: string;
props: PropDefinition[];
connectionAnnotations?: string;
}

interface ISecretDefinitionBuilder {
addProp(prop: PropDefinition): this;
setName(name: string): this;

build(): SecretDefinition;
}
Expand All @@ -699,7 +705,8 @@ interface ISecretDefinitionBuilder {
*
* @example
* const secretDefinition = new SecretDefinitionBuilder()
* .setName("DigitalOcean Token")
* .setName("DigitalOcean Token")
* .setConnectionAnnotations("Registry Token")
* .addProp(
* new PropBuilder()
* .setKind("string")
Expand Down Expand Up @@ -746,6 +753,18 @@ declare class SecretDefinitionBuilder implements ISecretDefinitionBuilder {
*/
addProp(prop: PropDefinition): this;

/**
* Adds the specified connection annotations to the output socket for the secret
*
* @param {string} connectionAnnotations - the connection annotations to create for the output socket.
*
* @returns this
*
* @example
* .setConnectionAnnotation("Registry Token")
*/
setConnectionAnnotation(connectionAnnotations: string): this;

build(): SecretDefinition;
}

Expand Down Expand Up @@ -802,4 +821,4 @@ declare class AssetBuilder implements IAssetBuilder {
addDocLink(key: string, value: string): this;

build(): Asset;
}
}

0 comments on commit 647fd94

Please sign in to comment.