go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth_service/impl/servers/imports/server.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package imports contains Imports endpoints implementation.
    16  package imports
    17  
    18  import (
    19  	"encoding/json"
    20  
    21  	"go.chromium.org/luci/common/errors"
    22  	"go.chromium.org/luci/server/router"
    23  
    24  	"go.chromium.org/luci/auth_service/impl/model"
    25  )
    26  
    27  type GroupsJSON struct {
    28  	Groups    []string `json:"group_imports"`
    29  	AuthDBRev int64    `json:"auth_db_rev"`
    30  }
    31  
    32  // HandleTarballIngestHandler handles the endpoint for ingesting tarballs
    33  // returns a JSON response of the groups that were found in the tarball.
    34  func HandleTarballIngestHandler(ctx *router.Context) error {
    35  	c, r, w := ctx.Request.Context(), ctx.Request, ctx.Writer
    36  	var err error
    37  	tarballName := ctx.Params.ByName("tarballName")
    38  	groups, revision, err := model.IngestTarball(c, tarballName, r.Body)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	groupResp, err := json.Marshal(GroupsJSON{
    44  		Groups:    groups,
    45  		AuthDBRev: revision,
    46  	})
    47  
    48  	w.Header().Set("Content-Type", "application/json")
    49  
    50  	if err != nil {
    51  		return errors.Annotate(err, "Error while marshaling JSON").Err()
    52  	}
    53  
    54  	if _, err = w.Write(groupResp); err != nil {
    55  		return errors.Annotate(err, "Error while writing JSON").Err()
    56  	}
    57  
    58  	return err
    59  }