diff --git a/README.md b/README.md index ff08a21..e6cc4cf 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 @@ -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 @@ -270,6 +271,21 @@ cloudflare account user show [ --account-id ] [ --debug ] ``` +#### cloudflare account user delete +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
List all Cloudflare zone under account
@@ -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 @@ -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 diff --git a/RELEASE.md b/RELEASE.md index 5598e51..dbf16a4 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -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 diff --git a/cmd/account_user_delete.go b/cmd/account_user_delete.go new file mode 100644 index 0000000..5952485 --- /dev/null +++ b/cmd/account_user_delete.go @@ -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") +} diff --git a/pkg/api/account/user.go b/pkg/api/account/user.go index ee00f0a..634e43c 100644 --- a/pkg/api/account/user.go +++ b/pkg/api/account/user.go @@ -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", "") @@ -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", "") @@ -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{} @@ -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 +}