Skip to content

Commit

Permalink
Add account user delete command
Browse files Browse the repository at this point in the history
Add new command for account user
update README.md
  • Loading branch information
epiHATR committed Aug 12, 2022
1 parent def4ade commit 19b1e9c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 20 deletions.
48 changes: 32 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ extract windows-amd64-v0.x.x.zip to a folder
| [cloudflare account user add](#cmd_cloudflare_account_user_add) | add new user to a Cloudflare account/organization |
| [cloudflare account user list](#cmd_cloudflare_account_user_list) | manage users in a Cloudflare account/organization |
| [cloudflare account user show](#cmd_cloudflare_account_user_show) | show information details of a user in a Cloudflare account/organization |
| [cloudflare account user delete](#cmd_cloudflare_account_user_delete) | remove an user from a Cloudflare account/organization |
| | |
| [cloudflare zone list](#cmd_cloudflare_zone_list) | list Cloudflare zone in account |
| [cloudflare zone show](#cmd_cloudflare_zone_show) | show Cloudflare zone common information |
Expand Down Expand Up @@ -168,10 +169,10 @@ cloudflare account update [ --account-id ]
[ --data | -d ]

#global flags
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
```

#### cloudflare account role <a name="cmd_cloudflare_account_role"></a>
Expand Down Expand Up @@ -250,10 +251,10 @@ cloudflare account user add [ --account-id ]
[ --role-id '--role-id=role1 --role-id=role2' ]

#global flags
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
```

#### cloudflare account user show<a name="cmd_cloudflare_account_user_show"></a>
Expand All @@ -270,6 +271,21 @@ cloudflare account user show [ --account-id ]
[ --debug ]
```

#### cloudflare account user delete<a name="cmd_cloudflare_account_user_delete"></a>
Show information details of a user in a Cloudlare managed accounts/organization

```bash
cloudflare account user delete [ --account-id ]
[ --user-id ]
[ --force]

#global flags
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
```

#### cloudflare zone list<a name="cmd_cloudflare_zone_list"></a>
<p>List all Cloudflare zone under account</p>

Expand Down Expand Up @@ -333,10 +349,10 @@ cloudflare zone dns list [ --zone-id | -i]
[ --type | -t ]

#global flags
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
```

#### cloudflare zone dns show <a name="cmd_cloudflare_zone_dns_show"></a>
Expand Down Expand Up @@ -472,10 +488,10 @@ cloudflare zone zone plan upgrade [ --zone-id ]
[ --plan-id | -i]
[ --plan-name | -n]
#global flags
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
```

## Development <a name="developments"></a>
Expand Down
2 changes: 1 addition & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Cloudflare API v0.1.45
- [x] cloudflare account user list
- [x] cloudflare account user add
- [x] cloudflare account user show
- [ ] cloudflare account user delete
- [x] cloudflare account user delete

- [x] cloudflare account role
- [x] cloudflare account role list
Expand Down
74 changes: 74 additions & 0 deletions cmd/account_user_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright © 2022 Hai.Tran (github.com/epiHATR)
*/
package cmd

import (
"cloudflare/pkg/api/account"
"cloudflare/pkg/consts/text"
"cloudflare/pkg/model/response"
"cloudflare/pkg/util/output"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

var accountUserDeleteAccountId string = ""
var accountUserDeleteUserId string = ""
var userForceDelete bool = false
var userDeleteConfirmText string = "no"

// userDeleteCmdCmd represents the userDeleteCmd command
var userDeleteCmdCmd = &cobra.Command{
Use: "delete",
Short: "delete an user from a Cloudflare account/organization",
Long: text.SubCmdHelpText,
Run: func(cmd *cobra.Command, args []string) {
errText := []string{}
if accountUserDeleteAccountId == "" {
errText = append(errText, "--account-id")
}
if accountUserDeleteUserId == "" {
errText = append(errText, "--user-id")
}
if len(errText) > 0 {
fmt.Fprintln(os.Stderr, "Error: Missing mandatory inputs, following flags are required: ", strings.Join(errText, ", "))
fmt.Fprintln(os.Stderr, text.SubCmdHelpText)
os.Exit(1)
}

if !userForceDelete {
for {
fmt.Print("Please confirm your action (yes/no): ")
fmt.Scan(&userDeleteConfirmText)
if strings.ToLower(userDeleteConfirmText) == "yes" || strings.ToLower(userDeleteConfirmText) == "no" {
break
}
}
} else {
userDeleteConfirmText = "yes"
}

if userDeleteConfirmText == "yes" {
res := account.AccountDeleteUser(accountUserDeleteAccountId, accountUserDeleteUserId)
if !res.Success {
fmt.Fprintln(os.Stderr, "Error: failed to delete user. The error is", res.Errors[0].Message)
os.Exit(1)
} else {
result := response.CmdResponse{}
result.Success = res.Success
output.PrintOut(result, flagQuery, flagOutput)
}
}
},
}

func init() {
accountUserCmd.AddCommand(userDeleteCmdCmd)
userDeleteCmdCmd.Flags().StringVarP(&accountUserDeleteAccountId, "account-id", "", "", "ID of Cloudflare account/organization")
userDeleteCmdCmd.Flags().StringVarP(&accountUserDeleteUserId, "user-id", "", "", "ID of Cloudflare account/organization")
userDeleteCmdCmd.Flags().BoolVarP(&userForceDelete, "--force", "f", false, "force delete user without confirm")
}
21 changes: 18 additions & 3 deletions pkg/api/account/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func GetAccountUsers(accountId string) response.AccountUsersResponse {
log.Println("get all user in Cloudflare managed accounts/organizations")
log.Println("get all users in Cloudflare managed accounts/organizations")
queryUrl := fmt.Sprintf(endpoint.ApiEndPoint+endpoint.AccountUsersEndpoint, accountId)

respData := request.CreateRequest(queryUrl, "GET", "")
Expand All @@ -27,7 +27,7 @@ func GetAccountUsers(accountId string) response.AccountUsersResponse {
}

func GetAccountUserDetail(accountId string, userId string) response.AccountUserDetailResponse {
log.Println("get details of an users in a Cloudflare managed accounts/organizations")
log.Println("get details of an user in a Cloudflare managed accounts/organizations")
queryUrl := fmt.Sprintf(endpoint.ApiEndPoint+endpoint.AccountUsersEndpoint+"/"+userId, accountId)

respData := request.CreateRequest(queryUrl, "GET", "")
Expand All @@ -42,7 +42,7 @@ func GetAccountUserDetail(accountId string, userId string) response.AccountUserD
}

func AccountAddUser(accountId string, email string, status string, roles []string) response.AccountUserDetailResponse {
log.Println("get details of an users in a Cloudflare managed accounts/organizations")
log.Println("get details of an user in a Cloudflare managed accounts/organizations")
queryUrl := fmt.Sprintf(endpoint.ApiEndPoint+endpoint.AccountUsersEndpoint, accountId)

reqBody := payload.UserAddRequest{}
Expand All @@ -63,3 +63,18 @@ func AccountAddUser(accountId string, email string, status string, roles []strin
}
return resObj
}

func AccountDeleteUser(accountId string, userId string) response.AccountUserDetailResponse {
log.Println("removing an user in a Cloudflare managed accounts/organizations")
queryUrl := fmt.Sprintf(endpoint.ApiEndPoint+endpoint.AccountUsersEndpoint+"/"+userId, accountId)

respData := request.CreateRequest(queryUrl, "DELETE", "")
resObj := response.AccountUserDetailResponse{}

err := json.Unmarshal(respData, &resObj)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
return resObj
}

0 comments on commit 19b1e9c

Please sign in to comment.