def create(ciDetails): """ This function creates a new ci in the ci list based on the passed in ci data :param ci: ci to create in ci structure :return: 201 on success, 406 on ci exists """ key = ciDetails.get("key", None) # Does the ci exist already? existing_ci = (CI.query.filter(CI.key == key).one_or_none()) if existing_ci is None: schema = CISchema() new_ci = schema.load(ciDetails, session=db.session) db.session.add(new_ci) db.session.commit() # Serialize and return the newly created deployment # in the response data = schema.dump(new_ci) return data, 201 # Otherwise, it already exists, that's an error else: abort(406, f"CI already exists")
def update(key, ciDetails): """ This function updates an existing ci in the ci list :param key: key of the ci to update in the ci list :param ci: ci to update :return: updated ci """ app.logger.debug(pformat(ciDetails)) if ciDetails["key"] != key: abort(400, f"Key mismatch in path and body") # Does the ci exist in ci list? existing_ci = CI.query.filter(CI.key == key).one_or_none() # Does ci exist? if existing_ci is not None: schema = CISchema() update_ci = schema.load(ciDetails, session=db.session) update_ci.key = ciDetails['key'] db.session.merge(update_ci) db.session.commit() # return the updted ci in the response data = schema.dump(update_ci) return data, 200 # otherwise, nope, deployment doesn't exist, so that's an error else: abort(404, f"CI not found")
def create(ciDetails): """ This function creates a new ci in the ci list based on the passed in ci data :param ci: ci to create in ci structure :return: 201 on success, 406 on ci exists """ # Remove id as it's created automatically if "id" in ciDetails: del ciDetails["id"] # Does the ci exist already? existing_ci = ( db.session.query(CI).filter(CI.value == ciDetails["value"]).one_or_none() ) if existing_ci is None: schema = CISchema() new_ci = schema.load(ciDetails, session=db.session) db.session.add(new_ci) db.session.commit() # Serialize and return the newly created deployment # in the response data = schema.dump(new_ci) return data, 201 # Otherwise, it already exists, that's an error else: abort(406, "CI already exists")
def read_all(): """ This function responds to a request for /api/ci with the complete lists of CIs :return: json string of list of CIs """ # Create the list of CIs from our data ci = db.session.query(CI).order_by(CI.id).all() app.logger.debug(pformat(ci)) # Serialize the data for the response ci_schema = CISchema(many=True) data = ci_schema.dump(ci) return data
class ExtendedActivatorSchema(Schema): def __init__(self, **kwargs): super().__init__(**kwargs) id = fields.Int() isActive = fields.Boolean() lastUpdated = fields.Str() isFavourite = fields.Boolean() name = fields.Str() available = fields.Boolean() sensitivity = fields.Str() envs = fields.Nested(LZEnvironmentSchema(many=True)) userCapacity = fields.Int() serverCapacity = fields.Int() regions = fields.List(fields.Str()) hosting = fields.List(fields.Str()) apiManagement = fields.List(fields.Str()) ciId = fields.Int() ci = fields.Nested(CISchema(many=True)) cdId = fields.Int() cd = fields.Nested(CDSchema(many=True)) sourceControlId = fields.Int() sourceControl = fields.Nested(SourceControlSchema(many=False)) businessUnitId = fields.Int() businessUnit = fields.Nested(BusinessUnitSchema(many=False)) technologyOwner = fields.Str() technologyOwnerEmail = fields.Str() billing = fields.Str() activator = fields.Str() status = fields.Str() accessRequestedById = fields.Int() accessRequestedBy = fields.Nested(ExtendedUserSchema(many=False)) source = fields.Str() gitRepoUrl = fields.Str() activatorMetadata = fields.Nested( ExtendedActivatorMetadataSchema(many=False)) @post_load(pass_original=True) def deserialize_post_load(self, data, original_data, **kwargs): # logger.debug( # "ExtendedActivatorSchema::post_load::serialize_post_load: %s", data # ) if original_data.regions is not None: data["regions"] = json.dumps(original_data.regions) if original_data.hosting is not None: data["hosting"] = json.dumps(original_data.hosting) if original_data.apiManagement is not None: data["apiManagement"] = json.dumps(original_data.apiManagement) return data @post_dump(pass_original=True) def deserialize_post_dump(self, data, original_data, **kwargs): # logger.debug("ExtendedActivatorSchema::post_dump %s", original_data) if original_data.regions is not None: data["regions"] = json.loads(original_data.regions) if original_data.hosting is not None: data["hosting"] = json.loads(original_data.hosting) if original_data.apiManagement is not None: data["apiManagement"] = json.loads(original_data.apiManagement) return data
class ExtendedSolutionSchema(Schema): __envelope__ = {"single": "solution", "many": "solutions"} id = fields.Int() isActive = fields.Boolean() lastUpdated = fields.Str() isFavourite = fields.Boolean() name = fields.Str() description = fields.Str() businessUnitId = fields.Int() costCentre = fields.Str() ciId = fields.Int() ci = fields.Nested(CISchema(many=False)) cdId = fields.Int() cd = fields.Nested(CDSchema(many=False)) sourceControlId = fields.Int() sourceControl = fields.Nested(SourceControlSchema(many=False)) environments = fields.Nested(LZEnvironmentSchema(many=True)) favourite = fields.Boolean() teamId = fields.Int() applications = fields.Nested(ExtendedApplicationSchema(many=True)) team = fields.Nested(ExtendedTeamSchema(many=False)) deploymentFolderId = fields.Str() businessUnit = fields.Nested(BusinessUnitSchema(many=False)) deploymentState = fields.Str() isSandbox = fields.Boolean()
def read_one(id): """ This function responds to a request for /ci/{id} with one matching ci from CIs :param application: id of ci to find :return: ci matching id """ ci = db.session.query(CI).filter(CI.id == id).one_or_none() if ci is not None: # Serialize the data for the response ci_schema = CISchema() data = ci_schema.dump(ci) return data else: abort(404, "CI with id {id} not found".format(id=id))
def read_one(key): """ This function responds to a request for /api/ci/{key} with one matching ci from CIs :param application: key of ci to find :return: ci matching key """ ci = (CI.query.filter(CI.key == key).one_or_none()) if ci is not None: # Serialize the data for the response ci_schema = CISchema() data = ci_schema.dump(ci) return data else: abort(404, "CI with key {key} not found".format(key=key))
def read_keyValues(): """ This function responds to a request for /keyValues/ci with the complete lists of CIs :return: json string of list of CIs """ # Create the list of CIs from our data ci = db.session.query(CI).order_by(CI.id).all() app.logger.debug(pformat(ci)) # Serialize the data for the response ci_schema = CISchema(many=True) data = ci_schema.dump(ci) keyValues = [] for d in data: keyValuePair = {} keyValuePair["key"] = d.get("id") keyValuePair["value"] = d.get("value") keyValues.append(keyValuePair) print(keyValues) return keyValues