Skip to content

Commit

Permalink
fix: Cleanup (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zagronitay authored and hermanschaaf committed Aug 9, 2022
1 parent f41a2ac commit fbc6db3
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 75 deletions.
79 changes: 35 additions & 44 deletions plugins/cloudflare/client/client.go
Expand Up @@ -44,37 +44,25 @@ func (c *Client) Logger() hclog.Logger {
return c.logger
}

func getCloudflareClient(config *Config) (*cloudflare.API, error) {
// Try to get the API token from the environment
token := config.Token
if token == "" {
token = getApiTokenFromEnv()
}

if token != "" {
clientApi, err := cloudflare.NewWithAPIToken(token)
if err != nil {
return nil, err
}
return clientApi, nil
}

apiKey := config.ApiKey
apiEmail := config.ApiEmail

if config.ApiKey == "" || config.ApiEmail == "" {
apiKey, apiEmail = getApiKeyAndEmailFromEnv()
func (c *Client) withAccountId(accountId string) *Client {
return &Client{
logger: c.logger.With("account_id", obfuscateId(accountId)),
accountsZones: c.accountsZones,
clients: c.clients,
ClientApi: c.clients[accountId],
AccountId: accountId,
}
}

if apiKey != "" && apiEmail != "" {
clientApi, err := cloudflare.New(apiKey, apiEmail)
if err != nil {
return nil, err
}
return clientApi, nil
func (c *Client) withZoneId(accountId, zoneId string) *Client {
return &Client{
logger: c.logger.With("account_id", obfuscateId(accountId), "zone_id", obfuscateId(zoneId)),
accountsZones: c.accountsZones,
clients: c.clients,
ClientApi: c.clients[accountId],
AccountId: accountId,
ZoneId: zoneId,
}

return nil, errors.New("no API token or API key/email provided")
}

func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag.Diagnostics) {
Expand Down Expand Up @@ -136,25 +124,28 @@ func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag
return &c, nil
}

func (c *Client) withAccountId(accountId string) *Client {
return &Client{
logger: c.logger.With("account_id", obfuscateId(accountId)),
accountsZones: c.accountsZones,
clients: c.clients,
ClientApi: c.clients[accountId],
AccountId: accountId,
func getCloudflareClient(config *Config) (*cloudflare.API, error) {
// Try to get the API token from the environment
token := config.Token
if token == "" {
token = getApiTokenFromEnv()
}
}

func (c *Client) withZoneId(accountId, zoneId string) *Client {
return &Client{
logger: c.logger.With("account_id", obfuscateId(accountId), "zone_id", obfuscateId(zoneId)),
accountsZones: c.accountsZones,
clients: c.clients,
ClientApi: c.clients[accountId],
AccountId: accountId,
ZoneId: zoneId,
if token != "" {
return cloudflare.NewWithAPIToken(token)
}

apiKey, apiEmail := config.ApiKey, config.ApiEmail

if config.ApiKey == "" || config.ApiEmail == "" {
apiKey, apiEmail = getApiKeyAndEmailFromEnv()
}

if apiKey != "" && apiEmail != "" {
return cloudflare.New(apiKey, apiEmail)
}

return nil, errors.New("no API token or API key/email provided")
}

func obfuscateId(accountId string) string {
Expand Down
5 changes: 5 additions & 0 deletions plugins/cloudflare/client/filter.go
Expand Up @@ -7,6 +7,11 @@ func DeleteAccountFilter(meta schema.ClientMeta, _ *schema.Resource) []interface
return []interface{}{"account_id", client.AccountId}
}

func DeleteAccountZoneFilter(meta schema.ClientMeta, _ *schema.Resource) []interface{} {
client := meta.(*Client)
return []interface{}{"account_id", client.AccountId, "zone_id", client.ZoneId}
}

func DeleteFilter(_ schema.ClientMeta, _ *schema.Resource) []interface{} {
return []interface{}{}
}
9 changes: 5 additions & 4 deletions plugins/cloudflare/resources/services/accounts.go
Expand Up @@ -12,10 +12,11 @@ import (
//go:generate cq-gen --resource accounts --config accounts.hcl --output .
func Accounts() *schema.Table {
return &schema.Table{
Name: "cloudflare_accounts",
Description: "Account represents the root object that owns resources.",
Resolver: fetchAccounts,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Name: "cloudflare_accounts",
Description: "Account represents the root object that owns resources.",
Resolver: fetchAccounts,
DeleteFilter: client.DeleteFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Columns: []schema.Column{
{
Name: "id",
Expand Down
4 changes: 4 additions & 0 deletions plugins/cloudflare/resources/services/accounts.hcl
Expand Up @@ -11,6 +11,10 @@ resource "cloudflare" "" "accounts" {
]
}

deleteFilter "DeleteFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteFilter"
}

column "id" {
description = "The unique universal identifier for a Cloudflare account."
}
Expand Down
9 changes: 5 additions & 4 deletions plugins/cloudflare/resources/services/dns_records.go
Expand Up @@ -12,10 +12,11 @@ import (
//go:generate cq-gen --resource dns_records --config dns_records.hcl --output .
func DNSRecords() *schema.Table {
return &schema.Table{
Name: "cloudflare_dns_records",
Description: "DNSRecord represents a DNS record in a zone.",
Multiplex: client.ZoneMultiplex,
Resolver: fetchDnsRecords,
Name: "cloudflare_dns_records",
Description: "DNSRecord represents a DNS record in a zone.",
Multiplex: client.ZoneMultiplex,
DeleteFilter: client.DeleteAccountZoneFilter,
Resolver: fetchDnsRecords,
Columns: []schema.Column{
{
Name: "account_id",
Expand Down
4 changes: 4 additions & 0 deletions plugins/cloudflare/resources/services/dns_records.hcl
Expand Up @@ -11,6 +11,10 @@ resource "cloudflare" "" "dns_records" {
]
}

deleteFilter "DeleteAccountZoneFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteAccountZoneFilter"
}

userDefinedColumn "account_id" {
description = "The Account ID of the resource."
type = "string"
Expand Down
6 changes: 4 additions & 2 deletions plugins/cloudflare/resources/services/ips.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/cloudflare/cloudflare-go"
"github.com/cloudquery/cq-provider-cloudflare/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
Expand All @@ -16,8 +17,9 @@ type IpWrapper struct {
//go:generate cq-gen --resource ips --config ips.hcl --output .
func Ips() *schema.Table {
return &schema.Table{
Name: "cloudflare_ips",
Resolver: fetchIps,
Name: "cloudflare_ips",
Resolver: fetchIps,
DeleteFilter: client.DeleteFilter,
Columns: []schema.Column{
{
Name: "ip",
Expand Down
5 changes: 5 additions & 0 deletions plugins/cloudflare/resources/services/ips.hcl
Expand Up @@ -3,6 +3,11 @@ output_directory = "."
add_generate = true

resource "cloudflare" "" "ips" {

deleteFilter "DeleteFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteFilter"
}

userDefinedColumn "ip" {
type = "string"
description = "Cloudflare ip cidr address."
Expand Down
11 changes: 6 additions & 5 deletions plugins/cloudflare/resources/services/waf.go
Expand Up @@ -12,11 +12,12 @@ import (
//go:generate cq-gen --resource waf --config waf.hcl --output .
func Wafs() *schema.Table {
return &schema.Table{
Name: "cloudflare_waf",
Description: "WAFPackage represents a WAF package configuration.",
Resolver: fetchWafs,
Multiplex: client.ZoneMultiplex,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Name: "cloudflare_waf",
Description: "WAFPackage represents a WAF package configuration.",
Resolver: fetchWafs,
Multiplex: client.ZoneMultiplex,
DeleteFilter: client.DeleteAccountZoneFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Columns: []schema.Column{
{
Name: "account_id",
Expand Down
4 changes: 4 additions & 0 deletions plugins/cloudflare/resources/services/waf.hcl
Expand Up @@ -15,6 +15,10 @@ resource "cloudflare" "" "waf" {
]
}

deleteFilter "DeleteAccountZoneFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteAccountZoneFilter"
}

userDefinedColumn "account_id" {
description = "The Account ID of the resource."
type = "string"
Expand Down
10 changes: 9 additions & 1 deletion plugins/cloudflare/resources/services/workers.hcl
Expand Up @@ -9,6 +9,10 @@ resource "cloudflare" "" "workers_scripts" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.AccountMultiplex"
}

deleteFilter "DeleteAccountFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteAccountFilter"
}

options {
primary_keys = [
"id"
Expand Down Expand Up @@ -102,10 +106,14 @@ resource "cloudflare" "" "workers_scripts" {
resource "cloudflare" "" "workers_routes" {
path = "github.com/cloudflare/cloudflare-go/.WorkerRoute"

multiplex "CFZone" {
multiplex "CFZone" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.ZoneMultiplex"
}

deleteFilter "DeleteAccountZoneFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteAccountZoneFilter"
}

options {
primary_keys = [
"id"
Expand Down
11 changes: 6 additions & 5 deletions plugins/cloudflare/resources/services/workers_routes.go
Expand Up @@ -11,11 +11,12 @@ import (
//go:generate cq-gen --resource workers_routes --config workers.hcl --output .
func WorkersRoutes() *schema.Table {
return &schema.Table{
Name: "cloudflare_workers_routes",
Description: "WorkerRoute is used to map traffic matching a URL pattern to a workers API reference: https://api.cloudflare.com/#worker-routes-properties",
Resolver: fetchWorkersRoutes,
Multiplex: client.ZoneMultiplex,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Name: "cloudflare_workers_routes",
Description: "WorkerRoute is used to map traffic matching a URL pattern to a workers API reference: https://api.cloudflare.com/#worker-routes-properties",
Resolver: fetchWorkersRoutes,
Multiplex: client.ZoneMultiplex,
DeleteFilter: client.DeleteAccountZoneFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Columns: []schema.Column{
{
Name: "account_id",
Expand Down
11 changes: 6 additions & 5 deletions plugins/cloudflare/resources/services/workers_scripts.go
Expand Up @@ -12,11 +12,12 @@ import (
//go:generate cq-gen --resource workers_scripts --config workers.hcl --output .
func WorkersScripts() *schema.Table {
return &schema.Table{
Name: "cloudflare_workers_scripts",
Description: "WorkerMetaData contains worker script information such as size, creation & modification dates.",
Resolver: fetchWorkersScripts,
Multiplex: client.AccountMultiplex,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Name: "cloudflare_workers_scripts",
Description: "WorkerMetaData contains worker script information such as size, creation & modification dates.",
Resolver: fetchWorkersScripts,
Multiplex: client.AccountMultiplex,
DeleteFilter: client.DeleteAccountFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Columns: []schema.Column{
{
Name: "account_id",
Expand Down
11 changes: 6 additions & 5 deletions plugins/cloudflare/resources/services/zones.go
Expand Up @@ -12,11 +12,12 @@ import (
//go:generate cq-gen --resource zones --config zones.hcl --output .
func Zones() *schema.Table {
return &schema.Table{
Name: "cloudflare_zones",
Description: "Zone describes a Cloudflare zone.",
Resolver: fetchZones,
Multiplex: client.AccountMultiplex,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Name: "cloudflare_zones",
Description: "Zone describes a Cloudflare zone.",
Resolver: fetchZones,
Multiplex: client.AccountMultiplex,
DeleteFilter: client.DeleteAccountFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Columns: []schema.Column{
{
Name: "account_id",
Expand Down
4 changes: 4 additions & 0 deletions plugins/cloudflare/resources/services/zones.hcl
Expand Up @@ -9,6 +9,10 @@ resource "cloudflare" "" "zones" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.AccountMultiplex"
}

deleteFilter "DeleteAccountFilter" {
path = "github.com/cloudquery/cq-provider-cloudflare/client.DeleteAccountFilter"
}

options {
primary_keys = [
"id"
Expand Down

0 comments on commit fbc6db3

Please sign in to comment.