kubernetes-sigs / cluster-api-provider-openstack

Cluster API implementation for OpenStack
https://cluster-api-openstack.sigs.k8s.io/
Apache License 2.0
289 stars 253 forks source link

Separate API filter parameters into ID and Filter to improve discovery and validation #1975

Closed mdbooth closed 6 months ago

mdbooth commented 6 months ago

This applies to all our filter types:

Taking RouterFilter as an example, we have:

type RouterFilter struct {
    ID          string `json:"id,omitempty"`
    Name        string `json:"name,omitempty"`
    Description string `json:"description,omitempty"`
    ProjectID   string `json:"projectID,omitempty"`

    FilterByNeutronTags `json:",inline"`
}

This struct encode 2 different behaviours:

We would like to make this behaviour both obvious to users, and impossible to misuse. To achieve this, we turn the Filter struct into a Param struct which has an ID field and a Filter field. The Filter field no longer includes an ID. Taking the RouterFilter example again, this becomes:

type RouterParam struct {
    ID          optional.String `json:"id,omitempty"`
    Filter     *RouterFilter   `json:"filter,omitempty"`
}

type RouterFilter struct {
    Name        string `json:"name,omitempty"`
    Description string `json:"description,omitempty"`
    ProjectID   string `json:"projectID,omitempty"`

    FilterByNeutronTags `json:",inline"`
}

We now use RouterParam everywhere we formerly used RouterFilter. We also add API validation which ensures that exactly one of ID or Filter must be set so the user will get an immediate admission failure if they attempt to specify an invalid filter.

/type feature