def _ensure_present(self, user): update_password = self.parameters.pop('update_password') if user: self.parameters.pop('password_change', None) if not update_password: self.parameters.pop('password', None) modified_attributes = ctera_common.get_modified_attributes( user, self.parameters) if modified_attributes: self._ctera_portal.users.modify(self.parameters['name'], **modified_attributes) self.ansible_module.ctera_return_value().changed().msg( 'User modified').put(name=self.parameters['name']) else: self.ansible_module.ctera_return_value().skipped().msg( 'User details did not change').put( name=self.parameters['name']) else: self.parameters[ 'password_change'] = self._translate_password_change( self.parameters['password_change']) create_params = { k: v for k, v in self.parameters.items() if k in CteraPortalLocalUser._create_params } if create_params.get('password') is None: raise CTERAException( message="Cannot create new user without a password") self._ctera_portal.users.add(**create_params) self.ansible_module.ctera_return_value().changed().msg( 'User created').put(**create_params)
def _ensure_present(self, volume): if volume: modified_attributes = ctera_common.get_modified_attributes( volume, self.parameters) if modified_attributes: desired_size = modified_attributes.get('size') if desired_size is not None: self._ctera_filer.volumes.modify(self.parameters['name'], size=desired_size) self.ansible_module.ctera_return_value().changed().msg( 'Volume modified').put(name=self.parameters['name'], size=desired_size) else: self.ansible_module.ctera_return_value().skipped().msg( 'Currently you can only modify the volume size').put( name=self.parameters['name']) else: self.ansible_module.ctera_return_value().skipped().msg( 'Volume details did not change').put( name=self.parameters['name']) else: create_params = { k: v for k, v in self.parameters.items() if k in CteraFilerVolume._create_params } self._ctera_filer.volumes.add(**create_params) self.ansible_module.ctera_return_value().changed().msg( 'Volume created').put(**create_params)
def _ensure_present(self, user): if user: modified_attributes = ctera_common.get_modified_attributes( user, self.parameters) if modified_attributes: self._ctera_filer.users.modify(self.parameters['username'], **modified_attributes) self.ansible_module.ctera_return_value().changed().msg( 'User modified').put(username=self.parameters['username'], **modified_attributes) else: self.ansible_module.ctera_return_value().skipped().msg( 'User details did not change').put( username=self.parameters['username']) else: create_params = { k: v for k, v in self.parameters.items() if k in CteraFilerUser._create_params } if create_params.get('password') is None: raise CTERAException( message="Cannot create new user without a password") self._ctera_filer.users.add(**create_params) self.ansible_module.ctera_return_value().changed().msg( 'User created').put(**create_params)
def _handle_modify(self, tenant): messages = [] changed = False if tenant.pop('activation_status') == 'Disabled': self._ctera_portal.portals.undelete(self.parameters['name']) changed = True messages.append('Tenant was undeleted') modified_attributes = ctera_common.get_modified_attributes(tenant, self.parameters) if modified_attributes: new_plan = modified_attributes.pop('plan', None) if new_plan is not None: self._ctera_portal.portals.subscribe(self.parameters['name'], new_plan) changed = True messages.append('Plan was changed') self.ansible_module.ctera_return_value().put(plan=new_plan) if modified_attributes: messages.append('Modifying tenant details is not supported') else: messages.append('Tenant details did not change') if changed: self.ansible_module.ctera_return_value().changed() else: self.ansible_module.ctera_return_value().skipped() self.ansible_module.ctera_return_value().put(name=self.parameters['name']).msg(' '.join(messages))
def _ensure_static(self, config): modified_attributes = ctera_common.get_modified_attributes( config, self.parameters) if config['mode'] == 'static' and not modified_attributes: self.ansible_module.ctera_return_value().msg( "IP Configuration did not change") return self._ctera_filer.network.set_static_ipaddr( **ctera_common.filter_parameters( self.parameters, CteraFilerNetwork._set_static_params)) self.ansible_module.ctera_return_value().changed().msg( "IP Configuration set")
def _handle_modify(self, share): modified_attributes = ctera_common.get_modified_attributes( share, self.parameters) if modified_attributes: acl_changes = modified_attributes.get('acl') if acl_changes is not None: modified_attributes['acl'] = [ self._make_ShareAccessControlEntry(acl_entry) for acl_entry in acl_changes ] self._ctera_filer.shares.modify(self.parameters['name'], **modified_attributes) self.ansible_module.ctera_return_value().changed().msg( 'Share modified').put(name=self.parameters['name']) else: self.ansible_module.ctera_return_value().skipped().msg( 'Share details did not change').put( name=self.parameters['name'])
def _ensure_present(self, plan): if plan: modified_attributes = ctera_common.get_modified_attributes( plan, self.parameters) if modified_attributes: self._ctera_portal.plans.modify( self.parameters['name'], **self._translate_params_obj(modified_attributes)) self.ansible_module.ctera_return_value().changed().msg( 'Plan modified').put(name=self.parameters['name']) else: self.ansible_module.ctera_return_value().skipped().msg( 'Plan details did not change').put( name=self.parameters['name']) else: self._ctera_portal.plans.add( self.parameters['name'], **self._translate_params_obj(self.parameters)) self.ansible_module.ctera_return_value().changed().msg( 'Plan created').put(name=self.parameters['name'])
def _ensure_enabled(self, current_config): messages = {'changed': [], 'skipped': []} if current_config[self._mode_field] == gateway_enum.Mode.Enabled: messages['skipped'].append('%s already enabled' % self._share_type) else: self._manager.enable() messages['changed'].append('%s enabled' % self._share_type) current_config = self._get_current_config() modified_attributes = ctera_common.get_modified_attributes( current_config, self.parameters) if modified_attributes: self._manager.modify(**modified_attributes) messages['changed'].append('%s configuration updated' % self._share_type) else: messages['skipped'].append('%s configuration already up to date' % self._share_type) ctera_common.set_result(self.ansible_module, messages)
def _ensure_enabled(self, current_config): if current_config['mode'] == gateway_enum.Mode.Enabled: modified_attributes = ctera_common.get_modified_attributes( current_config, self.parameters) if modified_attributes: self._ctera_filer.syslog.modify(**modified_attributes) self.ansible_module.ctera_return_value().changed().msg( 'Updated Syslog server configuration').put( server=self.parameters['server']) else: self.ansible_module.ctera_return_value().msg( 'Syslog server details did not change').put( server=self.parameters['server']) else: enable_params = { k: v for k, v in self.parameters.items() if k in CteraFilerSyslog._enable_params } self._ctera_filer.syslog.enable(**enable_params) self.ansible_module.ctera_return_value().changed().msg( 'Syslog server enabled')
def test_get_modified_attributes_with__equal_list(self): current = dict(first='a', second=['b'], third='c') desired = dict(first='a', second=['b'], third='d') self.assertDictEqual(ctera_common.get_modified_attributes(current, desired), dict(third='d'))
def test_get_modified_attributes_not_in_desired(self): current = dict(first='a', second='b') desired = dict(third='c') self.assertDictEqual(ctera_common.get_modified_attributes(current, desired), {})
def test_get_modified_attributes_equal(self): current = dict(first='a', second='b') self.assertDictEqual(ctera_common.get_modified_attributes(current, current), {})