def get_profile(self, name): """ Get a Profile object representing the named profile from the data store. :param name: The name of the profile. :return: A Profile object. """ profile_path = PROFILE_PATH % {"profile_id": name} try: _ = self.etcd_client.read(profile_path) profile = Profile(name) except EtcdKeyNotFound: raise KeyError("%s is not a configured profile." % name) tags_path = TAGS_PATH % {"profile_id": name} try: tags_result = self.etcd_client.read(tags_path) tags = json.loads(tags_result.value) profile.tags = set(tags) except EtcdKeyNotFound: pass rules_path = RULES_PATH % {"profile_id": name} try: rules_result = self.etcd_client.read(rules_path) rules = Rules.from_json(rules_result.value) profile.rules = rules except EtcdKeyNotFound: pass return profile
def _translate_profile(self, profile, mapping_fn): """ Translate the profile by converting between Docker network names and IDs in the profile name and tags. The direction of transalation depends on the mapping function provided. This should only be called for Docker network profiles. :return: The translated profile. """ # Create a new profile mapping the name of the one supplied. translated_profile = Profile(mapping_fn(profile.name)) # Add the translated tags. for tag in profile.tags: translated_profile.tags.add(mapping_fn(tag)) # Add the updated rules. for rule in profile.rules.inbound_rules: rule = copy.copy(rule) if "src_tag" in rule: rule["src_tag"] = mapping_fn(rule["src_tag"]) if "dst_tag" in rule: rule["dst_tag"] = mapping_fn(rule["dst_tag"]) translated_profile.rules.inbound_rules.append(rule) for rule in profile.rules.outbound_rules: rule = copy.copy(rule) if "src_tag" in rule: rule["src_tag"] = mapping_fn(rule["src_tag"]) if "dst_tag" in rule: rule["dst_tag"] = mapping_fn(rule["dst_tag"]) translated_profile.rules.outbound_rules.append(rule) return translated_profile