Budget
Smarter API Budget Manifest handler.
A Budget is managed by superusers: only they may apply or delete one. Anyone may get and describe the budgets that are attached to a resource that they may see.
- class smarter.apps.account.manifest.brokers.budget.SAMBudgetBroker(*args, **kwargs)[source]
Bases:
AbstractBroker[Budget,Budget]Broker for
SAMBudget manifests.The broker converts between Budget manifests and the
BudgetDjango ORM model and itsResourceConstraintattachments, and implements thesmarterCLI commands for Budgets:apply,describe,get,deleteandexample_manifest.spec.resourcesis declarative: applying a manifest attaches the budget to the resources that it lists, and detaches it from the others.- property SerializerClass: Type[ModelSerializer]
The Django ORM model serializer class for the Budget.
- __init__(*args, **kwargs)[source]
Initialize the broker from a manifest, a manifest file, or the Django ORM.
A manifest takes precedence over a file path. With neither, the broker is initialized from the ORM instance named
nameand owned by the user profile, if there is one.- Parameters:
request – The request, or None. It may also be passed positionally in
args.args – Positional arguments, among which the request, user, account, user profile and manifest are found by their types.
name – The resource’s name.
kind – The resource’s SAM kind, e.g.
Guardrail.api_version – The manifest’s api version.
manifest – The manifest, as a Pydantic model or as a dict, e.g. from
parse_manifest().file_path – The path of a manifest file, read when no manifest is passed.
url – Accepted, for compatibility, but not used.
kwargs –
user,accountanduser_profile, and the rest, which are passed on toSmarterRequestMixin.
- Raises:
SmarterValueError – If the kind, name or manifest is invalid.
SAMBrokerError – If the manifest’s kind is not the broker’s kind.
- apply(request, *args, **kwargs)[source]
Create or update the Budget from the manifest, then attach it to spec.resources, and detach it from the others.
Only superusers may apply a Budget.
- Return type:
- property budget: Budget | None
The Budget with the broker’s name, if it exists.
It is never created here:
apply()does that.
- delete(request, *args, **kwargs)[source]
Delete the Budget, which detaches it from its resources and removes their locks.
Superusers only.
- Return type:
- dependencies()[source]
Return brokers for the resources that depend on this Budget.
No resource refers to a Budget. A Budget refers to the resources that it constrains, and deleting it detaches it from them.
- Returns:
An empty list.
- Return type:
- classmethod dependents(instance)[source]
Return the resources that depend on a Budget.
No resource refers to a Budget. A Budget refers to the resources that it constrains, and deleting it detaches it from them.
- deploy(request, *args, **kwargs)[source]
Refuse to deploy: a Budget is not deployable.
- Parameters:
request (
HttpRequest) – The request.- Raises:
SAMBrokerErrorNotImplemented – always.
- Return type:
- describe(request, *args, **kwargs)[source]
Return the Budget as a manifest, with the budget versus the actual spending of each resource.
- Return type:
- example_manifest(request, *args, **kwargs)[source]
Return an example Budget manifest: a monthly allowance for a student.
- Return type:
- get(request, *args, **kwargs)[source]
Return the Budgets that the user may see, optionally filtered by name.
- Return type:
- is_visible_budget(budget)[source]
Superusers see every budget.
Others see those attached to a resource that they may see.
- Return type:
- logs(request, *args, **kwargs)[source]
Return the Budget’s logs.
A Budget has none.
- Parameters:
request (
HttpRequest) – The request.- Return type:
- Returns:
A successful response with empty data.
- property manifest: SAMBudget | None
The Budget manifest, as a Pydantic model, from the client’s manifest data.
- property orm_meta_instance: Budget | None
The Budget.
Budgets are not owned, so the base class’s lookup by owner does not apply.
- prompt(request, *args, **kwargs)[source]
Refuse to prompt: a Budget is not an LLM resource.
- Parameters:
request (
HttpRequest) – The request.- Raises:
SAMBrokerErrorNotImplemented – always.
- Return type:
- classmethod references(instance)[source]
Return the resources that a Budget uses: none.
A Budget limits the resources of its
spec.resources, but does not use them.
- resolve_account(account_number)[source]
Return the Account of a manifest’s
accountNumber.- Parameters:
account_number (
str|None) – The account number, or None for the broker’s own Account.- Return type:
- Returns:
The Account.
- Raises:
SAMBudgetBrokerError – when no account number is given and the broker has no Account, or when no Account has the account number.
- status_for(budget)[source]
The budget versus the actual spending of each resource that the user may see.
- undeploy(request, *args, **kwargs)[source]
Refuse to undeploy: a Budget is not deployable.
- Parameters:
request (
HttpRequest) – The request.- Raises:
SAMBrokerErrorNotImplemented – always.
- Return type:
- exception smarter.apps.account.manifest.brokers.budget.SAMBudgetBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]
Bases:
SAMBrokerErrorBase exception for Smarter API Budget Broker handling.
- property get_formatted_err_message
The label of this error, used in its string form.