@@ -389,3 +389,30 @@ def base_channel_to_series(channel):
389389 :return: str series (e.g. focal)
390390 """
391391 return get_version_series (origin .Channel .parse (channel ).track )
392+
393+
394+ def should_upgrade_resource (available_resource , existing_resources ):
395+ """Called in the context of upgrade_charm. Given a resource R, takes a look at the resources we
396+ already have and decides if we need to refresh R.
397+
398+ :param dict[str] available_resource: The dict representing the client.Resource coming from the
399+ charmhub api. We're considering if we need to refresh this during upgrade_charm.
400+ :param dict[str] existing_resources: The dict coming from resources_facade.ListResources
401+ representing the resources of the currently deployed charm.
402+
403+ :result bool: The decision to refresh the given resource
404+ """
405+ # should upgrade resource?
406+ res_name = available_resource .get ('Name' , available_resource .get ('name' ))
407+ # no, if it's upload
408+ if existing_resources [res_name ].origin == 'upload' :
409+ return False
410+
411+ # no, if we already have it (and upstream doesn't have a newer res available)
412+ if res_name in existing_resources :
413+ available_rev = available_resource .get ('Revision' , available_resource .get ('revision' , - 1 ))
414+ u_fields = existing_resources [res_name ].unknown_fields
415+ existing_rev = u_fields .get ('Revision' , u_fields .get ('revision' , - 1 ))
416+ if existing_rev >= available_rev :
417+ return False
418+ return True
0 commit comments