2N/A#
2N/A# CDDL HEADER START
2N/A#
2N/A# The contents of this file are subject to the terms of the
2N/A# Common Development and Distribution License (the "License").
2N/A# You may not use this file except in compliance with the License.
2N/A#
2N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A# or http://www.opensolaris.org/os/licensing.
2N/A# See the License for the specific language governing permissions
2N/A# and limitations under the License.
2N/A#
2N/A# When distributing Covered Code, include this CDDL HEADER in each
2N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A# If applicable, add the following below this CDDL HEADER, with the
2N/A# fields enclosed by brackets "[]" replaced with your own identifying
2N/A# information: Portions Copyright [yyyy] [name of copyright owner]
2N/A#
2N/A# CDDL HEADER END
2N/A#
2N/A
2N/A#
2N/A# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A#
2N/A
2N/A""" Service FMRI handling """
2N/A
2N/Aclass FMRI:
2N/A """ Immutable service or instance FMRI type """
2N/A
2N/A def __init__(self, scope, service, instance):
2N/A """ Constructs an FMRI from a scope, service, and instance """
2N/A if scope != None and scope == "localhost":
2N/A self._scope = None
2N/A else:
2N/A self._scope = scope
2N/A self._service = service
2N/A self._instance = instance
2N/A
2N/A @staticmethod
2N/A def parse(fmri):
2N/A """ Parse a svc: FMRI string into an FMRI object """
2N/A
2N/A # Scheme
2N/A scheme = "svc:"
2N/A if not fmri.startswith(scheme):
2N/A raise ValueError, "not an svc: FMRI"
2N/A fmri = fmri[len(scheme):]
2N/A
2N/A # Authority
2N/A scope = None
2N/A if fmri.startswith("//"):
2N/A fmri = fmri[2:]
2N/A parts = fmri.partition('/')
2N/A if len(parts[1]) == 0:
2N/A raise ValueError, "invalid URI"
2N/A
2N/A if len(parts[0]) > 0:
2N/A scope = parts[0]
2N/A fmri = parts[2]
2N/A
2N/A # Traditional SMF lenience
2N/A if fmri.startswith("/"):
2N/A fmri = fmri[1:]
2N/A
2N/A # Finally, the meat
2N/A parts = fmri.partition(':')
2N/A service = parts[0]
2N/A if len(service) == 0:
2N/A raise ValueError, "invalid URI"
2N/A if service.endswith("/"):
2N/A raise ValueError, "non service/instance FMRI"
2N/A if len(parts[2]) > 0:
2N/A instance = parts[2]
2N/A if instance.find("/") != -1:
2N/A raise ValueError, "non service/instance FMRI"
2N/A else:
2N/A instance = None
2N/A
2N/A return FMRI(scope, service, instance)
2N/A
2N/A def make_instance(self, instance):
2N/A """ Returns a new FMRI for the named instance of a service """
2N/A if self._instance:
2N/A raise TypeError, "not a service FMRI"
2N/A return FMRI(self._scope, self._service, instance)
2N/A
2N/A def make_service(self):
2N/A """ Returns a new FMRI for just the service portion """
2N/A if self._instance:
2N/A return FMRI(self._scope, self._service, None)
2N/A return self
2N/A
2N/A def get_scope(self):
2N/A """ Returns the scope of the FMRI, or None for localhost """
2N/A return self._scope
2N/A
2N/A def get_instance(self):
2N/A """ Returns the instance, or None for a service FMRI """
2N/A return self._instance
2N/A
2N/A def get_service(self):
2N/A """ Returns the service """
2N/A return self._service
2N/A
2N/A def get_fmri(self):
2N/A """ Returns the FMRI in canonical string form """
2N/A result = "svc:"
2N/A if self._scope:
2N/A result = result + "//" + self._scope
2N/A result = result + "/" + self._service
2N/A if self._instance:
2N/A result = result + ":" + self._instance
2N/A return result
2N/A
2N/A def __str__(self):
2N/A return self.get_fmri()