Skip to content

Commit

Permalink
Merge pull request #671 from cehrig/cehrig/ip-address-management
Browse files Browse the repository at this point in the history
Adds new resource cloudflare_byo_ip_prefix
  • Loading branch information
jacobbednarz authored May 4, 2020
2 parents 15ce48d + 14b1179 commit 4c10939
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 14 deletions.
1 change: 1 addition & 0 deletions cloudflare/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func Provider() terraform.ResourceProvider {
"cloudflare_custom_ssl": resourceCloudflareCustomSsl(),
"cloudflare_filter": resourceCloudflareFilter(),
"cloudflare_firewall_rule": resourceCloudflareFirewallRule(),
"cloudflare_byo_ip_prefix": resourceCloudflareBYOIPPrefix(),
"cloudflare_load_balancer_monitor": resourceCloudflareLoadBalancerMonitor(),
"cloudflare_load_balancer_pool": resourceCloudflareLoadBalancerPool(),
"cloudflare_load_balancer": resourceCloudflareLoadBalancer(),
Expand Down
6 changes: 6 additions & 0 deletions cloudflare/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func testAccPreCheckLogpushToken(t *testing.T) {
}
}

func testAccPreCheckBYOIPPrefix(t *testing.T) {
if v := os.Getenv("CLOUDFLARE_BYO_IP_PREFIX_ID"); v == "" {
t.Skip("Skipping acceptance test as CLOUDFLARE_BYO_IP_PREFIX_ID is not set")
}
}

func generateRandomResourceName() string {
return acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
}
105 changes: 105 additions & 0 deletions cloudflare/resource_cloudflare_byo_ip_prefix.go
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
}
50 changes: 50 additions & 0 deletions cloudflare/resource_cloudflare_byo_ip_prefix_test.go
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)
}
14 changes: 0 additions & 14 deletions cloudflare/resource_cloudflare_zone_settings_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,17 +902,3 @@ func schemaValueEquals(a, b interface{}) bool {

return reflect.DeepEqual(a, b)
}

func boolFromString(status string) bool {
if status == "on" {
return true
}
return false
}

func stringFromBool(status bool) string {
if status {
return "on"
}
return "off"
}
14 changes: 14 additions & 0 deletions cloudflare/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ func cloudflareErrorIsOneOfCodes(err error, codes []int) bool {

return false
}

func boolFromString(status string) bool {
if status == "on" {
return true
}
return false
}

func stringFromBool(status bool) string {
if status {
return "on"
}
return "off"
}
37 changes: 37 additions & 0 deletions website/docs/r/byo_ip_prefix.html.markdown
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
```

0 comments on commit 4c10939

Please sign in to comment.