Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Improved documentation of Event model fields using description and examples #5475

Open
2 tasks done
xkortex opened this issue Oct 30, 2024 · 2 comments
Open
2 tasks done
Assignees
Labels
feature-request feature request parser Parser (Pydantic) utility

Comments

@xkortex
Copy link

xkortex commented Oct 30, 2024

Use case

It would be convenient if models for well-known types such as EventBridgeModel, which map to well-documented objects in the AWS API, to use Field(description=..., examples =...). This serves as a really useful refresher for acceptable values, and adds this metadata to .model_schema_json() (useful for swagger docs, model factories and the like). To show you what I mean, I whipped up this class really quick before realizing ALP had a EventBridgeModel class already.

BaseModel classes with description and examples
class AWSEvent(BaseModel):
    model_config = ConfigDict(extra='allow', alias_generator=snake_to_kebab)

    # version: str # Currently 0 (zero) for all events, there is no need to allocate it at this time.
    id: str = Field(
        description="A Version 4 UUID generated for every event.",
        examples=['17793124-05d4-b198-2fde-7ededc63b103'],
    )
    account: str = Field(
        description="The 12-digit AWS account ID of the owner of the service emitting the event.",
        examples=['111122223333'],
    )
    detail_type: str = Field(
        alias='detail-type',
        serialization_alias='detail-type',
        description="Identifies the type of event",
        examples=['Scheduled Event', 'Object Created'],
    )
    source: str = Field(
        description="Identifies the service that generated the event",
        examples=['aws.s3', 'aws.events'],
    )
    time: datetime = Field(description="The time the event occurred.", examples=['2006-01-02T15:04:05Z'])
    region: str = Field(description="Identifies the AWS Region the event originated.", examples=['us-east-1'])
    resources: list[str] = Field(
        description="A JSON array that contains the Amazon Resource Name (ARN) "
        "of service(s) that generated the event",
        examples=['arn:aws:s3:::amzn-s3-demo-bucket1', 'arn:aws:events:us-east-1:123456789012:rule/SampleRule'],
    )

    detail: dict[str, Nested] = Field(
        description="A JSON object that contains information about the event. "
        "The service generating the event determines the content of this field."
    )


class S3ObjectEvent(AWSEvent):
    detail_type: str = Field(
        alias='detail-type',
        pattern='Object .+',
        examples=[
            'Object Created',
            'Object Deleted',
            'Object Restore Initiated',
            'Object Restore Completed',
            'Object Restore Expired',
            'Object Tags Added',
            'Object Tags Deleted',
            'Object ACL Updated',
            'Object Storage Class Changed',
            'Object Access Tier Changed',
        ],
    )
    source: Literal['aws.s3']


class ScheduledEvent(AWSEvent):
    detail_type: str = Field(alias='detail-type', pattern='Scheduled Event', examples=['Scheduled Event'])
    source: Literal['aws.events']
I mostly pulled these right from AWS documentation pages [like this](https://docs.aws.amazon.com/AmazonS3/latest/userguide/ev-events.html). It might even be able to be autogenerated from botocore, not sure about that rabbit hole.

See also
#5476

Solution/User Experience

Basically the example above but adapted for how you have EventBridgeModel and its children structured.

Alternative solutions

No response

Acknowledgment

@xkortex xkortex added feature-request feature request triage Pending triage from maintainers labels Oct 30, 2024
Copy link

boring-cyborg bot commented Oct 30, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hi @xkortex! This is a really good idea to improve our models with better documentation. We probably can't do this for all our models because some events are very generic, but EventBridge is definitely one of them. Let's start with EventBridge and evaluate others?

Just a note: we can change the models to use Fields, but we can't change the model name or the field name, ok?

If you are open to submitting a PR, please do so.

@leandrodamascena leandrodamascena moved this from Triage to Pending customer in Powertools for AWS Lambda (Python) Nov 8, 2024
@leandrodamascena leandrodamascena added parser Parser (Pydantic) utility and removed triage Pending triage from maintainers labels Nov 8, 2024
@leandrodamascena leandrodamascena self-assigned this Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request parser Parser (Pydantic) utility
Projects
Status: Pending customer
Development

No branches or pull requests

2 participants