From e51458f89be94b86d6b5dab0c8ebe0e7ba197003 Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Tue, 8 Dec 2009 23:27:52 -0500 Subject: Add SSSDDomain.set_name() function to SSSDConfig API This function will change the name of an existing domain --- server/config/SSSDConfig.py | 44 ++++++++++++++++++++++++++++++++++++++--- server/config/SSSDConfigTest.py | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) (limited to 'server') diff --git a/server/config/SSSDConfig.py b/server/config/SSSDConfig.py index 2abafe15a..3b8c128e2 100644 --- a/server/config/SSSDConfig.py +++ b/server/config/SSSDConfig.py @@ -743,6 +743,29 @@ class SSSDDomain(SSSDConfigObject): else: self.options[option] = value + def set_name(self, newname): + """ + Change the name of the domain + + newname: + New name for this domain + + === Returns === + No return value. + + === Errors === + TypeError: + newname was not a string + """ + + if type(newname) != str: + raise TypeError + + if not self.oldname: + # Only set the oldname once + self.oldname = self.name + self.name = newname + def add_provider(self, provider, provider_type): """ Add a new provider type to the domain @@ -819,14 +842,14 @@ class SSSDDomain(SSSDConfigObject): # Remove any unused options when removing the provider. options = self.list_provider_options(provider, provider_type) - + # Trim any options that are used by other providers, # if that provider is in use for (prov, ptype) in self.providers: # Ignore the one being removed if (prov, ptype) == (provider, provider_type): continue - + provider_options = self.list_provider_options(prov, ptype) overlap = options_overlap(options.keys(), provider_options.keys()) for opt in overlap: @@ -1286,6 +1309,18 @@ class SSSDConfig(SSSDChangeConf): raise TypeError name = domain.get_name() + + oldindex = None + if domain.oldname and domain.oldname != name: + # We are renaming this domain + # Remove the old section + oldindex = self.delete_option('section', 'domain/%s' % + domain.oldname) + + # Reset the oldname, in case we're not done with + # this domain object. + domain.oldname = None; + sectionname = 'domain/%s' % name # Ensure that the existing section is removed # This way we ensure that we are getting a @@ -1300,7 +1335,10 @@ class SSSDConfig(SSSDChangeConf): addkw.append( { 'type' : 'option', 'name' : option, 'value' : str(value) } ) - self.add_section(sectionname, addkw, index) + if oldindex: + self.add_section(sectionname, addkw, oldindex) + else: + self.add_section(sectionname, addkw, index) if domain.active: if domain.get_name not in self.list_active_domains(): diff --git a/server/config/SSSDConfigTest.py b/server/config/SSSDConfigTest.py index 3d8b596ac..41cd270e3 100644 --- a/server/config/SSSDConfigTest.py +++ b/server/config/SSSDConfigTest.py @@ -780,6 +780,23 @@ class SSSDConfigTestSSSDDomain(unittest.TestCase): domain.remove_option('nosuchoption') self.assertFalse('nosuchoption' in domain.get_all_options().keys()) + def testSetName(self): + domain = SSSDConfig.SSSDDomain('sssd', self.schema) + + # Positive test - Change the name once + domain.set_name('sssd2'); + self.assertEqual(domain.get_name(), 'sssd2') + self.assertEqual(domain.oldname, 'sssd') + + # Positive test - Change the name a second time + domain.set_name('sssd3') + self.assertEqual(domain.get_name(), 'sssd3') + self.assertEqual(domain.oldname, 'sssd') + + # Negative test - try setting the name to a non-string + self.assertRaises(TypeError, + domain.set_name, 4) + class SSSDConfigTestSSSDConfig(unittest.TestCase): def setUp(self): pass @@ -1122,9 +1139,11 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): self.assertTrue('IPA' in sssdconfig.list_domains()) self.assertTrue('IPA' in sssdconfig.list_active_domains()) + self.assertTrue(sssdconfig.has_section('domain/IPA')) sssdconfig.delete_domain('IPA') self.assertFalse('IPA' in sssdconfig.list_domains()) self.assertFalse('IPA' in sssdconfig.list_active_domains()) + self.assertFalse(sssdconfig.has_section('domain/IPA')) def testSaveDomain(self): sssdconfig = SSSDConfig.SSSDConfig("etc/sssd.api.conf", @@ -1148,6 +1167,23 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): # Negative Test - Type Error self.assertRaises(TypeError, sssdconfig.save_domain, self) + # Positive test - Change the domain name and save it + domain.set_name('example.com2') + self.assertEqual(domain.name,'example.com2') + self.assertEqual(domain.oldname,'example.com') + sssdconfig.save_domain(domain) + + self.assertTrue('example.com2' in sssdconfig.list_domains()) + self.assertTrue('example.com2' in sssdconfig.list_active_domains()) + self.assertTrue(sssdconfig.has_section('domain/example.com2')) + self.assertEqual(sssdconfig.get('domain/example.com2', + 'ldap_uri'), + 'ldap://ldap.example.com') + self.assertFalse('example.com' in sssdconfig.list_domains()) + self.assertFalse('example.com' in sssdconfig.list_active_domains()) + self.assertFalse('example.com' in sssdconfig.list_inactive_domains()) + self.assertFalse(sssdconfig.has_section('domain/example.com')) + if __name__ == "__main__": error = 0 -- cgit