示例#1
0
def create_volume(request):
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    cinder = CinderRestAPI(auth_url, token)
    if request.method == 'POST' and request.is_ajax():
        data = json.loads(request.POST.get("data"))
        result = cinder.create_volume(data)
    elif request.method == 'GET':
        glance = GlanceRestAPI(auth_url, token)
        result_volume_types = cinder.get_volume_type_list()
        result_images = glance.get_image_list()
        result = {}
        if result_volume_types.get("success"):
            result["success"] = {
                "volume_types": result_volume_types["success"].get("volume_types")
            }
        else:
            result["error"] = {
                "volume_types": result_volume_types.get("error")
            }
        if result_images.get("success"):
            if result.get("success") is None:
                result["success"] = {}
            result["success"]["images"] = result_images["success"].get("images")
        else:
            if result.get("error") is None:
                result["error"] = {}
            result["error"]["images"] = result_images.get("error")

    else:
        result = {}

    return JsonResponse(result)
示例#2
0
def delete_volume(request, volume_id):
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    cinder = CinderRestAPI(auth_url, token)
    if request.method == 'POST' and request.is_ajax():
        result = cinder.delete_volume(volume_id)
        if result.get("error") is None:
            result = {"success": ""}
        return JsonResponse(result)
    else:
        raise Http404()
示例#3
0
def get_vm_in_asg(request):
    if request.is_ajax() and request.method == 'POST':
        auth_url = request.session.get("auth_url")
        token = request.session.get("passToken")
        stack_id = request.POST.get("stack_id")
        volume_search = request.POST.get("volume_search")
        # stack_name = request.POST.get("name")
        nova = NovaRestAPI(auth_url, token)
        cinder = CinderRestAPI(auth_url, token)
        result_servers = nova.get_server_detail_list(
            fields=["id", "name", "metadata"])
        result_volumes = {}
        if volume_search:
            result_volumes = cinder.get_volume_detail_list()
        result = {"success": {"servers": [], "volumes": []}}
        if result_servers.get("success"):
            for result_server in result_servers["success"].get("servers"):
                if result_server.get("metadata") and result_server[
                        "metadata"].get("metering.stack") == stack_id:
                    if volume_search and result_volumes.get("success"):
                        for result_volume in result_volumes["success"].get(
                                "volumes"):
                            for attachment in result_volume.get("attachments"):
                                if attachment.get("server_id"
                                                  ) == result_server.get("id"):
                                    volumes = [result_volume]
                                    result["success"]["volumes"].extend(
                                        volumes)
                    servers = [result_server]
                    result["success"]["servers"].extend(servers)
        else:
            result = result_servers
        # heat = HeatRestAPI(auth_url, token)
        # result = heat.get_resource_in_stack(stack_name, stack_id, {"type": "OS::Heat::AutoScalingGroup"})
        # if result.get("success"):
        #     as_groups = result["success"].get("resources")
        #     for as_group in as_groups:
        #         asg_stack_id = as_group.get("physical_resource_id")
        #         result = heat.find_server_in_autoscaling(asg_stack_id)
        #         if result.get("success"):
        #             asg_stack_resources = result["success"]["resources"]
        #             result = {"success": {"servers": [], "error_msg_list": []}}
        #             for asg_stack_resource in asg_stack_resources:
        #                 server_name = "." + asg_stack_resource.get("logical_resource_id") + "."
        #                 result_servers = nova.get_server_detail_list({"name": server_name})
        #                 if result_servers.get("success"):
        #                     servers = result_servers["success"].get("servers")
        #                     for server in servers:
        #                         server["asg_name"] = asg_stack_resource.get("logical_resource_id")
        #                     result["success"]["servers"].extend(servers)
        #                 else:
        #                     result["success"]["error_msg_list"].append(result_servers.get("error"))
        return JsonResponse(result)
