Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Update for cloudflare_load_balancer_pool #140

Merged
merged 3 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions cloudflare/resource_cloudflare_load_balancer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
func resourceCloudflareLoadBalancerPool() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareLoadBalancerPoolCreate,
Update: resourceCloudflareLoadBalancerPoolUpdate,
Read: resourceCloudflareLoadBalancerPoolRead,
Delete: resourceCloudflareLoadBalancerPoolDelete,
Importer: &schema.ResourceImporter{
Expand All @@ -29,29 +30,25 @@ func resourceCloudflareLoadBalancerPool() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("[-_a-zA-Z0-9]+"), "Only alphanumeric characters, hyphens and underscores are allowed."),
},

"origins": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: originsElem,
},

"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},

"minimum_origins": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ForceNew: true,
},

"check_regions": {
Expand All @@ -61,27 +58,23 @@ func resourceCloudflareLoadBalancerPool() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ForceNew: true,
},

"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 1024),
ForceNew: true,
},

"monitor": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 32),
ForceNew: true,
},

"notification_email": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"created_on": {
Expand Down Expand Up @@ -165,6 +158,43 @@ func resourceCloudflareLoadBalancerPoolCreate(d *schema.ResourceData, meta inter
return resourceCloudflareLoadBalancerPoolRead(d, meta)
}

func resourceCloudflareLoadBalancerPoolUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

loadBalancerPool := cloudflare.LoadBalancerPool{
ID: d.Id(),
Name: d.Get("name").(string),
Origins: expandLoadBalancerOrigins(d.Get("origins").(*schema.Set)),
Enabled: d.Get("enabled").(bool),
MinimumOrigins: d.Get("minimum_origins").(int),
}

if checkRegions, ok := d.GetOk("check_regions"); ok {
loadBalancerPool.CheckRegions = expandInterfaceToStringList(checkRegions.(*schema.Set).List())
}

if description, ok := d.GetOk("description"); ok {
loadBalancerPool.Description = description.(string)
}

if monitor, ok := d.GetOk("monitor"); ok {
loadBalancerPool.Monitor = monitor.(string)
}

if notificationEmail, ok := d.GetOk("notification_email"); ok {
loadBalancerPool.NotificationEmail = notificationEmail.(string)
}

log.Printf("[DEBUG] Updating Cloudflare Load Balancer Pool from struct: %+v", loadBalancerPool)

_, err := client.ModifyLoadBalancerPool(loadBalancerPool)
if err != nil {
return errors.Wrap(err, "error updating load balancer pool")
}

return resourceCloudflareLoadBalancerPoolRead(d, meta)
}

func expandLoadBalancerOrigins(originSet *schema.Set) (origins []cloudflare.LoadBalancerOrigin) {
for _, iface := range originSet.List() {
o := iface.(map[string]interface{})
Expand Down
42 changes: 0 additions & 42 deletions cloudflare/resource_cloudflare_load_balancer_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,48 +66,6 @@ func TestAccCloudflareLoadBalancerPool_FullySpecified(t *testing.T) {
})
}

/**
Any change to a load balancer pool results in a new resource
Although the API client contains a modify method, this always results in 405 status
*/
func TestAccCloudflareLoadBalancerPool_ForceNew(t *testing.T) {
t.Parallel()
var loadBalancerPool cloudflare.LoadBalancerPool
var initialId string
rnd := acctest.RandString(10)
name := "cloudflare_load_balancer_pool." + rnd

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflareLoadBalancerPoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareLoadBalancerPoolConfigBasic(rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareLoadBalancerPoolExists(name, &loadBalancerPool),
),
},
{
PreConfig: func() {
initialId = loadBalancerPool.ID
},
Config: testAccCheckCloudflareLoadBalancerPoolConfigFullySpecified(rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareLoadBalancerPoolExists(name, &loadBalancerPool),
func(state *terraform.State) error {
if initialId == loadBalancerPool.ID {
return fmt.Errorf("id should be different after recreation, but is unchanged: %s ",
loadBalancerPool.ID)
}
return nil
},
),
},
},
})
}

func TestAccCloudflareLoadBalancerPool_CreateAfterManualDestroy(t *testing.T) {
t.Parallel()
var loadBalancerPool cloudflare.LoadBalancerPool
Expand Down