コード例 #1
0
 def test_identifier(self, warning_mock):
     # empty string
     self.assertFalse(identifier.is_valid(''))
     # generated uuid
     self.assertTrue(identifier.is_valid(identifier.generate_uuid()))
     self.assertFalse(warning_mock.called)
     # any string
     self.assertTrue(identifier.is_valid('blah'))
     self.assertTrue(warning_mock.called)
コード例 #2
0
 def test_identifier(self, warning_mock):
     # empty string
     self.assertFalse(identifier.is_valid(''))
     # generated uuid
     self.assertTrue(identifier.is_valid(identifier.generate_uuid()))
     self.assertFalse(warning_mock.called)
     # any string
     self.assertTrue(identifier.is_valid('blah'))
     self.assertTrue(warning_mock.called)
コード例 #3
0
class Measurement(cadftype.CADFAbstractType):

    result = cadftype.ValidatorDescriptor(MEASUREMENT_KEYNAME_RESULT)
    metric = cadftype.ValidatorDescriptor(
        MEASUREMENT_KEYNAME_METRIC, lambda x: isinstance(x, metric.Metric))
    metricId = cadftype.ValidatorDescriptor(MEASUREMENT_KEYNAME_METRICID,
                                            lambda x: identifier.is_valid(x))
    calculatedBy = cadftype.ValidatorDescriptor(
        MEASUREMENT_KEYNAME_CALCBY,
        (lambda x: isinstance(x, resource.Resource) and x.is_valid()))

    def __init__(self, result=None, metric=None, metricId=None,
                 calculatedBy=None):

        # Measurement.result
        if result is not None:
            setattr(self, MEASUREMENT_KEYNAME_RESULT, result)

        # Measurement.metricId
        if metricId is not None:
            setattr(self, MEASUREMENT_KEYNAME_METRICID, metricId)

        # Measurement.metric
        if metric is not None:
            setattr(self, MEASUREMENT_KEYNAME_METRIC, metric)

        # Measurement.calculaedBy
        if calculatedBy is not None:
            setattr(self, MEASUREMENT_KEYNAME_CALCBY, calculatedBy)

    # self validate this cadf:Measurement type against schema
    def is_valid(self):
        return (self._isset(MEASUREMENT_KEYNAME_RESULT) and
                (self._isset(MEASUREMENT_KEYNAME_METRIC) ^
                 self._isset(MEASUREMENT_KEYNAME_METRICID)))
コード例 #4
0
ファイル: host.py プロジェクト: varunarya10/pycadf
class Host(cadftype.CADFAbstractType):

    id = cadftype.ValidatorDescriptor(HOST_KEYNAME_ID,
                                      lambda x: identifier.is_valid(x))
    address = cadftype.ValidatorDescriptor(
        HOST_KEYNAME_ADDR, lambda x: isinstance(x, six.string_types))
    agent = cadftype.ValidatorDescriptor(
        HOST_KEYNAME_AGENT, lambda x: isinstance(x, six.string_types))
    platform = cadftype.ValidatorDescriptor(
        HOST_KEYNAME_PLATFORM, lambda x: isinstance(x, six.string_types))

    def __init__(self, id=None, address=None, agent=None, platform=None):

        # Host.id
        if id is not None:
            setattr(self, HOST_KEYNAME_ID, id)
        # Host.address
        if address is not None:
            setattr(self, HOST_KEYNAME_ADDR, address)
        # Host.agent
        if agent is not None:
            setattr(self, HOST_KEYNAME_AGENT, agent)
        # Host.platform
        if platform is not None:
            setattr(self, HOST_KEYNAME_PLATFORM, platform)

    # TODO(mrutkows): validate this cadf:Host type against schema
    def is_valid(self):
        return True
