package com.bocloud.ims.service.scheduler.scheduler;

import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.common.model.RequestContext;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.resource.CloudVendorService;
import com.bocloud.ims.interfaces.resource.SyncService;
import com.bocloud.service.core.CurrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * f5定时同步类
 *
 * @author liuyuanyuan
 */
@Component
public class F5SyncScheduler {
	@Autowired
	private CloudVendorService cloudVendorService;
	@Autowired
	private SyncService syncService;
	@Autowired
	private CurrentService service;

	@Scheduled(cron = "${f5.sync.schedule:0 0/5 * * * ?}")
	protected void sync() {
		if (service.isLeader()) {
			List<CloudVendor> vendors = this.cloudVendorService.listAll();
			String type;

			for (CloudVendor vendor : vendors) {
				type = vendor.getType();
				if (CloudProvider.F5.name().equals(type)) {
					RequestContext context = new RequestContext();
					context.setTarget(1L);
					syncService.sync(vendor.getId(), context, true);
				}
			}
		}
	}
}
