def system_update(request_data, op_ctx: ctx.OperationContext): """Request handler for system update operation. :return: Dictionary with system update status. """ required = [RequestKey.SERVER_ACTION] req_utils.validate_payload(request_data, required) # Telemetry data preparation server_action = request_data.get(RequestKey.SERVER_ACTION) cse_operation = server_action or 'invalid server action' if server_action == 'enable': cse_operation = CseOperation.SYSTEM_ENABLE elif server_action == 'disable': cse_operation = CseOperation.SYSTEM_DISABLE elif server_action == 'stop': cse_operation = CseOperation.SYSTEM_STOP status = OperationStatus.FAILED if op_ctx.client.is_sysadmin: # circular dependency between request_processor.py and service.py import container_service_extension.server.service as service try: result = service.Service().update_status( request_data.get(RequestKey.SERVER_ACTION)) status = OperationStatus.SUCCESS return result finally: record_user_action(cse_operation=cse_operation, status=status) record_user_action(cse_operation=cse_operation, status=status) raise e.UnauthorizedRequestError( error_message='Unauthorized to update CSE')
def reload_templates(request_data, op_ctx): """.""" user_context = op_ctx.get_user_context(api_version=None) user_client = user_context.client if not user_client.is_sysadmin: raise e.UnauthorizedRequestError( error_message= 'Unauthorized to reload CSE native and TKG templates.' # noqa: E501 ) org = vcd_utils.get_org(user_client, user_context.org_name) user_href = org.get_user(user_context.name).get('href') task = Task(user_client) task_resource = task.update( status=TaskStatus.RUNNING.value, namespace='vcloud.cse', operation="Reloading native templates.", operation_name='template operation', details='', progress=None, owner_href=user_context.org_href, owner_name=user_context.org_name, owner_type='application/vnd.vmware.vcloud.org+xml', user_href=user_href, user_name=user_context.name, org_href=user_context.org_href) task_href = task_resource.get('href') op_ctx.is_async = True _reload_templates_async(op_ctx, task_href) return {"task_href": task_href}
def get_server_config(request_data, op_ctx: ctx.OperationContext): """.""" if op_ctx.client.is_sysadmin: # TODO: Find a better way to access to the config dict # in ServerConfig object server_config = deepcopy( server_utils.get_server_runtime_config()._config) server_config['mqtt']['token'] = "REDACTED" server_config['mqtt']['token_id'] = "REDACTED" for vc in server_config.get('vcs', []): vc['password'] = "******" server_config['vcd']['password'] = "******" rde_version = server_config['service']['rde_version_in_use'] rde_version_str = f"{rde_version.major}.{rde_version.minor}.{rde_version.patch}" # noqa: E501 server_config['service']['rde_version_in_use'] = rde_version_str return server_config raise e.UnauthorizedRequestError( error_message='Unauthorized to access CSE server configuration.')
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 org_vdc_list(request_data, op_ctx: ctx.OperationContext): """Request handler for ovdc list operation. :return: dictionary containing list of Org VDCs :rtype: dict """ # NOTE: Response sent out by this handler should be paginated data = req_utils.flatten_request_data(request_data, [RequestKey.QUERY_PARAMS]) defaults = { RequestKey.LIST_PKS_PLANS: False, 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]) list_pks_plans = utils.str_to_bool( validated_data[RequestKey.LIST_PKS_PLANS]) # noqa: E501 # Record telemetry data # TODO: enhance telemetry to record the page number and page size 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) if list_pks_plans and not op_ctx.client.is_sysadmin(): raise e.UnauthorizedRequestError( 'Operation denied. Enterprise PKS plans visible only ' 'to System Administrators.') ovdcs = [] result = \ vcd_utils.get_ovdcs_by_page(op_ctx.client, 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) 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( op_ctx.sysadmin_client, 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 = op_ctx.client.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) api_path = CseServerOperationInfo.PKS_ORG_VDC_LIST.api_path_format next_page_uri = vcd_utils.create_cse_page_uri(op_ctx.client, api_path, vcd_uri=next_page_uri) prev_page_uri = vcd_utils.create_cse_page_uri(op_ctx.client, 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, # noqa: E501 prev_page_uri=prev_page_uri) # noqa: E501