diff --git a/README.md b/README.md index 1c505d4..124de05 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 +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 ] @@ -230,7 +245,6 @@ cloudflare zone dns show [ --id | -i ] #### cloudflare zone dns add

Add a Cloudflare DNS record and return its result

- See data payload format at [Cloudflare API documentation - create DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record) ```bash @@ -245,7 +259,6 @@ cloudflare zone dns add [ --zone-id ] #### cloudflare zone dns update

Update a Cloudflare DNS record

- See data payload format at [Cloudflare API documentation - create DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record) ```bash diff --git a/RELEASE.md b/RELEASE.md index 25ca35d..66a935b 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -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 diff --git a/cmd/account_update.go b/cmd/account_update.go new file mode 100644 index 0000000..4528345 --- /dev/null +++ b/cmd/account_update.go @@ -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") +} diff --git a/pkg/api/account/account.go b/pkg/api/account/account.go index d3ad1ff..6094bbf 100644 --- a/pkg/api/account/account.go +++ b/pkg/api/account/account.go @@ -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 +} diff --git a/pkg/consts/text/text.go b/pkg/consts/text/text.go index 8aed532..e4796d4 100644 --- a/pkg/consts/text/text.go +++ b/pkg/consts/text/text.go @@ -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`