-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1902 from mlhnono68/feat-accounts-resource
- Loading branch information
Showing
13 changed files
with
265 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
cloudflare_account | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
page_title: "cloudflare_account Resource - Cloudflare" | ||
subcategory: "" | ||
description: |- | ||
Provides a Cloudflare Account resource. Account is the basic resource for | ||
working with Cloudflare zones, teams and users. | ||
--- | ||
|
||
# cloudflare_account (Resource) | ||
|
||
Provides a Cloudflare Account resource. Account is the basic resource for | ||
working with Cloudflare zones, teams and users. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "cloudflare_account" "example" { | ||
name = "some-enterprise-account" | ||
type = "enterprise" | ||
enforce_twofactor = true | ||
} | ||
``` | ||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the account that is displayed in the Cloudflare dashboard. | ||
|
||
### Optional | ||
|
||
- `enforce_twofactor` (Boolean) Whether 2FA is enforced on the account. Defaults to `false`. | ||
- `type` (String) Account type. Available values: `enterprise`, `standard`. Defaults to `standard`. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
```shell | ||
$ terraform import cloudflare_account.example <account_id> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,3 +69,5 @@ Optional: | |
|
||
- `field` (String) Field for type matcher. | ||
- `value` (String) Value for matcher. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$ terraform import cloudflare_account.example <account_id> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "cloudflare_account" "example" { | ||
name = "some-enterprise-account" | ||
type = "enterprise" | ||
enforce_twofactor = true | ||
} |
4 changes: 2 additions & 2 deletions
4
..._email_routing_rule_catch_all/resource.tf → ...flare_email_routing_catch_all/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/MakeNowJust/heredoc/v2" | ||
cloudflare "github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
accountTypeStandard = "standard" | ||
accountTypeEnterprise = "enterprise" | ||
) | ||
|
||
func resourceCloudflareAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: resourceCloudflareAccountSchema(), | ||
CreateContext: resourceCloudflareAccountCreate, | ||
ReadContext: resourceCloudflareAccountRead, | ||
UpdateContext: resourceCloudflareAccountUpdate, | ||
DeleteContext: resourceCloudflareAccountDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Description: heredoc.Doc(` | ||
Provides a Cloudflare Account resource. Account is the basic resource for | ||
working with Cloudflare zones, teams and users. | ||
`), | ||
} | ||
} | ||
|
||
func resourceCloudflareAccountCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
accountName := d.Get("name").(string) | ||
accountType := d.Get("type").(string) | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("Creating Cloudflare Account: name %s", accountName)) | ||
|
||
account := cloudflare.Account{ | ||
Name: accountName, | ||
Type: accountType, | ||
} | ||
acc, err := client.CreateAccount(ctx, account) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error creating account %q: %w", accountName, err)) | ||
} | ||
|
||
d.SetId(acc.ID) | ||
|
||
return resourceCloudflareAccountRead(ctx, d, meta) | ||
} | ||
|
||
func resourceCloudflareAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
accountID := d.Id() | ||
|
||
foundAcc, _, err := client.Account(ctx, accountID) | ||
if err != nil { | ||
var notFoundError *cloudflare.NotFoundError | ||
if errors.As(err, ¬FoundError) { | ||
tflog.Info(ctx, fmt.Sprintf("Account %s no longer exists", d.Id())) | ||
d.SetId("") | ||
return nil | ||
} | ||
return diag.FromErr(fmt.Errorf("error finding Account %q: %w", d.Id(), err)) | ||
} | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("AccountDetails: %#v", foundAcc)) | ||
|
||
d.Set("name", foundAcc.Name) | ||
d.Set("type", foundAcc.Type) | ||
d.Set("enforce_twofactor", foundAcc.Settings.EnforceTwoFactor) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudflareAccountUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
accountID := d.Id() | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("Updating Cloudflare Account: id %s", accountID)) | ||
|
||
updatedAcc := cloudflare.Account{} | ||
if accountName, ok := d.GetOk("name"); ok { | ||
updatedAcc.Name = accountName.(string) | ||
} | ||
|
||
if enforce_twofactor, ok := d.GetOk("enforce_twofactor"); ok { | ||
updatedAcc.Settings.EnforceTwoFactor = enforce_twofactor.(bool) | ||
} | ||
|
||
_, err := client.UpdateAccount(ctx, accountID, updatedAcc) | ||
if err != nil { | ||
tflog.Error(ctx, fmt.Sprintf("%#v", err)) | ||
return diag.FromErr(fmt.Errorf("error updating Account %q: %w", d.Id(), err)) | ||
} | ||
|
||
return resourceCloudflareAccountRead(ctx, d, meta) | ||
} | ||
|
||
func resourceCloudflareAccountDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
accountID := d.Id() | ||
|
||
tflog.Debug(ctx, fmt.Sprintf("Deleting Cloudflare Account: id %s", accountID)) | ||
|
||
err := client.DeleteAccount(ctx, accountID) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error deleting Cloudflare Account: %w", err)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccCloudflareAccount_Basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
rnd := generateRandomResourceName() | ||
name := fmt.Sprintf("cloudflare_account.%s", rnd) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckCloudflareAccountName(fmt.Sprintf("%s_old", rnd)), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
name, "name", fmt.Sprintf("%s_old", rnd)), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckCloudflareAccountName(fmt.Sprintf("%s_new", rnd)), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
name, "name", fmt.Sprintf("%s_new", rnd)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCloudflareAccountName(name string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_account" "%[1]s" { | ||
name = "%[1]s" | ||
}`, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceCloudflareAccountSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The name of the account that is displayed in the Cloudflare dashboard.", | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: fmt.Sprintf("Account type. %s", renderAvailableDocumentationValuesStringSlice([]string{accountTypeEnterprise, accountTypeStandard})), | ||
Default: accountTypeStandard, | ||
ValidateFunc: validation.StringInSlice([]string{accountTypeEnterprise, accountTypeStandard}, false), | ||
ForceNew: true, // "Updating account type is not supported from client api" | ||
}, | ||
"enforce_twofactor": { | ||
Description: "Whether 2FA is enforced on the account.", | ||
Type: schema.TypeBool, | ||
Default: false, | ||
Optional: true, | ||
}, | ||
} | ||
} |