Пример #1
0
 def post(self):
     """
     Add a new wim. The request must provide the wim details.
     used by: `katana wim add -f [yaml file]`
     """
     # TODO: Test connectivity with the WIM
     new_uuid = str(uuid.uuid4())
     # Create the object and store it in the object collection
     try:
         wim_id = request.json["id"]
         if request.json["type"] == "odl-wim":
             wim = odl_wimUtils.Wim(request.json['url'])
         elif request.json["type"] == "test-wim":
             wim = test_wimUtils.Wim(request.json['url'])
         else:
             return "Error: Not supported WIM type", 400
     except KeyError:
         return f"Error: Required fields: {self.req_fields}", 400
     thebytes = pickle.dumps(wim)
     obj_json = {
         "_id": new_uuid,
         "id": request.json["id"],
         "obj": Binary(thebytes)
     }
     request.json['_id'] = new_uuid
     request.json['created_at'] = time.time()  # unix epoch
     request.json['slices'] = {}
     try:
         new_uuid = mongoUtils.add('wim', request.json)
     except pymongo.errors.DuplicateKeyError:
         return f"WIM with id {wim_id} already exists", 400
     mongoUtils.add('wim_obj', obj_json)
     return f"Created {new_uuid}", 201
Пример #2
0
    def put(self, uuid):
        """
        Update the details of a specific wim.
        used by: `katana wim update -f [yaml file] [uuid]`
        """
        data = request.json
        data["_id"] = uuid
        old_data = mongoUtils.get("wim", uuid)

        if old_data:
            data["created_at"] = old_data["created_at"]
            data["slices"] = old_data["slices"]
            try:
                for entry in self.req_fields:
                    if data[entry] != old_data[entry]:
                        return "Cannot update field: " + entry, 400
            except KeyError:
                return f"Error: Required fields: {self.req_fields}", 400
            else:
                mongoUtils.update("wim", uuid, data)
            return f"Modified {uuid}", 200
        else:
            new_uuid = uuid
            data = request.json
            data["_id"] = new_uuid
            data["created_at"] = time.time()  # unix epoch
            try:
                wim_id = request.json["id"]
                if request.json["type"] == "odl-wim":
                    wim = odl_wimUtils.Wim(request.json["url"])
                elif request.json["type"] == "test-wim":
                    wim = test_wimUtils.Wim(request.json["url"])
                else:
                    return "Error: Not supported WIM type", 400
            except KeyError:
                return f"Error: Required fields: {self.req_fields}", 400
            thebytes = pickle.dumps(wim)
            obj_json = {"_id": new_uuid, "id": data["id"], "obj": Binary(thebytes)}
            data["slices"] = {}
            try:
                new_uuid = mongoUtils.add("wim", data)
            except pymongo.errors.DuplicateKeyError:
                return f"WIM with id {wim_id} already exists", 400
            try:
                monitoring_url = request.json["monitoring-url"]
            except KeyError:
                pass
            else:
                with open("/targets/wim_targets.json", mode="r") as prom_file:
                    prom = json.load(prom_file)
                    prom.append({"targets": [monitoring_url], "labels": {"wim_id": wim_id}})
                with open("/targets/wim_targets.json", mode="w") as prom_file:
                    json.dump(prom, prom_file)
            mongoUtils.add("wim_obj", obj_json)
            return f"Created {new_uuid}", 201
Пример #3
0
    def put(self, uuid):
        """
        Update the details of a specific wim.
        used by: `katana wim update -f [yaml file] [uuid]`
        """
        data = request.json
        data['_id'] = uuid
        old_data = mongoUtils.get("wim", uuid)

        if old_data:
            data["created_at"] = old_data["created_at"]
            data["slices"] = old_data["slices"]
            try:
                for entry in self.req_fields:
                    if data[entry] != old_data[entry]:
                        return "Cannot update field: " + entry, 400
            except KeyError:
                return f"Error: Required fields: {self.req_fields}", 400
            else:
                mongoUtils.update("wim", uuid, data)
            return f"Modified {uuid}", 200
        else:
            new_uuid = uuid
            data = request.json
            data['_id'] = new_uuid
            data['created_at'] = time.time()  # unix epoch
            try:
                wim_id = request.json["id"]
                if request.json["type"] == "odl-wim":
                    wim = odl_wimUtils.Wim(request.json['url'])
                elif request.json["type"] == "test-wim":
                    wim = test_wimUtils.Wim(request.json['url'])
                else:
                    return "Error: Not supported WIM type", 400
            except KeyError:
                return f"Error: Required fields: {self.req_fields}", 400
            thebytes = pickle.dumps(wim)
            obj_json = {
                "_id": new_uuid,
                "id": data["id"],
                "obj": Binary(thebytes)
            }
            data['slices'] = {}
            try:
                new_uuid = mongoUtils.add('wim', data)
            except pymongo.errors.DuplicateKeyError:
                return f"WIM with id {wim_id} already exists", 400
            mongoUtils.add('wim_obj', obj_json)
            return f"Created {new_uuid}", 201
Пример #4
0
 def post(self):
     """
     Add a new wim. The request must provide the wim details.
     used by: `katana wim add -f [file]`
     """
     # TODO: Test connectivity with the WIM
     new_uuid = str(uuid.uuid4())
     # Create the object and store it in the object collection
     try:
         wim_id = request.json["id"]
         if request.json["type"] == "odl-wim":
             wim = odl_wimUtils.Wim(request.json["url"])
         elif request.json["type"] == "test-wim":
             wim = test_wimUtils.Wim(request.json["url"])
         else:
             return "Error: Not supported WIM type", 400
     except KeyError:
         return f"Error: Required fields: {self.req_fields}", 400
     thebytes = pickle.dumps(wim)
     obj_json = {
         "_id": new_uuid,
         "id": request.json["id"],
         "obj": Binary(thebytes)
     }
     request.json["_id"] = new_uuid
     request.json["created_at"] = time.time()  # unix epoch
     request.json["slices"] = {}
     try:
         new_uuid = mongoUtils.add("wim", request.json)
     except pymongo.errors.DuplicateKeyError:
         return f"WIM with id {wim_id} already exists", 400
     try:
         monitoring_url = request.json["monitoring-url"]
     except KeyError:
         pass
     else:
         with open("/targets/wim_targets.json", mode="r") as prom_file:
             prom = json.load(prom_file)
             prom.append({
                 "targets": [monitoring_url],
                 "labels": {
                     "wim_id": wim_id
                 }
             })
         with open("/targets/wim_targets.json", mode="w") as prom_file:
             json.dump(prom, prom_file)
     mongoUtils.add("wim_obj", obj_json)
     return new_uuid, 201