How to delete a Bundle in Groovy?

As the title says, I've been trying for hours (while watching the Red Sox win!) to code a way to delete a bundle. Using HPM's code to debug how it's done with apps/drivers, it's pretty straight forward BUT for bundles it seems to be different.

With apps/drivers the page source has this,


<button class="mdl-button mdl-js-button mdl-button--raised  mdl-button--accent pull-right" type="button" onclick="deleteApp(); return false">Delete</button>


<form action="/driver/edit/update" method="post" name="deleteform" id="deleteform">
    <input type="hidden" name="id" value="112" id="id" />
    <input type="hidden" name="_action_delete" value="Delete" class="delete btn btn-danger" />
</form>

and HPM uses that info like this...

        def params = [
			uri: "http://127.0.0.1:8080",
			path: "/driver/editor/update",
			requestContentType: "application/x-www-form-urlencoded",
			headers: [
				"Cookie": state.cookie
			],
			body: [
				id: id,
				"_action_delete": "Delete"
			],
			timeout: 300,
			textParser: true,
			ignoreSSLIssues: true
		]

		httpPost(params) { resp ->
			if (resp.data == null)
				paragraph "Success!"
			else {
				def matcherText = resp.data.text.replace("\n","").replace("\r","")
				def matcher = matcherText.find(/<div class="alert-close close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;<\/span><\/div>(.+?)<\/div>/)
			}
		}

Which runs:

function deleteApp() {
  var resp = confirm("Are you sure?");
  if (resp == true) {
      document.getElementById('deleteform').submit();
  }
}

BUT, Bundles page source shows that they don't have the code needed to virtually push the Delete button! Bundles only have this line but not the rest.

<button class="btn mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" style="width: 8em; margin-right: 0.5em;" onclick="deleteBundle()">Delete</button>

Which sends it to deleteBundle():

function deleteBundle() {
    if (confirm('Are you sure?')) {
        $.get('/bundle/delete/' + bundleId, function(data) {
            window.location.href = '/bundle/list';
        });
    }
}

Now if you're still reading :grin:...

I can put 'http://(hub ip):8080/bundle/delete/999' in a browser and it will delete the bundle and bring me back to the Bundle list page (like it says it will in the function).


SO, is there a way to do it in Groovy!? Without dumping me out to the bundle list screen?

Thanks

Want my HPM code for this?

Or just hints? :slight_smile:

Hint 1: Web Scaping. TheBearMay has a working example. Thanks @thebearmay
Hint 2: The latest platform has a new function that works even better.

:smiley:

3 Likes

Not quite sure I understand what you are asking... it seems httpGet() should work?
https://docs.hubitat.com/index.php?title=Common_Methods_Object#httpGet

2 Likes

That did it, thanks!

To close out this thread, this is what I have. It might be right, it might be wrong... but it works! :grinning:

def uninstallBundle(id) {
    login()
    if(logEnable) log.debug "In uninstallBundle - Uninstalling Bundle - ${id}"
	try{
		def params = [
            uri: "http://127.0.0.1:8080/bundle/delete/${id}",
			headers: [ "Cookie": state.cookie ],
			timeout: 300,
		]

		httpGet(params) { resp ->
            paragraph "Success!"
		}
	}
	catch (e) {
        paragraph "Failed!"
        log.error(getExceptionMessageWithLine(e))
	}
}
1 Like