Skip to content

Commit 72741ab

Browse files
feat: add remove family of iam commands (#144)
Co-authored-by: Davide Bianchi <[email protected]>
1 parent 9aa55dc commit 72741ab

File tree

17 files changed

+919
-14
lines changed

17 files changed

+919
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- `company iam add group-member` command
2020
- `company iam edit serviceaccount` command
2121
- `company iam edit group` command
22+
- `company iam remove user` command
23+
- `company iam remove group` command
24+
- `company iam remove serviceaccount` command
25+
- `company iam remove group-member` command
2226

2327
### Changed
2428

@@ -31,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3135
### Fixed
3236

3337
- remove conflicting shortand flag `-v` from `miactl marketplace delete` command
38+
- creation of basic auth service account
3439

3540
## [0.10.0] - 2023-12-20
3641

docs/30_commands.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,85 @@ Available flags for the command:
346346
- `--group-id`, the id of the group to edit
347347
- `--role`, the new Company role of the group
348348

349+
#### remove user
350+
351+
The `company iam remove user` subcommand allows you to remove a user from a company. Alternatively you can use the
352+
`no-include-groups` flag for only remove the role directly associated to a user, but leave intact its groups memberships.
353+
354+
Usage:
355+
356+
```sh
357+
miactl company iam remove user [flags]
358+
```
359+
360+
Available flags for the command:
361+
362+
- `--endpoint`, to set the Console endpoint (default is `https://console.cloud.mia-platform.eu`)
363+
- `--certificate-authority`, to provide the path to a custom CA certificate
364+
- `--insecure-skip-tls-verify`, to disallow the check the validity of the certificate of the remote endpoint
365+
- `--context`, to specify a different context from the currently selected one
366+
- `--company-id`, to set the ID of the desired Company
367+
- `--user-id`, the id of the user to remove
368+
- `--no-include-groups`, set this flag for keeping the user memberhip, and only remove the role attached to the user
369+
370+
#### remove group
371+
372+
The `company iam remove group` subcommand allows you to remove a group and all its memberships from a company.
373+
374+
Usage:
375+
376+
```sh
377+
miactl company iam remove group [flags]
378+
```
379+
380+
Available flags for the command:
381+
382+
- `--endpoint`, to set the Console endpoint (default is `https://console.cloud.mia-platform.eu`)
383+
- `--certificate-authority`, to provide the path to a custom CA certificate
384+
- `--insecure-skip-tls-verify`, to disallow the check the validity of the certificate of the remote endpoint
385+
- `--context`, to specify a different context from the currently selected one
386+
- `--company-id`, to set the ID of the desired Company
387+
- `--group-id`, the id of the group to remove
388+
389+
#### remove serviceaccount
390+
391+
The `company iam remove serviceaccount` subcommand allows you to removeo a service account in your Company.
392+
393+
Usage:
394+
395+
```sh
396+
miactl company iam remove serviceaccount [flags]
397+
```
398+
399+
Available flags for the command:
400+
401+
- `--endpoint`, to set the Console endpoint (default is `https://console.cloud.mia-platform.eu`)
402+
- `--certificate-authority`, to provide the path to a custom CA certificate
403+
- `--insecure-skip-tls-verify`, to disallow the check the validity of the certificate of the remote endpoint
404+
- `--context`, to specify a different context from the currently selected one
405+
- `--company-id`, to set the ID of the desired Company
406+
- `--service-account-id`, the id of the service account to remove
407+
408+
#### remove group-member
409+
410+
The `company iam remove group-member` subcommand allows you to remove one or more users from a group in your Company.
411+
412+
Usage:
413+
414+
```sh
415+
miactl company iam remove group-member [flags]
416+
```
417+
418+
Available flags for the command:
419+
420+
- `--group-id`, the group id where to remove the users
421+
- `--user-id`, the list of user ids to remove from the group
422+
- `--endpoint`, to set the Console endpoint (default is `https://console.cloud.mia-platform.eu`)
423+
- `--certificate-authority`, to provide the path to a custom CA certificate
424+
- `--insecure-skip-tls-verify`, to disallow the check the validity of the certificate of the remote endpoint
425+
- `--context`, to specify a different context from the currently selected one
426+
- `--company-id`, to set the ID of the desired Company
427+
349428
## project
350429

351430
This command allows you to manage `miactl` Projects.

internal/client/response.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ func (r *Response) ParseResponse(obj interface{}) error {
9090
return r.err
9191
}
9292

93-
err := json.Unmarshal(r.body, obj)
94-
if err != nil && err != io.EOF {
95-
return fmt.Errorf("error during response parsing: %w", err)
96-
}
97-
9893
return parseBody(r.body, obj)
9994
}
10095

