def order_get(request): param = { "time_stamp": int(round(time.time() * 1000)), } ak, sk = Fit2CloudClient(settings.CLOUD_CONF, settings.cloud_secret_key).get_work_space(param) if ak and sk: _param = { "orderId": request.GET.get("order_id"), "time_stamp": int(round(time.time() * 1000)), # "time_stamp": 1517905240318, } _conf = settings.CLOUD_CONF.copy() _conf["access_key"] = ak _res = Fit2CloudClient(_conf, sk).order_get(_param) print(_res["success"]) if _res["success"]: res = { "status": _res.get("data")["status"], } else: res = { "status": "NONE", } return JsonResponse(res)
def get_cluster_list(request): # 工作空间接口请求 param = { "time_stamp": int(round(time.time() * 1000)), } ak, sk = Fit2CloudClient(settings.CLOUD_CONF, settings.cloud_secret_key).get_work_space(param) logging.error("ak, sk: ", ak, sk) if ak and sk: _param = { # "currPage": 1, # "pageSize": 1000, "time_stamp": int(round(time.time() * 1000)), } _conf = settings.CLOUD_CONF.copy() _conf["access_key"] = ak res = Fit2CloudClient(_conf, sk).get_cluster_list(_param) return JsonResponse(res) logging.error("not cluster list") return JsonResponse({"code": "1001", "msg": "not cluster list"})
def order_create(request): param = { "time_stamp": int(round(time.time() * 1000)), } post = { "clusterRoleId": 2, "count": 1, "description": "需要机器配置:1c1g", "expireTime": 1518451199999, "installAgent": True, "productId": "c8509c0d-e518-4532-90d9-be8b840b1fc9" } # 工作空间接口请求 ak, sk = Fit2CloudClient(settings.CLOUD_CONF, settings.cloud_secret_key).get_work_space(param) print("ak, sk : ", ak, sk) if ak and sk: _param = { # "time_stamp": int(round(time.time() * 1000)), "time_stamp": 1517905240318, } _conf = settings.CLOUD_CONF.copy() _conf["access_key"] = ak print("_conf: ", _conf) res = Fit2CloudClient(_conf, sk).order_create(_param, json.dumps(post)) return JsonResponse(res) logging.error("not cluster create") return JsonResponse({"code": "1001", "msg": "not order create"})
def cpu_sync(sender, instance, created, *args, **kwargs): """ 触发CMDB创建 :param sender: :param instance: :param created: :param args: :param kwargs: :return: """ if created: data = instance.__dict__.copy() asset = instance.device data["deviceName"] = asset.deviceName data.pop("dt_created") data.pop("dt_updated") data.pop("_state") data.pop("_device_cache") data.pop("id") param = { "time_stamp": int(round(time.time() * 1000)), } post = { "cpus": [ data ] } res = Fit2CloudClient(settings.CMDB_CONF, settings.secret_key).ph_cpu_add( param, json.dumps(post) ) print(res) return None
def asset_list(request): # 收敛前端提交参数 param = { "time_stamp": int(round(time.time() * 1000)), } # 获取vm接口数据 res = Fit2CloudClient(settings.CMDB_CONF, settings.secret_key).query_ph_device(param) data = res["data"]["items"] if res["code"] == "0000" else {} return render(request, "asset/asset_list.html", locals())
def user_get(request): param = { "time_stamp": int(round(time.time() * 1000)), } _conf = settings.CLOUD_CONF.copy() _conf.pop("user") client = Fit2CloudClient(_conf, settings.cloud_secret_key) res = client.user_get(param) return JsonResponse(res)
def get_disk_list(request): # 收敛前端提交参数 param = { "cloudAccountId": 1, "time_stamp": int(round(time.time() * 1000)), "cloud": "aws", } # 获取disk接口数据 res = Fit2CloudClient(settings.CMDB_CONF, settings.secret_key).query_disk(param) return JsonResponse(res)
def get_instance_list(request): param = { "time_stamp": int(round(time.time() * 1000)), "currPage": 1, "pageSize": 500, } _conf = settings.CLOUD_CONF.copy() _conf.pop("user") client = Fit2CloudClient(_conf, settings.cloud_secret_key) res = client.get_instance_list(param) return JsonResponse(res)
def asset_add(request): url = request.META.get('HTTP_REFERER') param = { "time_stamp": int(round(time.time() * 1000)), } # # test 数据 # post = { # "devices": [ # { # "deviceName": "server-id-001", # "deviceType": "web", # "computerRoomName": "北京机房", # "sn": "sn123456", # "cupPosition": 8, # "remoteIp": "192.168.1.1", # "localIp": "47.24.16.88", # "manageIp": "47.24.16.88", # "des": "", # "os": "linux", # "raid": "", # "type": "", # "doublePower": 1, # "manager": "张三", # "dataState": 1 # } # ] # } if request.method == "POST": form = AssetForm(request.POST) if form.is_valid(): data = form.cleaned_data # 根据运管数据格式打包 post = { "devices": [ data ] } res = Fit2CloudClient(settings.CMDB_CONF, settings.secret_key).ph_device_add( param, json.dumps(post) ) return JsonResponse(res) messages.warning(request, form.errors) return HttpResponseRedirect(url)
def channel_sync(sender, instance, created, *args, **kwargs): """ 渠道-组织同步, itsm --> 云管 :param sender: :param instance: :param created: :param args: :param kwargs: :return: """ if created: post = { "name": instance.name, "description": "sync", } _param = { "time_stamp": int(round(time.time() * 1000)), } _conf = settings.CLOUD_CONF.copy() res = Fit2CloudClient(_conf, settings.cloud_secret_key).org_add( _param, json.dumps(post)) if not res.get("success"): print(res.get("message"))
def asset_del_sync(sender, instance, *args, **kwargs): """ 触发CMDB删除 :param sender: :param instance: :param args: :param kwargs: :return: """ param = { "time_stamp": int(round(time.time() * 1000)), } post = { "deviceName": "ser01", } res = Fit2CloudClient(settings.CMDB_CONF, settings.secret_key).ph_device_delete( param, json.dumps(post) ) print(res) return None
def resource_info(request): param = { "time_stamp": int(round(time.time() * 1000)), "currPage": 1, "pageSize": 500, } _conf = settings.CLOUD_CONF.copy() _conf.pop("user") client = Fit2CloudClient(_conf, settings.cloud_secret_key) res = client.get_instance_list(param) data = res.get("data") instance_list = data.get("items") instance_info = {i["id"]: i for i in instance_list} if request.method == "GET": instance_id = int(request.GET.get("instance_id")) ret = { "hostname": instance_info[instance_id].get("hostname"), "localIp": instance_info[instance_id].get("localIp"), "groupEnvName": instance_info[instance_id].get("groupEnvName"), "instanceType": instance_info[instance_id].get("instanceType"), } return JsonResponse(ret)
def event_create_order(request, pk): url = request.META.get('HTTP_REFERER') try: event = Event.objects.get(id=pk) if event.state == "draft": messages.warning(request, "当前事件未提交") return HttpResponseRedirect(url) if event.technician_id is not request.user.id: messages.warning(request, "您不是当前处理人,无法关闭事件") return HttpResponseRedirect(url) if event.event_type == "request": # 应用对照信息查询 app_name = event.app_name product_info = ProductInfo.objects.filter(app_name=app_name) if not product_info: messages.warning(request, "当前应用名称无法部署,请联系管理员维护应用对照信息") return HttpResponseRedirect(url) # 云管订单创建 param = { "time_stamp": int(round(time.time() * 1000)), } post = { "clusterRoleId": 1, "count": 1, "description": "需要机器配置:1c1g", "expireTime": 4679277169, "installAgent": True, "productId": product_info[0].product_id } # 用户信息查询 _conf = settings.CLOUD_CONF.copy() user_res = Fit2CloudClient(_conf, settings.cloud_secret_key).user_get({ "time_stamp": int(round(time.time() * 1000)) }) if user_res.get("success"): user_data = user_res.get("data") user_info = {i["name"]: i for i in user_data} user_email = user_info[request.user.username]["email"] _conf["user"] = user_email # 工作空间接口请求 ak, sk = Fit2CloudClient( _conf, settings.cloud_secret_key).get_work_space(param) if ak and sk: _param = { "time_stamp": int(round(time.time() * 1000)), } # _conf = settings.CLOUD_CONF.copy() _conf["access_key"] = ak order = Fit2CloudClient(_conf, sk).order_create( _param, json.dumps(post)) logger.info("新生成订单: ", order) event.cloud_order = order.get("data") else: # TODO 故障报修事件关闭逻辑 pass # 执行关闭 event.state = "ended" event.save() return HttpResponseRedirect(url) except Exception as e: messages.warning(request, "事件查询异常: {}".format(e)) return HttpResponseRedirect(url)
def user_sync(sender, instance, created, *args, **kwargs): """ Portal提交用户 --> itsm --> 云管 :param sender: :param instance: :param created: :param args: :param kwargs: :return: """ if created: # 公共参数,实例化 _conf = settings.CLOUD_CONF.copy() _conf.pop("user") # 不传user,查询全部组织 client = Fit2CloudClient(_conf, settings.cloud_secret_key) workspace_name = "{}-{}".format(instance.channel_name, instance.department) # 当前组织信息查询打包 org_res = client.org_get( {"time_stamp": int(round(time.time() * 1000))}) org_id = 0 if org_res.get("success"): org_list = org_res.get("data") org_info = {i.get("name"): i for i in org_list} org_id = org_info[instance.channel_name]["id"] # 2 工作空间sync到云管 post = { "name": workspace_name, "description": "sync", "costCenterId": org_id } workspace_add_res = client.workspace_add( {"time_stamp": int(round(time.time() * 1000))}, json.dumps(post)) else: logger.info("组织信息获取失败") # 3 用户sync到云管 TODO post = { "accessToken": "vstecs.c0m", "email": instance.email, "name": instance.username, "status": "active", "userType": instance.fit_user_type, } user_add_res = Fit2CloudClient( _conf, settings.cloud_secret_key).user_add( {"time_stamp": int(round(time.time() * 1000))}, json.dumps(post)) logger.info("useraddres: ", user_add_res) user_id = 0 if user_add_res.get("success"): user_data = user_add_res.get("data") user_id = user_data["id"] # 4 授权 工作空间id 查询用户id 加上组织ID 角色ID 通过授权接口授权 # 工作空间信息查询 workspace_res = client.get_all_work_space( {"time_stamp": int(round(time.time() * 1000))}) print("wp_res: ", workspace_res) workspace_id = 0 if workspace_res.get("success"): workspace_list = workspace_res.get("data") workspace_info = {i.get("name"): i for i in workspace_list} workspace_id = workspace_info[workspace_name]["id"] # 组织管理员-组织授权 if instance.fit_user_type == 2: # 组织授权,调用组织授权接口 org_co_permission_post = { "userId": user_id, "costCenterId": org_id, } org_co_permission_res = client.org_co_permission( {"time_stamp": int(round(time.time() * 1000))}, json.dumps(org_co_permission_post), ) logger.info("组织授权成功{}".format(org_co_permission_res)) elif instance.fit_user_type == 3: # 普通用户-工作空间授权授权 co_permission_post = { "groupId": workspace_id, "userId": user_id, "groupRoleId": 3, } co_permission_res = client.space_co_permission( {"time_stamp": int(round(time.time() * 1000))}, json.dumps(co_permission_post), ) logger.info("空间授权成功{}".format(co_permission_res)) else: logger.info("用户配置文件未标示云管用户类型,请检查账号")