コード例 #5
0
ファイル: metric.py プロジェクト: varunarya10/pycadf
class Metric(cadftype.CADFAbstractType):

    metricId = cadftype.ValidatorDescriptor(METRIC_KEYNAME_METRICID,
                                            lambda x: identifier.is_valid(x))
    unit = cadftype.ValidatorDescriptor(
        METRIC_KEYNAME_UNIT, lambda x: isinstance(x, six.string_types))
    name = cadftype.ValidatorDescriptor(
        METRIC_KEYNAME_NAME, lambda x: isinstance(x, six.string_types))

    def __init__(self, metricId=None, unit=None, name=None):
        # Metric.id
        setattr(self, METRIC_KEYNAME_METRICID, metricId
                or identifier.generate_uuid())

        # Metric.unit
        if unit is not None:
            setattr(self, METRIC_KEYNAME_UNIT, unit)

        # Metric.name
        if name is not None:
            setattr(self, METRIC_KEYNAME_NAME, name)

    # TODO(mrutkows): add mechanism for annotations, OpenStack may choose
    # not to support this "extension mechanism" and is not required (and not
    # critical in many audit contexts)
    def set_annotations(self, value):
        raise NotImplementedError()
        # setattr(self, METRIC_KEYNAME_ANNOTATIONS, value)

    # self validate cadf:Metric type against schema
    def is_valid(self):
        # Existence test, id, and unit attributes must both exist
        return (self._isset(METRIC_KEYNAME_METRICID)
                and self._isset(METRIC_KEYNAME_UNIT))
コード例 #6
0
    def test_identifier_valid_id_extra_chars_is_valid(self, warning_mock):
        # valid uuid with additional characters according to:
        # https://docs.python.org/2/library/uuid.html
        valid_ids = [
            '{1234567890abcdef1234567890abcdef}',
            '{12345678-1234-5678-1234-567812345678}',
            'urn:uuid:12345678-1234-5678-1234-567812345678'
        ]

        for value in valid_ids:
            self.assertTrue(identifier.is_valid(value))
            self.assertFalse(warning_mock.called)
コード例 #7
0
class Reporterstep(cadftype.CADFAbstractType):

    role = cadftype.ValidatorDescriptor(
        REPORTERSTEP_KEYNAME_ROLE,
        lambda x: cadftype.is_valid_reporter_role(x))
    reporter = cadftype.ValidatorDescriptor(
        REPORTERSTEP_KEYNAME_REPORTER,
        (lambda x: isinstance(x, resource.Resource) and x.is_valid()))
    reporterId = cadftype.ValidatorDescriptor(REPORTERSTEP_KEYNAME_REPORTERID,
                                              lambda x: identifier.is_valid(x))
    reporterTime = cadftype.ValidatorDescriptor(
        REPORTERSTEP_KEYNAME_REPORTERTIME, lambda x: timestamp.is_valid(x))

    def __init__(self,
                 role=cadftype.REPORTER_ROLE_MODIFIER,
                 reporterTime=None,
                 reporter=None,
                 reporterId=None):
        """Create ReporterStep data type

        :param role: optional role of Reporterstep. Defaults to 'modifier'
        :param reporterTime: utc time of Reporterstep.
        :param reporter: CADF Resource of reporter
        :param reporterId: id of CADF resource for reporter
        """
        # Reporterstep.role
        setattr(self, REPORTERSTEP_KEYNAME_ROLE, role)

        # Reporterstep.reportTime
        if reporterTime is not None:
            setattr(self, REPORTERSTEP_KEYNAME_REPORTERTIME, reporterTime)

        # Reporterstep.reporter
        if reporter is not None:
            setattr(self, REPORTERSTEP_KEYNAME_REPORTER, reporter)

        # Reporterstep.reporterId
        if reporterId is not None:
            setattr(self, REPORTERSTEP_KEYNAME_REPORTERID, reporterId)

    # self validate this cadf:Reporterstep type against schema
    def is_valid(self):
        """Validation to ensure Reporterstep required attributes are set.
        """
        return (self._isset(REPORTERSTEP_KEYNAME_ROLE)
                and (self._isset(REPORTERSTEP_KEYNAME_REPORTER)
                     ^ self._isset(REPORTERSTEP_KEYNAME_REPORTERID)))
