mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Make Appinstance registration include environment (#1014)
This commit is contained in:
		
							parent
							
								
									8ac3f6fc36
								
							
						
					
					
						commit
						20a4aeff97
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -52,3 +52,5 @@ package-lock.json
 | 
				
			|||||||
/website/yarn.lock
 | 
					/website/yarn.lock
 | 
				
			||||||
/website/translated_docs
 | 
					/website/translated_docs
 | 
				
			||||||
/website/i18n/*
 | 
					/website/i18n/*
 | 
				
			||||||
 | 
					.env
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -18,6 +18,7 @@ const COLUMNS = [
 | 
				
			|||||||
    'client_ip',
 | 
					    'client_ip',
 | 
				
			||||||
    'last_seen',
 | 
					    'last_seen',
 | 
				
			||||||
    'created_at',
 | 
					    'created_at',
 | 
				
			||||||
 | 
					    'environment',
 | 
				
			||||||
];
 | 
					];
 | 
				
			||||||
const TABLE = 'client_instances';
 | 
					const TABLE = 'client_instances';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -30,6 +31,7 @@ const mapRow = (row) => ({
 | 
				
			|||||||
    clientIp: row.client_ip,
 | 
					    clientIp: row.client_ip,
 | 
				
			||||||
    lastSeen: row.last_seen,
 | 
					    lastSeen: row.last_seen,
 | 
				
			||||||
    createdAt: row.created_at,
 | 
					    createdAt: row.created_at,
 | 
				
			||||||
 | 
					    environment: row.environment,
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const mapToDb = (client) => ({
 | 
					const mapToDb = (client) => ({
 | 
				
			||||||
@ -38,6 +40,7 @@ const mapToDb = (client) => ({
 | 
				
			|||||||
    sdk_version: client.sdkVersion || '',
 | 
					    sdk_version: client.sdkVersion || '',
 | 
				
			||||||
    client_ip: client.clientIp,
 | 
					    client_ip: client.clientIp,
 | 
				
			||||||
    last_seen: client.lastSeen || 'now()',
 | 
					    last_seen: client.lastSeen || 'now()',
 | 
				
			||||||
 | 
					    environment: client.environment || 'default',
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class ClientInstanceStore implements IClientInstanceStore {
 | 
					export default class ClientInstanceStore implements IClientInstanceStore {
 | 
				
			||||||
@ -79,7 +82,7 @@ export default class ClientInstanceStore implements IClientInstanceStore {
 | 
				
			|||||||
        const rows = instances.map(mapToDb);
 | 
					        const rows = instances.map(mapToDb);
 | 
				
			||||||
        await this.db(TABLE)
 | 
					        await this.db(TABLE)
 | 
				
			||||||
            .insert(rows)
 | 
					            .insert(rows)
 | 
				
			||||||
            .onConflict(['app_name', 'instance_id'])
 | 
					            .onConflict(['app_name', 'instance_id', 'environment'])
 | 
				
			||||||
            .merge();
 | 
					            .merge();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -126,7 +129,7 @@ export default class ClientInstanceStore implements IClientInstanceStore {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        await this.db(TABLE)
 | 
					        await this.db(TABLE)
 | 
				
			||||||
            .insert(mapToDb(details))
 | 
					            .insert(mapToDb(details))
 | 
				
			||||||
            .onConflict(['app_name', 'instance_id'])
 | 
					            .onConflict(['app_name', 'instance_id', 'environment'])
 | 
				
			||||||
            .merge();
 | 
					            .merge();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        stopTimer();
 | 
					        stopTimer();
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,13 @@
 | 
				
			|||||||
import { Request, Response } from 'express';
 | 
					import { Response } from 'express';
 | 
				
			||||||
import Controller from '../controller';
 | 
					import Controller from '../controller';
 | 
				
			||||||
import { IUnleashServices } from '../../types';
 | 
					import { IUnleashServices } from '../../types';
 | 
				
			||||||
import { IUnleashConfig } from '../../types/option';
 | 
					import { IUnleashConfig } from '../../types/option';
 | 
				
			||||||
import { Logger } from '../../logger';
 | 
					import { Logger } from '../../logger';
 | 
				
			||||||
import ClientMetricsService from '../../services/client-metrics';
 | 
					import ClientMetricsService from '../../services/client-metrics';
 | 
				
			||||||
 | 
					import { IAuthRequest, User } from '../../server-impl';
 | 
				
			||||||
 | 
					import { IClientApp } from '../../types/model';
 | 
				
			||||||
 | 
					import ApiUser from '../../types/api-user';
 | 
				
			||||||
 | 
					import { ALL } from '../../types/models/api-token';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class RegisterController extends Controller {
 | 
					export default class RegisterController extends Controller {
 | 
				
			||||||
    logger: Logger;
 | 
					    logger: Logger;
 | 
				
			||||||
@ -22,9 +26,20 @@ export default class RegisterController extends Controller {
 | 
				
			|||||||
        this.post('/', this.handleRegister);
 | 
					        this.post('/', this.handleRegister);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async handleRegister(req: Request, res: Response): Promise<void> {
 | 
					    private resolveEnvironment(user: User, data: IClientApp) {
 | 
				
			||||||
        const data = req.body;
 | 
					        if (user instanceof ApiUser) {
 | 
				
			||||||
        const clientIp = req.ip;
 | 
					            if (user.environment !== ALL) {
 | 
				
			||||||
 | 
					                return user.environment;
 | 
				
			||||||
 | 
					            } else if (user.environment === ALL && data.environment) {
 | 
				
			||||||
 | 
					                return data.environment;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return 'default';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async handleRegister(req: IAuthRequest, res: Response): Promise<void> {
 | 
				
			||||||
 | 
					        const { body: data, ip: clientIp, user } = req;
 | 
				
			||||||
 | 
					        data.environment = this.resolveEnvironment(user, data);
 | 
				
			||||||
        await this.metrics.registerClient(data, clientIp);
 | 
					        await this.metrics.registerClient(data, clientIp);
 | 
				
			||||||
        return res.status(202).end();
 | 
					        return res.status(202).end();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -13,4 +13,5 @@ export const clientRegisterSchema = joi
 | 
				
			|||||||
            .items(joi.string(), joi.any().strip()),
 | 
					            .items(joi.string(), joi.any().strip()),
 | 
				
			||||||
        started: joi.date().required(),
 | 
					        started: joi.date().required(),
 | 
				
			||||||
        interval: joi.number().required(),
 | 
					        interval: joi.number().required(),
 | 
				
			||||||
 | 
					        environment: joi.string().optional(),
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
				
			|||||||
@ -10,6 +10,7 @@ export interface INewClientInstance {
 | 
				
			|||||||
    sdkVersion?: string;
 | 
					    sdkVersion?: string;
 | 
				
			||||||
    clientIp?: string;
 | 
					    clientIp?: string;
 | 
				
			||||||
    lastSeen?: Date;
 | 
					    lastSeen?: Date;
 | 
				
			||||||
 | 
					    environment?: string;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
export interface IClientInstanceStore
 | 
					export interface IClientInstanceStore
 | 
				
			||||||
    extends Store<
 | 
					    extends Store<
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.up = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					    ALTER TABLE client_instances DROP CONSTRAINT client_instances_pkey;
 | 
				
			||||||
 | 
					    ALTER TABLE client_instances ADD COLUMN environment varchar(255) NOT NULL DEFAULT 'default';
 | 
				
			||||||
 | 
					    ALTER TABLE client_instances ADD CONSTRAINT client_instances_pkey PRIMARY KEY (app_name, environment, instance_id);
 | 
				
			||||||
 | 
					    CREATE INDEX client_instances_environment_idx ON client_instances(environment);
 | 
				
			||||||
 | 
					  `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.down = function (db, cb) {
 | 
				
			||||||
 | 
					    db.runSql(
 | 
				
			||||||
 | 
					        `
 | 
				
			||||||
 | 
					      DROP INDEX client_instances_environment_idx;
 | 
				
			||||||
 | 
					    ALTER TABLE client_instances DROP CONSTRAINT client_instances_pkey;
 | 
				
			||||||
 | 
					    ALTER TABLE client_instances ADD CONSTRAINT client_instances_pkey PRIMARY KEY (app_name, instance_id);
 | 
				
			||||||
 | 
					    ALTER TABLE client_instances DROP COLUMN environment;
 | 
				
			||||||
 | 
					  `,
 | 
				
			||||||
 | 
					        cb,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports._meta = {
 | 
				
			||||||
 | 
					    version: 1,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user