diff --git a/cmd/provision/proxies/remote-service-gcp/apiproxy/resources/py/get-runtime-version.py b/cmd/provision/proxies/remote-service-gcp/apiproxy/resources/py/get-runtime-version.py index 7f5094c..5f736be 100644 --- a/cmd/provision/proxies/remote-service-gcp/apiproxy/resources/py/get-runtime-version.py +++ b/cmd/provision/proxies/remote-service-gcp/apiproxy/resources/py/get-runtime-version.py @@ -12,12 +12,33 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +Set the "runtime_version" flow variable to a semver-style version made +by splitting APIGEE_DPCOLOR by "-" and concatenating the first three +segments that are digits. If this fails, the var is set to "unknown." +""" + import os -version = os.environ.get("APIGEE_DPCOLOR", "") -if len(version) > 0: - dig = [c for c in version if c.isdigit()] - flow.setVariable("runtime_version", ".".join(dig[0:3])) -else: - flow.setVariable("runtime_version", "unknown") +def make_semver(version_string): + if not version_string: + return "unknown" + + nums = [] + for segment in version_string.split("-"): + try: + int(segment) + nums.append(segment) + except ValueError: + pass + + if len(nums) < 3: + return "unknown" + + return ".".join(nums[:3]) + + +dp_color = os.environ.get("APIGEE_DPCOLOR", "") +semver = make_semver(dp_color) +flow.setVariable("runtime_version", semver)