コード例 #8
0
class Measurement(cadftype.CADFAbstractType):

    result = cadftype.ValidatorDescriptor(MEASUREMENT_KEYNAME_RESULT)
    metric = cadftype.ValidatorDescriptor(
        MEASUREMENT_KEYNAME_METRIC, lambda x: isinstance(x, metric.Metric))
    metricId = cadftype.ValidatorDescriptor(MEASUREMENT_KEYNAME_METRICID,
                                            lambda x: identifier.is_valid(x))
    calculatedBy = cadftype.ValidatorDescriptor(
        MEASUREMENT_KEYNAME_CALCBY,
        (lambda x: isinstance(x, resource.Resource) and x.is_valid()))

    def __init__(self, result=None, metric=None, metricId=None,
                 calculatedBy=None):
        """Create Measurement data type

        :param result: value of measurement
        :param metric: Metric data type of current measurement
        :param metricId: id of Metric data type of current measurement
        :param calculatedBy: Resource that calculated measurement
        """
        # Measurement.result
        if result is not None:
            setattr(self, MEASUREMENT_KEYNAME_RESULT, result)

        # Measurement.metricId
        if metricId is not None:
            setattr(self, MEASUREMENT_KEYNAME_METRICID, metricId)

        # Measurement.metric
        if metric is not None:
            setattr(self, MEASUREMENT_KEYNAME_METRIC, metric)

        # Measurement.calculaedBy
        if calculatedBy is not None:
            setattr(self, MEASUREMENT_KEYNAME_CALCBY, calculatedBy)

    # self validate this cadf:Measurement type against schema
    def is_valid(self):
        """Validation to ensure Measurement required attributes are set.
        """
        return (self._isset(MEASUREMENT_KEYNAME_RESULT) and
                (self._isset(MEASUREMENT_KEYNAME_METRIC) ^
                 self._isset(MEASUREMENT_KEYNAME_METRICID)))
コード例 #9
0
    def test_identifier_joined_uuids_are_valid(self, warning_mock):
        # multiple uuids joined together
        long_128_uuids = [
            ('3adce28e67e44544a5a9d5f1ab54f578a86d310aac3a465e9d'
             'd2693a78b45c0e42dce28e67e44544a5a9d5f1ab54f578a86d'
             '310aac3a465e9dd2693a78b45c0e'),
            ('{3adce28e67e44544a5a9d5f1ab54f578a86d310aac3a465e9d'
             'd2693a78b45c0e42dce28e67e44544a5a9d5f1ab54f578a86d'
             '310aac3a465e9dd2693a78b45c0e}'),
            ('{12345678-1234-5678-1234-567812345678'
             '12345678-1234-5678-1234-567812345678'
             '12345678-1234-5678-1234-567812345678'
             '12345678-1234-5678-1234-567812345678}'),
            ('urn:uuid:3adce28e67e44544a5a9d5f1ab54f578a86d310aac3a465e9d'
             'd2693a78b45c0e42dce28e67e44544a5a9d5f1ab54f578a86d'
             '310aac3a465e9dd2693a78b45c0e')
        ]

        for value in long_128_uuids:
            self.assertTrue(identifier.is_valid(value))
            self.assertFalse(warning_mock.called)
