many: implement new REST API: GET /2.0/interfaces #559

Merged
merged 33 commits into from Mar 3, 2016

Conversation

Projects
None yet
3 participants
Contributor

zyga commented Mar 1, 2016

This branch implements the new REST API for listing interface connections.

The /2.0/interfaces endpoint now returns an object with two lists, one for plugs and one for slots. This change allows clients to discover disconnected slots. Each of the plugs and slots also contains a list of connections (possibly empty). Each connection is described by a plug or slot "reference", which holds only two identifier fields, "snap" and either "plug" or "slot".

There are supporting changes in the backend, client API and client commands (snap interfaces).

zyga added some commits Feb 29, 2016

client,cmd/snap: specialize Name to either Plug or Slot
This patch changes the public snappy API to refer to Plug.Name as .Plug
and Slot.Name as .Slot.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: add Repository.SlotConnections()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: change GET /2.0/interfaces to match spec
This patch changes the REST method for listing all the interfaces to
match the current spec. Instead of returning an array of plugs and their
connections it returns an object containing two arrays, one for plugs
and one for slots. This change allows for discovery of disconnected
plugs.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
client,cmd/snap: change wire format to match spec
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap: make snap interfaces display disconnected slots
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: use {plug,slot}Ref instead of {slot,plug}Connection
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: add json encoding struct tags
This patch adds struct tags for Plug and Slot so that there can be less
duplication in the daemon package.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: use Slot and Plug from interfaces to shorten code
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: add PlugRef and SlotRef
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: use PlugRef and SlotRef from interfaces package
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: add implementation of sorting for PlugRef and SlotRef
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: move rest of the sorting code to sorting.go
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: store Connections in each Slot and Plug
This patch adds a way to store connections inside each Slot and Plug.
While the repository doesn't actually use it in any way it is a
convenient data format to have so that we don't need to create similar
structures in the deamon code. The added advantage is that the core
can now compute this information in one go, consistently, while holding
only one lock.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: add InterfaceConnections()
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: use InterfaceConnections() to simplify implementation
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap: make snap interfaces treat ubuntu-core specially
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap: tweak help message
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
client,cmd/snap: unify internal and public api for working with inter…
…faces

This patch simplifies and unifies the public (client) API for
interfaces. Plug and PlugConnection is replaced with a better Plug (that
also holds connections) and a small PlugRef. The same change is applied
to Slot. This brings the API in sync with the server API.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
po: refresh translation templates
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
docs: update semantics and syntax of GET /2.0/interfaces
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces,daemon: use {Plug,Slot}.Name
This patch renames the last aspect of the slot/interface and plug.plug
vs plug.name rename. It attains parity with the client/ package. Both
plugs and slots have a .Name fields but that field is translated to
"plug" and "slot" in the type-less JSON representation.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
client: fix function comment for golint
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

@niemeyer niemeyer changed the title from interfaces,daemon,docs,client,cmd/snap: implement new REST API: GET /2.0/interfaces to many: implement new REST API: GET /2.0/interfaces Mar 1, 2016

