Skip to content

Commit

Permalink
add namespace creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ikethecoder committed Jul 4, 2023
1 parent af0e5c5 commit add25e2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
40 changes: 40 additions & 0 deletions src/controllers/v2/NamespaceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Tags,
Delete,
Query,
Post,
} from 'tsoa';
import { ValidateError, FieldErrors } from 'tsoa';
import { KeystoneService } from '../ioc/keystoneInjector';
Expand Down Expand Up @@ -136,6 +137,37 @@ export class NamespaceController extends Controller {
return result.data.namespace;
}

/**
* Create a namespace
*
* @summary Create Namespace
* @param ns
* @param request
* @returns
*/
@Post()
@OperationId('create-namespace')
@Security('jwt', [])
public async create(
@Query() name: String,
@Request() request: any
): Promise<String> {
const result = await this.keystone.executeGraphQL({
context: this.keystone.createContext(request),
query: createNS,
variables: { name },
});
logger.debug('Result %j', result);
if (result.errors) {
const errors: FieldErrors = {};
result.errors.forEach((err: any, ind: number) => {
errors[`d${ind}`] = { message: err.message };
});
throw new ValidateError(errors, 'Unable to create namespace');
}
return result.data.createNamespace.name;
}

/**
* Delete the namespace
* > `Required Scope:` Namespace.Manage
Expand Down Expand Up @@ -234,3 +266,11 @@ const deleteNS = gql`
forceDeleteNamespace(namespace: $ns, force: $force)
}
`;

const createNS = gql`
mutation CreateNamespace($name: String) {
createNamespace(name: $name) {
name
}
}
`;
12 changes: 7 additions & 5 deletions src/lists/extensions/Namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import { GWAService } from '../../services/gwaapi';
import {
camelCaseAttributes,
regExprValidation,
transformSingleValueAttributes,
} from '../../services/utils';
import getSubjectToken from '../../auth/auth-token';
Expand All @@ -54,6 +55,7 @@ import {
getResource,
transformOrgAndOrgUnit,
} from '../../services/keycloak/namespace-details';
import { newNamespaceID } from '@/services/identifiers';

const logger = Logger('ext.Namespace');

Expand Down Expand Up @@ -422,7 +424,7 @@ module.exports = {
},
},
{
schema: 'createNamespace(namespace: String!): Namespace',
schema: 'createNamespace(namespace: String): Namespace',
resolver: async (
item: any,
args: any,
Expand All @@ -431,10 +433,10 @@ module.exports = {
{ query, access }: any
) => {
const namespaceValidationRule = '^[a-z][a-z0-9-]{4,14}$';
const re = new RegExp(namespaceValidationRule);
assert.strictEqual(
re.test(args.namespace),
true,

regExprValidation(
namespaceValidationRule,
args.namespace ? args.namespace : newNamespaceID(),
'Namespace name must be between 5 and 15 alpha-numeric lowercase characters and begin with an alphabet.'
);

Expand Down
10 changes: 7 additions & 3 deletions src/services/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ export function isEnvironmentID(id: string): boolean {
}

export function newProductID(): string {
return uuidv4().replace(/-/g, '').toUpperCase().substr(0, 12);
return uuidv4().replace(/-/g, '').toUpperCase().substring(0, 12);
}
export function newApplicationID(): string {
return uuidv4().replace(/-/g, '').toUpperCase().substr(0, 11);
return uuidv4().replace(/-/g, '').toUpperCase().substring(0, 11);
}
export function newEnvironmentID(): string {
return uuidv4().replace(/-/g, '').toUpperCase().substr(0, 8);
return uuidv4().replace(/-/g, '').toUpperCase().substring(0, 8);
}

export function newNamespaceID(): string {
return 'gw-' + uuidv4().replace(/-/g, '').toLowerCase().substring(0, 5);
}
1 change: 0 additions & 1 deletion src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export function regExprValidation(
value: string,
errorMessage: string
) {
const namespaceValidationRule = '^[a-z][a-z0-9-]{4,14}$';
const re = new RegExp(rule);
assert.strictEqual(re.test(value), true, errorMessage);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/services/identifiers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fetch from 'node-fetch';
import { newNamespaceID } from '../../services/identifiers';

describe('Identifiers', function () {
it('it should be a valid namespace', async function () {
const result = newNamespaceID();
expect(result).toHaveLength(8);
expect(result.startsWith('gw-')).toBeTruthy();
console.log(result);
});
});

0 comments on commit add25e2

Please sign in to comment.