コード例 #10
0
class Reporterstep(cadftype.CADFAbstractType):

    role = cadftype.ValidatorDescriptor(
        REPORTERSTEP_KEYNAME_ROLE,
        lambda x: cadftype.is_valid_reporter_role(x))
    reporter = cadftype.ValidatorDescriptor(
        REPORTERSTEP_KEYNAME_REPORTER,
        (lambda x: isinstance(x, resource.Resource) and x.is_valid()))
    reporterId = cadftype.ValidatorDescriptor(REPORTERSTEP_KEYNAME_REPORTERID,
                                              lambda x: identifier.is_valid(x))
    reporterTime = cadftype.ValidatorDescriptor(
        REPORTERSTEP_KEYNAME_REPORTERTIME, lambda x: timestamp.is_valid(x))

    def __init__(self,
                 role=cadftype.REPORTER_ROLE_MODIFIER,
                 reporterTime=None,
                 reporter=None,
                 reporterId=None):
        # Reporterstep.role
        setattr(self, REPORTERSTEP_KEYNAME_ROLE, role)

        # Reporterstep.reportTime
        if reporterTime is not None:
            setattr(self, REPORTERSTEP_KEYNAME_REPORTERTIME, reporterTime)

        # Reporterstep.reporter
        if reporter is not None:
            setattr(self, REPORTERSTEP_KEYNAME_REPORTER, reporter)

        # Reporterstep.reporterId
        if reporterId is not None:
            setattr(self, REPORTERSTEP_KEYNAME_REPORTERID, reporterId)

    # self validate this cadf:Reporterstep type against schema
    def is_valid(self):
        return (self._isset(REPORTERSTEP_KEYNAME_ROLE)
                and (self._isset(REPORTERSTEP_KEYNAME_REPORTER)
                     ^ self._isset(REPORTERSTEP_KEYNAME_REPORTERID)))
コード例 #11
0
class Host(cadftype.CADFAbstractType):

    id = cadftype.ValidatorDescriptor(HOST_KEYNAME_ID,
                                      lambda x: identifier.is_valid(x))
    address = cadftype.ValidatorDescriptor(
        HOST_KEYNAME_ADDR, lambda x: isinstance(x, six.string_types))
    agent = cadftype.ValidatorDescriptor(
        HOST_KEYNAME_AGENT, lambda x: isinstance(x, six.string_types))
    platform = cadftype.ValidatorDescriptor(
        HOST_KEYNAME_PLATFORM, lambda x: isinstance(x, six.string_types))

    def __init__(self, id=None, address=None, agent=None, platform=None):
        """Create Host data type

        :param id: id of Host
        :param address: optional Address of Host
        :param agent: agent (name) of Host
        :param platform: platform of Host
        """

        # Host.id
        if id is not None:
            setattr(self, HOST_KEYNAME_ID, id)
        # Host.address
        if address is not None:
            setattr(self, HOST_KEYNAME_ADDR, address)
        # Host.agent
        if agent is not None:
            setattr(self, HOST_KEYNAME_AGENT, agent)
        # Host.platform
        if platform is not None:
            setattr(self, HOST_KEYNAME_PLATFORM, platform)

    # TODO(mrutkows): validate this cadf:Host type against schema
    def is_valid(self):
        """Validation to ensure Host required attributes are set.
        """
        return True
