def test_key_for_config(self):
     self.assertEqual(key_for_config("ConfigValue"),
                      "/calico/v1/config/ConfigValue")
 def test_key_for_config(self):
     self.assertEqual(key_for_config("ConfigValue"), "/calico/v1/config/ConfigValue")
    def provide_felix_config(self):
        """provide_felix_config

        Specify the prefix of the TAP interfaces that Felix should
        look for and work with.  This config setting does not have a
        default value, because different cloud systems will do
        different things.  Here we provide the prefix that Neutron
        uses.
        """
        LOG.info("Providing Felix configuration")

        # First create the ClusterGUID, if it is not already set.
        cluster_guid_key = datamodel_v1.key_for_config('ClusterGUID')
        try:
            cluster_guid = self.client.read(cluster_guid_key).value
            LOG.info('ClusterGUID is %s', cluster_guid)
        except etcd.EtcdKeyNotFound:
            # Generate and write a globally unique cluster GUID.  Write it
            # idempotently into the datastore. The prevExist=False creates the
            # value (safely with CaS) if it doesn't exist.
            LOG.info('ClusterGUID not set yet (%s)', cluster_guid_key)
            guid = uuid.uuid4()
            guid_string = guid.get_hex()
            try:
                self.client.write(cluster_guid_key,
                                  guid_string,
                                  prevExist=False)
            except etcd.EtcdAlreadyExist:
                LOG.info('ClusterGUID is now set - another orchestrator or' +
                         ' Neutron server instance must have just written it')
                pass

        # Read other config values that should exist.  We will write them only
        # if they're not already (collectively) set as we want them.
        prefix = None
        reporting_enabled = None
        ready = None
        iface_pfx_key = datamodel_v1.key_for_config('InterfacePrefix')
        reporting_key = datamodel_v1.key_for_config('EndpointReportingEnabled')
        try:
            prefix = self.client.read(iface_pfx_key).value
            reporting_enabled = self.client.read(reporting_key).value
            ready = self.client.read(datamodel_v1.READY_KEY).value
        except etcd.EtcdKeyNotFound:
            LOG.info('%s values are missing', datamodel_v1.CONFIG_DIR)

        prefixes = prefix.split(',') if prefix else []
        if 'tap' not in prefixes:
            prefixes.append('tap')
        prefix_new = ','.join(prefixes)

        # Now write the values that need writing.
        if prefix != prefix_new:
            LOG.info('%s -> %s', iface_pfx_key, prefix_new)
            self.client.write(iface_pfx_key, prefix_new)
        if reporting_enabled != "true":
            LOG.info('%s -> true', reporting_key)
            self.client.write(reporting_key, 'true')
        if ready != 'true':
            # TODO(nj) Set this flag only once we're really ready!
            LOG.info('%s -> true', datamodel_v1.READY_KEY)
            self.client.write(datamodel_v1.READY_KEY, 'true')
    def provide_felix_config(self):
        """provide_felix_config

        Specify the prefix of the TAP interfaces that Felix should
        look for and work with.  This config setting does not have a
        default value, because different cloud systems will do
        different things.  Here we provide the prefix that Neutron
        uses.
        """
        LOG.info("Providing Felix configuration")

        # First create the ClusterGUID, if it is not already set.
        cluster_guid_key = datamodel_v1.key_for_config('ClusterGUID')
        try:
            cluster_guid = self.client.read(cluster_guid_key).value
            LOG.info('ClusterGUID is %s', cluster_guid)
        except etcd.EtcdKeyNotFound:
            # Generate and write a globally unique cluster GUID.  Write it
            # idempotently into the datastore. The prevExist=False creates the
            # value (safely with CaS) if it doesn't exist.
            LOG.info('ClusterGUID not set yet (%s)', cluster_guid_key)
            guid = uuid.uuid4()
            guid_string = guid.get_hex()
            try:
                self.client.write(cluster_guid_key,
                                  guid_string,
                                  prevExist=False)
            except etcd.EtcdAlreadyExist:
                LOG.info('ClusterGUID is now set - another orchestrator or' +
                         ' Neutron server instance must have just written it')
                pass

        # Read other config values that should exist.  We will write them only
        # if they're not already (collectively) set as we want them.
        prefix = None
        reporting_enabled = None
        ready = None
        iface_pfx_key = datamodel_v1.key_for_config('InterfacePrefix')
        reporting_key = datamodel_v1.key_for_config('EndpointReportingEnabled')
        try:
            prefix = self.client.read(iface_pfx_key).value
            reporting_enabled = self.client.read(reporting_key).value
            ready = self.client.read(datamodel_v1.READY_KEY).value
        except etcd.EtcdKeyNotFound:
            LOG.info('%s values are missing', datamodel_v1.CONFIG_DIR)

        prefixes = prefix.split(',') if prefix else []
        if 'tap' not in prefixes:
            prefixes.append('tap')
        prefix_new = ','.join(prefixes)

        # Now write the values that need writing.
        if prefix != prefix_new:
            LOG.info('%s -> %s', iface_pfx_key, prefix_new)
            self.client.write(iface_pfx_key, prefix_new)
        if reporting_enabled != "true":
            LOG.info('%s -> true', reporting_key)
            self.client.write(reporting_key, 'true')
        if ready != 'true':
            # TODO(nj) Set this flag only once we're really ready!
            LOG.info('%s -> true', datamodel_v1.READY_KEY)
            self.client.write(datamodel_v1.READY_KEY, 'true')