def get(self, request, **kwargs): """Respond to HTTP GET requests. Args: request (Request) HTTP Request object - request.query_params (dict) - type (str) - the name of the feature; feature type - beta (bool) - feature flag; this signals that this is a pre-release feature. kwargs (dict) optional keyword args """ query_params = request.query_params user_access = request.user.access LOG.debug( f"User Access RBAC permissions: {str(user_access)}. Org Admin: {str(request.user.admin)}" ) admin_user = request.user.admin LOG.debug(f"User Access admin user: {str(admin_user)}") # only show pre-release features in approved environments flag = query_params.get( "beta", "False") # query_params are strings, not bools. if flag.lower() == "true" and not settings.ENABLE_PRERELEASE_FEATURES: return Response({"data": False}) source_type = query_params.get("type") if source_type: source_accessor = next( (item for item in self._source_types if item.get("type") == source_type.lower()), False) if source_accessor: access_class = source_accessor.get("access_class") if admin_user: access_granted = True else: access_granted = access_class(user_access).access return Response({"data": access_granted}) else: return Response({f"Unknown source type: {source_type}"}, status=status.HTTP_400_BAD_REQUEST) data = [] for source_type in self._source_types: access_granted = False if admin_user: access_granted = True else: access_granted = source_type.get("access_class")( user_access).access data.append({ "type": source_type.get("type"), "access": access_granted }) paginator = ListPaginator(data, request) return paginator.get_paginated_response(data)
def get(self, request, *args, **kwargs): """Gets a list of users current settings.""" if not kwargs: user_settings = get_account_settings(request) else: setting = kwargs["setting"] self.validate_setting(setting) user_settings = self.get_account_setting(request, setting).data user_settings = UserSettingSerializer(user_settings, many=False).data paginated = ListPaginator(user_settings, request) return paginated.get_paginated_response(user_settings["settings"])
def cloud_accounts(request): """View for cloud accounts.""" serializer = QueryParamsSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) cloud_accounts_copy = copy.deepcopy(CLOUD_ACCOUNTS) paginator = ListPaginator(cloud_accounts_copy, request) return paginator.paginated_response
def metrics(request): """Provide the openapi information.""" source_type = request.query_params.get("source_type") serializer = QueryParamsSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) cost_model_metric_map_copy = copy.deepcopy( metric_constants.COST_MODEL_METRIC_MAP) if source_type: # Filter on source type cost_model_metric_map_copy = list( filter(lambda x: x.get("source_type") == source_type, cost_model_metric_map_copy)) # Convert source_type to human readable. for metric_map in cost_model_metric_map_copy: mapped_source_type = metric_map.get("source_type") readable_source_type = metric_constants.SOURCE_TYPE_MAP.get( mapped_source_type) if not (mapped_source_type and readable_source_type): raise CostModelMetricMapJSONException( "SOURCE_TYPE_MAP or COST_MODEL_METRIC_MAP is missing a source_type." ) metric_map["source_type"] = readable_source_type data = cost_model_metric_map_copy paginator = ListPaginator(data, request) return paginator.paginated_response
def get_currency(request): """Get Currency Data. This method is responsible for passing request data to the reporting APIs. Args: request (Request): The HTTP request object Returns: (Response): The report in a Response object """ return ListPaginator(CURRENCIES, request).paginated_response
def get_currency(request): """Get Currency Data. This method is responsible for passing request data to the reporting APIs. Args: request (Request): The HTTP request object Returns: (Response): The report in a Response object """ try: data = load_currencies(CURRENCY_FILE_NAME) paginator = ListPaginator(data, request) return paginator.paginated_response except (FileNotFoundError, json.JSONDecodeError): return Response(status=status.HTTP_404_NOT_FOUND)
def get(self, request): """Gets a list for all supported cost_typs currently available.""" return ListPaginator(COST_TYPES, request).paginated_response
def get(self, request, **kwargs): query_params = request.query_params user_access = request.user.access LOG.debug( f"User Access RBAC permissions: {str(user_access)}. Org Admin: {str(request.user.admin)}" ) admin_user = request.user.admin LOG.debug(f"User Access admin user: {str(admin_user)}") source_types = [ { "type": "aws", "access_class": AWSUserAccess }, { "type": "ocp", "access_class": OCPUserAccess }, { "type": "gcp", "access_class": GCPUserAccess }, { "type": "azure", "access_class": AzureUserAccess }, { "type": "cost_model", "access_class": CostModelUserAccess }, ] source_type = query_params.get("type") if source_type: source_accessor = next( (item for item in source_types if item.get("type") == source_type.lower()), False) if source_accessor: access_class = source_accessor.get("access_class") if admin_user: access_granted = True else: access_granted = access_class(user_access).access return Response({"data": access_granted}) else: return Response({f"Unknown source type: {source_type}"}, status=status.HTTP_400_BAD_REQUEST) data = [] for source_type in source_types: access_granted = False if admin_user: access_granted = True else: access_granted = source_type.get("access_class")( user_access).access data.append({ "type": source_type.get("type"), "access": access_granted }) paginator = ListPaginator(data, request) return paginator.get_paginated_response(data)
def get(self, request, **kwargs): tenant = get_tenant(request.user) with tenant_context(tenant): aws_account_count = AWSCostSummaryByAccountP.objects.values("usage_account_id").distinct().count() gcp_account_count = GCPCostSummaryByAccountP.objects.values("account_id").distinct().count() gcp_project_count = GCPCostSummaryByProjectP.objects.values("project_id").distinct().count() aws_org_unit_count = ( AWSOrganizationalUnit.objects.filter(deleted_timestamp__isnull=True) .values("org_unit_id") .distinct() .count() ) azure_sub_guid_count = AzureCostSummaryByAccountP.objects.values("subscription_guid").distinct().count() ocp_cluster_count = OCPCostSummaryP.objects.values("cluster_id").distinct().count() ocp_node_count = OCPCostSummaryByNodeP.objects.values("node").distinct().count() ocp_project_count = OCPCostSummaryByProjectP.objects.values("namespace").distinct().count() cost_model_count = CostModel.objects.count() aws_account_dict = { "value": "aws.account", "path": "/api/cost-management/v1/resource-types/aws-accounts/", "count": aws_account_count, } aws_org_unit_dict = { "value": "aws.organizational_unit", "path": "/api/cost-management/v1/resource-types/aws-organizational-units/", "count": aws_org_unit_count, } azure_sub_guid_dict = { "value": "azure.subscription_guid", "path": "/api/cost-management/v1/resource-types/azure-subscription-guids/", "count": azure_sub_guid_count, } ocp_cluster_dict = { "value": "openshift.cluster", "path": "/api/cost-management/v1/resource-types/openshift-clusters/", "count": ocp_cluster_count, } ocp_node_dict = { "value": "openshift.node", "path": "/api/cost-management/v1/resource-types/openshift-nodes/", "count": ocp_node_count, } ocp_project_dict = { "value": "openshift.project", "path": "/api/cost-management/v1/resource-types/openshift-projects/", "count": ocp_project_count, } gcp_account_dict = { "value": "gcp.account", "path": "/api/cost-management/v1/resource-types/gcp-accounts/", "count": gcp_account_count, } gcp_project_dict = { "value": "gcp.projects", "path": "/api/cost-management/v1/resource-types/gcp-projects/", "count": gcp_project_count, } cost_model_dict = { "value": "cost_model", "path": "/api/cost-management/v1/resource-types/cost-models/", "count": cost_model_count, } data = [ aws_account_dict, aws_org_unit_dict, azure_sub_guid_dict, ocp_cluster_dict, ocp_node_dict, ocp_project_dict, gcp_account_dict, gcp_project_dict, cost_model_dict, ] paginator = ListPaginator(data, request) return paginator.get_paginated_response(data)