client/interfaces.go
@@ -26,30 +26,44 @@ import (
// Plug represents a capacity offered by a snap.
type Plug struct {
+ Snap string `json:"snap"`
// NOTE: the discrepancy between go and json field name is intentional.
@niemeyer

niemeyer Mar 1, 2016

Contributor

Please drop the note.

client/interfaces.go
}
// Slot represents the potential of a given snap to connect to a given plug.
type Slot struct {
+ Snap string `json:"snap"`
// NOTE: the discrepancy between go and json field name is intentional.
@niemeyer

niemeyer Mar 1, 2016

Contributor

Please drop the note.

client/interfaces.go
- Plug
- Connections []Slot `json:"connections"`
+// InterfaceConnections contains information about all plugs, slots and their connections
+type InterfaceConnections struct {
@niemeyer

niemeyer Mar 1, 2016

Contributor

s/InterfaceConnections/Interfaces/

This lists plugs and slots, whether connected or not.

client/interfaces.go
-// AllPlugs returns information about all the plugs and their connections.
-func (client *Client) AllPlugs() (connections []PlugConnections, err error) {
+// InterfaceConnections returns information about all the plugs, slots and their connections.
+func (client *Client) InterfaceConnections() (connections InterfaceConnections, err error) {
@niemeyer

niemeyer Mar 1, 2016

Contributor

s/InterfaceConnections/Interfaces/

@niemeyer

niemeyer Mar 1, 2016

Contributor

Also, s/connections/interfaces/

daemon/api.go
- })
- }
- return SyncResponse(plugs)
+// getInterfaces returns a response with a list of all the plugs and slots and their connections.
@niemeyer

niemeyer Mar 1, 2016

Contributor

"returns a response with a list of all the plugs and slots" => "returns all plugs and slots"

client/interfaces.go
@@ -59,8 +73,8 @@ type InterfaceAction struct {
Slot *Slot `json:"slot,omitempty"`
}
-// AllPlugs returns information about all the plugs and their connections.
-func (client *Client) AllPlugs() (connections []PlugConnections, err error) {
+// InterfaceConnections returns information about all the plugs, slots and their connections.
@niemeyer

niemeyer Mar 1, 2016

Contributor

// Interfaces returns all plugs, slots and their connections.

cmd/snap/cmd_interfaces.go
- fmt.Fprintf(w, "%s:%s\t", plug.Snap, plug.Plug.Name)
+ // The OS snap (always ubuntu-core) is special and enable abbreviated
+ // display syntax on the plug-side of the connection.
+ if plug.Snap != "ubuntu-core" {
@niemeyer

niemeyer Mar 1, 2016

Contributor

Nitpick suggestion for coding style: in most cases it's easier for humans to read branching logic on the positive side.

So, first I'd list the "snap is ubuntu-core" one, then the alternative.

interfaces/repo.go
}
-type bySlotSnapAndName []*Slot
+// InterfaceConnections returns object holding a lists of all the plugs and slots and their connections.
@niemeyer

niemeyer Mar 1, 2016

Contributor

// Interfaces returns all plugs, slots, and their connections.

I wasn't considering having that and SlotRef/PlugRef in the internal APIs, but maybe that's a good idea.

interfaces/core.go
+ Name string `json:"slot"`
+}
+
+// InterfaceConnections contains information about all plugs, slots and their connections
@niemeyer

niemeyer Mar 1, 2016

Contributor

// Interfaces holds information about a list of plugs and slots, and their connections.

-
-type byPlugSnapAndName []*Plug
+// SlotConnections returns all of the plugs that are connected a given slot.
+func (r *Repository) SlotConnections(snapName, slotName string) []*Plug {
@niemeyer

niemeyer Mar 1, 2016

Contributor

That makes no sense if Plug and Slot have a list of Connections in it. Let's talk online on this one.

@zyga

zyga Mar 2, 2016

Contributor

I've fixed everything else. I see why this is controversial, let's talk as suggested and find a better way to do this.

zyga added some commits Mar 2, 2016

client: drop some NOTE comments
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
many: apply s/InterfaceConnection/Interfaces/g
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
many: use "interfaces" to refer to Interfaces{}
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: tweak function comment
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
daemon: tweak more stale "skills" comments
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
client: tweak function comment
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
cmd/snap: swap if/else logic so that positive case is first
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: tweak structure comment
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Contributor

niemeyer commented Mar 2, 2016

LGTM

Contributor

niemeyer commented Mar 3, 2016

}
-type bySlotSnapAndName []*Slot
+// Interfaces returns object holding a lists of all the plugs and slots and their connections.
+func (r *Repository) Interfaces() *Interfaces {
@mvo5

mvo5 Mar 3, 2016

Collaborator

Pardon my ignorance here, the branch is a bit long. Is this covered directly by tests? It seems like it is worthwhile to test if its not already.

@zyga

zyga Mar 3, 2016

Contributor

I have test for this, ironically piled up in a separate branch so that size doesn't grow beyond reviewable bounds.

+
+package interfaces
+
+type bySlotRef []SlotRef
@mvo5

mvo5 Mar 3, 2016

Collaborator

Is this covered by tests? If not we may consider it - TBH I'm sitting a bit on the fence, its so simple tests look almost like overkill. But then they are probably easy enough (and quick enough) to add :)

@zyga

zyga Mar 3, 2016

Contributor

This is not covered yet. I can conjure a a few quick tests for that.

@zyga

zyga Mar 3, 2016

Contributor

Done

Collaborator

mvo5 commented Mar 3, 2016

This looks good to me, I just have two questions and we need to know what to do about SlotConnections().

Contributor

zyga commented Mar 3, 2016

@mvo5 SlotConnections and a few other methods are now going away as they are entirely replaced by Interfaces(). I have this piled up after this branch lands.

interfaces: add extra tests for sorting methods
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

zyga added a commit that referenced this pull request Mar 3, 2016

Merge pull request #559 from zyga/interfaces-api-changes
many: implement new REST API: GET /2.0/interfaces

@zyga zyga merged commit 64e7592 into snapcore:master Mar 3, 2016

3 checks passed

Integration tests Success 67 tests run, 0 skipped, 0 failed.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage decreased (-0.1%) to 70.587%
Details

@zyga zyga deleted the zyga:interfaces-api-changes branch Mar 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment