Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: action create backend, organization, project, workspace #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ kubernetes:
permission:
# setting this to `false` will disable permissions
enabled: true

# kusion
kusion:
baseUrl: ${KUSION_BASE_URL}
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@backstage/plugin-search-backend-module-techdocs": "^0.3.2",
"@backstage/plugin-search-backend-node": "^1.3.5",
"@backstage/plugin-techdocs-backend": "^1.11.2",
"@kusion/backstage-plugin-scaffolder-backend-module-kusion": "^0.1.0",
"app": "link:../app",
"better-sqlite3": "^9.0.0",
"node-gyp": "^10.0.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ backend.add(import('@backstage/plugin-search-backend-module-techdocs'));
// kubernetes
backend.add(import('@backstage/plugin-kubernetes-backend'));

// kusion
backend.add(import('@kusion/backstage-plugin-scaffolder-backend-module-kusion'));

backend.start();
1 change: 1 addition & 0 deletions plugins/scaffolder-backend-module-kusion/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
59 changes: 59 additions & 0 deletions plugins/scaffolder-backend-module-kusion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Backstage Plugin Scaffolder Backend Module Kusion

### Getting Started

You need a `Kusion Server` running. You can find the server [here](https://github.com/KusionStack/kusion).

You need to add the following to your `app-config.yaml`. For example:

```yaml
backend:
kusion:
baseUrl: 'http://localhost:3000'
```

### From your Backstage root directory

```bash
# From your Backstage root directory
yarn add --cwd packages/backend @kusion/backstage-plugin-scaffolder-backend-module-kusion
```

### Workspace

#### Kusion Create Workspace

The Kusion Workspace Create action that allows you to create a new Kusion Workspace from a template.

`kusion:workspace:create`

```yaml
apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
name: create-workspace
title: Create Workspace Template
description: A template to create a workspace
tags:
- kusion
- workspace
spec:
steps:
- id: create-workspace
name: Create Workspace
action: kusion:workspace:create
input:
name: ${{ parameters.name }}
description: ${{ parameters.description }}
labels: ${{ parameters.labels }}
owners: ${{ parameters.owners }}
backendID: ${{ parameters.backend_id }}
output:
text:
- title: Workspace create status
description: The status of workspace creation
content: |
Success: `${{ steps['create-workspace'].output.success }}`
Message: `${{ steps['create-workspace'].output.message }}`
Data: `${{ steps['create-workspace'].output.data }}`
```
50 changes: 50 additions & 0 deletions plugins/scaffolder-backend-module-kusion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@kusion/backstage-plugin-scaffolder-backend-module-kusion",
"version": "0.1.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"author": {
"email": "[email protected]",
"name": "hoangndst"
},
"private": true,
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "backend-plugin-module",
"pluginId": "scaffolder",
"pluginPackage": "@backstage/plugin-scaffolder-backend"
},
"scripts": {
"start": "backstage-cli package start",
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack"
},
"repository": {
"type": "git",
"url": "https://github.com/KusionStack/kusion-backstage-plugin",
"directory": "plugins/scaffolder-backend-module-kusion"
},
"dependencies": {
"@backstage/backend-plugin-api": "^1.0.2",
"@backstage/config": "^1.3.0",
"@backstage/plugin-scaffolder-node": "^0.6.0",
"@kusionstack/kusion-api-client-sdk": "^1.1.3",
"yaml": "^2.6.1"
},
"devDependencies": {
"@backstage/cli": "^0.29.0",
"@backstage/plugin-scaffolder-node-test-utils": "^0.1.15"
},
"files": [
"dist"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { TemplateExample } from '@backstage/plugin-scaffolder-node';
import yaml from 'yaml';

export const examples: TemplateExample[] = [
{
description: 'Create a backend in Kusion',
example: yaml.stringify({
steps: [
{
id: 'create-backend',
action: 'kusion:backend:create',
name: 'Create backend',
input: {
name: 'my-backend',
description: 'This is my backend',
backendConfig: {
"configs": {
"region": "string",
"endpoint": "string",
"accessKeyID": "string",
"accessKeySecret": "string",
"bucket": "string",
"prefix": "string",
},
"type": "s3"
},
},
},
],
}),
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
import { Config } from '@backstage/config';
import { configKusionApi } from '../../api';
import {
BackendService,
CreateBackendData,
} from '@kusionstack/kusion-api-client-sdk';
import { examples } from './createBackend.example';

/**
* Creates a `kusion:backend:create` Scaffolder action.
*
* @public
*/
export function createCreateBackendAction(options: { config: Config }) {
const { config } = options;
return createTemplateAction<{
name: string;
description: string;
backendConfig: {
type: string;
configs: Record<string, string>;
};
}>({
id: 'kusion:backend:create',
examples,
schema: {
input: {
type: 'object',
required: ['name', 'backendConfig'],
properties: {
name: {
title: 'Backend Name',
type: 'string',
},
description: {
title: 'Backend Description',
type: 'string',
},
backendConfig: {
title: 'Backend Configuration',
type: 'object',
required: ['type', 'configs'],
properties: {
type: {
title: 'Backend Type',
type: 'string',
},
configs: {
title: 'Backend Configs',
type: 'object',
additionalProperties: {
type: 'string',
},
},
},
},
},
},
output: {
type: 'object',
properties: {
success: {
title: 'Success',
type: 'boolean',
},
message: {
title: 'Message',
type: 'string',
},
data: {
title: 'Data',
type: 'object',
},
},
},
},
async handler(ctx) {
const { name, description, backendConfig } = ctx.input;
configKusionApi({ configApi: config });
const requestBody: CreateBackendData = {
body: {
name: name,
description: description,
backendConfig: backendConfig,
},
};

ctx.logger.info(
'Creating backend with the following request body: ',
requestBody,
);

const response = await BackendService.createBackend(requestBody);

if (!response.data?.success) {
ctx.logger.error(`
Unable to create backend, ${response.data?.message}`);
ctx.output('success', response.data?.success);
ctx.output('message', response.data?.message);
ctx.output('data', JSON.stringify(response.data?.data));
throw new Error(`Unable to create backend, ${response.data?.message}`);
}

ctx.logger.info('Backend created successfully');
ctx.output('success', response.data?.success);
ctx.output('message', response.data?.message);
ctx.output('data', JSON.stringify(response.data?.data));
},
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { TemplateExample } from '@backstage/plugin-scaffolder-node';
import yaml from 'yaml';

export const examples: TemplateExample[] = [
{
description: 'Delete a backend in Kusion',
example: yaml.stringify({
steps: [
{
id: 'delete-backend',
action: 'kusion:backend:delete',
name: 'Delete backend',
input: {
id: '1',
},
},
],
}),
},
];
Loading
Loading