Skip to content

Commit 5027166

Browse files
committed
feat: add some action
1 parent bea89f5 commit 5027166

21 files changed

+685
-366
lines changed

plugins/scaffolder-backend-module-kusion/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ You need to add the following to your `app-config.yaml`. For example:
1010
backend:
1111
kusion:
1212
baseUrl: 'http://localhost:3000'
13-
proxyPath: '/api/v1' # Optional - Default is '/api/v1'
14-
token: 'your-token'
1513
```
1614
1715
### From your Backstage root directory

plugins/scaffolder-backend-module-kusion/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"main": "src/index.ts",
55
"types": "src/index.ts",
66
"license": "Apache-2.0",
7+
"author": {
8+
"email": "[email protected]",
9+
"name": "hoangndst"
10+
},
711
"private": true,
812
"publishConfig": {
913
"access": "public",
@@ -28,6 +32,7 @@
2832
"@backstage/backend-plugin-api": "^1.0.2",
2933
"@backstage/config": "^1.3.0",
3034
"@backstage/plugin-scaffolder-node": "^0.6.0",
35+
"@kusionstack/kusion-api-client-sdk": "^1.1.3",
3136
"node-fetch": "^2.7.0",
3237
"yaml": "^2.6.1"
3338
},

plugins/scaffolder-backend-module-kusion/src/actions/backend/createBackend.test.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

plugins/scaffolder-backend-module-kusion/src/actions/backend/createBackend.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
22
import { Config } from '@backstage/config';
3-
import { createKusionApi } from '../../api';
3+
import { configKusionApi } from '../../api';
4+
import {
5+
BackendService,
6+
CreateBackendData,
7+
} from '@kusionstack/kusion-api-client-sdk';
48
import { examples } from './createBackend.example';
59

610
/**
@@ -73,33 +77,35 @@ export function createCreateBackendAction(options: { config: Config }) {
7377
},
7478
async handler(ctx) {
7579
const { name, description, backendConfig } = ctx.input;
76-
const kusionApi = createKusionApi({ configApi: config });
77-
const requestBody = {
78-
name,
79-
description,
80-
backendConfig,
80+
configKusionApi({ configApi: config });
81+
const requestBody: CreateBackendData = {
82+
body: {
83+
name: name,
84+
description: description,
85+
backendConfig: backendConfig,
86+
},
8187
};
8288

8389
ctx.logger.info(
8490
'Creating backend with the following request body: ',
8591
requestBody,
8692
);
8793

88-
const response = await kusionApi.post('backends', requestBody);
94+
const response = await BackendService.createBackend(requestBody);
8995

90-
if (!response.success || response.data === undefined) {
96+
if (!response.data?.success) {
9197
ctx.logger.error(`
92-
Unable to create backend, ${response.message}`);
93-
ctx.output('success', response.success);
94-
ctx.output('message', response.message);
95-
ctx.output('data', '{}');
96-
throw new Error(`Unable to create backend, ${response.message}`);
98+
Unable to create backend, ${response.data?.message}`);
99+
ctx.output('success', response.data?.success);
100+
ctx.output('message', response.data?.message);
101+
ctx.output('data', JSON.stringify(response.data?.data));
102+
throw new Error(`Unable to create backend, ${response.data?.message}`);
97103
}
98104

99105
ctx.logger.info('Backend created successfully');
100-
ctx.output('success', response.success);
101-
ctx.output('message', response.message);
102-
ctx.output('data', JSON.stringify(response.data));
106+
ctx.output('success', response.data?.success);
107+
ctx.output('message', response.data?.message);
108+
ctx.output('data', JSON.stringify(response.data?.data));
103109
},
104110
});
105111
}

plugins/scaffolder-backend-module-kusion/src/api/types.ts renamed to plugins/scaffolder-backend-module-kusion/src/actions/backend/deleteBackend.example.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
const DEFAULT_PROXY_PATH = '/api/v1';
17+
import { TemplateExample } from '@backstage/plugin-scaffolder-node';
18+
import yaml from 'yaml';
1819