internal/clioptions/clioptions.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ type CLIOptions struct {
4848

4949
IAMRole string
5050

51-
UserEmail string
52-
UserID string
51+
UserEmail string
52+
UserID string
53+
KeepUserGroupMemeberships bool
5354

5455
UserEmails []string
56+
UserIDs []string
5557
GroupID string
5658

5759
ServiceAccountID string
@@ -153,6 +155,10 @@ func (o *CLIOptions) AddEditServiceAccountFlags(flags *pflag.FlagSet) {
153155
flags.StringVarP(&o.ServiceAccountID, "service-account-id", "", "", "the service account id to edit")
154156
}
155157

158+
func (o *CLIOptions) AddRemoveServiceAccountFlags(flags *pflag.FlagSet) {
159+
flags.StringVarP(&o.ServiceAccountID, "service-account-id", "", "", "the service account id to remove")
160+
}
161+
156162
func (o *CLIOptions) AddNewUserFlags(flags *pflag.FlagSet) {
157163
flags.StringVarP(&o.IAMRole, "role", "r", "", "the company role of the user")
158164
flags.StringVarP(&o.UserEmail, "email", "", "", "the email of the user to add")
@@ -163,11 +169,16 @@ func (o *CLIOptions) AddEditUserFlags(flags *pflag.FlagSet) {
163169
flags.StringVarP(&o.UserID, "user-id", "", "", "the user id to edit")
164170
}
165171

172+
func (o *CLIOptions) AddRemoveUserFlags(flags *pflag.FlagSet) {
173+
flags.StringVarP(&o.UserID, "user-id", "", "", "the user id to remove")
174+
flags.BoolVarP(&o.KeepUserGroupMemeberships, "no-include-groups", "", false, "keep the user membership in the company groups")
175+
}
176+
166177
func (o *CLIOptions) CreateNewGroupFlags(flags *pflag.FlagSet) {
167178
flags.StringVarP(&o.IAMRole, "role", "r", "", "the company role of the group")
168179
}
169180

170-
func (o *CLIOptions) AddMemberToGroupFlags(flags *pflag.FlagSet) {
181+
func (o *CLIOptions) AddNewMembersToGroupFlags(flags *pflag.FlagSet) {
171182
flags.StringSliceVarP(&o.UserEmails, "user-email", "", []string{}, "the list of user email to add to the group")
172183
flags.StringVarP(&o.GroupID, "group-id", "", "", "the group id where to add the users")
173184
}
@@ -177,6 +188,15 @@ func (o *CLIOptions) AddEditGroupFlags(flags *pflag.FlagSet) {
177188
flags.StringVarP(&o.GroupID, "group-id", "", "", "the group id to edit")
178189
}
179190

191+
func (o *CLIOptions) AddRemoveGroupFlags(flags *pflag.FlagSet) {
192+
flags.StringVarP(&o.GroupID, "group-id", "", "", "the group id to remove")
193+
}
194+
195+
func (o *CLIOptions) AddRemoveMembersFromGroupFlags(flags *pflag.FlagSet) {
196+
flags.StringSliceVarP(&o.UserIDs, "user-id", "", []string{}, "the list of user id to remove to the group")
197+
flags.StringVarP(&o.GroupID, "group-id", "", "", "the group id where to remove the users")
198+
}
199+
180200
func (o *CLIOptions) AddMarketplaceApplyFlags(cmd *cobra.Command) {
181201
cmd.Flags().StringArrayVarP(&o.MarketplaceResourcePaths, "file-path", "f", []string{}, "paths to JSON/YAML files or folder of files containing a Marketplace item definition")
182202
err := cmd.MarkFlagRequired("file-path")

internal/cmd/company/iam.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ via a group or through service accounts.`,
3939
iam.ListCmd(o),
4040
iam.AddCmd(o),
4141
iam.EditCmd(o),
42+
iam.RemoveCmd(o),
4243
)
4344

4445
return cmd

internal/cmd/company/iam/group/add_member.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func AddMemberCmd(options *clioptions.CLIOptions) *cobra.Command {
4747
},
4848
}
4949

50-
options.AddMemberToGroupFlags(cmd.Flags())
50+
options.AddNewMembersToGroupFlags(cmd.Flags())
5151
return cmd
5252
}
5353

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright Mia srl
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package group
17+
18+
import (
19+
"context"
20+
"fmt"
21+
22+
"github.com/mia-platform/miactl/internal/client"
23+
"github.com/mia-platform/miactl/internal/clioptions"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
const (
28+
removeGroupTemplate = "/api/companies/%s/groups/%s"
29+
)
30+
31+
func RemoveCmd(options *clioptions.CLIOptions) *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: "group",
34+
Short: "Remove a group from a company",
35+
Long: "Remove a group from a company",
36+
37+
Args: cobra.NoArgs,
38+
Run: func(cmd *cobra.Command, args []string) {
39+
restConfig, err := options.ToRESTConfig()
40+
cobra.CheckErr(err)
41+
client, err := client.APIClientForConfig(restConfig)
42+
cobra.CheckErr(err)
43+
44+
err = removeCompanyGroup(cmd.Context(), client, restConfig.CompanyID, options.GroupID)
45+
cobra.CheckErr(err)
46+
},
47+
}
48+
49+
options.AddRemoveGroupFlags(cmd.Flags())
50+
return cmd
51+
}
52+
53+
func removeCompanyGroup(ctx context.Context, client *client.APIClient, companyID, groupID string) error {
54+
if len(companyID) == 0 {
55+
return fmt.Errorf("company id is required, please set it via flag or context")
56+
}
57+
58+
if len(groupID) == 0 {
59+
return fmt.Errorf("the group id is required")
60+
}
61+
62+
resp, err := client.
63+
Delete().
64+
APIPath(fmt.Sprintf(removeGroupTemplate, companyID, groupID)).
65+
Do(ctx)
66+
67+
if err != nil {
68+
return err
69+
}
70+
71+
if err := resp.Error(); err != nil {
72+
return err
73+
}
74+
75+
fmt.Printf("group %s successfully removed\n", groupID)
76+
return nil
77+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright Mia srl
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package group
17+
18+
import (
19+
"context"
20+
"fmt"
21+
22+
"github.com/mia-platform/miactl/internal/client"
23+
"github.com/mia-platform/miactl/internal/clioptions"
24+
"github.com/mia-platform/miactl/internal/resources"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
const (
29+
removeMemberTemplate = "/api/companies/%s/groups/%s/members"
30+
)
31+
32+
func RemoveMemberCmd(options *clioptions.CLIOptions) *cobra.Command {
33+
cmd := &cobra.Command{
34+
Use: "group-member",
35+
Short: "Remove one or more users from a group",
36+
Long: "Remove one or more users from a company group. The users can be removed via their ids",
37+
38+
Args: cobra.NoArgs,
39+
Run: func(cmd *cobra.Command, args []string) {
40+
restConfig, err := options.ToRESTConfig()
41+
cobra.CheckErr(err)
42+
client, err := client.APIClientForConfig(restConfig)
43+
cobra.CheckErr(err)
44+
45+
err = removeMemberFromGroup(cmd.Context(), client, restConfig.CompanyID, options.GroupID, options.UserIDs)
46+
cobra.CheckErr(err)
47+
},
48+
}
49+
50+
options.AddRemoveMembersFromGroupFlags(cmd.Flags())
51+
return cmd
52+
}
53+
54+
func removeMemberFromGroup(ctx context.Context, client *client.APIClient, companyID, groupID string, userIDs []string) error {
55+
if len(companyID) == 0 {
56+
return fmt.Errorf("company id is required, please set it via flag or context")
57+
}
58+
59+
if len(groupID) == 0 {
60+
return fmt.Errorf("a group id is required")
61+
}
62+
63+
if len(userIDs) < 1 {
64+
return fmt.Errorf("at least one user id must be used")
65+
}
66+
67+
payload := resources.RemoveMembersToGroup{
68+
Members: userIDs,
69+
}
70+
71+
body, err := resources.EncodeResourceToJSON(payload)
72+
if err != nil {
73+
return fmt.Errorf("failed to encode request body: %w", err)
74+
}
75+
76+
resp, err := client.
77+
Delete().
78+
APIPath(fmt.Sprintf(removeMemberTemplate, companyID, groupID)).
79+
Body(body).
80+
Do(ctx)
81+
82+
if err != nil {
83+
return err
84+
}
85+
86+
if err := resp.Error(); err != nil {
87+
return err
88+
}
89+
90+
fmt.Println("the users has been removed from the group")
91+
return nil
92+
}

0 commit comments

Comments
 (0)