示例#4
0
def retrieveQuotaList(request):
    if request.is_ajax() and request.method == 'POST':
        # sess = login("admin", "chiron", "admin", "http://192.168.10.6/identity/v3", 'default')
        token = request.session.get('passToken')
        domain_name = request.session.get("domain_name")
        project_id = request.session.get("project_id")
        auth_url = request.session.get("auth_url")
        description = request.session.get("description")
        # sess = login(token, domain_name, project_name)
        try:
            nova = NovaRestAPI(auth_url, token)
        except Unauthorized as e:
            request.session["error"] = {
                "title": e.message,
                "message": e.details,
                "code": 401
            }
            return redirect("/dashboard/login/?auth_url=" + auth_url +
                            "&domain_name=" + domain_name + "&description=" +
                            description)
        resultNovaQuotaList = nova.get_nova_default_quotas(project_id).get(
            "success").get("quota_set")
        quotaList = []

        for key in resultNovaQuotaList.keys():
            if key == "id" or key == "floating_ips" or key == "security_group_rules" or key == "fixed_ips" or key == "security_groups":
                continue
            quota = {"name": key, "limit": resultNovaQuotaList[key]}
            quotaList.append(quota)

        cinder = CinderRestAPI(auth_url, token)

        resultCinderQuotaList = cinder.get_cinder_quotas_defulat(
            project_id).get("success")
        if resultCinderQuotaList:
            for key in resultCinderQuotaList:
                if key == "id" or key == "floating_ips" or key == "security_group_rules" or key == "fixed_ips" or key == "security_groups":
                    continue
                quota = {"name": key, "limit": resultCinderQuotaList[key]}
                quotaList.append(quota)

        return JsonResponse({"success": {'quotaList': quotaList}})
    else:
        token = request.session.get('passToken')
        if not token:
            return redirect(
                "/dashboard/domains/?next=/dashboard/admin/defaults")
        return render(request, 'admin/defaults/index.html',
                      {'instanceList': ""})
示例#5
0
def retrieve_block_storage_service_list(request):
    if request.is_ajax() and request.method == 'POST':

        token = request.session.get('passToken')
        auth_url = request.session.get("auth_url")
        cinder = CinderRestAPI(auth_url, token)
        block_storage_service_list = [
            service for service in cinder.getBlockStorageServiceList().get(
                "success").get("services")
        ]
        return JsonResponse({
            "success": {
                'blockStorageServiceList': block_storage_service_list
            }
        })
示例#6
0
def get_volume_by_id(request, volume_id):
    # logger.info("get_volume_by_id")
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    if not token:
        return redirect("/dashboard/domains/?next=/dashboard/admin/volumes/" + volume_id)
    cinder = CinderRestAPI(auth_url, token)
    keystone = KeystoneRestAPI(auth_url, token)
    result_volume = cinder.get_volume_by_id(volume_id)
    volume = result_volume.get("success").get("volume")
    result_project = keystone.get_project(volume.get("os-vol-tenant-attr:tenant_id"))
    if result_project.get("success"):
        project = result_project["success"].get("project")
        volume["project_name"] = project.get("name")
    volume["host"] = volume.get("os-vol-host-attr:host")
    volume["type"] = volume.get("volume_type")
    return render(request, 'admin/volumes/info.html', {'volume': volume})
示例#7
0
def volume_list(request):
    """
    볼륨 목록
    :param request:
    :return:
    """
    auth_url = request.session.get("auth_url")
    token = request.session.get("passToken")
    project_id = request.session.get("project_id")
    if request.is_ajax() and request.method == 'POST':
        cinder = CinderRestAPI(auth_url, token)
        result = cinder.get_volume_detail_list()
        if result.get("success"):
            volumes = result["success"].get("volumes")
        else:
            volumes = []

        return JsonResponse({"success": {"volume_list": volumes}})
示例#8
0
def sync(request, volume_id):
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    service_id = request.POST.get("service_id")
    cinder = CinderRestAPI(auth_url, token)
    try:
        from sdsecgui.db.soa_db_connector import SOAManagerDBConnector
        m_conn = SOAManagerDBConnector.getInstance()
        m_conn.insert_volume(auth_url, cinder, service_id, volume_id)
        result = True
    except Exception as e:
        from sdsec.settings import logger
        logger.debug(e.message)
        result = False
    return JsonResponse({"result": result})
示例#9
0
def get_volume_list(request):
    # logger.info("get_volume_list")
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    cinder = CinderRestAPI(auth_url, token)
    keystone = KeystoneRestAPI(auth_url, token)
    result_volume_list = cinder.get_volume_detail_list()
    if result_volume_list.get("success"):
        volume_list = result_volume_list["success"].get("volumes")
        for volume in volume_list:
            result_project = keystone.get_project(volume.get("os-vol-tenant-attr:tenant_id"))
            if result_project.get("success"):
                project = result_project["success"].get("project")
                volume["project_name"] = project.get("name")
            volume["host"] = volume.get("os-vol-host-attr:host")
    else:
        volume_list = result_volume_list
        volume_list["error"]["message"] = volume_list["error"]["message"].replace("<br /><br />\n \n\n", "")
    # TODO: soam sync delete
    try:
        soam_sync = bool(config.get("SETTINGS", "PORTAL_SYNC") == "True")
    except Exception as e:
        soam_sync = False
    return render(request, 'admin/volumes/index.html', {'volume_list': volume_list, "sync": soam_sync})
