cloudtools / troposphere

troposphere - Python library to create AWS CloudFormation descriptions
BSD 2-Clause "Simplified" License
4.93k stars 1.45k forks source link

Validate objects when initialized #2032

Open markpeek opened 2 years ago

markpeek commented 2 years ago

Opening for @MattJaccino based on questions in another issue.

Not specific to WAFv2, but one thing that did strike me as odd was that resource objects can be successfully initialized without all of the required properties being given. I thought I remembered it erroring out in situations like that, or am I missing something. ?

I thought with a property defined as

class SomeProperty(AWSProperty):
    props: PropsDictType = {
        "Prop1": (TYPE, True),
        "Prop2": (TYPE, False),
    }

if you tried to initialize without defining Prop1, it would throw some kind of exception. It seems like you can initialize resource objects without including all of the required properties, or with none at all.

Was this always the case, or did something change? If not, could that potentially be a feature request?

markpeek commented 2 years ago

This has always been the case due to the initial design of troposphere allowing for a more getter/setter interface:

>>> instance = ec2.Instance("myinstance")
>>> instance.ImageId = "ami-951945d0"
>>> instance.InstanceType = "t1.micro"
>>> t.add_resource(instance)

But this does bring up the point on whether validation could be done at object creation or when added to a template.