Here it is Friday afternoon, and my efforts to lash my brain into actual technology creation are becoming less and less effective. So let’s dabble in meta-REST instead, shall we?
On Wednesday we unveiled the first public draft of the APIs for the Sun Cloud. There was a ton of feedback, in comments, twitter, email, and the project forums.
One of the comments wondered why, to create things, for example a VM in a
Cluster, you needed to POST to a special
create-vm URI. Why
not just POST the representation of the VM to the cluster?
Uh, well, because when we cooked up the idea of special purpose “controller” URIs, we liked it so much that we went overboard. Unless someone has a good reason not to, I’m going to go and make the obvious changes: To create a Cluster or an Address or a VNet, POST the right representation to a VDC’s URI. To create a VM, POST to a cluster’s URI.
Which, by the way, will require almost no code changes.
The next argument is about all the other “controller” functions. Deploying
a model, starting and stopping and rebooting a machine, attaching networks.
The argument is that it’d be more RESTful to have some
fields in the appropriate representations, and just update those fields to the
desired new state values.
Now, doing this, once again, would require almost no code changes.
But I don’t buy it, and here’s why. If I want to update some fields in an existing resource, I’m inclined to think about PUT. But that doesn’t work because it’s supposed to be idempotent, and rebooting a server sure isn’t. Well, OK, do it with POST I guess; no biggie.
But you’re not really changing a state, you’re
requesting a specific set of actions to happen, as a result of which the state
may or may not attain the desired value. In fact, when you hit the
deploy switch, the state
deploying and then after some unpredictable amount
of time to
deployed. And the
is the classic case of a box with a big red switch on the side; the problem is
how to push the switch.
So, the more I think of it, the more I think that these resources are like
buttons, with only one defined operation: push. People have been whining
about “write-only resources” but I don’t have a problem with that because it
seems accurate. The
halt buttons don’t really have any state,
so you shouldn’t expect anything useful from a GET.
And finally: as I’ve said a couple of times now, moving from one of these models to another has almost no effect on the underlying code. REST isn’t good because it’s REST, it’s good because it’s good. Which is to say, I’d like to hear an actual engineering/implementation argument as to why POSTing state is better than just hitting the switch.
(Related: Bill de hÓra’s “Just” use POST.)
(Related: Roy Fielding’s It is okay to use POST.)