19-
type KusionResponse = {
20-
success: boolean;
21-
message: string;
22-
data?: any;
23-
traceID?: string;
24-
startTime?: Date;
25-
endTime?: Date;
26-
costTime?: number;
27-
};
20+
export const examples: TemplateExample[] = [
21+
{
22+
description: 'Delete a backend in Kusion',
23+
example: yaml.stringify({
24+
steps: [
25+
{
26+
id: 'delete-backend',
27+
action: 'kusion:backend:delete',
28+
name: 'Delete backend',
29+
input: {
30+
id: '1',
31+
},
32+
},
33+
],
34+
}),
35+
},
36+
];
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2024 The Backstage Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
18+
import { Config } from '@backstage/config';
19+
import { configKusionApi } from '../../api';
20+
import {
21+
BackendService,
22+
DeleteBackendData,
23+
} from '@kusionstack/kusion-api-client-sdk';
24+
import { examples } from './deleteBackend.example';
25+
26+
/**
27+
* Creates a `kusion:backend:delete` Scaffolder action.
28+
*
29+
* @public
30+
*/
31+
export function createDeleteBackendAction(options: { config: Config }) {
32+
const { config } = options;
33+
return createTemplateAction<{
34+
id: string;
35+
}>({
36+
id: 'kusion:backend:delete',
37+
examples,
38+
schema: {
39+
input: {
40+
type: 'object',
41+
required: ['id'],
42+
properties: {
43+
id: {
44+
title: 'Backend ID',
45+
type: 'string',
46+
},
47+
},
48+
},
49+
output: {
50+
type: 'object',
51+
properties: {
52+
success: {
53+
title: 'Success',
54+
type: 'boolean',
55+
},
56+
message: {
57+
title: 'Message',
58+
type: 'string',
59+
},
60+
data: {
61+
title: 'Data',
62+
type: 'object',
63+
},
64+
},
65+
},
66+
},
67+
async handler(ctx) {
68+
const { id } = ctx.input;
69+
configKusionApi({ configApi: config });
70+
71+
ctx.logger.info('Deleting backend with ID: %s', id);
72+
73+
const request: DeleteBackendData = {
74+
path: {
75+
backendID: Number(id),
76+
},
77+
};
78+
79+
const response = await BackendService.deleteBackend(request);
80+
81+
if (!response.data?.success) {
82+
ctx.logger.error(`
83+
Unable to delete backend, ${response.data?.message}`);
84+
ctx.output('success', response.data?.success);
85+
ctx.output('message', response.data?.message);
86+
ctx.output('data', JSON.stringify(response.data?.data));
87+
throw new Error(`Unable to delete backend, ${response.data?.message}`);
88+
}
89+
90+
ctx.logger.info('Backend deleted successfully');
91+
ctx.output('success', response.data?.success);
92+
ctx.output('message', response.data?.message);
93+
ctx.output('data', response.data?.data);
94+
},
95+
});
96+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from "../backend"
1+
export * from "./createBackend";
2+
export * from "./deleteBackend";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 The Backstage Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { TemplateExample } from '@backstage/plugin-scaffolder-node';
18+
import yaml from 'yaml';
19+
20+
export const examples: TemplateExample[] = [
21+
{
22+
description: 'Create an organization in Kusion',
23+
example: yaml.stringify({
24+
steps: [
25+
{
26+
id: 'create-organization',
27+
action: 'kusion:organization:create',
28+
name: 'Create Organization',
29+
input: {
30+
name: 'my-workspace',
31+
description: 'This is my organization',
32+
labels: ['label1', 'label2'],
33+
owners: ['owner1', 'owner2'],
34+
},
35+
},
36+
],
37+
}),
38+
},
39+
];

0 commit comments

Comments
 (0)