Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
client,cmd/snap: rename skills to interfaces (part 2) #523
Merged
zyga
merged 3 commits into
snapcore:master
from
zyga:client-rename-skills-to-interfaces
Feb 26, 2016
Commits
Jump to file or symbol
Failed to load files and symbols.
| @@ -0,0 +1,150 @@ | ||
| +// -*- Mode: Go; indent-tabs-mode: t -*- | ||
| + | ||
| +/* | ||
| + * Copyright (C) 2016 Canonical Ltd | ||
| + * | ||
| + * This program is free software: you can redistribute it and/or modify | ||
| + * it under the terms of the GNU General Public License version 3 as | ||
| + * published by the Free Software Foundation. | ||
| + * | ||
| + * This program is distributed in the hope that it will be useful, | ||
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| + * GNU General Public License for more details. | ||
| + * | ||
| + * You should have received a copy of the GNU General Public License | ||
| + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| + * | ||
| + */ | ||
| + | ||
| +package client | ||
| + | ||
| +import ( | ||
| + "bytes" | ||
| + "encoding/json" | ||
| +) | ||
| + | ||
| +// Plug represents a capacity offered by a snap. | ||
| +type Plug struct { | ||
| + Name string `json:"name"` | ||
| + Snap string `json:"snap"` | ||
| + // NOTE: json format intentionally using old "skill" terminology | ||
|
|
||
| + Interface string `json:"type,omitempty"` | ||
| + Attrs map[string]interface{} `json:"attrs,omitempty"` | ||
| + Apps []string `json:"apps,omitempty"` | ||
| + Label string `json:"label,omitempty"` | ||
| +} | ||
| + | ||
| +// Slot represents the potential of a given snap to connect to a given plug. | ||
| +type Slot struct { | ||
| + Name string `json:"name"` | ||
| + Snap string `json:"snap"` | ||
| + // NOTE: json format intentionally using old "skill" terminology | ||
| + Interface string `json:"type,omitempty"` | ||
| + Attrs map[string]interface{} `json:"attrs,omitempty"` | ||
| + Apps []string `json:"apps,omitempty"` | ||
| + Label string `json:"label,omitempty"` | ||
| +} | ||
| + | ||
| +// PlugConnections represents a single plug and slots that are connected to it. | ||
| +type PlugConnections struct { | ||
niemeyer
Contributor
|
||
| + Plug | ||
| + // NOTE: json format intentionally using old "skill" terminology | ||
| + Connections []Slot `json:"granted_to"` | ||
niemeyer
Contributor
|
||
| +} | ||
| + | ||
| +// InterfaceAction represents an action performed on the interface system. | ||
| +type InterfaceAction struct { | ||
| + Action string `json:"action"` | ||
| + // NOTE: json format intentionally using old "skill" terminology | ||
| + Plug *Plug `json:"skill,omitempty"` | ||
| + Slot *Slot `json:"slot,omitempty"` | ||
| +} | ||
| + | ||
| +// AllPlugs returns information about all the plugs and their connections. | ||
| +func (client *Client) AllPlugs() (connections []PlugConnections, err error) { | ||
| + err = client.doSync("GET", "/2.0/skills", nil, nil, &connections) | ||
| + return | ||
| +} | ||
| + | ||
| +// performInterfaceAction performs a single action on the interface system. | ||
| +func (client *Client) performInterfaceAction(sa *InterfaceAction) error { | ||
| + b, err := json.Marshal(sa) | ||
| + if err != nil { | ||
| + return err | ||
| + } | ||
| + var rsp interface{} | ||
| + if err := client.doSync("POST", "/2.0/skills", nil, bytes.NewReader(b), &rsp); err != nil { | ||
| + return err | ||
| + } | ||
| + return nil | ||
| +} | ||
| + | ||
| +// Connect establishes a connection between a plug and a slot. | ||
| +// The plug and the slot must have the same interface. | ||
| +func (client *Client) Connect(plugSnapName, plugName, slotSnapName, slotName string) error { | ||
| + return client.performInterfaceAction(&InterfaceAction{ | ||
| + Action: "grant", | ||
| + Plug: &Plug{ | ||
| + Snap: plugSnapName, | ||
| + Name: plugName, | ||
| + }, | ||
| + Slot: &Slot{ | ||
| + Snap: slotSnapName, | ||
| + Name: slotName, | ||
| + }, | ||
| + }) | ||
| +} | ||
| + | ||
| +// Disconnect breaks the connection between a plug and a slot. | ||
| +func (client *Client) Disconnect(plugSnapName, plugName, slotSnapName, slotName string) error { | ||
| + return client.performInterfaceAction(&InterfaceAction{ | ||
| + Action: "revoke", | ||
| + Plug: &Plug{ | ||
| + Snap: plugSnapName, | ||
| + Name: plugName, | ||
| + }, | ||
| + Slot: &Slot{ | ||
| + Snap: slotSnapName, | ||
| + Name: slotName, | ||
| + }, | ||
| + }) | ||
| +} | ||
| + | ||
| +// AddPlug adds a plug to the interface system. | ||
| +func (client *Client) AddPlug(plug *Plug) error { | ||
| + return client.performInterfaceAction(&InterfaceAction{ | ||
| + Action: "add-skill", | ||
| + Plug: plug, | ||
| + }) | ||
| +} | ||
| + | ||
| +// RemovePlug removes a plug from the interface system. | ||
| +func (client *Client) RemovePlug(snapName, plugName string) error { | ||
| + return client.performInterfaceAction(&InterfaceAction{ | ||
| + Action: "remove-skill", | ||
| + Plug: &Plug{ | ||
| + Snap: snapName, | ||
| + Name: plugName, | ||
| + }, | ||
| + }) | ||
| +} | ||
| + | ||
| +// AddSlot adds a slot to the system. | ||
| +func (client *Client) AddSlot(slot *Slot) error { | ||
| + return client.performInterfaceAction(&InterfaceAction{ | ||
| + Action: "add-slot", | ||
| + Slot: slot, | ||
| + }) | ||
| +} | ||
| + | ||
| +// RemoveSlot removes a slot from the system. | ||
| +func (client *Client) RemoveSlot(snapName, slotName string) error { | ||
| + return client.performInterfaceAction(&InterfaceAction{ | ||
| + Action: "remove-slot", | ||
| + Slot: &Slot{ | ||
| + Snap: snapName, | ||
| + Name: slotName, | ||
| + }, | ||
| + }) | ||
| +} | ||
Oops, something went wrong.
If the intention is to fix it in stages, that sounds fine, but the note should be:
// TODO Fix terminology in JSON.
As it is it sounds like a design decision.