forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structs; impl Create for CloudFlare PageRules
This commit adds most of the boilerplate for implementing CloudFlare Page Rules, and implements the Create method. `PageRuleActionValue`s still need a validator; this is complex and depends on `PageRuleActionId`. Read, Update, and Delete methods will throw 'not implemented' errors. Towards hashicorp#9040.
- Loading branch information
Showing
4 changed files
with
267 additions
and
1 deletion.
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
139 changes: 139 additions & 0 deletions
139
builtin/providers/cloudflare/resource_cloudflare_page_rule.go
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,139 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceCloudFlarePageRule() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudFlarePageRuleCreate, | ||
Read: resourceCloudFlarePageRuleRead, | ||
Update: resourceCloudFlarePageRuleUpdate, | ||
Delete: resourceCloudFlarePageRuleDelete, | ||
|
||
SchemaVersion: 1, | ||
Schema: map[string]*schema.Schema{ | ||
"domain": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"targets": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"url_pattern": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"actions": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"action": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validatePageRuleActionID, | ||
}, | ||
|
||
"value": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validatePageRuleActionValue, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"priority": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Default: 1, | ||
Optional: true, | ||
}, | ||
|
||
"status": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Default: "active", | ||
Optional: true, | ||
ValidateFunc: validatePageRuleStatus, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCloudFlarePageRuleCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
|
||
targets := d.Get("targets").([]interface{}) | ||
actions := d.Get("actions").([]interface{}) | ||
|
||
newPageRuleTargets := make([]cloudflare.PageRuleTarget, 0, len(targets)) | ||
newPageRuleActions := make([]cloudflare.PageRuleAction, 0, len(actions)) | ||
|
||
for _, target := range targets { | ||
newPageRuleTarget := cloudflare.PageRuleTarget{ | ||
Target: "url", | ||
Constraint: struct { | ||
Operator string `json:"operator"` | ||
Value string `json:"value"` | ||
}{ | ||
Operator: "matches", | ||
Value: target.(schema.Resource).Schema["url_pattern"].Elem.(string), | ||
}, | ||
} | ||
newPageRuleTargets = append(newPageRuleTargets, newPageRuleTarget) | ||
} | ||
|
||
for _, action := range actions { | ||
newPageRuleActions = append(newPageRuleActions, cloudflare.PageRuleAction{ | ||
ID: action.(schema.Resource).Schema["action"].Elem.(string), | ||
Value: action.(schema.Resource).Schema["value"].Elem.(string), | ||
}) | ||
} | ||
|
||
newPageRule := cloudflare.PageRule{ | ||
Targets: newPageRuleTargets, | ||
Actions: newPageRuleActions, | ||
Priority: d.Get("priority").(int), | ||
Status: d.Get("status").(string), | ||
} | ||
|
||
zoneName := d.Get("domain").(string) | ||
|
||
zoneId, err := client.ZoneIDByName(zoneName) | ||
if err != nil { | ||
return fmt.Errorf("Error finding zone %q: %s", zoneName, err) | ||
} | ||
|
||
d.Set("zone_id", zoneId) | ||
log.Printf("[DEBUG] CloudFlare Page Rule create configuration: %#v", newPageRule) | ||
|
||
err = client.CreatePageRule(zoneId, newPageRule) | ||
if err != nil { | ||
return fmt.Errorf("Failed to create page rule: %s", err) | ||
} | ||
|
||
return resourceCloudFlarePageRuleRead(d, meta) | ||
} | ||
|
||
func resourceCloudFlarePageRuleRead(d *schema.ResourceData, meta interface{}) error { | ||
return fmt.Errorf("Page Rule Read not implemented.") | ||
} | ||
|
||
func resourceCloudFlarePageRuleUpdate(d *schema.ResourceData, meta interface{}) error { | ||
return fmt.Errorf("Page Rule Update not implemented.") | ||
} | ||
|
||
func resourceCloudFlarePageRuleDelete(d *schema.ResourceData, meta interface{}) error { | ||
return fmt.Errorf("Page Rule Delete not implemented.") | ||
} |
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