resource_obj_opt.py 10.2 KB
Newer Older
qunfeng qiu's avatar
qunfeng qiu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Descripttion: 资源对象操作
@Author: guohb65
@Email: guohb65@chinaunicom.cn
@Date: 2019/12/13 9:18
@LastEditors  : guohb65
@LastEditTime : 2019-12-20 14:51:26
'''
import json

from cucc_common_pkg.util_pkg.common_func import CommonFunc
from cucc_common_pkg.util_pkg.const import ConstGen, RequestMethod, K8SRestStr


class ResourceObjOpt(object):

    @staticmethod
    def object_create(api_server, api_url, namespace, obj_type, body_data, token=None, ca_crt_data=None,
                      client_crt_data=None, client_key_data=None):
        """
        对象创建
        :param api_server:
        :param api_url:
        :param namespace:
        :param obj_type:
        :param body_data:
        :param token:
        :param ca_crt_data:
        :param client_crt_data:
        :param client_key_data:
        :return:
        """
        request_url = api_server + api_url + K8SRestStr.NAMESPACES
        if namespace is None and obj_type is K8SRestStr.NAMESPACES and body_data is not None:
            pass
        elif namespace is not None and obj_type is not None and body_data is not None:
            request_url += ConstGen.BACKSLASH + namespace + ConstGen.BACKSLASH + obj_type
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")
        headers = {K8SRestStr.CONTENT_TYPE: K8SRestStr.APPLICATION_JSON}
        params = {ConstGen.DATA_STR: json.dumps(
            body_data), K8SRestStr.HEADERS: headers}
        return CommonFunc().pre_exec_rest_api(RequestMethod.POST.value, request_url, params, token,
                                              ca_crt_data, client_crt_data, client_key_data)

    @staticmethod
    def object_create_dryrun(api_server, api_url, namespace, obj_type, body_data, token=None, ca_crt_data=None,
                             client_crt_data=None, client_key_data=None):
        """
        对象创建
        :param api_server:
        :param api_url:
        :param namespace:
        :param obj_type:
        :param body_data:
        :param token:
        :param ca_crt_data:
        :param client_crt_data:
        :param client_key_data:
        :return:
        """
        request_url = api_server + api_url + K8SRestStr.NAMESPACES
        if namespace is None and obj_type is K8SRestStr.NAMESPACES and body_data is not None:
            pass
        elif namespace is not None and obj_type is not None and body_data is not None:
            request_url += ConstGen.BACKSLASH + namespace + ConstGen.BACKSLASH + obj_type
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")
        headers = {K8SRestStr.CONTENT_TYPE: K8SRestStr.APPLICATION_JSON}
        param_dict = {"dryRun": "All"}
        params = {ConstGen.DATA_STR: json.dumps(body_data), K8SRestStr.HEADERS: headers,
                  ConstGen.PARAMS_STR: param_dict}
        return CommonFunc().pre_exec_rest_api(RequestMethod.POST.value, request_url, params, token,
                                              ca_crt_data, client_crt_data, client_key_data)

    @staticmethod
    def object_patch_dryrun(api_server, api_url, namespace, obj_name, obj_type, body_data, token=None, ca_crt_data=None,
                            client_crt_data=None, client_key_data=None):
        """
        对象创建
        :param obj_name:
        :param api_server:
        :param api_url:
        :param namespace:
        :param obj_type:
        :param body_data:
        :param token:
        :param ca_crt_data:
        :param client_crt_data:
        :param client_key_data:
        :return:
        """
        request_url = api_server + api_url + K8SRestStr.NAMESPACES
        if namespace is None and obj_type is K8SRestStr.NAMESPACES and body_data is not None:
            pass
        elif namespace is not None and obj_type is not None and body_data is not None:
            request_url += ConstGen.BACKSLASH + namespace + ConstGen.BACKSLASH + obj_type + ConstGen.BACKSLASH + obj_name
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")
        headers = {K8SRestStr.CONTENT_TYPE: K8SRestStr.MERGE_PATCH_JSON}
        param_dict = {"dryRun": "All"}
        params = {ConstGen.DATA_STR: json.dumps(body_data), K8SRestStr.HEADERS: headers,
                  ConstGen.PARAMS_STR: param_dict}
        return CommonFunc().pre_exec_rest_api(RequestMethod.PATCH.value, request_url, params, token,
                                              ca_crt_data, client_crt_data, client_key_data)

    @staticmethod
    def object_alter(api_server, api_url, namespace, obj_type, obj_name, body_data, token=None, ca_crt_data=None,
                     client_crt_data=None, client_key_data=None):
        """
        对象更新
        :param api_server:
        :param api_url:
        :param namespace:
        :param obj_type:
        :param body_data:
        :param token:
        :param ca_crt_data:
        :param client_crt_data:
        :param client_key_data:
        :return:
        """
        request_url = api_server + api_url + K8SRestStr.NAMESPACES
        if namespace is None and obj_type is K8SRestStr.NAMESPACES and body_data is not None:
            pass
        elif namespace is not None and obj_type is not None and body_data is not None:
            request_url += ConstGen.BACKSLASH + namespace + ConstGen.BACKSLASH + obj_type + ConstGen.BACKSLASH + obj_name
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")
        if obj_type is K8SRestStr.DEPLOYMENTS:
            headers = {K8SRestStr.CONTENT_TYPE: K8SRestStr.STRATEGIC_MERGE_PATCH_JSON}
        else:
            headers = {K8SRestStr.CONTENT_TYPE: K8SRestStr.MERGE_PATCH_JSON}
        params = {ConstGen.DATA_STR: json.dumps(body_data), K8SRestStr.HEADERS: headers}
        return CommonFunc().pre_exec_rest_api(RequestMethod.PATCH.value, request_url, params, token,
                                              ca_crt_data, client_crt_data, client_key_data)

    @staticmethod
    def object_opt(api_server, api_url, opt_type, namespace, obj_type, obj_name, body_data, token=None,
                   ca_crt_data=None, client_crt_data=None, client_key_data=None):
        """
        对象操作(包含查询、更新和删除)
        :param api_server:
        :param api_url:
        :param opt_type:
        :param namespace:
        :param obj_type:
        :param obj_name:
        :param body_data:
        :param token:
        :param ca_crt_data:
        :param client_crt_data:
        :param client_key_data:
        :return:
        """
        request_url = api_server + api_url
        if opt_type is not None and obj_name is not None:
            if opt_type is RequestMethod.PUT.value and body_data is not None:
                params = {ConstGen.DATA_STR: json.dumps(body_data)}
            elif opt_type is RequestMethod.PATCH.value and body_data is not None:
                headers = {
                    K8SRestStr.CONTENT_TYPE: K8SRestStr.PATCH_CONTENT_TYPE_STR}
                params = {ConstGen.DATA_STR: json.dumps(
                    body_data), K8SRestStr.HEADERS: headers}
            elif opt_type is RequestMethod.DELETE.value or opt_type is RequestMethod.GET.value:
                params = None
            else:
                return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "方法不支持")
            if namespace is None:
                request_url += obj_type + ConstGen.BACKSLASH + obj_name
            elif namespace is not None and obj_type is not None:
                request_url += K8SRestStr.NAMESPACES + ConstGen.BACKSLASH + namespace + ConstGen.BACKSLASH + obj_type \
                               + ConstGen.BACKSLASH + obj_name
            return CommonFunc().pre_exec_rest_api(opt_type, request_url, params, token,
                                                  ca_crt_data, client_crt_data, client_key_data)
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")

    @staticmethod
    def object_merge_patch(api_server, api_url, namespace, obj_type, obj_name, body_data, token=None,
                           ca_crt_data=None, client_crt_data=None, client_key_data=None):
        """
        对象操作(包含查询、更新和删除)
        :param api_server:
        :param api_url:
        :param opt_type:
        :param namespace:
        :param obj_type:
        :param obj_name:
        :param body_data:
        :param token:
        :param ca_crt_data:
        :param client_crt_data:
        :param client_key_data:
        :return:
        """
        request_url = api_server + api_url
        if obj_name is not None:
            headers = {K8SRestStr.CONTENT_TYPE: K8SRestStr.MERGE_PATCH_JSON}
            params = {ConstGen.DATA_STR: json.dumps(body_data), K8SRestStr.HEADERS: headers}
            if namespace is None:
                request_url += obj_type + ConstGen.BACKSLASH + obj_name
            elif namespace is not None and obj_type is not None:
                request_url += K8SRestStr.NAMESPACES + ConstGen.BACKSLASH + namespace + ConstGen.BACKSLASH + obj_type \
                               + ConstGen.BACKSLASH + obj_name
            return CommonFunc().pre_exec_rest_api(RequestMethod.PATCH.value, request_url, params, token,
                                                  ca_crt_data, client_crt_data, client_key_data)
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")

    @staticmethod
    def object_list(api_server, api_url, namespace, obj_type, token=None, params=None, ca_crt_data=None,
                    client_crt_data=None, client_key_data=None):
        request_url = api_server + api_url
        if namespace is None and obj_type is not None:
            request_url += obj_type
        elif namespace is not None and obj_type is not None:
            request_url += K8SRestStr.NAMESPACES + ConstGen.BACKSLASH + \
                           namespace + ConstGen.BACKSLASH + obj_type
        else:
            return CommonFunc.gen_inner_resp_data(ConstGen.FAIL_CODE, "缺少参数")
        return CommonFunc().pre_exec_rest_api(RequestMethod.GET.value, request_url, params, token,
                                              ca_crt_data, client_crt_data, client_key_data)