github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/daemon/api_cohort.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 daemon 21 22 import ( 23 "context" 24 "encoding/json" 25 "net/http" 26 27 "github.com/snapcore/snapd/client" 28 "github.com/snapcore/snapd/overlord/auth" 29 ) 30 31 var cohortsCmd = &Command{ 32 Path: "/v2/cohorts", 33 POST: postCohorts, 34 WriteAccess: authenticatedAccess{}, 35 } 36 37 func postCohorts(c *Command, r *http.Request, user *auth.UserState) Response { 38 var inst client.CohortAction 39 dec := json.NewDecoder(r.Body) 40 if err := dec.Decode(&inst); err != nil { 41 return BadRequest("cannot decode request body into cohort instruction: %v", err) 42 } 43 if dec.More() { 44 return BadRequest("spurious content after cohort instruction") 45 } 46 47 if inst.Action != "create" { 48 return BadRequest("unknown cohort action %q", inst.Action) 49 } 50 51 if len(inst.Snaps) == 0 { 52 // nothing to do ¯\_(ツ)_/¯ 53 return SyncResponse(map[string]string{}) 54 } 55 56 cohorts, err := storeFrom(c.d).CreateCohorts(context.TODO(), inst.Snaps) 57 if err != nil { 58 return InternalError(err.Error()) 59 } 60 return SyncResponse(cohorts) 61 }