def org_vdc_list(request_data, op_ctx: ctx.OperationContext): """Request handler for orgvdc list operation. This handler returns a paginated response. :return: Dictionary containing list of org VDC K8s provider metadata :rtype: dict """ # NOTE: Response sent out by this function should be paginated data = req_utils.flatten_request_data(request_data, [RequestKey.QUERY_PARAMS]) defaults = { PaginationKey.PAGE_NUMBER: CSE_PAGINATION_FIRST_PAGE_NUMBER, PaginationKey.PAGE_SIZE: CSE_PAGINATION_DEFAULT_PAGE_SIZE } validated_data = {**defaults, **data} page_number = int(validated_data[PaginationKey.PAGE_NUMBER]) page_size = int(validated_data[PaginationKey.PAGE_SIZE]) # Record telemetry data # TODO: enhance telemetry to record the page number and page size data. cse_params = copy.deepcopy(validated_data) cse_params[PayloadKey. SOURCE_DESCRIPTION] = thread_local_data.get_thread_local_data( ThreadLocalData.USER_AGENT) # noqa: E501 record_user_action_details(cse_operation=CseOperation.OVDC_LIST, cse_params=cse_params) client_v33 = op_ctx.get_client(api_version=DEFAULT_API_VERSION) result = vcd_utils.get_ovdcs_by_page(client_v33, page=page_number, page_size=page_size) org_vdcs = result[PaginationKey.VALUES] result_total = result[PaginationKey.RESULT_TOTAL] next_page_uri = result.get(PaginationKey.NEXT_PAGE_URI) prev_page_uri = result.get(PaginationKey.PREV_PAGE_URI) sysadmin_client_v33 = \ op_ctx.get_sysadmin_client(api_version=DEFAULT_API_VERSION) ovdcs = _get_cse_ovdc_list(sysadmin_client_v33, org_vdcs) api_path = CseServerOperationInfo.ORG_VDC_LIST.api_path_format next_page_uri = vcd_utils.create_cse_page_uri(client_v33, api_path, vcd_uri=next_page_uri) prev_page_uri = vcd_utils.create_cse_page_uri(client_v33, api_path, vcd_uri=prev_page_uri) return server_utils.construct_paginated_response( values=ovdcs, result_total=result_total, page_number=page_number, page_size=page_size, next_page_uri=next_page_uri, prev_page_uri=prev_page_uri)
def ovdc_list(request_data, op_ctx: ctx.OperationContext): """Request handler for ovdc list operation. :return: List of dictionaries with org VDC k8s provider metadata. """ # NOTE: Response sent out by this function should not be # paginated data = req_utils.flatten_request_data( request_data, [RequestKey.QUERY_PARAMS]) # Record telemetry data cse_params = copy.deepcopy(data) record_user_action_details(cse_operation=CseOperation.OVDC_LIST, cse_params=cse_params) client_v33 = op_ctx.get_client(api_version=DEFAULT_API_VERSION) sysadmin_client_v33 = \ op_ctx.get_sysadmin_client(api_version=DEFAULT_API_VERSION) org_vdcs = vcd_utils.get_all_ovdcs(client_v33) return _get_cse_ovdc_list(sysadmin_client_v33, org_vdcs)
def ovdc_list(request_data, op_ctx: ctx.OperationContext): """Request handler for ovdc list operation. :return: List of dictionaries with org VDC k8s provider metadata. :rtype: list """ # NOTE: response sent out by this handler should not be paginated data = req_utils.flatten_request_data( request_data, [RequestKey.QUERY_PARAMS]) defaults = { RequestKey.LIST_PKS_PLANS: False, } validated_data = {**defaults, **data} list_pks_plans = utils.str_to_bool(validated_data[RequestKey.LIST_PKS_PLANS]) # noqa: E501 # Record telemetry data cse_params = copy.deepcopy(validated_data) cse_params[RequestKey.LIST_PKS_PLANS] = list_pks_plans cse_params[PayloadKey.SOURCE_DESCRIPTION] = thread_local_data.get_thread_local_data(ThreadLocalData.USER_AGENT) # noqa: E501 record_user_action_details(cse_operation=CseOperation.OVDC_LIST, cse_params=cse_params) client_v33 = op_ctx.get_client(api_version=DEFAULT_API_VERSION) if list_pks_plans and not client_v33.is_sysadmin(): raise e.UnauthorizedRequestError( 'Operation denied. Enterprise PKS plans visible only ' 'to System Administrators.') ovdcs = [] org_vdcs = vcd_utils.get_all_ovdcs(client_v33) sysadmin_client_v33 = \ op_ctx.get_sysadmin_client(api_version=DEFAULT_API_VERSION) for ovdc in org_vdcs: ovdc_name = ovdc.get('name') org_name = ovdc.get('orgName') ovdc_id = vcd_utils.extract_id(ovdc.get('id')) k8s_metadata = ovdc_utils.get_ovdc_k8s_provider_metadata( sysadmin_client_v33, ovdc_id=ovdc_id, ovdc_name=ovdc_name, org_name=org_name) k8s_provider = k8s_metadata[K8S_PROVIDER_KEY] ovdc_dict = { OvdcInfoKey.OVDC_NAME: ovdc_name, OvdcInfoKey.ORG_NAME: org_name, OvdcInfoKey.K8S_PROVIDER: k8s_provider } if list_pks_plans: pks_plans = '' pks_server = '' if k8s_provider == K8sProvider.PKS: # vc name for vdc can only be found using typed query qfilter = f"name=={urllib.parse.quote(ovdc_name)};" \ f"orgName=={urllib.parse.quote(org_name)}" q = client_v33.get_typed_query( vcd_client.ResourceType.ADMIN_ORG_VDC.value, query_result_format=vcd_client.QueryResultFormat.RECORDS, # noqa: E501 qfilter=qfilter) # should only ever be one element in the generator ovdc_records = list(q.execute()) if len(ovdc_records) == 0: raise vcd_e.EntityNotFoundException( f"Org VDC {ovdc_name} not found in org {org_name}") ovdc_record = None for record in ovdc_records: ovdc_record = pyvcd_utils.to_dict( record, resource_type=vcd_client.ResourceType.ADMIN_ORG_VDC.value) # noqa: E501 break vc_to_pks_plans_map = {} pks_contexts = pksbroker_manager.create_pks_context_for_all_accounts_in_org(op_ctx) # noqa: E501 for pks_context in pks_contexts: if pks_context['vc'] in vc_to_pks_plans_map: continue pks_broker = pksbroker.PksBroker(pks_context, op_ctx) plans = pks_broker.list_plans() plan_names = [plan.get('name') for plan in plans] vc_to_pks_plans_map[pks_context['vc']] = \ [plan_names, pks_context['host']] pks_plan_and_server_info = vc_to_pks_plans_map.get( ovdc_record['vcName'], []) if len(pks_plan_and_server_info) > 0: pks_plans = pks_plan_and_server_info[0] pks_server = pks_plan_and_server_info[1] ovdc_dict[PKSOvdcInfoKey.PKS_API_SERVER] = pks_server ovdc_dict[PKSOvdcInfoKey.AVAILABLE_PKS_PLANS] = pks_plans ovdcs.append(ovdc_dict) return ovdcs
def create_pks_context_for_all_accounts_in_org( op_ctx: ctx.OperationContext): # noqa: E501 """Create PKS context for accounts in a given Org. If user is Sysadmin Creates PKS contexts for all PKS accounts defined in the entire system. else Creates PKS contexts for all PKS accounts assigned to the org. However if separate service accounts for each org hasn't been configured by admin via pks.yaml, then PKS accounts of the PKS server corresponding to the vCenters powering the individual orgVDC of the org will be picked up for creating the PKS contexts. :return: list of dict, where each dictionary is a PKS context :rtype: list """ pks_cache = server_utils.get_pks_cache() if pks_cache is None: return [] client_v33 = op_ctx.get_client(api_version=DEFAULT_API_VERSION) if client_v33.is_sysadmin(): all_pks_account_info = pks_cache.get_all_pks_account_info_in_system() pks_ctx_list = [ ovdc_utils.construct_pks_context(pks_account_info, credentials_required=True) for pks_account_info in all_pks_account_info ] # noqa: E501 return pks_ctx_list if pks_cache.do_orgs_have_exclusive_pks_account(): pks_account_infos = pks_cache.get_exclusive_pks_accounts_info_for_org( op_ctx.user.org_name) # noqa: E501 pks_ctx_list = [ ovdc_utils.construct_pks_context(pks_account_info, credentials_required=True) for pks_account_info in pks_account_infos ] # noqa: E501 return pks_ctx_list org_resource = client_v33.get_org() org = Org(client_v33, resource=org_resource) vdc_names = [vdc['name'] for vdc in org.list_vdcs()] # Constructing dict instead of list to avoid duplicates # TODO() figure out a way to add pks contexts to a set directly pks_ctx_dict = {} sysadmin_client_v33 = \ op_ctx.get_sysadmin_client(api_version=DEFAULT_API_VERSION) for vdc_name in vdc_names: # this is a full blown pks_account_info + pvdc_info + # compute_profile_name dictionary k8s_metadata = ovdc_utils.get_ovdc_k8s_provider_metadata( sysadmin_client_v33, ovdc_name=vdc_name, org_name=op_ctx.user.org_name, include_credentials=True) if k8s_metadata[K8S_PROVIDER_KEY] == K8sProvider.PKS: pks_ctx_dict[k8s_metadata['vc']] = k8s_metadata return list(pks_ctx_dict.values())