github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  }
    35  
    36  func postCohorts(c *Command, r *http.Request, user *auth.UserState) Response {
    37  	var inst client.CohortAction
    38  	dec := json.NewDecoder(r.Body)
    39  	if err := dec.Decode(&inst); err != nil {
    40  		return BadRequest("cannot decode request body into cohort instruction: %v", err)
    41  	}
    42  	if dec.More() {
    43  		return BadRequest("spurious content after cohort instruction")
    44  	}
    45  
    46  	if inst.Action != "create" {
    47  		return BadRequest("unknown cohort action %q", inst.Action)
    48  	}
    49  
    50  	if len(inst.Snaps) == 0 {
    51  		// nothing to do ¯\_(ツ)_/¯
    52  		return SyncResponse(map[string]string{}, nil)
    53  	}
    54  
    55  	cohorts, err := getStore(c).CreateCohorts(context.TODO(), inst.Snaps)
    56  	if err != nil {
    57  		return InternalError(err.Error())
    58  	}
    59  	return SyncResponse(cohorts, nil)
    60  }