Implement replication service interface

This commit is contained in:
Steven Zou 2017-05-10 15:41:20 +08:00
parent 54df5ec053
commit e3929f0e95
6 changed files with 199 additions and 50 deletions

View File

@ -5,11 +5,13 @@ import { SharedModule } from '../shared/shared.module';
import { SERVICE_CONFIG, IServiceConfig } from '../service.config';
describe('AccessLogService', () => {
beforeEach(() => {
const mockConfig:IServiceConfig = {
logBaseEndpoint:"/api/logs/testing"
};
const mockConfig: IServiceConfig = {
logBaseEndpoint: "/api/logs/testing"
};
let config: IServiceConfig;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
SharedModule
@ -19,14 +21,22 @@ describe('AccessLogService', () => {
{
provide: AccessLogService,
useClass: AccessLogDefaultService
},{
}, {
provide: SERVICE_CONFIG,
useValue: mockConfig
}]
});
config = TestBed.get(SERVICE_CONFIG);
});
it('should be initialized', inject([AccessLogDefaultService], (service: AccessLogService) => {
expect(service).toBeTruthy();
}));
it('should inject the right config', () => {
expect(config).toBeTruthy();
expect(config.logBaseEndpoint).toEqual("/api/logs/testing");
});
});

View File

@ -5,7 +5,7 @@
* @interface Base
*/
export interface Base {
id?: string;
id?: string | number;
name?: string;
creation_time?: Date;
update_time?: Date;
@ -80,7 +80,18 @@ export interface Endpoint extends Base { }
* @export
* @interface ReplicationRule
*/
export interface ReplicationRule { }
export interface ReplicationRule extends Base {
project_id: number;
project_name: string;
target_id: number;
target_name: string;
enabled: number;
description?: string;
cron_str?: string;
start_time?: Date;
error_job_count?: number;
deleted: number;
}
/**
* Interface for replication job.
@ -88,7 +99,13 @@ export interface ReplicationRule { }
* @export
* @interface ReplicationJob
*/
export interface ReplicationJob { }
export interface ReplicationJob extends Base {
status: string;
repository: string;
policy_id: number;
operation: string;
tags: string;
}
/**
* Interface for access log.

View File

@ -1,20 +1,43 @@
import { TestBed, inject } from '@angular/core/testing';
import { ReplicationService, ReplicationDefaultService } from './replication.service';
import { SharedModule } from '../shared/shared.module';
import { SERVICE_CONFIG, IServiceConfig } from '../service.config';
describe('ReplicationService', () => {
const mockConfig: IServiceConfig = {
replicationRuleEndpoint: "/api/policies/replication/testing",
replicationJobEndpoint: "/api/jobs/replication/testing"
};
let config: IServiceConfig;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
SharedModule
],
providers: [
ReplicationDefaultService,
{
provide: ReplicationService,
useClass: ReplicationDefaultService
}, {
provide: SERVICE_CONFIG,
useValue: mockConfig
}]
});
config = TestBed.get(SERVICE_CONFIG);
});
it('should be initialized', inject([ReplicationDefaultService], (service: ReplicationService) => {
expect(service).toBeTruthy();
}));
it('should inject the right config', () => {
expect(config).toBeTruthy();
expect(config.replicationRuleEndpoint).toEqual("/api/policies/replication/testing");
expect(config.replicationJobEndpoint).toEqual("/api/jobs/replication/testing");
});
});

View File

@ -1,8 +1,11 @@
import { Observable } from 'rxjs/Observable';
import { RequestQueryParams } from './RequestQueryParams';
import { ReplicationJob, ReplicationRule } from './interface';
import { Injectable } from "@angular/core";
import { Injectable, Inject } from "@angular/core";
import 'rxjs/add/observable/of';
import { Http, RequestOptions } from '@angular/http';
import { SERVICE_CONFIG, IServiceConfig } from '../service.config';
import { buildHttpRequestOptions, HTTP_JSON_OPTIONS } from '../utils';
/**
* Define the service methods to handle the replication (rule and job) related things.
@ -22,77 +25,77 @@ export abstract class ReplicationService {
* @param {(number | string)} [projectId]
* @param {string} [ruleName]
* @param {RequestQueryParams} [queryParams]
* @returns {(Observable<ReplicationRule[]> | ReplicationRule[])}
* @returns {(Observable<ReplicationRule[]> | Promise<ReplicationRule[]> | ReplicationRule[])}
*
* @memberOf ReplicationService
*/
abstract getReplicationRules(projectId?: number | string, ruleName?: string, queryParams?: RequestQueryParams): Observable<ReplicationRule[]> | ReplicationRule[];
abstract getReplicationRules(projectId?: number | string, ruleName?: string, queryParams?: RequestQueryParams): Observable<ReplicationRule[]> | Promise<ReplicationRule[]> | ReplicationRule[];
/**
* Get the specified replication rule.
*
* @abstract
* @param {(number | string)} ruleId
* @returns {(Observable<ReplicationRule> | ReplicationRule)}
* @returns {(Observable<ReplicationRule> | Promise<ReplicationRule> | ReplicationRule)}
*
* @memberOf ReplicationService
*/
abstract getReplicationRule(ruleId: number | string): Observable<ReplicationRule> | ReplicationRule;
abstract getReplicationRule(ruleId: number | string): Observable<ReplicationRule> | Promise<ReplicationRule> | ReplicationRule;
/**
* Create new replication rule.
*
* @abstract
* @param {ReplicationRule} replicationRule
* @returns {(Observable<any> | any)}
* @returns {(Observable<any> | Promise<any> | any)}
*
* @memberOf ReplicationService
*/
abstract createReplicationRule(replicationRule: ReplicationRule): Observable<any> | any;
abstract createReplicationRule(replicationRule: ReplicationRule): Observable<any> | Promise<any> | any;
/**
* Update the specified replication rule.
*
* @abstract
* @param {ReplicationRule} replicationRule
* @returns {(Observable<any> | any)}
* @returns {(Observable<any> | Promise<any> | any)}
*
* @memberOf ReplicationService
*/
abstract updateReplicationRule(replicationRule: ReplicationRule): Observable<any> | any;
abstract updateReplicationRule(replicationRule: ReplicationRule): Observable<any> | Promise<any> | any;
/**
* Delete the specified replication rule.
*
* @abstract
* @param {(number | string)} ruleId
* @returns {(Observable<any> | any)}
* @returns {(Observable<any> | Promise<any> | any)}
*
* @memberOf ReplicationService
*/
abstract deleteReplicationRule(ruleId: number | string): Observable<any> | any;
abstract deleteReplicationRule(ruleId: number | string): Observable<any> | Promise<any> | any;
/**
* Enable the specified replication rule.
*
* @abstract
* @param {(number | string)} ruleId
* @returns {(Observable<any> | any)}
* @returns {(Observable<any> | Promise<any> | any)}
*
* @memberOf ReplicationService
*/
abstract enableReplicationRule(ruleId: number | string): Observable<any> | any;
abstract enableReplicationRule(ruleId: number | string): Observable<any> | Promise<any> | any;
/**
* Disable the specified replication rule.
*
* @abstract
* @param {(number | string)} ruleId
* @returns {(Observable<any> | any)}
* @returns {(Observable<any> | Promise<any> | any)}
*
* @memberOf ReplicationService
*/
abstract disableReplicationRule(ruleId: number | string): Observable<any> | any;
abstract disableReplicationRule(ruleId: number | string): Observable<any> | Promise<any> | any;
/**
* Get the jobs for the specified replication rule.
@ -106,11 +109,11 @@ export abstract class ReplicationService {
* @abstract
* @param {(number | string)} ruleId
* @param {RequestQueryParams} [queryParams]
* @returns {(Observable<ReplicationJob> | ReplicationJob)}
* @returns {(Observable<ReplicationJob> | Promise<ReplicationJob[]> | ReplicationJob)}
*
* @memberOf ReplicationService
*/
abstract getJobs(ruleId: number | string, queryParams?: RequestQueryParams): Observable<ReplicationJob[]> | ReplicationJob[];
abstract getJobs(ruleId: number | string, queryParams?: RequestQueryParams): Observable<ReplicationJob[]> | Promise<ReplicationJob[]> | ReplicationJob[];
}
@ -123,35 +126,121 @@ export abstract class ReplicationService {
*/
@Injectable()
export class ReplicationDefaultService extends ReplicationService {
public getReplicationRules(projectId?: number | string, ruleName?: string, queryParams?: RequestQueryParams): Observable<ReplicationRule[]> | ReplicationRule[] {
return Observable.of([]);
_ruleBaseUrl: string;
_jobBaseUrl: string;
constructor(
private http: Http,
@Inject(SERVICE_CONFIG) private config: IServiceConfig
) {
super();
this._ruleBaseUrl = this.config.replicationRuleEndpoint ?
this.config.replicationRuleEndpoint : '/api/policies/replication';
this._jobBaseUrl = this.config.replicationJobEndpoint ?
this.config.replicationJobEndpoint : '/api/jobs/replication';
}
public getReplicationRule(ruleId: number | string): Observable<ReplicationRule> | ReplicationRule {
return Observable.of({});
//Private methods
//Check if the rule object is valid
_isValidRule(rule: ReplicationRule): boolean {
return rule !== undefined && rule != null && rule.name !== undefined && rule.name.trim() !== '' && rule.target_id !== 0;
}
public createReplicationRule(replicationRule: ReplicationRule): Observable<any> | any {
return Observable.of({});
public getReplicationRules(projectId?: number | string, ruleName?: string, queryParams?: RequestQueryParams): Observable<ReplicationRule[]> | Promise<ReplicationRule[]> | ReplicationRule[] {
if (!queryParams) {
queryParams = new RequestQueryParams();
}
if (projectId) {
queryParams.set('project_id', '' + projectId);
}
if (ruleName) {
queryParams.set('name', ruleName);
}
return this.http.get(this._ruleBaseUrl, buildHttpRequestOptions(queryParams)).toPromise()
.then(response => response.json() as ReplicationRule[])
.catch(error => Promise.reject(error))
}
public updateReplicationRule(replicationRule: ReplicationRule): Observable<any> | any {
return Observable.of({});
public getReplicationRule(ruleId: number | string): Observable<ReplicationRule> | Promise<ReplicationRule> | ReplicationRule {
if (!ruleId) {
return Promise.reject("Bad argument");
}
let url: string = `${this._ruleBaseUrl}/${ruleId}`;
return this.http.get(url, HTTP_JSON_OPTIONS).toPromise()
.then(response => response.json() as ReplicationRule)
.catch(error => Promise.reject(error));
}
public deleteReplicationRule(ruleId: number | string): Observable<any> | any {
return Observable.of({});
public createReplicationRule(replicationRule: ReplicationRule): Observable<any> | Promise<any> | any {
if (!this._isValidRule(replicationRule)) {
return Promise.reject('Bad argument');
}
return this.http.post(this._ruleBaseUrl, JSON.stringify(replicationRule), HTTP_JSON_OPTIONS).toPromise()
.then(response => response)
.catch(error => Promise.reject(error));
}
public enableReplicationRule(ruleId: number | string): Observable<any> | any {
return Observable.of({});
public updateReplicationRule(replicationRule: ReplicationRule): Observable<any> | Promise<any> | any {
if (!this._isValidRule(replicationRule) || !replicationRule.id) {
return Promise.reject('Bad argument');
}
let url: string = `${this._ruleBaseUrl}/${replicationRule.id}`;
return this.http.put(url, JSON.stringify(replicationRule), HTTP_JSON_OPTIONS).toPromise()
.then(response => response)
.catch(error => Promise.reject(error));
}
public disableReplicationRule(ruleId: number | string): Observable<any> | any {
return Observable.of({});
public deleteReplicationRule(ruleId: number | string): Observable<any> | Promise<any> | any {
if (!ruleId || ruleId <= 0) {
return Promise.reject('Bad argument');
}
let url: string = `${this._ruleBaseUrl}/${ruleId}`;
return this.http.delete(url, HTTP_JSON_OPTIONS).toPromise()
.then(response => response)
.catch(error => Promise.reject(error));
}
public getJobs(ruleId: number | string, queryParams?: RequestQueryParams): Observable<ReplicationJob[]> | ReplicationJob[] {
return Observable.of([]);
public enableReplicationRule(ruleId: number | string): Observable<any> | Promise<any> | any {
if (!ruleId || ruleId <= 0) {
return Promise.reject('Bad argument');
}
let url: string = `${this._ruleBaseUrl}/${ruleId}/enablement`;
return this.http.put(url, { enabled: 1 }, HTTP_JSON_OPTIONS).toPromise()
.then(response => response)
.catch(error => Promise.reject(error));
}
public disableReplicationRule(ruleId: number | string): Observable<any> | Promise<any> | any {
if (!ruleId || ruleId <= 0) {
return Promise.reject('Bad argument');
}
let url: string = `${this._ruleBaseUrl}/${ruleId}/enablement`;
return this.http.put(url, { enabled: 0 }, HTTP_JSON_OPTIONS).toPromise()
.then(response => response)
.catch(error => Promise.reject(error));
}
public getJobs(ruleId: number | string, queryParams?: RequestQueryParams): Observable<ReplicationJob[]> | Promise<ReplicationJob[]> | ReplicationJob[] {
if (!ruleId || ruleId <= 0) {
return Promise.reject('Bad argument');
}
if (!queryParams) {
queryParams = new RequestQueryParams();
}
queryParams.set('policy_id', '' + ruleId);
return this.http.get(this._jobBaseUrl, buildHttpRequestOptions(queryParams)).toPromise()
.then(response => response.json() as ReplicationJob[])
.catch(error => Promise.reject(error));
}
}

View File

@ -5,11 +5,13 @@ import { SharedModule } from '../shared/shared.module';
import { SERVICE_CONFIG, IServiceConfig } from '../service.config';
describe('RepositoryService', () => {
beforeEach(() => {
const mockConfig: IServiceConfig = {
repositoryBaseEndpoint: "/api/repositories/testing"
};
const mockConfig: IServiceConfig = {
repositoryBaseEndpoint: "/api/repositories/testing"
};
let config: IServiceConfig;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
SharedModule
@ -24,9 +26,17 @@ describe('RepositoryService', () => {
useValue: mockConfig
}]
});
config = TestBed.get(SERVICE_CONFIG);
});
it('should be initialized', inject([RepositoryDefaultService], (service: RepositoryService) => {
expect(service).toBeTruthy();
}));
it('should inject the right config', () => {
expect(config).toBeTruthy();
expect(config.repositoryBaseEndpoint).toEqual("/api/repositories/testing");
});
});

View File

@ -40,11 +40,11 @@ describe('TagService', () => {
}
}];
beforeEach(() => {
const mockConfig: IServiceConfig = {
repositoryBaseEndpoint: "/api/repositories/testing"
};
const mockConfig: IServiceConfig = {
repositoryBaseEndpoint: "/api/repositories/testing"
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
SharedModule