コード例 #12
0
class Resource(cadftype.CADFAbstractType):

    typeURI = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_TYPEURI, lambda x: cadftaxonomy.is_valid_resource(x))
    id = cadftype.ValidatorDescriptor(RESOURCE_KEYNAME_ID,
                                      lambda x: identifier.is_valid(x))
    name = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_NAME, lambda x: isinstance(x, six.string_types))
    domain = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_DOMAIN, lambda x: isinstance(x, six.string_types))
    credential = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_CRED,
        (lambda x: isinstance(x, credential.Credential) and x.is_valid()))
    host = cadftype.ValidatorDescriptor(RESOURCE_KEYNAME_HOST,
                                        lambda x: isinstance(x, host.Host))
    # TODO(mrutkows): validate the "ref" attribute is indeed a URI (format),
    # If it is a URL, we do not need to validate it is accessible/working,
    # for audit purposes this could have been a valid URL at some point
    # in the past or a URL that is only valid within some domain (e.g. a
    # private cloud)
    ref = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_REF, lambda x: isinstance(x, six.string_types))
    geolocation = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_GEO, lambda x: isinstance(x, geolocation.Geolocation))
    geolocationId = cadftype.ValidatorDescriptor(
        RESOURCE_KEYNAME_GEOID, lambda x: identifier.is_valid(x))

    def __init__(self,
                 id=None,
                 typeURI=cadftaxonomy.UNKNOWN,
                 name=None,
                 ref=None,
                 domain=None,
                 credential=None,
                 host=None,
                 geolocation=None,
                 geolocationId=None):

        # Resource.id
        setattr(self, RESOURCE_KEYNAME_ID, id or identifier.generate_uuid())

        # Resource.typeURI
        if (getattr(self, RESOURCE_KEYNAME_ID) != "target"
                and getattr(self, RESOURCE_KEYNAME_ID) != "initiator"):
            setattr(self, RESOURCE_KEYNAME_TYPEURI, typeURI)

        # Resource.name
        if name is not None:
            setattr(self, RESOURCE_KEYNAME_NAME, name)

        # Resource.ref
        if ref is not None:
            setattr(self, RESOURCE_KEYNAME_REF, ref)

        # Resource.domain
        if domain is not None:
            setattr(self, RESOURCE_KEYNAME_DOMAIN, domain)

        # Resource.credential
        if credential is not None:
            setattr(self, RESOURCE_KEYNAME_CRED, credential)

        # Resource.host
        if host is not None:
            setattr(self, RESOURCE_KEYNAME_HOST, host)

        # Resource.geolocation
        if geolocation is not None:
            setattr(self, RESOURCE_KEYNAME_GEO, geolocation)

        # Resource.geolocationId
        if geolocationId:
            setattr(self, RESOURCE_KEYNAME_GEOID, geolocationId)

    # Resource.address
    def add_address(self, addr):
        if (addr is not None and isinstance(addr, endpoint.Endpoint)):
            if addr.is_valid():
                # Create the list of Endpoints if needed
                if not hasattr(self, RESOURCE_KEYNAME_ADDRS):
                    setattr(self, RESOURCE_KEYNAME_ADDRS, list())

                addrs = getattr(self, RESOURCE_KEYNAME_ADDRS)
                addrs.append(addr)
            else:
                raise ValueError('Invalid endpoint')
        else:
            raise ValueError('Invalid endpoint. Value must be an Endpoint')

    # Resource.attachments
    def add_attachment(self, attach_val):
        if (attach_val is not None
                and isinstance(attach_val, attachment.Attachment)):
            if attach_val.is_valid():
                # Create the list of Attachments if needed
                if not hasattr(self, RESOURCE_KEYNAME_ATTACHMENTS):
                    setattr(self, RESOURCE_KEYNAME_ATTACHMENTS, list())

                attachments = getattr(self, RESOURCE_KEYNAME_ATTACHMENTS)
                attachments.append(attach_val)
            else:
                raise ValueError('Invalid attachment')
        else:
            raise ValueError('Invalid attachment. Value must be an Attachment')

    # self validate this cadf:Resource type against schema
    def is_valid(self):
        return (self._isset(RESOURCE_KEYNAME_ID)
                and (self._isset(RESOURCE_KEYNAME_TYPEURI) or
                     ((getattr(self, RESOURCE_KEYNAME_ID) == "target"
                       or getattr(self, RESOURCE_KEYNAME_ID) == "initiator")
                      and len(vars(self).keys()) == 1)))
