github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/connections.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package client
    21  
    22  import (
    23  	"net/url"
    24  )
    25  
    26  // Connection describes a connection between a plug and a slot.
    27  type Connection struct {
    28  	Slot      SlotRef `json:"slot"`
    29  	Plug      PlugRef `json:"plug"`
    30  	Interface string  `json:"interface"`
    31  	// Manual is set for connections that were established manually.
    32  	Manual bool `json:"manual"`
    33  	// Gadget is set for connections that were enabled by the gadget snap.
    34  	Gadget bool `json:"gadget"`
    35  	// SlotAttrs is the list of attributes of the slot side of the connection.
    36  	SlotAttrs map[string]interface{} `json:"slot-attrs,omitempty"`
    37  	// PlugAttrs is the list of attributes of the plug side of the connection.
    38  	PlugAttrs map[string]interface{} `json:"plug-attrs,omitempty"`
    39  }
    40  
    41  // Connections contains information about connections, as well as related plugs
    42  // and slots.
    43  type Connections struct {
    44  	// Established is the list of connections that are currently present.
    45  	Established []Connection `json:"established"`
    46  	// Undersired is a list of connections that are manually denied.
    47  	Undesired []Connection `json:"undesired"`
    48  	Plugs     []Plug       `json:"plugs"`
    49  	Slots     []Slot       `json:"slots"`
    50  }
    51  
    52  // ConnectionOptions contains criteria for selecting matching connections, plugs
    53  // and slots.
    54  type ConnectionOptions struct {
    55  	// Snap selects connections with the snap on one of the sides, as well
    56  	// as plugs and slots of a given snap.
    57  	Snap string
    58  	// Interface selects connections, plugs or slots using given interface.
    59  	Interface string
    60  	// All when true, selects established and undesired connections as well
    61  	// as all disconnected plugs and slots.
    62  	All bool
    63  }
    64  
    65  // Connections returns matching plugs, slots and their connections. Unless
    66  // specified by matching options, returns established connections.
    67  func (client *Client) Connections(opts *ConnectionOptions) (Connections, error) {
    68  	var conns Connections
    69  	query := url.Values{}
    70  	if opts != nil && opts.Snap != "" {
    71  		query.Set("snap", opts.Snap)
    72  	}
    73  	if opts != nil && opts.Interface != "" {
    74  		query.Set("interface", opts.Interface)
    75  	}
    76  	if opts != nil && opts.All {
    77  		query.Set("select", "all")
    78  	}
    79  	_, err := client.doSync("GET", "/v2/connections", query, nil, nil, &conns)
    80  	return conns, err
    81  }