示例#10
0
    def insert_service_resource(self, params, service_template, request):
        auth_url = params[0]
        service_id = params[2]
        token = request.session.get("passToken")
        ctrl_header = request.session.get("ctrl_header")

        neutron = NeutronRestAPI(auth_url, token)
        nova = NovaRestAPI(auth_url, token)
        glance = GlanceRestAPI(auth_url, token)
        cinder = CinderRestAPI(auth_url, token)

        control = ControlEngine(ctrl_header)

        # router
        routers = service_template.get("vrouter_list")
        for router in routers:
            self.insert_router(auth_url, neutron, service_id,
                               router.get("vrouter_id"))

        # network
        networks = service_template.get("network_list")
        for network in networks:
            self.insert_network(auth_url, control, neutron, service_id,
                                network.get("network_id"))

        # server
        servers = service_template.get("vm_list")
        for server in servers:
            self.insert_server(auth_url, nova, service_id, server.get("vm_id"),
                               glance)

        # volume
        volumes = service_template.get("volume_list")
        for volume in volumes:
            self.insert_volume(auth_url, cinder, service_id,
                               volume.get("volume_id"))
示例#11
0
def project_modal(request):
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    modal_title = request.GET.get("modal_title")
    project_id = request.GET.get("project_id")
    domain_id = request.session.get("domain_id")
    domain_name = request.session.get("domain_name")
    data = {
        "modal_title": modal_title,
        "input_domain_id": domain_id,
        "input_domain_name": domain_name,
        "project_id": project_id
    }

    keystone = KeystoneRestAPI(auth_url, token)

    u_result = keystone.get_user_list({"domain_id": domain_id})
    if u_result.get("success"):
        data["users"] = u_result["success"].get("users")

    g_result = keystone.get_group_list()
    if g_result.get("success"):
        data["groups"] = g_result["success"].get("groups")

    r_result = keystone.get_role_list()
    if r_result.get("success"):
        data["roles"] = r_result["success"].get("roles")

    if project_id:
        p_result = keystone.get_project(project_id)
        if p_result.get("success"):
            project = p_result["success"].get("project")
            data["project_name"] = project.get("name")
            data["description"] = project.get("description")
            data["enabled"] = project.get("enabled")
        ra_result = keystone.get_role_assignments(
            {"scope.project.id": project_id})
        if ra_result.get("success"):
            role_assignments = ra_result["success"].get("role_assignments")
            response_role_assignments = {"users": [], "groups": []}
            for role_assignment in role_assignments:  # role_assignment_role 루프
                role_assignment_user = role_assignment.get("user")
                if role_assignment_user:
                    select_user = filter(
                        lambda user: user.get("id") == role_assignment_user.
                        get("id"), data["users"])
                    if select_user:
                        role_assignment_user["name"] = select_user[0].get(
                            "name")
                        data["users"][data["users"].index(
                            select_user[0])]["assigned"] = True

                role_assignment_group = role_assignment.get("group")
                if role_assignment_group:
                    select_group = filter(
                        lambda group: group.get("id") == role_assignment_group.
                        get("id"), data["groups"])
                    if select_group:
                        role_assignment_group["name"] = select_group[0].get(
                            "name")
                        data["groups"][data["groups"].index(
                            select_group[0])]["assigned"] = True

                role_assignment_role = role_assignment.get("role")
                if role_assignment_role:
                    select_role = filter(
                        lambda role: role.get("id") == role_assignment_role.
                        get("id"), data["roles"])
                    if select_role:
                        role_assignment_role["name"] = select_role[0].get(
                            "name")

                if role_assignment_role:
                    if role_assignment_user:
                        select_user = [
                            user for user in response_role_assignments["users"]
                            if user.get("id") == role_assignment_user.get("id")
                        ]  # 이미 있는지 확인
                        if len(select_user) == 0:  # 없으면 만들기
                            response_role_assignments["users"].append({
                                "name":
                                role_assignment_user.get("name"),
                                "id":
                                role_assignment_user.get("id"),
                                "roles": [role_assignment_role],
                                "roles_display":
                                role_assignment_role.get("name")
                            })
                        elif len(select_user) == 1:  # 있으면 role 추가
                            select_user[0]["roles"].append(
                                role_assignment_role)
                            select_user[0][
                                "roles_display"] += ", " + role_assignment_role.get(
                                    "name")

                    if role_assignment_group:
                        select_group = [
                            group
                            for group in response_role_assignments["groups"] if
                            group.get("id") == role_assignment_group.get("id")
                        ]  # 이미 있는지 확인
                        if len(select_group) == 0:  # 없으면 만들기
                            response_role_assignments["groups"].append({
                                "name":
                                role_assignment_group.get("name"),
                                "id":
                                role_assignment_group.get("id"),
                                "roles": [role_assignment_role],
                                "roles_display":
                                role_assignment_role.get("name")
                            })
                        elif len(select_group) == 1:  # 있으면 role 추가
                            select_group[0]["roles"].append(
                                role_assignment_role)
                            select_group[0][
                                "roles_display"] += ", " + role_assignment_role.get(
                                    "name")

            data["role_assignments"] = response_role_assignments
            data["role_assignments_json"] = json.dumps(
                response_role_assignments)

        nova = NovaRestAPI(auth_url, token)
        cinder = CinderRestAPI(auth_url, token)
        neutron = NeutronRestAPI(auth_url, token)
        result_nova_quota_set = nova.get_quotas(project_id)
        result_neutron_quotas = neutron.get_quotas(project_id)
        result_cinder_quotas = cinder.get_cinder_quotas(project_id)
        # result_cinder_limits = cinder.get_limits(project_id)
        if result_nova_quota_set.get("success"):
            nova_quota_set = result_nova_quota_set["success"].get("quota_set")
            data["nova_quota_set"] = nova_quota_set
        if result_neutron_quotas.get("success"):
            neutron_quotas = result_neutron_quotas["success"].get("quota")
            data["neutron_quotas"] = neutron_quotas
        # if result_cinder_limits.get("success"):
        #     cinder_limits = result_cinder_limits["success"]["limits"].get("absolute")
        #     data["cinder_limits"] = cinder_limits
        if result_cinder_quotas.get("success"):
            cinder_quotas = result_cinder_quotas["success"].get("quota_set")
            data["cinder_quotas"] = cinder_quotas

    return render(request, 'identity/projects/modal.html', data)
