def test_patch_object(self): object_name = 'aJaid0pei4waj2m' new_object_name = 'guta9IneeTei9fa' create(self.__api, model="dcim", obj="sites", data={ 'name': object_name, 'slug': object_name }) res = get(self.__api, model="dcim", obj="sites", name=object_name) self.assertTrue(type(res) is dict) self.assertIn('name', res) self.assertIn('slug', res) self.assertEquals(res['name'], object_name) self.assertEquals(res['slug'], object_name) res = patch(self.__api, model="dcim", obj="sites", name=object_name, data={"slug": new_object_name}) res = get(self.__api, model="dcim", obj="sites", name=object_name) self.assertTrue(type(res) is dict) self.assertIn('name', res) self.assertIn('slug', res) self.assertEquals(res['slug'], new_object_name) delete(self.__api, model="dcim", obj="sites", name=object_name)
def test_get_list_by_tenant(self): res = create(self.__api, model="dcim", obj="device-roles", data={ 'name': 'testdev9', 'slug': 'testdev9', 'color': 'ffffff' }) device_role_id = res['id'] res = create(self.__api, model="dcim", obj="sites", data={ 'name': 'testdev9', 'slug': 'testdev9' }) site_id = res['id'] res = create(self.__api, model="dcim", obj="device-types", data={ 'name': 'testdev9', 'slug': 'testdev9', 'manufacturer': 1, 'model': 'testdev9' }) device_type_id = res['id'] res = create(self.__api, model="dcim", obj="devices", data={ 'name': 'testdev9', 'slug': 'testdev9', 'device_role': device_role_id, 'site': site_id, 'device_type': device_type_id }) device_id = res['id'] res = get_list_grouped_by_tenant(self.__api, model="dcim", obj="devices") self.assertTrue(type(res) is dict) self.assertTrue('unclassified' in res) self.assertTrue('hosts' in res['unclassified']) self.assertTrue('testdev9' in res['unclassified']['hosts']) res = delete(self.__api, model="dcim", obj="devices", ident=device_id) res = delete(self.__api, model="dcim", obj="device-roles", ident=device_role_id) res = delete(self.__api, model="dcim", obj="device-types", ident=device_type_id) res = delete(self.__api, model="dcim", obj="sites", ident=site_id)
def test_create_object(self): """ """ res = create(self.__api, model="dcim", obj="sites", data={ 'name': 'testsite9', 'slug': 'testsite9' }) self.assertIn('id', res.keys()) self.__last_id = res['id']
def test_crud_object(self): res = create(self.__api, model="dcim", obj="sites", data={ 'name': 'ohvi7Xiew6VohSee1ael', 'slug': 'ohvi7Xiew6VohSee1ael' }) self.assertTrue(type(res) is dict) self.__last_id = res['id'] res = get(self.__api, model='dcim', obj='sites', ident=self.__last_id) self.assertTrue(type(res) is dict) res = delete(self.__api, model="dcim", obj="sites", name='ohvi7Xiew6VohSee1ael') self.assertTrue( type(res) is dict or type(res) is unicode and len(res) == 0)
def main(): module = AnsibleModule( argument_spec=dict(name=dict(required=False), ident=dict(required=False), model=dict(required=True), obj=dict(required=True), token=dict(required=True), url=dict(required=True), state=dict(required=True, choices=['present', 'absent']), template=dict(required=False), data=dict(required=False)), # supports_check_mode=True, mutually_exclusive=[('name', 'ident')]) api = Api(url=module.params['url'], token=module.params['token']) changed = False failed = False res = {} current = get(api, model=module.params['model'], obj=module.params['obj'], ident=module.params['ident'], name=module.params['name']) # state = present if module.params['state'] == 'present': if 'template' in module.params and module.params[ 'template'] is not None: # get current object # get the data from the template template = module.params['template'] env = Environment(loader=FileSystemLoader(path.dirname(template))) template = env.get_template(path.basename(template)) data = json.loads(template.render().replace("'", "\"")) elif 'data' in module.params: data = json.loads(module.params['data'].replace("'", "\"")) # if current object data and required object data are the same if not json_are_the_same(data, current): #FIX if 'detail' in current and current['detail'] == 'Not found.': res = create(api, model=module.params['model'], obj=module.params['obj'], name=module.params['name'], ident=module.params['ident'], data=data) else: res = update(api, model=module.params['model'], obj=module.params['obj'], name=module.params['name'], ident=module.params['ident'], data=data) # if update failed because of already used slug or name if ('name' in res and "already exists" in res['name'][0]) or ( 'slug' in res and "already exists" in res['slug'][0]): changed = False failed = False elif 'non_field_errors' in res: changed = False failed = False elif missing_field(res): changed = False failed = True else: changed = True result = res else: result = "Nothing changed." elif module.params['state'] == 'absent': res = delete(api, model=module.params['model'], obj=module.params['obj'], name=module.params['name'], ident=module.params['ident']) result = res else: raise Exception module.exit_json(changed=changed, result=result, failed=failed)