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

Extend Cloudflare provider #456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions providers/cloudflare/cloudflare_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func (CloudflareProvider) GetResourceConnections() map[string]map[string][]strin

func (p *CloudflareProvider) GetSupportedService() map[string]terraform_utils.ServiceGenerator {
return map[string]terraform_utils.ServiceGenerator{
"access": &AccessGenerator{},
"dns": &DNSGenerator{},
"firewall": &FirewallGenerator{},
"access": &AccessGenerator{},
"dns": &DNSGenerator{},
"firewall": &FirewallGenerator{},
"page_rules": &PageRulesGenerator{},
}
}

Expand Down
51 changes: 43 additions & 8 deletions providers/cloudflare/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@ func (*FirewallGenerator) createAccessRuleResources(api *cf.API, zoneID, zoneNam
return resources, nil
}

func (*FirewallGenerator) createRateLimitResources(api *cf.API, zoneID, zoneName string) ([]terraform_utils.Resource, error) {
resources := []terraform_utils.Resource{}

for page := 1; ; page++ {
rateLimits, resultInfo, err := api.ListRateLimits(zoneID, cf.PaginationOptions{Page: page, PerPage: 10})
if err != nil {
log.Println(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you return error, not need log in resource level

return resources, err
}

for _, rateLimit := range rateLimits {
resources = append(resources, terraform_utils.NewResource(
rateLimit.ID,
fmt.Sprintf("%s_%s", zoneName, rateLimit.ID),
"cloudflare_rate_limit",
"cloudflare",
map[string]string{
"zone_id": zoneID,
},
[]string{},
map[string]interface{}{},
))
}

if resultInfo.Count < resultInfo.PerPage {
break
}
}

return resources, nil
}

func (*FirewallGenerator) createFilterResources(api *cf.API, zoneID, zoneName string) ([]terraform_utils.Resource, error) {
resources := []terraform_utils.Resource{}
filters, err := api.Filters(zoneID, cf.PaginationOptions{})
Expand Down Expand Up @@ -159,6 +191,7 @@ func (g *FirewallGenerator) InitResources() error {
g.createFilterResources,
g.createAccessRuleResources,
g.createZoneLockdownsResources,
g.createRateLimitResources,
}

for _, zone := range zones {
Expand All @@ -178,7 +211,7 @@ func (g *FirewallGenerator) InitResources() error {

func (g *FirewallGenerator) PostConvertHook() error {
for i, resourceRecord := range g.Resources {
// If Zone Name exists, delete ZoneID
// If zone ID exists, delete zone name
if _, zoneIDExist := resourceRecord.Item["zone_id"]; zoneIDExist {
delete(g.Resources[i].Item, "zone")
}
Expand All @@ -193,13 +226,15 @@ func (g *FirewallGenerator) PostConvertHook() error {
if resourceRecord.InstanceInfo.Type == "cloudflare_filter" {
continue
}
filterID := resourceRecord.Item["filter_id"]
for _, filterResource := range g.Resources {
if filterResource.InstanceInfo.Type != "cloudflare_filter" {
continue
}
if filterID == filterResource.InstanceState.ID {
g.Resources[i].Item["filter_id"] = "${cloudflare_filter." + filterResource.ResourceName + ".id}"
filterID, ok := resourceRecord.Item["filter_id"]
if ok {
for _, filterResource := range g.Resources {
if filterResource.InstanceInfo.Type != "cloudflare_filter" {
continue
}
if filterID == filterResource.InstanceState.ID {
g.Resources[i].Item["filter_id"] = "${cloudflare_filter." + filterResource.ResourceName + ".id}"
}
}
}
}
Expand Down
105 changes: 105 additions & 0 deletions providers/cloudflare/page_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//cloudflare_access_rule
//cloudflare_rate_limit

package cloudflare

import (
"fmt"
"log"

"github.com/GoogleCloudPlatform/terraformer/terraform_utils"
cf "github.com/cloudflare/cloudflare-go"
)

type PageRulesGenerator struct {
CloudflareService
}

func (*PageRulesGenerator) createPageRuleResources(api *cf.API, zoneID, zoneName string) ([]terraform_utils.Resource, error) {
resources := []terraform_utils.Resource{}

pageRules, err := api.ListPageRules(zoneID)
if err != nil {
log.Println(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

return resources, err
}
for _, pageRule := range pageRules {
resources = append(resources, terraform_utils.NewResource(
pageRule.ID,
fmt.Sprintf("%s_%s", zoneName, pageRule.ID),
"cloudflare_page_rule",
"cloudflare",
map[string]string{
"zone_id": zoneID,
},
[]string{},
map[string]interface{}{},
))
}

return resources, nil

}

func (g *PageRulesGenerator) InitResources() error {
api, err := g.initializeAPI()
if err != nil {
panic(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why panic if it's return error?

return err
}

zones, err := api.ListZones()
if err != nil {
panic(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why panic if it's return error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I was copying the other services in this provider. I assumed there were reasons beyond my understanding for this madness... :)

I can definitely make it more proper Go, tho. Gimme a day or two.

return err
}

funcs := []func(*cf.API, string, string) ([]terraform_utils.Resource, error){
g.createPageRuleResources,
}

for _, zone := range zones {
for _, f := range funcs {
// Getting all firewall filters
tmpRes, err := f(api, zone.ID, zone.Name)
if err != nil {
panic(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why panic if it's return error?

return err
}
g.Resources = append(g.Resources, tmpRes...)
}
}

return nil
}

func (g *PageRulesGenerator) PostConvertHook() error {
for i, resourceRecord := range g.Resources {
// If zone ID exists, delete zone name
if _, zoneIDExist := resourceRecord.Item["zone_id"]; zoneIDExist {
delete(g.Resources[i].Item, "zone")
}

if resourceRecord.InstanceInfo.Type == "cloudflare_page_rule" {
if resourceRecord.Item["priority"].(string) == "0" {
delete(g.Resources[i].Item, "priority")
}
}
}

return nil
}