-
Notifications
You must be signed in to change notification settings - Fork 562
/
api_aliases.go
170 lines (151 loc) · 4.56 KB
/
api_aliases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2020 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 daemon
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
)
var (
aliasesCmd = &Command{
Path: "/v2/aliases",
GET: getAliases,
POST: changeAliases,
ReadAccess: openAccess{},
WriteAccess: authenticatedAccess{},
}
)
// aliasAction is an action performed on aliases
type aliasAction struct {
Action string `json:"action"`
Snap string `json:"snap"`
App string `json:"app"`
Alias string `json:"alias"`
// old now unsupported api
Aliases []string `json:"aliases"`
}
func changeAliases(c *Command, r *http.Request, user *auth.UserState) Response {
var a aliasAction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&a); err != nil {
return BadRequest("cannot decode request body into an alias action: %v", err)
}
if len(a.Aliases) != 0 {
return BadRequest("cannot interpret request, snaps can no longer be expected to declare their aliases")
}
var taskset *state.TaskSet
var err error
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
switch a.Action {
default:
return BadRequest("unsupported alias action: %q", a.Action)
case "alias":
taskset, err = snapstate.Alias(st, a.Snap, a.App, a.Alias)
case "unalias":
if a.Alias == a.Snap {
// Do What I mean:
// check if a snap is referred/intended
// or just an alias
var snapst snapstate.SnapState
err := snapstate.Get(st, a.Snap, &snapst)
if err != nil && !errors.Is(err, state.ErrNoState) {
return InternalError("%v", err)
}
if errors.Is(err, state.ErrNoState) { // not a snap
a.Snap = ""
}
}
if a.Snap != "" {
a.Alias = ""
taskset, err = snapstate.DisableAllAliases(st, a.Snap)
} else {
taskset, a.Snap, err = snapstate.RemoveManualAlias(st, a.Alias)
}
case "prefer":
taskset, err = snapstate.Prefer(st, a.Snap)
}
if err != nil {
return errToResponse(err, nil, BadRequest, "%v")
}
var summary string
switch a.Action {
case "alias":
summary = fmt.Sprintf(i18n.G("Setup alias %q => %q for snap %q"), a.Alias, a.App, a.Snap)
case "unalias":
if a.Alias != "" {
summary = fmt.Sprintf(i18n.G("Remove manual alias %q for snap %q"), a.Alias, a.Snap)
} else {
summary = fmt.Sprintf(i18n.G("Disable all aliases for snap %q"), a.Snap)
}
case "prefer":
summary = fmt.Sprintf(i18n.G("Prefer aliases of snap %q"), a.Snap)
}
change := newChange(st, a.Action, summary, []*state.TaskSet{taskset}, []string{a.Snap})
st.EnsureBefore(0)
return AsyncResponse(nil, change.ID())
}
type aliasStatus struct {
Command string `json:"command"`
Status string `json:"status"`
Manual string `json:"manual,omitempty"`
Auto string `json:"auto,omitempty"`
}
// getAliases produces a response with a map snap -> alias -> aliasStatus
func getAliases(c *Command, r *http.Request, user *auth.UserState) Response {
state := c.d.overlord.State()
state.Lock()
defer state.Unlock()
res := make(map[string]map[string]aliasStatus)
allStates, err := snapstate.All(state)
if err != nil {
return InternalError("cannot list local snaps: %v", err)
}
for snapName, snapst := range allStates {
if len(snapst.Aliases) != 0 {
snapAliases := make(map[string]aliasStatus)
res[snapName] = snapAliases
autoDisabled := snapst.AutoAliasesDisabled
for alias, aliasTarget := range snapst.Aliases {
aliasStatus := aliasStatus{
Manual: aliasTarget.Manual,
Auto: aliasTarget.Auto,
}
status := "auto"
tgt := aliasTarget.Effective(autoDisabled)
if tgt == "" {
status = "disabled"
tgt = aliasTarget.Auto
} else if aliasTarget.Manual != "" {
status = "manual"
}
aliasStatus.Status = status
aliasStatus.Command = snap.JoinSnapApp(snapName, tgt)
snapAliases[alias] = aliasStatus
}
}
}
return SyncResponse(res)
}