Skip to content

Commit ea6735b

Browse files
authored
feat!: Migrate CloudFlare plugin to v2 (#1777)
1 parent bf05794 commit ea6735b

85 files changed

Lines changed: 2032 additions & 3191 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

plugins/source/cloudflare/client/client.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package client
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"os"
78

89
"github.com/cloudflare/cloudflare-go"
9-
"github.com/cloudquery/cq-provider-sdk/provider/diag"
10-
"github.com/cloudquery/cq-provider-sdk/provider/schema"
11-
"github.com/hashicorp/go-hclog"
10+
"github.com/cloudquery/plugin-sdk/schema"
11+
"github.com/cloudquery/plugin-sdk/specs"
12+
"github.com/rs/zerolog"
1213
)
1314

1415
type AccountZones map[string]struct {
@@ -19,7 +20,7 @@ type AccountZones map[string]struct {
1920
type Clients map[string]Api
2021

2122
type Client struct {
22-
logger hclog.Logger
23+
logger zerolog.Logger
2324

2425
accountsZones AccountZones
2526
clients Clients
@@ -31,7 +32,7 @@ type Client struct {
3132

3233
const MaxItemsPerPage = 200
3334

34-
func New(logger hclog.Logger, clients Clients, clientApi Api, accountsZones AccountZones) Client {
35+
func New(logger zerolog.Logger, clients Clients, clientApi Api, accountsZones AccountZones) Client {
3536
return Client{
3637
logger: logger,
3738
accountsZones: accountsZones,
@@ -40,23 +41,23 @@ func New(logger hclog.Logger, clients Clients, clientApi Api, accountsZones Acco
4041
}
4142
}
4243

43-
func (c *Client) Logger() hclog.Logger {
44-
return c.logger
44+
func (c *Client) Logger() *zerolog.Logger {
45+
return &c.logger
4546
}
4647

47-
func (c *Client) withAccountId(accountId string) *Client {
48+
func (c *Client) withAccountID(accountId string) *Client {
4849
return &Client{
49-
logger: c.logger.With("account_id", obfuscateId(accountId)),
50+
logger: c.logger.With().Str("account_id", accountId).Logger(),
5051
accountsZones: c.accountsZones,
5152
clients: c.clients,
5253
ClientApi: c.clients[accountId],
5354
AccountId: accountId,
5455
}
5556
}
5657

57-
func (c *Client) withZoneId(accountId, zoneId string) *Client {
58+
func (c *Client) withZoneID(accountId, zoneId string) *Client {
5859
return &Client{
59-
logger: c.logger.With("account_id", obfuscateId(accountId), "zone_id", obfuscateId(zoneId)),
60+
logger: c.logger.With().Str("account_id", accountId).Str("zone_id", zoneId).Logger(),
6061
accountsZones: c.accountsZones,
6162
clients: c.clients,
6263
ClientApi: c.clients[accountId],
@@ -65,31 +66,30 @@ func (c *Client) withZoneId(accountId, zoneId string) *Client {
6566
}
6667
}
6768

68-
func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag.Diagnostics) {
69-
var diags diag.Diagnostics
70-
71-
ctx := context.Background()
72-
providerConfig := config.(*Config)
73-
74-
clientApi, err := getCloudflareClient(providerConfig)
69+
func Configure(ctx context.Context, logger zerolog.Logger, s specs.Source) (schema.ClientMeta, error) {
70+
cfSpec := &Spec{}
71+
if err := s.UnmarshalSpec(cfSpec); err != nil {
72+
return nil, fmt.Errorf("failed to unmarshal cloudflare spec: %w", err)
73+
}
7574

75+
clientApi, err := getCloudflareClient(cfSpec)
7676
if err != nil {
77-
return nil, diags.Add(classifyError(err, diag.INTERNAL, diag.WithSeverity(diag.ERROR))) // TODO remove diag
77+
return nil, err
7878
}
7979

8080
var accountsZones = make(AccountZones)
8181

8282
// Get available accounts
8383
accounts, _, err := clientApi.Accounts(ctx, cloudflare.AccountsListParams{})
8484
if err != nil {
85-
return nil, diags.Add(classifyError(err, diag.INTERNAL, diag.WithSeverity(diag.ERROR))) // TODO remove diag
85+
return nil, err
8686
}
8787

8888
for _, account := range accounts {
8989
// Get available zones for each account
9090
zones, err := clientApi.ListZonesContext(ctx, cloudflare.WithZoneFilters("", account.ID, ""))
9191
if err != nil {
92-
// TODO log error and continue
92+
logger.Warn().Err(err).Msg("ListZonesContext failed")
9393
continue
9494
}
9595
var zoneIds []string
@@ -107,14 +107,14 @@ func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag
107107
}
108108

109109
if len(accountsZones) == 0 {
110-
return nil, diags.Add(classifyError(errors.New("no accounts found"), diag.INTERNAL, diag.WithSeverity(diag.ERROR))) // TODO remove diag
110+
return nil, errors.New("no accounts found")
111111
}
112112

113113
clients := make(Clients)
114114
for _, account := range accountsZones {
115-
c, err := getCloudflareClient(providerConfig)
115+
c, err := getCloudflareClient(cfSpec)
116116
if err != nil {
117-
return nil, diags.Add(classifyError(err, diag.INTERNAL, diag.WithSeverity(diag.ERROR))) // TODO remove diag
117+
return nil, err
118118
}
119119
c.AccountID = account.AccountId
120120
clients[account.AccountId] = c
@@ -124,7 +124,7 @@ func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag
124124
return &c, nil
125125
}
126126

127-
func getCloudflareClient(config *Config) (*cloudflare.API, error) {
127+
func getCloudflareClient(config *Spec) (*cloudflare.API, error) {
128128
// Try to get the API token from the environment
129129
token := config.Token
130130
if token == "" {
@@ -148,10 +148,6 @@ func getCloudflareClient(config *Config) (*cloudflare.API, error) {
148148
return nil, errors.New("no API token or API key/email provided")
149149
}
150150

151-
func obfuscateId(accountId string) string {
152-
return accountId[:4] + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
153-
}
154-
155151
func getApiTokenFromEnv() string {
156152
apiToken := os.Getenv("CLOUDFLARE_API_TOKEN")
157153
if apiToken == "" {

plugins/source/cloudflare/client/config.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

plugins/source/cloudflare/client/errors.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

plugins/source/cloudflare/client/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package client
22

3-
import "github.com/cloudquery/cq-provider-sdk/provider/schema"
3+
import "github.com/cloudquery/plugin-sdk/schema"
44

55
func DeleteAccountFilter(meta schema.ClientMeta, _ *schema.Resource) []interface{} {
66
client := meta.(*Client)

plugins/source/cloudflare/client/multiplexers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package client
22

3-
import "github.com/cloudquery/cq-provider-sdk/provider/schema"
3+
import "github.com/cloudquery/plugin-sdk/schema"
44

55
func AccountMultiplex(meta schema.ClientMeta) []schema.ClientMeta {
66
var l = make([]schema.ClientMeta, 0)
77
client := meta.(*Client)
88
for _, accountZones := range client.accountsZones {
9-
l = append(l, client.withAccountId(accountZones.AccountId))
9+
l = append(l, client.withAccountID(accountZones.AccountId))
1010
}
1111
return l
1212
}
@@ -16,7 +16,7 @@ func ZoneMultiplex(meta schema.ClientMeta) []schema.ClientMeta {
1616
client := meta.(*Client)
1717
for _, accountZones := range client.accountsZones {
1818
for _, zone := range accountZones.Zones {
19-
l = append(l, client.withZoneId(accountZones.AccountId, zone))
19+
l = append(l, client.withZoneID(accountZones.AccountId, zone))
2020
}
2121
}
2222
return l

plugins/source/cloudflare/client/resolvers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package client
33
import (
44
"context"
55

6-
"github.com/cloudquery/cq-provider-sdk/provider/schema"
6+
"github.com/cloudquery/plugin-sdk/schema"
77
)
88

9-
func ResolveAccountId(_ context.Context, meta schema.ClientMeta, r *schema.Resource, col schema.Column) error {
9+
func ResolveAccountID(_ context.Context, meta schema.ClientMeta, r *schema.Resource, col schema.Column) error {
1010
client := meta.(*Client)
1111
return r.Set(col.Name, client.AccountId)
1212
}
1313

14-
func ResolveZoneId(_ context.Context, meta schema.ClientMeta, r *schema.Resource, col schema.Column) error {
14+
func ResolveZoneID(_ context.Context, meta schema.ClientMeta, r *schema.Resource, col schema.Column) error {
1515
client := meta.(*Client)
1616
return r.Set(col.Name, client.ZoneId)
1717
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package client
2+
3+
type Spec struct {
4+
Token string `json:"api_token,omitempty"`
5+
ApiKey string `json:"api_key,omitempty"`
6+
ApiEmail string `json:"api_email,omitempty"`
7+
Accounts []string `json:"accounts,omitempty"`
8+
Zones []string `json:"zones,omitempty"`
9+
}
Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,61 @@
11
package client
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
47
"testing"
8+
"time"
59

6-
"github.com/cloudquery/cq-provider-sdk/logging"
7-
"github.com/cloudquery/cq-provider-sdk/provider"
8-
"github.com/cloudquery/cq-provider-sdk/provider/diag"
9-
"github.com/cloudquery/cq-provider-sdk/provider/schema"
10-
providertest "github.com/cloudquery/cq-provider-sdk/provider/testing"
10+
"github.com/cloudquery/plugin-sdk/plugins"
11+
"github.com/cloudquery/plugin-sdk/schema"
12+
"github.com/cloudquery/plugin-sdk/specs"
1113
"github.com/golang/mock/gomock"
12-
"github.com/hashicorp/go-hclog"
14+
"github.com/rs/zerolog"
1315
)
1416

1517
const (
1618
TestAccountID = "test_account"
1719
TestZoneID = "test_zone"
1820
)
1921

20-
func CFMockTestHelper(t *testing.T, table *schema.Table, builder func(*testing.T, *gomock.Controller) Clients) {
22+
func MockTestHelper(t *testing.T, table *schema.Table, builder func(*testing.T, *gomock.Controller) Clients) {
2123
t.Helper()
24+
table.IgnoreInTests = false
25+
2226
ctrl := gomock.NewController(t)
27+
defer ctrl.Finish()
2328

24-
cfg := ""
25-
26-
providertest.TestResource(t, providertest.ResourceTestCase{
27-
Provider: &provider.Provider{
28-
Name: "cloudflare_mock_test_provider",
29-
Version: "development",
30-
Configure: func(logger hclog.Logger, i interface{}) (schema.ClientMeta, diag.Diagnostics) {
31-
clients := builder(t, ctrl)
32-
33-
c := New(logging.New(&hclog.LoggerOptions{
34-
Level: hclog.Warn,
35-
}), clients, clients[TestAccountID], AccountZones{
36-
TestAccountID: {
37-
AccountId: TestAccountID,
38-
Zones: []string{TestZoneID},
39-
},
40-
})
41-
return &c, nil
42-
},
43-
ResourceMap: map[string]*schema.Table{
44-
"test_resource": table,
45-
},
46-
Config: func() provider.Config {
47-
return &Config{}
29+
newTestExecutionClient := func(ctx context.Context, _ zerolog.Logger, spec specs.Source) (schema.ClientMeta, error) {
30+
var cfSpec Spec
31+
if err := spec.UnmarshalSpec(&cfSpec); err != nil {
32+
return nil, fmt.Errorf("failed to unmarshal cloudflare spec: %w", err)
33+
}
34+
logger := zerolog.New(zerolog.NewTestWriter(t)).Output(
35+
zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.StampMicro},
36+
).Level(zerolog.DebugLevel).With().Timestamp().Logger()
37+
38+
clients := builder(t, ctrl)
39+
c := New(logger, clients, clients[TestAccountID], AccountZones{
40+
TestAccountID: {
41+
AccountId: TestAccountID,
42+
Zones: []string{TestZoneID},
4843
},
44+
})
45+
46+
return c.withZoneID(TestAccountID, TestZoneID), nil
47+
}
48+
49+
p := plugins.NewSourcePlugin(
50+
table.Name,
51+
"dev",
52+
[]*schema.Table{
53+
table,
4954
},
50-
Config: cfg,
55+
newTestExecutionClient,
56+
)
57+
plugins.TestSourcePluginSync(t, p, specs.Source{
58+
Name: "dev",
59+
Tables: []string{table.Name},
5160
})
5261
}

0 commit comments

Comments
 (0)