From d18aadaf5c29caf1efe91796df91ae8cbd4c1518 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Tue, 1 Oct 2019 09:41:56 +0200 Subject: [PATCH] cmd: add project group update command --- cmd/agola/cmd/projectgroupcreate.go | 4 +- cmd/agola/cmd/projectgroupupdate.go | 89 +++++++++++++++++++++++++++++ services/gateway/client/client.go | 17 +++++- 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 cmd/agola/cmd/projectgroupupdate.go diff --git a/cmd/agola/cmd/projectgroupcreate.go b/cmd/agola/cmd/projectgroupcreate.go index 0a7e003..5beb263 100644 --- a/cmd/agola/cmd/projectgroupcreate.go +++ b/cmd/agola/cmd/projectgroupcreate.go @@ -75,11 +75,11 @@ func projectGroupCreate(cmd *cobra.Command, args []string) error { log.Infof("creating project group") - project, _, err := gwclient.CreateProjectGroup(context.TODO(), req) + projectGroup, _, err := gwclient.CreateProjectGroup(context.TODO(), req) if err != nil { return errors.Errorf("failed to create project group: %w", err) } - log.Infof("project group %s created, ID: %s", project.Name, project.ID) + log.Infof("project group %s created, ID: %s", projectGroup.Name, projectGroup.ID) return nil } diff --git a/cmd/agola/cmd/projectgroupupdate.go b/cmd/agola/cmd/projectgroupupdate.go new file mode 100644 index 0000000..94c1ae4 --- /dev/null +++ b/cmd/agola/cmd/projectgroupupdate.go @@ -0,0 +1,89 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + + gwapitypes "agola.io/agola/services/gateway/api/types" + gwclient "agola.io/agola/services/gateway/client" + + "github.com/spf13/cobra" + errors "golang.org/x/xerrors" +) + +var cmdProjectGroupUpdate = &cobra.Command{ + Use: "update", + Short: "update a project group", + Run: func(cmd *cobra.Command, args []string) { + if err := projectGroupUpdate(cmd, args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +type projectGroupUpdateOptions struct { + ref string + + name string + parentPath string + visibility string +} + +var projectGroupUpdateOpts projectGroupUpdateOptions + +func init() { + flags := cmdProjectGroupUpdate.Flags() + + flags.StringVarP(&projectGroupUpdateOpts.ref, "ref", "", "", "current project group path or id") + flags.StringVarP(&projectGroupUpdateOpts.name, "name", "n", "", "project group name") + flags.StringVar(&projectGroupUpdateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "user/user01/group01/subgroub01") or project group id where the project group should be moved`) + flags.StringVar(&projectGroupUpdateOpts.visibility, "visibility", "public", `project group visibility (public or private)`) + + if err := cmdProjectGroupUpdate.MarkFlagRequired("ref"); err != nil { + log.Fatal(err) + } + + cmdProjectGroup.AddCommand(cmdProjectGroupUpdate) +} + +func projectGroupUpdate(cmd *cobra.Command, args []string) error { + gwclient := gwclient.NewClient(gatewayURL, token) + + req := &gwapitypes.UpdateProjectGroupRequest{} + + flags := cmd.Flags() + if flags.Changed("name") { + req.Name = &projectGroupUpdateOpts.name + } + if flags.Changed("parent") { + req.ParentRef = &projectGroupUpdateOpts.parentPath + } + if flags.Changed("visibility") { + if !IsValidVisibility(projectGroupUpdateOpts.visibility) { + return errors.Errorf("invalid visibility %q", projectGroupUpdateOpts.visibility) + } + req.Name = &projectGroupUpdateOpts.visibility + } + + log.Infof("updating project group") + projectGroup, _, err := gwclient.UpdateProjectGroup(context.TODO(), projectGroupUpdateOpts.ref, req) + if err != nil { + return errors.Errorf("failed to update project group: %w", err) + } + log.Infof("project group %s update, ID: %s", projectGroup.Name, projectGroup.ID) + + return nil +} diff --git a/services/gateway/client/client.go b/services/gateway/client/client.go index 3399e23..fe86e4f 100644 --- a/services/gateway/client/client.go +++ b/services/gateway/client/client.go @@ -140,9 +140,20 @@ func (c *Client) CreateProjectGroup(ctx context.Context, req *gwapitypes.CreateP return nil, nil, err } - project := new(gwapitypes.ProjectResponse) - resp, err := c.getParsedResponse(ctx, "POST", "/projectgroups", nil, jsonContent, bytes.NewReader(reqj), project) - return project, resp, err + projectGroup := new(gwapitypes.ProjectResponse) + resp, err := c.getParsedResponse(ctx, "POST", "/projectgroups", nil, jsonContent, bytes.NewReader(reqj), projectGroup) + return projectGroup, resp, err +} + +func (c *Client) UpdateProjectGroup(ctx context.Context, projectGroupRef string, req *gwapitypes.UpdateProjectGroupRequest) (*gwapitypes.ProjectResponse, *http.Response, error) { + reqj, err := json.Marshal(req) + if err != nil { + return nil, nil, err + } + + projectGroup := new(gwapitypes.ProjectResponse) + resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projectgroups", url.PathEscape(projectGroupRef)), nil, jsonContent, bytes.NewReader(reqj), projectGroup) + return projectGroup, resp, err } func (c *Client) DeleteProjectGroup(ctx context.Context, projectGroupRef string) (*http.Response, error) {