cmd: add org member add/remove commands

This commit is contained in:
Simone Gotti 2019-05-14 11:25:12 +02:00
parent aeda922a7d
commit 8f959c4500
5 changed files with 207 additions and 8 deletions

View File

@ -0,0 +1,28 @@
// 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 (
"github.com/spf13/cobra"
)
var cmdOrgMember = &cobra.Command{
Use: "member",
Short: "member",
}
func init() {
cmdOrg.AddCommand(cmdOrgMember)
}

View File

@ -0,0 +1,68 @@
// 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"
"github.com/pkg/errors"
"github.com/sorintlab/agola/internal/services/gateway/api"
"github.com/sorintlab/agola/internal/services/types"
"github.com/spf13/cobra"
)
var cmdOrgMemberAdd = &cobra.Command{
Use: "add",
Short: "adds or updates an organization member",
Run: func(cmd *cobra.Command, args []string) {
if err := orgMemberAdd(cmd, args); err != nil {
log.Fatalf("err: %v", err)
}
},
}
type orgMemberAddOptions struct {
orgname string
username string
role string
}
var orgMemberAddOpts orgMemberAddOptions
func init() {
flags := cmdOrgMemberAdd.Flags()
flags.StringVarP(&orgMemberAddOpts.orgname, "orgname", "n", "", "organization name")
flags.StringVar(&orgMemberAddOpts.username, "username", "", "user name")
flags.StringVarP(&orgMemberAddOpts.role, "role", "r", "member", "member role (owner or member)")
cmdOrgMemberAdd.MarkFlagRequired("orgname")
cmdOrgMemberAdd.MarkFlagRequired("username")
cmdOrgMember.AddCommand(cmdOrgMemberAdd)
}
func orgMemberAdd(cmd *cobra.Command, args []string) error {
gwclient := api.NewClient(gatewayURL, token)
log.Infof("adding/updating member %q to organization %q with role %q", orgMemberAddOpts.username, orgMemberAddOpts.orgname, orgMemberAddOpts.role)
_, _, err := gwclient.AddOrgMember(context.TODO(), orgMemberAddOpts.orgname, orgMemberAddOpts.username, types.MemberRole(orgMemberAddOpts.role))
if err != nil {
return errors.Wrapf(err, "failed to add/update organization member")
}
return nil
}

View File

@ -0,0 +1,65 @@
// 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"
"github.com/pkg/errors"
"github.com/sorintlab/agola/internal/services/gateway/api"
"github.com/spf13/cobra"
)
var cmdOrgMemberRemove = &cobra.Command{
Use: "remove",
Short: "removes an organization member",
Run: func(cmd *cobra.Command, args []string) {
if err := orgMemberRemove(cmd, args); err != nil {
log.Fatalf("err: %v", err)
}
},
}
type orgMemberRemoveOptions struct {
orgname string
username string
}
var orgMemberRemoveOpts orgMemberRemoveOptions
func init() {
flags := cmdOrgMemberRemove.Flags()
flags.StringVarP(&orgMemberRemoveOpts.orgname, "orgname", "n", "", "organization name")
flags.StringVar(&orgMemberRemoveOpts.username, "username", "", "user name")
cmdOrgMemberRemove.MarkFlagRequired("orgname")
cmdOrgMemberRemove.MarkFlagRequired("username")
cmdOrgMember.AddCommand(cmdOrgMemberRemove)
}
func orgMemberRemove(cmd *cobra.Command, args []string) error {
gwclient := api.NewClient(gatewayURL, token)
log.Infof("removing member %q from organization %q", orgMemberRemoveOpts.username, orgMemberRemoveOpts.orgname)
_, err := gwclient.RemoveOrgMember(context.TODO(), orgMemberRemoveOpts.orgname, orgMemberRemoveOpts.username)
if err != nil {
return errors.Wrapf(err, "failed to remove organization member")
}
return nil
}

View File

@ -27,6 +27,8 @@ import (
"strconv"
"strings"
"github.com/sorintlab/agola/internal/services/types"
"github.com/pkg/errors"
)
@ -396,3 +398,21 @@ func (c *Client) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*OrgResp
func (c *Client) DeleteOrg(ctx context.Context, orgRef string) (*http.Response, error) {
return c.getResponse(ctx, "DELETE", fmt.Sprintf("/orgs/%s", orgRef), nil, jsonContent, nil)
}
func (c *Client) AddOrgMember(ctx context.Context, orgRef, userRef string, role types.MemberRole) (*AddOrgMemberResponse, *http.Response, error) {
req := &AddOrgMemberRequest{
Role: role,
}
omj, err := json.Marshal(req)
if err != nil {
return nil, nil, err
}
res := new(AddOrgMemberResponse)
resp, err := c.getParsedResponse(ctx, "PUT", fmt.Sprintf("/orgs/%s/members/%s", orgRef, userRef), nil, jsonContent, bytes.NewReader(omj), res)
return res, resp, err
}
func (c *Client) RemoveOrgMember(ctx context.Context, orgRef, userRef string) (*http.Response, error) {
return c.getResponse(ctx, "DELETE", fmt.Sprintf("/orgs/%s/members/%s", orgRef, userRef), nil, jsonContent, nil)
}

View File

@ -200,17 +200,35 @@ func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
type OrgMemberResponse struct {
Organization *OrgResponse `json:"organization,omitempty"`
User *UserResponse `json:"user,omitempty"`
Role types.MemberRole `json:"role,omitempty"`
type OrgMembersResponse struct {
Organization *OrgResponse `json:"organization"`
Members []*OrgMemberResponse `json:"members"`
}
func createOrgMemberResponse(org *types.Organization, user *types.User, role types.MemberRole) *OrgMemberResponse {
type OrgMemberResponse struct {
User *UserResponse `json:"user"`
Role types.MemberRole `json:"role"`
}
func createOrgMemberResponse(user *types.User, role types.MemberRole) *OrgMemberResponse {
return &OrgMemberResponse{
User: createUserResponse(user),
Role: role,
}
}
type AddOrgMemberResponse struct {
Organization *OrgResponse `json:"organization"`
OrgMemberResponse
}
func createAddOrgMemberResponse(org *types.Organization, user *types.User, role types.MemberRole) *AddOrgMemberResponse {
return &AddOrgMemberResponse{
Organization: createOrgResponse(org),
User: createUserResponse(user),
Role: role,
OrgMemberResponse: OrgMemberResponse{
User: createUserResponse(user),
Role: role,
},
}
}
@ -247,7 +265,7 @@ func (h *AddOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
return
}
res := createOrgMemberResponse(ares.Org, ares.User, ares.OrganizationMember.MemberRole)
res := createAddOrgMemberResponse(ares.Org, ares.User, ares.OrganizationMember.MemberRole)
if err := httpResponse(w, http.StatusOK, res); err != nil {
h.log.Errorf("err: %+v", err)
}