示例#12
0
def update_project(request, project_id):
    if request.is_ajax() and request.method == 'POST':
        token = request.session.get('passToken')
        auth_url = request.session.get('auth_url')
        project = json.loads(request.POST.get("project"))
        quota = json.loads(request.POST.get("quotas"))
        data = {
            "project": {
                "description": project.get("description"),
                "domain_id": project.get("domain_id"),
                "name": project.get("name"),
                "enabled": project.get("enabled"),
                "is_domain": False,
            }
        }
        keystone = KeystoneRestAPI(auth_url, token)
        result = keystone.update_project(project_id, data)
        err_msg_list = []

        if result.get("success"):
            assign_list = json.loads(request.POST.get("assignList"))
            unassign_list = json.loads(request.POST.get("unassignList"))
            # 사용자 역할 할당
            for assign in assign_list.get("users"):
                user_id = assign.get("user_id")
                role_id_list = assign.get("role_id_list")
                for role_id in role_id_list:
                    assign_result = keystone.assign_role_to_user_on_projects(
                        project_id, user_id, role_id)
                    if assign_result.get("error"):
                        if not result.get("error"):
                            result["error"] = []
                        result["error"].append(assign_result["error"])
            # 사용자 역할 제거
            for unassign in unassign_list.get("users"):
                user_id = unassign.get("user_id")
                role_id_list = unassign.get("role_id_list")
                for role_id in role_id_list:
                    unassign_result = keystone.unassign_role_to_user_on_projects(
                        project_id, user_id, role_id)
                    if unassign_result.get("error"):
                        if not result.get("error"):
                            result["error"] = []
                        result["error"].append(unassign_result["error"])
            # 그룹 역할 할당
            for assign in assign_list.get("groups"):
                group_id = assign.get("group_id")
                role_id_list = assign.get("role_id_list")
                for role_id in role_id_list:
                    assign_result = keystone.assign_role_to_group_on_projects(
                        project_id, group_id, role_id)
                    if assign_result.get("error"):
                        if not result.get("error"):
                            result["error"] = []
                        result["error"].append(assign_result["error"])
            # 그룹 역할 제거
            for unassign in unassign_list.get("groups"):
                group_id = unassign.get("group_id")
                role_id_list = unassign.get("role_id_list")
                for role_id in role_id_list:
                    unassign_result = keystone.unassign_role_to_group_on_projects(
                        project_id, group_id, role_id)
                    if unassign_result.get("error"):
                        if not result.get("error"):
                            result["error"] = []
                        result["error"].append(unassign_result["error"])
            if quota["nova"].get("quota_set"):
                nova = NovaRestAPI(auth_url, token)
                result = nova.update_quotas(project_id, quota["nova"])
                if result.get("error"):
                    err_msg_list.append(result["error"])
            if quota["neutron"].get("quota"):
                neutron = NeutronRestAPI(auth_url, token)
                result = neutron.update_quotas(project_id, quota["neutron"])
                if result.get("error"):
                    err_msg_list.append(result["error"])
            if quota["cinder"].get("quota_set"):
                cinder = CinderRestAPI(auth_url, token)
                result = cinder.update_quotas(project_id, quota["cinder"])
                if result.get("error"):
                    err_msg_list.append(result["error"])

        return JsonResponse(result)
