Skip to content

Commit

Permalink
ensure we are passing around the correct ctx value
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Apr 26, 2023
1 parent f037b65 commit 611b19b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 52 deletions.
6 changes: 0 additions & 6 deletions .golintci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ run:
issues:
max-per-linter: 0
max-same-issues: 0
exclude:
# no way to flag these paths as ignore in contextcheck
- "Function `toRuleset->toRulesetRule->StringSet` should pass the context parameter"
- "Function `toRulesetResourceModel` should pass the context parameter"
- "Function `remapPreservedRuleIDs->toRuleset->toRulesetRule->StringSet` should pass the context parameter"
- "Function `buildChallengeWidgetFromModel->StringSet` should pass the context parameter"

linters:
disable-all: true
Expand Down
4 changes: 2 additions & 2 deletions internal/framework/expanders/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// StringList accepts a `types.List` and returns a slice of strings.
func StringList(in types.List) []string {
func StringList(ctx context.Context, in types.List) []string {
results := []string{}
_ = in.ElementsAs(context.Background(), &results, false)
_ = in.ElementsAs(ctx, &results, false)
return results
}
4 changes: 2 additions & 2 deletions internal/framework/expanders/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// StringSet accepts a `types.Set` and returns a slice of strings.
func StringSet(in types.Set) []string {
func StringSet(ctx context.Context, in types.Set) []string {
results := []string{}
_ = in.ElementsAs(context.Background(), &results, false)
_ = in.ElementsAs(ctx, &results, false)
return results
}
2 changes: 2 additions & 0 deletions internal/framework/flatteners/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
// } else {
// requestPayload.Enabled = types.BoolNull()
// }
//
// nolint: contextcheck
func Bool(in *bool) basetypes.BoolValue {
if reflect.ValueOf(in).IsNil() {
return types.BoolNull()
Expand Down
2 changes: 2 additions & 0 deletions internal/framework/flatteners/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
// }
//
// Not recommended if you care about returning an empty string for the state.
//
// nolint: contextcheck
func Int64(in int64) basetypes.Int64Value {
if in == 0 {
return types.Int64Null()
Expand Down
2 changes: 2 additions & 0 deletions internal/framework/flatteners/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
// StringSet accepts a `[]attr.Value` and returns a `basetypes.SetValue`. The
// return type automatically handles `SetNull` for empty results and coercing
// all element values to a string if there are any elements.
//
// nolint: contextcheck
func StringSet(in []attr.Value) basetypes.SetValue {
if len(in) == 0 {
return types.SetNull(types.StringType)
Expand Down
2 changes: 2 additions & 0 deletions internal/framework/flatteners/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
// }
//
// Not recommended if you care about returning an empty string for the state.
//
// nolint: contextcheck
func String(in string) basetypes.StringValue {
if in == "" {
return types.StringNull()
Expand Down
76 changes: 38 additions & 38 deletions internal/framework/service/rulesets/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *RulesetResource) Create(ctx context.Context, req resource.CreateRequest
Phase: rulesetPhase,
}

rulesetData := data.toRuleset()
rulesetData := data.toRuleset(ctx)

if len(rulesetData.Rules) > 0 {
rs.Rules = rulesetData.Rules
Expand Down Expand Up @@ -166,7 +166,7 @@ func (r *RulesetResource) Create(ctx context.Context, req resource.CreateRequest

data.ID = types.StringValue(ruleset.ID)

diags = resp.State.Set(ctx, toRulesetResourceModel(data.ZoneID, data.AccountID, ruleset))
diags = resp.State.Set(ctx, toRulesetResourceModel(ctx, data.ZoneID, data.AccountID, ruleset))
resp.Diagnostics.Append(diags...)
}

Expand Down Expand Up @@ -204,7 +204,7 @@ func (r *RulesetResource) Read(ctx context.Context, req resource.ReadRequest, re
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, toRulesetResourceModel(zoneID, accountID, ruleset))...)
resp.Diagnostics.Append(resp.State.Set(ctx, toRulesetResourceModel(ctx, zoneID, accountID, ruleset))...)
}

func (r *RulesetResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand All @@ -223,7 +223,7 @@ func (r *RulesetResource) Update(ctx context.Context, req resource.UpdateRequest
accountID := plan.AccountID
zoneID := plan.ZoneID.ValueString()

remappedRules, e := remapPreservedRuleIDs(state, plan)
remappedRules, e := remapPreservedRuleIDs(ctx, state, plan)
if e != nil {
resp.Diagnostics.AddError("failed to remap rule IDs from state", e.Error())
return
Expand All @@ -245,7 +245,7 @@ func (r *RulesetResource) Update(ctx context.Context, req resource.UpdateRequest

plan.ID = types.StringValue(rs.ID)

resp.Diagnostics.Append(resp.State.Set(ctx, toRulesetResourceModel(state.ZoneID, state.AccountID, rs))...)
resp.Diagnostics.Append(resp.State.Set(ctx, toRulesetResourceModel(ctx, state.ZoneID, state.AccountID, rs))...)
}

func (r *RulesetResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *RulesetResource) ImportState(ctx context.Context, req resource.ImportSt
//
// The reverse of this method is `toRuleset` which handles building an API
// representation using the proposed config.
func toRulesetResourceModel(zoneID, accountID basetypes.StringValue, in cloudflare.Ruleset) *RulesetResourceModel {
func toRulesetResourceModel(ctx context.Context, zoneID, accountID basetypes.StringValue, in cloudflare.Ruleset) *RulesetResourceModel {
data := RulesetResourceModel{
ID: types.StringValue(in.ID),
Description: types.StringValue(in.Description),
Expand Down Expand Up @@ -503,17 +503,17 @@ func toRulesetResourceModel(zoneID, accountID basetypes.StringValue, in cloudfla
}

if ruleResponse.ActionParameters.CacheKey.CustomKey.Cookie != nil {
include, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Cookie.Include)
checkPresence, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Cookie.CheckPresence)
include, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Cookie.Include)
checkPresence, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Cookie.CheckPresence)
key.Cookie = []*ActionParameterCacheKeyCustomKeyCookieModel{{
Include: include,
CheckPresence: checkPresence,
}}
}

if ruleResponse.ActionParameters.CacheKey.CustomKey.Header != nil {
include, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Header.Include)
checkPresence, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Header.CheckPresence)
include, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Header.Include)
checkPresence, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Header.CheckPresence)
if len(include.Elements()) > 0 || len(checkPresence.Elements()) > 0 {
key.Header = []*ActionParameterCacheKeyCustomKeyHeaderModel{{
Include: include,
Expand All @@ -524,22 +524,22 @@ func toRulesetResourceModel(zoneID, accountID basetypes.StringValue, in cloudfla
}

if ruleResponse.ActionParameters.CacheKey.CustomKey.Query != nil {
include, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include)
exclude, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude)
include, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include)
exclude, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude)

if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include != nil {
if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include.All {
include, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, []string{"*"})
include, _ = basetypes.NewSetValueFrom(ctx, types.StringType, []string{"*"})
} else {
include, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include.List)
include, _ = basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include.List)
}
}

if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude != nil {
if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude.All {
exclude, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, []string{"*"})
exclude, _ = basetypes.NewSetValueFrom(ctx, types.StringType, []string{"*"})
} else {
exclude, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude.List)
exclude, _ = basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude.List)
}
}

Expand Down Expand Up @@ -716,13 +716,13 @@ func toRulesetResourceModel(zoneID, accountID basetypes.StringValue, in cloudfla
//
// The reverse of this method is `toRulesetResourceModel` which handles building
// a state representation using the API response.
func (r *RulesetResourceModel) toRuleset() cloudflare.Ruleset {
func (r *RulesetResourceModel) toRuleset(ctx context.Context) cloudflare.Ruleset {
var rs cloudflare.Ruleset
var rules []cloudflare.RulesetRule

rs.ID = r.ID.ValueString()
for _, rule := range r.Rules {
rules = append(rules, rule.toRulesetRule())
rules = append(rules, rule.toRulesetRule(ctx))
}

rs.Rules = rules
Expand All @@ -732,7 +732,7 @@ func (r *RulesetResourceModel) toRuleset() cloudflare.Ruleset {

// toRulesetRule takes a state representation of a Ruleset Rule and transforms
// it into an API representation.
func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {
func (r *RulesModel) toRulesetRule(ctx context.Context) cloudflare.RulesetRule {
rr := cloudflare.RulesetRule{
Action: r.Action.ValueString(),
Expression: r.Expression.ValueString(),
Expand Down Expand Up @@ -769,16 +769,16 @@ func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {
}
}

if len(expanders.StringSet(ap.Phases)) > 0 {
rr.ActionParameters.Phases = expanders.StringSet(ap.Phases)
if len(expanders.StringSet(ctx, ap.Phases)) > 0 {
rr.ActionParameters.Phases = expanders.StringSet(ctx, ap.Phases)
}

if len(expanders.StringSet(ap.Products)) > 0 {
rr.ActionParameters.Products = expanders.StringSet(ap.Products)
if len(expanders.StringSet(ctx, ap.Products)) > 0 {
rr.ActionParameters.Products = expanders.StringSet(ctx, ap.Products)
}

if len(expanders.StringSet(ap.Rulesets)) > 0 {
rr.ActionParameters.Rulesets = expanders.StringSet(ap.Rulesets)
if len(expanders.StringSet(ctx, ap.Rulesets)) > 0 {
rr.ActionParameters.Rulesets = expanders.StringSet(ctx, ap.Rulesets)
}

if !ap.ID.IsNull() {
Expand Down Expand Up @@ -1072,8 +1072,8 @@ func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {
customKey := cloudflare.RulesetRuleActionParametersCustomKey{}

if len(ap.CacheKey[0].CustomKey[0].QueryString) > 0 {
includeQueryList := expanders.StringSet(ap.CacheKey[0].CustomKey[0].QueryString[0].Include)
excludeQueryList := expanders.StringSet(ap.CacheKey[0].CustomKey[0].QueryString[0].Exclude)
includeQueryList := expanders.StringSet(ctx, ap.CacheKey[0].CustomKey[0].QueryString[0].Include)
excludeQueryList := expanders.StringSet(ctx, ap.CacheKey[0].CustomKey[0].QueryString[0].Exclude)

if len(includeQueryList) > 0 {
if len(includeQueryList) == 1 && includeQueryList[0] == "*" {
Expand Down Expand Up @@ -1109,8 +1109,8 @@ func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {
}

if len(ap.CacheKey[0].CustomKey[0].Header) > 0 {
includeQueryList := expanders.StringSet(ap.CacheKey[0].CustomKey[0].Header[0].Include)
checkPresenceList := expanders.StringSet(basetypes.SetValue(ap.CacheKey[0].CustomKey[0].Header[0].CheckPresence))
includeQueryList := expanders.StringSet(ctx, ap.CacheKey[0].CustomKey[0].Header[0].Include)
checkPresenceList := expanders.StringSet(ctx, basetypes.SetValue(ap.CacheKey[0].CustomKey[0].Header[0].CheckPresence))

customKey.Header = &cloudflare.RulesetRuleActionParametersCustomKeyHeader{
RulesetRuleActionParametersCustomKeyFields: cloudflare.RulesetRuleActionParametersCustomKeyFields{
Expand All @@ -1122,8 +1122,8 @@ func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {
}

if len(ap.CacheKey[0].CustomKey[0].Cookie) > 0 {
includeQueryList := expanders.StringSet(ap.CacheKey[0].CustomKey[0].Cookie[0].Include)
checkPresenceList := expanders.StringSet(basetypes.SetValue(ap.CacheKey[0].CustomKey[0].Cookie[0].CheckPresence))
includeQueryList := expanders.StringSet(ctx, ap.CacheKey[0].CustomKey[0].Cookie[0].Include)
checkPresenceList := expanders.StringSet(ctx, basetypes.SetValue(ap.CacheKey[0].CustomKey[0].Cookie[0].CheckPresence))

if len(includeQueryList) > 0 || len(checkPresenceList) > 0 {
customKey.Cookie = &cloudflare.RulesetRuleActionParametersCustomKeyCookie{
Expand Down Expand Up @@ -1218,21 +1218,21 @@ func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {
rr.ActionParameters.FromValue = from
}

apCookieFields := expanders.StringSet(ap.CookieFields)
apCookieFields := expanders.StringSet(ctx, ap.CookieFields)
if len(apCookieFields) > 0 {
for _, cookie := range apCookieFields {
rr.ActionParameters.CookieFields = append(rr.ActionParameters.CookieFields, cloudflare.RulesetActionParametersLogCustomField{Name: cookie})
}
}

apRequestFields := expanders.StringSet(ap.RequestFields)
apRequestFields := expanders.StringSet(ctx, ap.RequestFields)
if len(apRequestFields) > 0 {
for _, request := range apRequestFields {
rr.ActionParameters.RequestFields = append(rr.ActionParameters.RequestFields, cloudflare.RulesetActionParametersLogCustomField{Name: request})
}
}

apResponseFields := expanders.StringSet(ap.ResponseFields)
apResponseFields := expanders.StringSet(ctx, ap.ResponseFields)
if len(apResponseFields) > 0 {
for _, request := range apResponseFields {
rr.ActionParameters.ResponseFields = append(rr.ActionParameters.ResponseFields, cloudflare.RulesetActionParametersLogCustomField{Name: request})
Expand All @@ -1242,7 +1242,7 @@ func (r *RulesModel) toRulesetRule() cloudflare.RulesetRule {

for _, rl := range r.Ratelimit {
rr.RateLimit = &cloudflare.RulesetRuleRateLimit{
Characteristics: expanders.StringSet(rl.Characteristics),
Characteristics: expanders.StringSet(ctx, rl.Characteristics),
Period: int(rl.Period.ValueInt64()),
RequestsPerPeriod: int(rl.RequestsPerPeriod.ValueInt64()),
ScorePerPeriod: int(rl.ScorePerPeriod.ValueInt64()),
Expand Down Expand Up @@ -1353,9 +1353,9 @@ func newRuleIDs(rulesetRules []cloudflare.RulesetRule) (ruleIDs, error) {
return r, nil
}

func remapPreservedRuleIDs(state, plan *RulesetResourceModel) ([]cloudflare.RulesetRule, error) {
currentRuleset := state.toRuleset()
plannedRuleset := plan.toRuleset()
func remapPreservedRuleIDs(ctx context.Context, state, plan *RulesetResourceModel) ([]cloudflare.RulesetRule, error) {
currentRuleset := state.toRuleset(ctx)
plannedRuleset := plan.toRuleset(ctx)

ids, err := newRuleIDs(currentRuleset.Rules)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/framework/service/turnstile/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *TurnstileWidgetResource) Create(ctx context.Context, req resource.Creat
return
}

widget := buildChallengeWidgetFromModel(data)
widget := buildChallengeWidgetFromModel(ctx, data)

createWidget, err := r.client.CreateTurnstileWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()),
cloudflare.CreateTurnstileWidgetParams{
Expand Down Expand Up @@ -115,7 +115,7 @@ func (r *TurnstileWidgetResource) Update(ctx context.Context, req resource.Updat
return
}

widget := buildChallengeWidgetFromModel(data)
widget := buildChallengeWidgetFromModel(ctx, data)

updatedWidget, err := r.client.UpdateTurnstileWidget(ctx, cloudflare.AccountIdentifier(data.AccountID.ValueString()), cloudflare.UpdateTurnstileWidgetParams{
OffLabel: widget.OffLabel,
Expand Down Expand Up @@ -162,14 +162,14 @@ func (r *TurnstileWidgetResource) ImportState(ctx context.Context, req resource.
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
}

func buildChallengeWidgetFromModel(widget *TurnstileWidgetModel) cloudflare.TurnstileWidget {
func buildChallengeWidgetFromModel(ctx context.Context, widget *TurnstileWidgetModel) cloudflare.TurnstileWidget {
built := cloudflare.TurnstileWidget{
SiteKey: widget.ID.ValueString(),
Name: widget.Name.ValueString(),
BotFightMode: widget.BotFightMode.ValueBool(),
Mode: widget.Mode.ValueString(),
Region: widget.Region.ValueString(),
Domains: expanders.StringSet(widget.Domains),
Domains: expanders.StringSet(ctx, widget.Domains),
OffLabel: widget.OffLabel.ValueBool(),
}

Expand Down

0 comments on commit 611b19b

Please sign in to comment.