Commit 6c13e2b1 authored by Meina Zhou's avatar Meina Zhou
Browse files

add replication rule

Signed-off-by: default avatarMeina Zhou <meinaz@vmware.com>
parent 3876abf6
Showing with 192 additions and 357 deletions
+192 -357
......@@ -149,9 +149,16 @@ h4 {
padding-left: 170px !important;
}
.form-group>label:first-child {
font-size: 14px;
width: 6.5rem;
.form-group {
>label:first-child{
font-size: 14px;
width: 8rem;
}
.radio {
label {
margin-right:10px;
}
}
}
.form-select {
......
......@@ -45,6 +45,7 @@ const ONE_DAY_SECONDS: number = 24 * ONE_HOUR_SECONDS;
})
export class CreateEditRuleComponent implements OnInit, OnDestroy {
_localTime: Date = new Date();
sourceList: Endpoint[] = [];
targetList: Endpoint[] = [];
projectList: Project[] = [];
selectedProjectList: Project[] = [];
......@@ -54,22 +55,13 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
isImmediate = false;
noProjectInfo = "";
noEndpointInfo = "";
isPushMode = true;
noSelectedProject = true;
noSelectedEndpoint = true;
filterCount = 0;
alertClosed = false;
triggerNames: string[] = ["Manual", "Immediate", "Scheduled"];
scheduleNames: string[] = ["Daily", "Weekly"];
weekly: string[] = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
filterSelect: string[] = ["repository", "tag", "label"];
filterSelect: string[] = ["type", "repository", "tag", "label"];
ruleNameTooltip = "REPLICATION.NAME_TOOLTIP";
headerTitle = "REPLICATION.ADD_POLICY";
......@@ -87,10 +79,16 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
deletedLabelCount = 0;
deletedLabelInfo: string;
namespaceList: [any] = [{
id: 1,
name: "namespace1"
}];
confirmSub: Subscription;
ruleForm: FormGroup;
formArrayLabel: FormArray;
copyUpdateForm: ReplicationRule;
cronString: string;
@Input() projectId: number;
@Input() projectName: string;
......@@ -142,23 +140,16 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
ngOnInit(): void {
if (this.withAdmiral) {
this.filterSelect = ["repository", "tag"];
this.filterSelect = ["type", "repository", "tag"];
}
toPromise<Endpoint[]>(this.endpointService.getEndpoints())
.then(targets => {
this.targetList = targets || [];
this.sourceList = targets || [];
})
.catch((error: any) => this.errorHandler.error(error));
if (!this.projectId) {
toPromise<Project[]>(this.proService.listProjects("", undefined))
.then(targets => {
this.projectList = targets || [];
})
.catch(error => this.errorHandler.error(error));
}
this.nameChecker
.pipe(debounceTime(300))
.pipe(distinctUntilChanged())
......@@ -186,39 +177,9 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
}
}
});
}
this.proNameChecker
.pipe(debounceTime(500))
.pipe(distinctUntilChanged())
.subscribe((resp: string) => {
let name = this.ruleForm.controls["projects"].value[0].name;
this.noProjectInfo = "";
this.selectedProjectList = [];
toPromise<Project[]>(this.proService.listProjects(name, undefined))
.then((res: any) => {
if (res) {
this.selectedProjectList = res.slice(0, 10);
// if input value exit in project list
let pro = res.find((data: any) => data.name === name);
if (!pro) {
this.noProjectInfo = "REPLICATION.NO_PROJECT_INFO";
this.noSelectedProject = true;
} else {
this.noProjectInfo = "";
this.noSelectedProject = false;
this.setProject([pro]);
}
} else {
this.noProjectInfo = "REPLICATION.NO_PROJECT_INFO";
this.noSelectedProject = true;
}
})
.catch((error: any) => {
this.errorHandler.error(error);
this.noProjectInfo = "REPLICATION.NO_PROJECT_INFO";
this.noSelectedProject = true;
});
});
sourceChange(event): void {
}
ngOnDestroy(): void {
......@@ -244,18 +205,17 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
createForm() {
this.formArrayLabel = this.fb.array([]);
this.ruleForm = this.fb.group({
name: ["", Validators.required],
description: "",
projects: this.fb.array([]),
sources: this.fb.array([]),
sourceNamespace: "",
targets: this.fb.array([]),
destinationNamespace: "",
trigger: this.fb.group({
kind: this.triggerNames[0],
schedule_param: this.fb.group({
type: this.scheduleNames[0],
weekday: 1,
offtime: "08:00"
cron: ""
})
}),
filters: this.fb.array([]),
......@@ -271,15 +231,12 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
trigger: {
kind: this.triggerNames[0],
schedule_param: {
type: this.scheduleNames[0],
weekday: 1,
offtime: "08:00"
cron: ""
}
},
replicate_existing_image_now: true,
replicate_deletion: false
});
this.setProject([this.emptyProject]);
this.setTarget([this.emptyEndpoint]);
this.setFilter([]);
......@@ -287,7 +244,6 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
}
updateForm(rule: ReplicationRule): void {
rule.trigger = this.updateTrigger(rule.trigger);
this.ruleForm.reset({
name: rule.name,
description: rule.description,
......@@ -295,7 +251,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
replicate_existing_image_now: rule.replicate_existing_image_now,
replicate_deletion: rule.replicate_deletion
});
this.setProject(rule.projects);
this.noSelectedProject = false;
this.setTarget(rule.targets);
this.noSelectedEndpoint = false;
......@@ -319,7 +275,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
let delLabel = '';
filterLabels.forEach((data: any) => {
if (data.kind === this.filterSelect[2]) {
if (data.kind === this.filterSelect[3]) {
if (!data.value.deleted) {
count++;
this.filterLabelInfo.push(data.value);
......@@ -341,7 +297,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
if (delLabel || count) {
let len = filterLabels.length;
for (let i = 0; i < len; i++) {
let lab = filterLabels.find(data => data.kind === this.filterSelect[2]);
let lab = filterLabels.find(data => data.kind === this.filterSelect[3]);
if (lab) { filterLabels.splice(filterLabels.indexOf(lab), 1); }
}
filterLabels.push({ kind: 'label', value: count + ' labels' });
......@@ -368,9 +324,19 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
this.ruleForm.setControl("filters", filterFormArray);
}
get sources(): FormArray {
return this.ruleForm.get("sources") as FormArray;
}
setSources(sources: Endpoint[]) {
const sourceFGs = sources.map(source => this.fb.group(source));
const sourceFormArray = this.fb.array(sourceFGs);
this.ruleForm.setControl("sources", sourceFormArray);
}
get targets(): FormArray {
return this.ruleForm.get("targets") as FormArray;
}
setTarget(targets: Endpoint[]) {
const targetFGs = targets.map(target => this.fb.group(target));
const targetFormArray = this.fb.array(targetFGs);
......@@ -402,7 +368,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
}
// if before select, $event is label
if (!this.withAdmiral && name === this.filterSelect[2] && data.name === value) {
if (!this.withAdmiral && name === this.filterSelect[3] && data.name === value) {
this.labelInputVal = controlArray.controls[index].get('value').value.split(' ')[0];
data.isOpen = false;
controlArray.controls[index].get('value').setValue('');
......@@ -425,7 +391,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
// when input value is label, then open label panel
openLabelList(labelTag: string, indexId: number, $event: any) {
if (!this.withAdmiral && labelTag === this.filterSelect[2]) {
if (!this.withAdmiral && labelTag === this.filterSelect[3]) {
this.filterListData.forEach((data, index) => {
if (index === indexId) {
data.isOpen = true;
......@@ -517,7 +483,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
if (this.filterCount >= this.filterSelect.length) {
this.isFilterHide = true;
}
if (controlArray.controls[this.filterCount - 1].get('kind').value === this.filterSelect[2] && this.labelInputVal) {
if (controlArray.controls[this.filterCount - 1].get('kind').value === this.filterSelect[3] && this.labelInputVal) {
controlArray.controls[this.filterCount - 1].get('value').setValue(this.labelInputVal + ' labels');
}
......@@ -569,23 +535,6 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
// Replication Schedule select value exchange
selectSchedule($event: any): void {
if ($event && $event.target && $event.target["value"]) {
switch ($event.target["value"]) {
case this.scheduleNames[1]:
this.weeklySchedule = true;
this.ruleForm.patchValue({
trigger: {
schedule_param: {
weekday: 1
}
}
});
break;
case this.scheduleNames[0]:
this.weeklySchedule = false;
break;
}
}
}
checkRuleName(): void {
......@@ -638,54 +587,6 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
});
}
updateTrigger(trigger: any) {
if (trigger["schedule_param"]) {
this.isScheduleOpt = true;
this.isImmediate = false;
trigger["schedule_param"]["offtime"] = this.getOfftime(
trigger["schedule_param"]["offtime"]
);
if (trigger["schedule_param"]["weekday"]) {
this.weeklySchedule = true;
} else {
// set default
trigger["schedule_param"]["weekday"] = 1;
}
} else {
if (trigger["kind"] === this.triggerNames[0]) {
this.isImmediate = false;
}
if (trigger["kind"] === this.triggerNames[1]) {
this.isImmediate = true;
}
trigger["schedule_param"] = {
type: this.scheduleNames[0],
weekday: this.weekly[0],
offtime: "08:00"
};
}
return trigger;
}
setTriggerVaule(trigger: any) {
if (!this.isScheduleOpt) {
delete trigger["schedule_param"];
return trigger;
} else {
if (!this.weeklySchedule) {
delete trigger["schedule_param"]["weekday"];
} else {
trigger["schedule_param"]["weekday"] = +trigger["schedule_param"][
"weekday"
];
}
trigger["schedule_param"]["offtime"] = this.setOfftime(
trigger["schedule_param"]["offtime"]
);
return trigger;
}
}
setFilterLabelVal(filters: any[]) {
let labels: any = filters.find(data => data.kind === this.filterSelect[2]);
......@@ -707,7 +608,7 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
// add new Replication rule
this.inProgress = true;
let copyRuleForm: ReplicationRule = this.ruleForm.value;
copyRuleForm.trigger = this.setTriggerVaule(copyRuleForm.trigger);
copyRuleForm.trigger = null;
// rewrite key name of label when filer contain labels.
if (copyRuleForm.filters) { this.setFilterLabelVal(copyRuleForm.filters); }
......@@ -791,12 +692,6 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
});
} else {
this.headerTitle = "REPLICATION.ADD_POLICY";
if (this.projectId) {
this.setProject([
{ project_id: this.projectId, name: this.projectName }
]);
this.noSelectedProject = false;
}
this.copyUpdateForm = clone(this.ruleForm.value);
}
}
......@@ -824,71 +719,6 @@ export class CreateEditRuleComponent implements OnInit, OnDestroy {
this.goToRegistry.emit();
}
// UTC time
public getOfftime(daily_time: any): string {
let timeOffset = 0; // seconds
if (daily_time && typeof daily_time === "number") {
timeOffset = +daily_time;
}
// Convert to current time
let timezoneOffset: number = this._localTime.getTimezoneOffset();
// Local time
timeOffset = timeOffset - timezoneOffset * 60;
if (timeOffset < 0) {
timeOffset = timeOffset + ONE_DAY_SECONDS;
}
if (timeOffset >= ONE_DAY_SECONDS) {
timeOffset -= ONE_DAY_SECONDS;
}
// To time string
let hours: number = Math.floor(timeOffset / ONE_HOUR_SECONDS);
let minutes: number = Math.floor(
(timeOffset - hours * ONE_HOUR_SECONDS) / 60
);
let timeStr: string = "" + hours;
if (hours < 10) {
timeStr = "0" + timeStr;
}
if (minutes < 10) {
timeStr += ":0";
} else {
timeStr += ":";
}
timeStr += minutes;
return timeStr;
}
public setOfftime(v: string) {
if (!v || v === "") {
return;
}
let values: string[] = v.split(":");
if (!values || values.length !== 2) {
return;
}
let hours: number = +values[0];
let minutes: number = +values[1];
// Convert to UTC time
let timezoneOffset: number = this._localTime.getTimezoneOffset();
let utcTimes: number = hours * ONE_HOUR_SECONDS + minutes * 60;
utcTimes += timezoneOffset * 60;
if (utcTimes < 0) {
utcTimes += ONE_DAY_SECONDS;
}
if (utcTimes >= ONE_DAY_SECONDS) {
utcTimes -= ONE_DAY_SECONDS;
}
return utcTimes;
}
hasChanges(): boolean {
let formValue = clone(this.ruleForm.value);
let initValue = clone(this.copyUpdateForm);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment