示例#1
0
    def delete(self, pk=None, fail_on_missing=False, **kwargs):
        """Remove the given object.

        If `fail_on_missing` is True, then the object's not being found is
        considered a failure; otherwise, a success with no change is reported.
        """
        # If we weren't given a primary key, determine which record we're
        # deleting.
        if not pk:
            existing_data = self._lookup(fail_on_missing=fail_on_missing,
                                         **kwargs)
            if not existing_data:
                return {'changed': False}
            pk = existing_data['id']

        # Attempt to delete the record.
        # If it turns out the record doesn't exist, handle the 404
        # appropriately (this is an okay response if `fail_on_missing` is
        # False).
        url = '%s%d/' % (self.endpoint, pk)
        debug.log('DELETE %s' % url, fg='blue', bold=True)
        try:
            client.delete(url)
            return {'changed': True}
        except exc.NotFound:
            if fail_on_missing:
                raise
            return {'changed': False}
示例#2
0
文件: base.py 项目: tee-jay/tower-cli
    def delete(self, pk=None, fail_on_missing=False, **kwargs):
        """Remove the given object.

        If `fail_on_missing` is True, then the object's not being found is
        considered a failure; otherwise, a success with no change is reported.
        """
        # If we weren't given a primary key, determine which record we're
        # deleting.
        if not pk:
            existing_data = self._lookup(fail_on_missing=fail_on_missing,
                                         **kwargs)
            if not existing_data:
                return {'changed': False}
            pk = existing_data['id']

        # Attempt to delete the record.
        # If it turns out the record doesn't exist, handle the 404
        # appropriately (this is an okay response if `fail_on_missing` is
        # False).
        url = '%s%d/' % (self.endpoint, pk)
        debug.log('DELETE %s' % url, fg='blue', bold=True)
        try:
            client.delete(url)
            return {'changed': True}
        except exc.NotFound:
            if fail_on_missing:
                raise
            return {'changed': False}
示例#3
0
 def write(self, pk=None, **kwargs):
     survey_input = kwargs.pop('survey_spec', None)
     if kwargs.get('extra_vars', None):
         kwargs['extra_vars'] = parser.process_extra_vars(
             kwargs['extra_vars'])
     ret = super(SurveyResource, self).write(pk=pk, **kwargs)
     if survey_input is not None and ret.get('id', None):
         if not isinstance(survey_input, dict):
             survey_input = json.loads(survey_input.strip(' '))
         if survey_input == {}:
             debug.log('Deleting the survey_spec.', header='details')
             r = client.delete(self._survey_endpoint(ret['id']))
         else:
             debug.log('Saving the survey_spec.', header='details')
             r = client.post(self._survey_endpoint(ret['id']),
                             data=survey_input)
         if r.status_code == 200:
             ret['changed'] = True
         if survey_input and not ret['survey_enabled']:
             debug.log('For survey to take effect, set survey_enabled'
                       ' field to True.', header='warning')
     return ret