Esempio n. 1
0
 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)
Esempio n. 2
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)),
        # supports_check_mode=True,
        mutually_exclusive=[('name', 'ident')])

    # we initialize the Api object
    api = Api(url=module.params['url'], token=module.params['token'])

    ansible_facts = dict()

    # if no name or id is specified, we enumerate objects
    if not module.params['name'] and not module.params['ident']:
        res = get_list(api,
                       model=module.params['model'],
                       obj=module.params['obj'])
        ansible_facts['netbox_result'] = res

    if module.params['name'] and not module.params['ident']:
        res = get(api,
                  model=module.params['model'],
                  obj=module.params['obj'],
                  name=module.params['name'])
        ansible_facts['netbox_result'] = res

    if not module.params['name'] and module.params['ident']:
        res = get(api,
                  model=module.params['model'],
                  obj=module.params['obj'],
                  ident=module.params['ident'])
        ansible_facts['netbox_result'] = res

    module.exit_json(changed=False, result=res, ansible_facts=ansible_facts)
Esempio n. 3
0
 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)
Esempio n. 4
0
 def test_get_object(self):
     res = get(self.__api, model="ipam", obj="aggregates", ident=1)
     self.assertTrue(type(res) is dict)
     self.assertGreater(len(res), 1)
Esempio n. 5
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)