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