-
Notifications
You must be signed in to change notification settings - Fork 632
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 #671 from cehrig/cehrig/ip-address-management
Adds new resource cloudflare_byo_ip_prefix
- Loading branch information
Showing
7 changed files
with
213 additions
and
14 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
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,105 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func resourceCloudflareBYOIPPrefix() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudflareBYOIPPrefixCreate, | ||
Read: resourceCloudflareBYOIPPrefixRead, | ||
Update: resourceCloudflareBYOIPPrefixUpdate, | ||
Delete: resourceCloudflareBYOIPPrefixDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceCloudflareBYOIPPrefixImport, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"prefix_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
"advertisement": { | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.StringInSlice([]string{"on", "off"}, false), | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCloudflareBYOIPPrefixCreate(d *schema.ResourceData, meta interface{}) error { | ||
prefixID := d.Get("prefix_id") | ||
d.SetId(prefixID.(string)) | ||
|
||
if err := resourceCloudflareBYOIPPrefixUpdate(d, meta); err != nil { | ||
return err | ||
} | ||
|
||
return resourceCloudflareBYOIPPrefixRead(d, meta) | ||
} | ||
|
||
func resourceCloudflareBYOIPPrefixImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
prefixID := d.Id() | ||
d.Set("prefix_id", prefixID) | ||
|
||
resourceCloudflareBYOIPPrefixRead(d, meta) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func resourceCloudflareBYOIPPrefixRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
|
||
prefix, err := client.GetPrefix(context.Background(), d.Id()) | ||
if err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("error reading IP prefix information for %q", d.Id())) | ||
} | ||
|
||
d.Set("description", prefix.Description) | ||
|
||
advertisementStatus, err := client.GetAdvertisementStatus(context.Background(), d.Id()) | ||
if err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("error reading advertisement status of IP prefix for %q", d.Id())) | ||
} | ||
|
||
d.Set("advertisement", stringFromBool(advertisementStatus.Advertised)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudflareBYOIPPrefixUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
|
||
if _, ok := d.GetOk("description"); ok && d.HasChange("description") { | ||
if _, err := client.UpdatePrefixDescription(context.Background(), d.Id(), d.Get("description").(string)); err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("cannot update prefix description for %q", d.Id())) | ||
} | ||
} | ||
|
||
if _, ok := d.GetOk("advertisement"); ok && d.HasChange("advertisement") { | ||
if _, err := client.UpdateAdvertisementStatus(context.Background(), d.Id(), boolFromString(d.Get("advertisement").(string))); err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("cannot update prefix advertisement status for %q", d.Id())) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Deletion of prefixes is not really supported, so we keep this as a dummy | ||
func resourceCloudflareBYOIPPrefixDelete(d *schema.ResourceData, meta interface{}) error { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccCloudflareBYOIPPrefix(t *testing.T) { | ||
t.Parallel() | ||
prefixID := os.Getenv("CLOUDFLARE_BYO_IP_PREFIX_ID") | ||
|
||
rnd := generateRandomResourceName() | ||
name := fmt.Sprintf("cloudflare_byo_ip_prefix.%s", rnd) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testAccPreCheckAccount(t) | ||
testAccPreCheckBYOIPPrefix(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckCloudflareBYOIPPrefixConfig(prefixID, "BYOIP Prefix Description old", rnd), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
name, "description", "BYOIP Prefix Description old"), | ||
), | ||
}, | ||
{ | ||
Config: testAccCheckCloudflareBYOIPPrefixConfig(prefixID, "BYOIP Prefix Description new", rnd), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
name, "description", "BYOIP Prefix Description new"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckCloudflareBYOIPPrefixConfig(prefixID, description, name string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_byo_ip_prefix" "%[3]s" { | ||
prefix_id = "%[1]s" | ||
description = "%[2]s" | ||
}`, prefixID, description, 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
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,37 @@ | ||
--- | ||
layout: "cloudflare" | ||
page_title: "Cloudflare: cloudflare_byo_ip_prefix" | ||
sidebar_current: "docs-cloudflare-resource-universal-ssl" | ||
description: |- | ||
Provides the ability to manage Bring-Your-Own-IP prefixes (BYOIP) which are used with or without Magic Transit. | ||
--- | ||
|
||
# cloudflare_byo_ip_prefix | ||
|
||
Provides the ability to manage Bring-Your-Own-IP prefixes (BYOIP) which are used with or without Magic Transit. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "cloudflare_byo_ip_prefix" "example" { | ||
prefix_id = "d41d8cd98f00b204e9800998ecf8427e" | ||
description = "Example IP Prefix" | ||
advertisement = "on" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `prefix_id` - (Required) The assigned Bring-Your-Own-IP prefix ID. | ||
* `description` - (Optional) The description of the prefix. | ||
* `advertisement` - (Optional) Whether or not the prefix shall be announced. A prefix can be activated or deactivated once every 15 minutes (attempting more regular updates will trigger rate limiting). Valid values: `on` or `off`. | ||
|
||
## Import | ||
|
||
The current settings for Bring-Your-Own-IP prefixes can be imported using the prefix ID. | ||
|
||
``` | ||
$ terraform import cloudflare_byo_ip_prefix.example d41d8cd98f00b204e9800998ecf8427e | ||
``` |