コード例 #13
0
ファイル: event.py プロジェクト: xdcrazyboy/OpenStackInAction
class Event(cadftype.CADFAbstractType):

    eventType = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_EVENTTYPE, lambda x: cadftype.is_valid_eventType(x))
    id = cadftype.ValidatorDescriptor(EVENT_KEYNAME_ID,
                                      lambda x: identifier.is_valid(x))
    eventTime = cadftype.ValidatorDescriptor(EVENT_KEYNAME_EVENTTIME,
                                             lambda x: timestamp.is_valid(x))
    initiator = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_INITIATOR, (lambda x: isinstance(x, resource.Resource)
                                  and x.is_valid() and x.id != 'initiator'))
    initiatorId = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_INITIATORID, lambda x: identifier.is_valid(x))
    action = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_ACTION, lambda x: cadftaxonomy.is_valid_action(x))
    target = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_TARGET, (lambda x: isinstance(x, resource.Resource) and x
                               .is_valid() and x.id != 'target'))
    targetId = cadftype.ValidatorDescriptor(EVENT_KEYNAME_TARGETID,
                                            lambda x: identifier.is_valid(x))
    outcome = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_OUTCOME, lambda x: cadftaxonomy.is_valid_outcome(x))
    reason = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_REASON,
        lambda x: isinstance(x, reason.Reason) and x.is_valid())
    severity = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_SEVERITY, lambda x: isinstance(x, six.string_types))
    observer = cadftype.ValidatorDescriptor(
        EVENT_KEYNAME_OBSERVER,
        (lambda x: isinstance(x, resource.Resource) and x.is_valid()))
    observerId = cadftype.ValidatorDescriptor(EVENT_KEYNAME_OBSERVERID,
                                              lambda x: identifier.is_valid(x))

    def __init__(self,
                 eventType=cadftype.EVENTTYPE_ACTIVITY,
                 id=None,
                 eventTime=None,
                 action=cadftaxonomy.UNKNOWN,
                 outcome=cadftaxonomy.UNKNOWN,
                 initiator=None,
                 initiatorId=None,
                 target=None,
                 targetId=None,
                 severity=None,
                 reason=None,
                 observer=None,
                 observerId=None):
        """Create an Event

        :param eventType: eventType of Event. Defaults to 'activity' type
        :param id: id of event. will generate uuid if None
        :param eventTime: time of event. will take current utc if None
        :param action: event's action (see Action taxonomy)
        :param outcome: Event's outcome (see Outcome taxonomy)
        :param initiator: Event's Initiator Resource
        :param initiatorId: Event's Initiator Resource id
        :param target: Event's Target Resource
        :param targetId: Event's Target Resource id
        :param severity: domain-relative severity of Event
        :param reason: domain-specific Reason type
        :param observer: Event's Observer Resource
        :param observerId: Event's Observer Resource id
        """
        # Establish typeURI for the CADF Event data type
        # TODO(mrutkows): support extended typeURIs for Event subtypes
        setattr(self, EVENT_KEYNAME_TYPEURI, TYPE_URI_EVENT)

        # Event.eventType (Mandatory)
        setattr(self, EVENT_KEYNAME_EVENTTYPE, eventType)

        # Event.id (Mandatory)
        setattr(self, EVENT_KEYNAME_ID, id or identifier.generate_uuid())

        # Event.eventTime (Mandatory)
        setattr(self, EVENT_KEYNAME_EVENTTIME, eventTime
                or timestamp.get_utc_now())

        # Event.action (Mandatory)
        setattr(self, EVENT_KEYNAME_ACTION, action)

        # Event.outcome (Mandatory)
        setattr(self, EVENT_KEYNAME_OUTCOME, outcome)

        # Event.observer (Mandatory if no observerId)
        if observer is not None:
            setattr(self, EVENT_KEYNAME_OBSERVER, observer)
        # Event.observerId (Dependent)
        if observerId is not None:
            setattr(self, EVENT_KEYNAME_OBSERVERID, observerId)

        # Event.initiator (Mandatory if no initiatorId)
        if initiator is not None:
            setattr(self, EVENT_KEYNAME_INITIATOR, initiator)
        # Event.initiatorId (Dependent)
        if initiatorId is not None:
            setattr(self, EVENT_KEYNAME_INITIATORID, initiatorId)

        # Event.target (Mandatory if no targetId)
        if target is not None:
            setattr(self, EVENT_KEYNAME_TARGET, target)
        # Event.targetId (Dependent)
        if targetId is not None:
            setattr(self, EVENT_KEYNAME_TARGETID, targetId)

        # Event.severity (Optional)
        if severity is not None:
            setattr(self, EVENT_KEYNAME_SEVERITY, severity)

        # Event.reason (Optional)
        if reason is not None:
            setattr(self, EVENT_KEYNAME_REASON, reason)

    # Event.reporterchain
    def add_reporterstep(self, step):
        """Add a Reporterstep

        :param step: Reporterstep to be added to reporterchain
        """
        if step is not None and isinstance(step, reporterstep.Reporterstep):
            if step.is_valid():
                # Create the list of Reportersteps if needed
                if not hasattr(self, EVENT_KEYNAME_REPORTERCHAIN):
                    setattr(self, EVENT_KEYNAME_REPORTERCHAIN, list())

                reporterchain = getattr(self, EVENT_KEYNAME_REPORTERCHAIN)
                reporterchain.append(step)
            else:
                raise ValueError('Invalid reporterstep')
        else:
            raise ValueError('Invalid reporterstep. '
                             'Value must be a Reporterstep')

    # Event.measurements
    def add_measurement(self, measure_val):
        """Add a measurement value

        :param measure_val: Measurement data type to be added to Event
        """
        if (measure_val is not None
                and isinstance(measure_val, measurement.Measurement)):

            if measure_val.is_valid():

                # Create the list of event.Measurements if needed
                if not hasattr(self, EVENT_KEYNAME_MEASUREMENTS):
                    setattr(self, EVENT_KEYNAME_MEASUREMENTS, list())

                measurements = getattr(self, EVENT_KEYNAME_MEASUREMENTS)
                measurements.append(measure_val)
            else:
                raise ValueError('Invalid measurement')
        else:
            raise ValueError('Invalid measurement. '
                             'Value must be a Measurement')

    # Event.tags
    def add_tag(self, tag_val):
        """Add Tag to Event

        :param tag_val: Tag to add to event
        """
        if tag.is_valid(tag_val):
            if not hasattr(self, EVENT_KEYNAME_TAGS):
                setattr(self, EVENT_KEYNAME_TAGS, list())
            getattr(self, EVENT_KEYNAME_TAGS).append(tag_val)
        else:
            raise ValueError('Invalid tag')

    # Event.attachments
    def add_attachment(self, attachment_val):
        """Add Attachment to Event

        :param attachment_val: Attachment to add to Event
        """
        if (attachment_val is not None
                and isinstance(attachment_val, attachment.Attachment)):

            if attachment_val.is_valid():
                # Create the list of Attachments if needed
                if not hasattr(self, EVENT_KEYNAME_ATTACHMENTS):
                    setattr(self, EVENT_KEYNAME_ATTACHMENTS, list())

                attachments = getattr(self, EVENT_KEYNAME_ATTACHMENTS)
                attachments.append(attachment_val)
            else:
                raise ValueError('Invalid attachment')
        else:
            raise ValueError('Invalid attachment. '
                             'Value must be an Attachment')

    # self validate cadf:Event record against schema
    def is_valid(self):
        """Validation to ensure Event required attributes are set.
        """
        # TODO(mrutkows): Eventually, make sure all attributes are
        # from either the CADF spec. (or profiles thereof)
        # TODO(mrutkows): validate all child attributes that are CADF types
        return (self._isset(EVENT_KEYNAME_TYPEURI)
                and self._isset(EVENT_KEYNAME_EVENTTYPE)
                and self._isset(EVENT_KEYNAME_ID)
                and self._isset(EVENT_KEYNAME_EVENTTIME)
                and self._isset(EVENT_KEYNAME_ACTION)
                and self._isset(EVENT_KEYNAME_OUTCOME)
                and (self._isset(EVENT_KEYNAME_INITIATOR)
                     ^ self._isset(EVENT_KEYNAME_INITIATORID))
                and (self._isset(EVENT_KEYNAME_TARGET)
                     ^ self._isset(EVENT_KEYNAME_TARGETID))
                and (self._isset(EVENT_KEYNAME_OBSERVER)
                     ^ self._isset(EVENT_KEYNAME_OBSERVERID)))
