make connector config update generic

This commit is contained in:
itsikkes 2016-06-04 23:25:15 +03:00
parent 5113cc6400
commit 1acab50f51
1 changed files with 34 additions and 18 deletions

View File

@ -65,29 +65,45 @@ class Job(restful.Resource):
class Connector(restful.Resource): class Connector(restful.Resource):
def get(self, **kw): def get(self, **kw):
type = request.args.get('type') contype = request.args.get('type')
if type == 'VCenterConnector':
vcenter = VCenterConnector() # if no type given - return list of types
properties = mongo.db.connector.find_one({"type": 'VCenterConnector'}) if not contype:
if properties: conlist = []
vcenter.load_properties(properties) for jobclass in available_jobs:
ret = vcenter.get_properties() if jobclass.connector.__name__ not in conlist:
ret["password"] = "" # for better security, don't expose password conlist.append(jobclass.connector.__name__)
return ret return {"oneOf": conlist}
return {}
con = get_connector_by_name(contype)
if not con:
return {}
properties = mongo.db.connector.find_one({"type": con.__class__.__name__})
if properties:
con.load_properties(properties)
ret = con.get_properties()
ret["password"] = "" # for better security, don't expose password
return ret
def post(self, **kw): def post(self, **kw):
settings_json = json.loads(request.data) settings_json = json.loads(request.data)
if settings_json.get("type") == 'VCenterConnector': contype = settings_json.get("type")
# preserve password # preserve password if empty given
properties = mongo.db.connector.find_one({"type": 'VCenterConnector'}) properties = mongo.db.connector.find_one({"type": contype})
if properties and (not settings_json.has_key("password") or not settings_json["password"]): if properties and (not settings_json.has_key("password") or not settings_json["password"]):
settings_json["password"] = properties.get("password") settings_json["password"] = properties.get("password")
return mongo.db.connector.update({"type": 'VCenterConnector'}, return mongo.db.connector.update({"type": contype},
{"$set": settings_json}, {"$set": settings_json},
upsert=True) upsert=True)
def get_connector_by_name(name):
for jobclass in available_jobs:
if name == jobclass.connector.__name__:
return jobclass.connector()
return None
def get_jobclass_by_name(name): def get_jobclass_by_name(name):