Ejemplo n.º 1
0
 def test_update_host(self):
     self.test_create_host()
     host = Host.find_one({"fqdn": TEST_HOST_1["fqdn"]})
     payload = TEST_HOST_2
     r = self.put_json("/api/v1/hosts/%s" % host._id, payload)
     self.assertEqual(r.status_code, 200)
     host = Host.find_one({"_id": host._id})
     self.assertEqual(host.fqdn, TEST_HOST_2["fqdn"])
     self.assertEqual(host.description, TEST_HOST_2["description"])
     self.assertItemsEqual(host.tags, TEST_HOST_2["tags"])
Ejemplo n.º 2
0
 def test_delete_host(self):
     self.test_create_host()
     host = Host.find_one({"fqdn": TEST_HOST_1["fqdn"]})
     r = self.delete("/api/v1/hosts/%s" % host._id)
     self.assertEqual(r.status_code, 200)
     data = json.loads(r.data)
     self.assertIn("data", data)
     host_data = data["data"]
     self.assertEqual(host_data["_id"], None)
     host = Host.find_one({"_id": host._id})
     self.assertIsNone(host)
Ejemplo n.º 3
0
def enc(host_id):
    from app.models import Host
    host_id = resolve_id(host_id)
    host = Host.find_one({
        "$or": [{
            "_id": host_id
        }, {
            "fqdn": host_id
        }, {
            "short_name": host_id
        }]
    })
    if host is None:
        return json_response({"errors": ["Host not found"]}, 404)

    puppet_data = {"classes": [], "parameters": {}}

    for tag in host.all_tags:
        class_match = CLASS_EXPR.match(tag)
        if class_match is not None:
            puppet_data["classes"].append(class_match.groups()[0])
            continue
        param_match = PARAM_EXPR.match(tag)
        if param_match is not None:
            k, v = param_match.groups()
            puppet_data["parameters"][k] = v
            continue
        env_match = ENV_EXPR.match(tag)
        if env_match is not None:
            puppet_data["environment"] = env_match.groups()[0]

    return yaml_response(puppet_data)
Ejemplo n.º 4
0
def set_hosts(group_id):
    from app.models import Host
    group = _get_group_by_id(group_id)
    if group is None:
        return json_response({"errors": ["Group not found"]}, 404)
    # TODO: check permissions!
    orig = group.host_ids
    upd = request.json["host_ids"]
    try:
        upd = [ObjectId(x) for x in upd]
    except InvalidId as e:
        return json_exception(e, 400)
    d = diff(orig, upd)
    exs = []
    for item in d.remove:
        try:
            h = Host.find_one({"_id": item})
            if h is not None:
                h.group_id = None
                h.save()
        except Exception as e:
            exs.append(e)
    for item in d.add:
        try:
            h = Host.find_one({"_id": item})
            if h is not None:
                h.group_id = group._id
                h.save()
        except Exception as e:
            exs.append(e)
    if len(exs) > 0:
        return json_response({
            "errors":
            ["%s: %s" % (x.__class__.__name__, x.message) for x in exs]
        })
    else:
        if "_fields" in request.values:
            fields = request.values["_fields"].split(",")
        else:
            fields = None
        return json_response({"data": group.to_dict(fields), "status": "ok"})
Ejemplo n.º 5
0
    def test_create_host(self):
        payload = TEST_HOST_1
        r = self.post_json("/api/v1/hosts/", payload)
        self.assertEqual(r.status_code, 201)
        data = json.loads(r.data)
        self.assertIn("data", data)

        host_data = data["data"]
        self.assertIs(type(host_data), list)
        self.assertEqual(len(host_data), 1)

        host = host_data[0]
        self.assertIn("_id", host)
        host = Host.find_one({"_id": ObjectId(host["_id"])})
        self.assertIsNotNone(host)
        self.assertEqual(payload["fqdn"], host.fqdn)
        self.assertEqual(payload["description"], host.description)
        self.assertItemsEqual(payload["tags"], host.tags)