Azure / partnercenter-cli-extension

Partner Center Azure CLI Extension
MIT License
12 stars 11 forks source link

Clean up the ```._resource``` field in the models #187

Open kevinhillinger opened 1 year ago

kevinhillinger commented 1 year ago

@dciborow submitted PR for azure/partnercenter-cli-extension#186 noting the issue with the syntax and that it violated linting rules.

Link to comment thread

We need to continue to support out of the box behavior that the CLI gives us inheriting from msrest.serialization.Model while having a pythonic and clean way for it to ignore the field when serializing out from the CLI.

dciborow commented 1 year ago

Option 1: Rename _resource -> resource

class Offer(Model):
    _attribute_map = {
        'id': {'key': 'id', 'type': 'str'},
        'alias': {'key': 'alias', 'type': 'str'},
        'type': {'key': 'type', 'type': 'str'}
    }

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.id = kwargs.get('id', None)
        self.alias = kwargs.get('alias', None)
        self.type = kwargs.get('type', None)
        self._resource = kwargs.get('resource', None)

Usage: offer_resource_id = offer.resource.durable_id

But, then we should consider if the field should be adjusted the same way in 'Plan' and 'PlanListing'.

Option 2: Add method to retrieve durable_id

class Offer(Model):
    _attribute_map = {
        'id': {'key': 'id', 'type': 'str'},
        'alias': {'key': 'alias', 'type': 'str'},
        'type': {'key': 'type', 'type': 'str'}
    }

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.id = kwargs.get('id', None)
        self.alias = kwargs.get('alias', None)
        self.type = kwargs.get('type', None)
        self._resource = kwargs.get('resource', None)

    def durable_id(self):
        return self._resource.durable_id

Usage: offer_resource_id = offer.durable_id

This PR in my fork demos what this change would look like - https://github.com/dciborow/partnercenter-cli-extension/pull/15/files

(this is more pythonic then making a function def resource(self): return self._resource)