Skip to content

Commit

Permalink
Add account update command
Browse files Browse the repository at this point in the history
Add new command for account
Update README.md
  • Loading branch information
epiHATR committed Aug 12, 2022
1 parent e9ecef0 commit 30b909e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extract windows-amd64-v0.x.x.zip to a folder
| [cloudflare account](#cmd_cloudflare_account) | manage Cloudflare accounts/organization |
| [cloudflare account list](#cmd_cloudflare_account_list) | list all Cloudflare accounts/organization |
| [cloudflare account show](#cmd_cloudflare_account_show) | show details of a cloudflare account/organization |
| [cloudflare account update](#cmd_cloudflare_account_update) | update information of 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 |
| [cloudflare zone create](#cmd_cloudflare_zone_create) | create new Cloudflare zone |
Expand Down Expand Up @@ -138,6 +139,20 @@ Show details of a Cloudlare managed account/organization
```bash
cloudflare account show [ --account-id ]

#global flags
[ --query | -q ]
[ --output |-o ]
[ --help | -h ]
[ --debug ]
```
#### cloudflare account update <a name="cmd_cloudflare_account_update"></a>
Update information details of a Cloudlare managed account/organization
See payload data at https://api.cloudflare.com/#accounts-update-account

```bash
cloudflare account update [ --account-id ]
[ --data | -d ]

#global flags
[ --query | -q ]
[ --output |-o ]
Expand Down Expand Up @@ -230,7 +245,6 @@ cloudflare zone dns show [ --id | -i ]

#### cloudflare zone dns add <a name="cmd_cloudflare_zone_dns_add"></a>
<p>Add a Cloudflare DNS record and return its result</p>

See data payload format at [Cloudflare API documentation - create DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record)

```bash
Expand All @@ -245,7 +259,6 @@ cloudflare zone dns add [ --zone-id ]

#### cloudflare zone dns update <a name="cmd_cloudflare_zone_dns_update"></a>
<p>Update a Cloudflare DNS record</p>

See data payload format at [Cloudflare API documentation - create DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record)

```bash
Expand Down
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Cloudflare API v0.1.45
- [x] cloudflare account
- [x] cloudflare account list
- [x] cloudflare account show
- [x] cloudflare account update

- [ ] cloudflare account member list
- [ ] cloudflare account member show
Expand Down
76 changes: 76 additions & 0 deletions cmd/account_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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"
"log"
"os"
"strings"

"github.com/spf13/cobra"
)

var accountUpdateCmdAccountId string = ""
var accountUpdateData string = ""
var accountForceUpdate bool = false
var accountUpdateConfirmText = "no"

var accountUpdateCmd = &cobra.Command{
Use: "update",
Short: "update a Cloudflare account/organization information",
Long: text.AccountUpdateLongText + text.SubCmdHelpText,
Run: func(cmd *cobra.Command, args []string) {
errText := []string{}
if len(accountUpdateCmdAccountId) <= 0 {
errText = append(errText, "--account-id")
}
if len(accountUpdateData) <= 0 {
errText = append(errText, "--data|-d")
}
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)
}

log.Println("Updating account id", accountUpdateCmdAccountId)
if !accountForceUpdate {
for {
fmt.Print("Please confirm your action (yes/no): ")
fmt.Scan(&accountUpdateConfirmText)
if strings.ToLower(accountUpdateConfirmText) == "yes" || strings.ToLower(accountUpdateConfirmText) == "no" {
break
}
}
} else {
accountUpdateConfirmText = "yes"
}

if accountUpdateConfirmText == "yes" {
res := account.UpdateAccount(accountUpdateCmdAccountId, accountUpdateData)

if !res.Success {
fmt.Fprintln(os.Stderr, "Error: failed to update account/organization. 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() {
accountCmd.AddCommand(accountUpdateCmd)
accountUpdateCmd.Flags().StringVarP(&accountUpdateCmdAccountId, "account-id", "", "", "ID of Cloudflare account/organization")
accountUpdateCmd.Flags().StringVarP(&accountUpdateData, "data", "d", "", "DNS id need to update")
accountUpdateCmd.Flags().BoolVarP(&accountForceUpdate, "--force", "f", false, "force update DNS records without confirm")
}
15 changes: 15 additions & 0 deletions pkg/api/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ func GetAccountDetails(accountId string) response.AccountDetailsResponse {
}
return resObj
}

func UpdateAccount(accountId string, payloadData string) response.AccountDetailsResponse {
log.Println("get all Cloudflare managed accounts/organizations")
queryUrl := fmt.Sprintf(endpoint.ApiEndPoint+endpoint.AccountDetailsEndpoint, accountId)

respData := request.CreateRequest(queryUrl, "PUT", payloadData)
resObj := response.AccountDetailsResponse{}

err := json.Unmarshal(respData, &resObj)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
return resObj
}
3 changes: 3 additions & 0 deletions pkg/consts/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ This command requires 'organization:read' on your Cloudflare token API configura

var AccountShowLongText = `Show details of a Cloudflare account/organization
This command requires 'organization:read' on your Cloudflare token API configuration permission`

var AccountUpdateLongText = `Update information of a Cloudflare account/organization
This command requires 'organization:write' on your Cloudflare token API configuration permission`

0 comments on commit 30b909e

Please sign in to comment.