diff --git a/packages/catalog-model/src/EntityPolicies.ts b/packages/catalog-model/src/EntityPolicies.ts index fa25334d5f..ca189c042e 100644 --- a/packages/catalog-model/src/EntityPolicies.ts +++ b/packages/catalog-model/src/EntityPolicies.ts @@ -23,6 +23,7 @@ import { } from './entity'; import { ComponentV1beta1Policy } from './kinds'; import { EntityPolicy } from './types'; +import { DefaultNamespaceEntityPolicy } from './entity/policies/DefaultNamespaceEntityPolicy'; // Helper that requires that all of a set of policies can be successfully // applied @@ -62,6 +63,7 @@ export class EntityPolicies implements EntityPolicy { return EntityPolicies.allOf([ EntityPolicies.allOf([ new SchemaValidEntityPolicy(), + new DefaultNamespaceEntityPolicy(), new NoForeignRootFieldsEntityPolicy(), new FieldFormatEntityPolicy(), new ReservedFieldsEntityPolicy(), diff --git a/packages/catalog-model/src/entity/policies/DefaultNamespaceEntityPolicy.test.ts b/packages/catalog-model/src/entity/policies/DefaultNamespaceEntityPolicy.test.ts new file mode 100644 index 0000000000..68658e5296 --- /dev/null +++ b/packages/catalog-model/src/entity/policies/DefaultNamespaceEntityPolicy.test.ts @@ -0,0 +1,61 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 yaml from 'yaml'; +import { DefaultNamespaceEntityPolicy } from './DefaultNamespaceEntityPolicy'; + +describe('DefaultNamespaceEntityPolicy', () => { + let withNamespace: any; + let withoutNamespace: any; + let policy: DefaultNamespaceEntityPolicy; + + beforeEach(() => { + withoutNamespace = yaml.parse(` + apiVersion: backstage.io/v1beta1 + kind: Component + metadata: + name: my-component-yay + `); + withNamespace = yaml.parse(` + apiVersion: backstage.io/v1beta1 + kind: Component + metadata: + name: my-component-yay + namespace: my-home + `); + policy = new DefaultNamespaceEntityPolicy(); + }); + + it('leaves untouched if it already has a namespace', async () => { + const result = policy.enforce(withNamespace); + await expect(result).resolves.toBe(withNamespace); + await expect(result).resolves.toEqual( + expect.objectContaining({ + metadata: { name: 'my-component-yay', namespace: 'my-home' }, + }), + ); + }); + + it('adds namespace in different object if it did not have one', async () => { + const result = policy.enforce(withoutNamespace); + await expect(result).resolves.not.toBe(withoutNamespace); + await expect(result).resolves.toEqual( + expect.objectContaining({ + metadata: { name: 'my-component-yay', namespace: 'default' }, + }), + ); + }); +}); diff --git a/packages/catalog-model/src/entity/policies/DefaultNamespaceEntityPolicy.ts b/packages/catalog-model/src/entity/policies/DefaultNamespaceEntityPolicy.ts new file mode 100644 index 0000000000..e5aba745c7 --- /dev/null +++ b/packages/catalog-model/src/entity/policies/DefaultNamespaceEntityPolicy.ts @@ -0,0 +1,38 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 lodash from 'lodash'; +import { EntityPolicy } from '../../types'; +import { Entity } from '../Entity'; + +/** + * Sets a default namespace if none was set. + */ +export class DefaultNamespaceEntityPolicy implements EntityPolicy { + private readonly namespace: string; + + constructor(namespace: string = 'default') { + this.namespace = namespace; + } + + async enforce(entity: Entity): Promise { + if (entity.metadata.namespace) { + return entity; + } + + return lodash.merge({ metadata: { namespace: this.namespace } }, entity); + } +}