コード例 #14
0
 def test_identifier_empty_string_is_invalid(self, warning_mock):
     # empty string
     self.assertFalse(identifier.is_valid(''))
     self.assertFalse(warning_mock.called)
コード例 #15
0
 def test_identifier_generated_uuid(self, warning_mock):
     # generated uuid
     self.assertTrue(identifier.is_valid(identifier.generate_uuid()))
     self.assertFalse(warning_mock.called)
コード例 #16
0
ファイル: geolocation.py プロジェクト: varunarya10/pycadf
class Geolocation(cadftype.CADFAbstractType):

    id = cadftype.ValidatorDescriptor(GEO_KEYNAME_ID,
                                      lambda x: identifier.is_valid(x))
    # TODO(mrutkows): we may want to do more validation to make
    # sure numeric range represented by string is valid
    latitude = cadftype.ValidatorDescriptor(GEO_KEYNAME_LATITUDE,
                                            lambda x: isinstance(
                                                x, six.string_types))
    longitude = cadftype.ValidatorDescriptor(GEO_KEYNAME_LONGITUDE,
                                             lambda x: isinstance(
                                                 x, six.string_types))
    elevation = cadftype.ValidatorDescriptor(GEO_KEYNAME_ELEVATION,
                                             lambda x: isinstance(
                                                 x, six.string_types))
    accuracy = cadftype.ValidatorDescriptor(GEO_KEYNAME_ACCURACY,
                                            lambda x: isinstance(
                                                x, six.string_types))
    city = cadftype.ValidatorDescriptor(GEO_KEYNAME_CITY,
                                        lambda x: isinstance(
                                            x, six.string_types))
    state = cadftype.ValidatorDescriptor(GEO_KEYNAME_STATE,
                                         lambda x: isinstance(
                                             x, six.string_types))
    regionICANN = cadftype.ValidatorDescriptor(
        GEO_KEYNAME_REGIONICANN,
        lambda x: isinstance(x, six.string_types))

    def __init__(self, id=None, latitude=None, longitude=None,
                 elevation=None, accuracy=None, city=None, state=None,
                 regionICANN=None):

        # Geolocation.id
        if id is not None:
            setattr(self, GEO_KEYNAME_ID, id)

        # Geolocation.latitude
        if latitude is not None:
            setattr(self, GEO_KEYNAME_LATITUDE, latitude)

        # Geolocation.longitude
        if longitude is not None:
            setattr(self, GEO_KEYNAME_LONGITUDE, longitude)

        # Geolocation.elevation
        if elevation is not None:
            setattr(self, GEO_KEYNAME_ELEVATION, elevation)

        # Geolocation.accuracy
        if accuracy is not None:
            setattr(self, GEO_KEYNAME_ACCURACY, accuracy)

        # Geolocation.city
        if city is not None:
            setattr(self, GEO_KEYNAME_CITY, city)

        # Geolocation.state
        if state is not None:
            setattr(self, GEO_KEYNAME_STATE, state)

        # Geolocation.regionICANN
        if regionICANN is not None:
            setattr(self, GEO_KEYNAME_REGIONICANN, regionICANN)

    # TODO(mrutkows): add mechanism for annotations, OpenStack may choose
    # not to support this "extension mechanism" and is not required (and not
    # critical in many audit contexts)
    def set_annotations(self, value):
        raise NotImplementedError()
        # setattr(self, GEO_KEYNAME_ANNOTATIONS, value)

    # self validate cadf:Geolocation type
    def is_valid(self):
        return True
コード例 #17
0
 def test_identifier_any_string_is_invalid(self, warning_mock):
     # any string
     self.assertTrue(identifier.is_valid('blah'))
     self.assertTrue(warning_mock.called)
コード例 #18
0
 def test_identifier_long_nonjoined_uuid_is_invalid(self, warning_mock):
     # long uuid not of size % 32
     char_42_id = '3adce28e67e44544a5a9d5f1ab54f578a86d310aac'
     self.assertTrue(identifier.is_valid(char_42_id))
     self.assertTrue(warning_mock.called)
コード例 #19
0
 def test_identifier_specific_exceptions_are_valid(self, warning_mock):
     # uuid exceptions
     for value in identifier.VALID_EXCEPTIONS:
         self.assertTrue(identifier.is_valid(value))
         self.assertFalse(warning_mock.called)
コード例 #20
0
ファイル: test_cadf_spec.py プロジェクト: rtmorgan/pycadf
 def test_identifier_empty(self):
     self.assertFalse(identifier.is_valid(''))
     self.assertTrue(identifier.is_valid(identifier.generate_uuid()))