示例#13
0
def sync_modal(request, volume_id):
    token = request.session.get('passToken')
    auth_url = request.session.get("auth_url")
    cinder = CinderRestAPI(auth_url, token)
    result = cinder.get_volume_by_id(volume_id)
    return render(request, 'admin/sync_modal.html', {'data': result["success"]["volume"]})
示例#14
0
def get_create_metadata(request):
    if request.method == 'POST':
        token = request.session.get('passToken')
        domain_name = request.session.get("domain_name")
        project_name = request.session.get("project_name")
        project_id = request.session.get("project_id")
        auth_url = request.session.get("auth_url")
        description = request.session.get("description")
        try:
            nova = NovaRestAPI(auth_url, token)
            cinder = CinderRestAPI(auth_url, token)
            glance = GlanceRestAPI(auth_url, token)
            neutron = NeutronRestAPI(auth_url, token)
        except Unauthorized as e:
            request.session["error"] = {"title": e.message, "message": e.details, "code": 401}
            return redirect("/dashboard/login/?auth_url=" + auth_url + "&domain_name=" + domain_name + "&description=" + description)
        server_metadata = {
            "info": {
                "availability_zones": [],
                "quotas": []
            },
            "source": {
                "images": [],
                "instance_snapshots": [],
                "volumes": [],
                "volume_snapshots": []
            },
            "flavor": {
                "flavors": []
            },
            "network": {
                "networks": []
            },
            "network_port": {
                "ports": []
            },
            "security_group": {
                "security_groups": []
            },
            "keypair": {
                "keypairs": []
            },
            "metadata": {
                "metadatas": []
            }
        }
        availability_zone_list = nova.get_availability_zone_detail()
        if availability_zone_list.get("success"):
            server_metadata["info"]["availability_zones"] += availability_zone_list.get("success").get("availabilityZoneInfo")

        quotas = nova.get_nova_default_quotas(project_id)
        if quotas.get("success"):
            server_metadata["info"]["quotas"] += quotas.get("success").get("quota_set")

        images = glance.get_image_list()
        if images.get("success"):
            server_metadata["source"]["images"] = images.get("success").get("images")

        # instance_snapshots = glance.get_image_list()
        # if instance_snapshots.get("success"):
        #     serverMetadata["source"]["instance_snapshots"] = instance_snapshots.get("success").get("instance_snapshots")

        volumes = cinder.get_volume_list()
        if volumes.get("success"):
            server_metadata["source"]["volumes"] = volumes.get("success").get("volumes")

        volume_snapshots = cinder.getSnapshotList(project_name)
        if volume_snapshots.get("success"):
            server_metadata["source"]["volume_snapshots"] = volume_snapshots.get("success").get("volume_snapshots")

        flavors = nova.get_flavor_list()
        if flavors.get("success"):
            server_metadata["flavor"]["flavors"] = flavors.get("success").get("flavors")

        networks = neutron.get_network_list()
        if networks.get("success"):
            server_metadata["network"]["networks"] = networks.get("success").get("networks")

        ports = neutron.get_port_list()
        if ports.get("success"):
            server_metadata["network_port"]["ports"] = ports.get("success").get("ports")

        keypairs = nova.get_keypairs_list()
        if keypairs.get("success"):
            server_metadata["keypair"]["keypairs"] = keypairs.get("success").get("keypairs")

        # metadatas = nova.getMetadata() // 서버id가필요한데 왜그런지?

        return JsonResponse({"success": server_metadata})