def create_host(self, name, description, variables='{}'): """Creates a host Args: name: The name of the host to create description: The description of the host variables: A json with the variables that will be set on the created host Returns: Host: The created host is successful, None otherwise Raises: InvalidVariables: The variables provided as argument is not valid json. """ try: _ = json.loads(variables) del _ except ValueError: raise InvalidVariables(variables) url = '{api}/hosts/'.format(api=self._tower.api) payload = {'name': name, 'description': description, 'inventory': self.id, 'enabled': True, 'instance_id': '', 'variables': variables} response = self._tower.session.post(url, data=json.dumps(payload)) return Host(self._tower, response.json()) if response.ok else None
def create_inventory(self, name, description, variables='{}'): """Creates an inventory. Args: name: The name of the inventory to create. description: The description of the inventory. variables: A json with the initial variables set on the inventory. Returns: Inventory: The created Inventory object on success, None otherwise. Raises: InvalidVariables: The variables provided as argument is not valid json. """ if not validate_json(variables): raise InvalidVariables(variables) payload = { 'name': name, 'description': description, 'organization': self.id, 'variables': variables } url = '{api}/inventories/'.format(api=self._tower.api) response = self._tower.session.post(url, json=payload) if not response.ok: self._logger.error( 'Error creating inventory "%s", response was "%s"', name, response.text) return Inventory(self._tower, response.json()) if response.ok else None
def create_group(self, name, description, variables='{}'): """Creates a group. Args: name: The name of the group to create. description: The description of the group. variables: A json with the variables that will be set on the created group. Returns: Group: The created group is successful, None otherwise. Raises: InvalidVariables: The variables provided as argument is not valid json. """ if not validate_json(variables): raise InvalidVariables(variables) url = '{api}/groups/'.format(api=self._tower.api) payload = { 'name': name, 'description': description, 'inventory': self.id, 'variables': variables } response = self._tower.session.post(url, json=payload) if not response.ok: self._logger.error('Error creating group "%s", response was "%s"', name, response.text) return Group(self._tower, response.json()) if response.ok else None
def create_inventory(self, name, description, variables='{}'): """Creates an inventory Args: name: The name of the inventory to create description: The description of the inventory variables: A json with the initial variables set on the inventory Returns: Inventory: The created Inventory object on success, None otherwise Raises: InvalidVariables: The variables provided as argument is not valid json. """ try: variables = json.loads(variables) except ValueError: raise InvalidVariables(variables) payload = { 'name': name, 'description': description, 'organization': self.id, 'variables': variables } url = '{api}/inventories/'.format(api=self._tower.api) response = self._tower.session.post(url, data=json.dumps(payload)) return Inventory(self._tower, response.json()) if response.ok else None
def create_host(self, name, description, variables='{}'): """Creates a host. Args: name: The name of the host to create. description: The description of the host. variables: A json with the variables that will be set on the created host. Returns: Host: The created host is successful, None otherwise. Raises: InvalidVariables: The variables provided as argument is not valid json. """ if not validate_json(variables): raise InvalidVariables(variables) url = f'{self._tower.api}/hosts/' payload = { 'name': name, 'description': description, 'inventory': self.id, 'enabled': True, 'instance_id': '', 'variables': variables } response = self._tower.session.post(url, json=payload) if not response.ok: self._logger.error('Error creating host "%s", response was "%s"', name, response.text) return Host(self._tower, response.json()) if response.ok else None