Merge pull request #1059 from spotify/freben/defaultttt
Add default namespace if none was given
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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' },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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<Entity> {
|
||||
if (entity.metadata.namespace) {
|
||||
return entity;
|
||||
}
|
||||
|
||||
return lodash.merge({ metadata: { namespace: this.namespace } }, entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user