@@ -523,3 +523,30 @@ def series_selector(series_arg='', charm_url=None, model_config=None, supported_
523523 # Charm hasn't specified a default (likely due to being a local charm
524524 # deployed by path). Last chance, best we can do is default to LTS.
525525 return DEFAULT_SUPPORTED_LTS
526+
527+
528+ def should_upgrade_resource (available_resource , existing_resources ):
529+ """Called in the context of upgrade_charm. Given a resource R, takes a look at the resources we
530+ already have and decides if we need to refresh R.
531+
532+ :param dict[str] available_resource: The dict representing the client.Resource coming from the
533+ charmhub api. We're considering if we need to refresh this during upgrade_charm.
534+ :param dict[str] existing_resources: The dict coming from resources_facade.ListResources
535+ representing the resources of the currently deployed charm.
536+
537+ :result bool: The decision to refresh the given resource
538+ """
539+ # should upgrade resource?
540+ res_name = available_resource .get ('Name' , available_resource .get ('name' ))
541+ # no, if it's upload
542+ if existing_resources [res_name ].origin == 'upload' :
543+ return False
544+
545+ # no, if we already have it (and upstream doesn't have a newer res available)
546+ if res_name in existing_resources :
547+ available_rev = available_resource .get ('Revision' , available_resource .get ('revision' , - 1 ))
548+ u_fields = existing_resources [res_name ].unknown_fields
549+ existing_rev = u_fields .get ('Revision' , u_fields .get ('revision' , - 1 ))
550+ if existing_rev >= available_rev :
551+ return False
552+ return True
0 commit comments