-
Notifications
You must be signed in to change notification settings - Fork 81
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
feat: add support for client-side prerequisite events #452
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
992dded
feat: add support for client-side prerequisite events
cwaldren-ld f9f29c3
adding test scaffolding
cwaldren-ld 17b4203
attempt to add a flag with prerequisites via LD API
cwaldren-ld fae991f
unique flag keys
cwaldren-ld 43ad690
add dedicated methods to create the flags
cwaldren-ld 461e14e
remove nesting
cwaldren-ld fe7b668
add another test
cwaldren-ld 4b3c33c
add flagBuilder pattern
cwaldren-ld 3668f83
add third test
cwaldren-ld b6eb56a
code layout
cwaldren-ld 8fe6899
use errors.As
cwaldren-ld 903cc72
don't have a redundant logging routine
cwaldren-ld ff75ac0
add test for mobile keys
cwaldren-ld 4e0c497
refactoring
cwaldren-ld 5bd4aa5
refactor to put flag setup in each test
cwaldren-ld 5856bcd
remove old comment
cwaldren-ld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
//go:build integrationtests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a whole new builder scheme just for these tests - we need a simple way to create flags via REST API. The existing methods were single-purpose. |
||
|
||
package integrationtests | ||
|
||
import ( | ||
"fmt" | ||
|
||
ldapi "github.com/launchdarkly/api-client-go/v13" | ||
"github.com/launchdarkly/go-sdk-common/v3/ldvalue" | ||
) | ||
|
||
type flagBuilder struct { | ||
key string | ||
projectKey string | ||
envKey string | ||
offVariation int | ||
fallthroughVariation int | ||
on bool | ||
variations []ldapi.Variation | ||
prerequisites []ldapi.Prerequisite | ||
clientSide ldapi.ClientSideAvailabilityPost | ||
helper *apiHelper | ||
} | ||
|
||
// newFlagBuilder creates a builder for a flag which will be created in the specified project and environment. | ||
// By default, the flag has two variations: off = false, and on = true. The flag is on by default. | ||
// Additionally, the flag is available to both mobile and client SDKs. | ||
func newFlagBuilder(helper *apiHelper, flagKey string, projectKey string, envKey string) *flagBuilder { | ||
builder := &flagBuilder{ | ||
key: flagKey, | ||
projectKey: projectKey, | ||
envKey: envKey, | ||
on: true, | ||
offVariation: 0, | ||
fallthroughVariation: 1, | ||
helper: helper, | ||
clientSide: ldapi.ClientSideAvailabilityPost{ | ||
UsingMobileKey: true, | ||
UsingEnvironmentId: true, | ||
}, | ||
} | ||
return builder.Variations(ldvalue.Bool(false), ldvalue.Bool(true)) | ||
} | ||
|
||
// Variations overwrites the flag's variations. A valid flag has two or more variations. | ||
func (f *flagBuilder) Variations(variation1 ldvalue.Value, variations ...ldvalue.Value) *flagBuilder { | ||
f.variations = nil | ||
for _, value := range append([]ldvalue.Value{variation1}, variations...) { | ||
valueAsInterface := value.AsArbitraryValue() | ||
f.variations = append(f.variations, ldapi.Variation{Value: &valueAsInterface}) | ||
} | ||
return f | ||
} | ||
|
||
// ClientSideUsingEnvironmentID enables the flag for clients that use environment ID for auth. | ||
func (f *flagBuilder) ClientSideUsingEnvironmentID(usingEnvID bool) *flagBuilder { | ||
f.clientSide.UsingEnvironmentId = usingEnvID | ||
return f | ||
} | ||
|
||
// ClientSideUsingMobileKey enables the flag for clients that use mobile keys for auth. | ||
func (f *flagBuilder) ClientSideUsingMobileKey(usingMobileKey bool) *flagBuilder { | ||
f.clientSide.UsingMobileKey = usingMobileKey | ||
return f | ||
} | ||
|
||
// Prerequisites overwrites the flag's prerequisites. | ||
func (f *flagBuilder) Prerequisites(prerequisites []ldapi.Prerequisite) *flagBuilder { | ||
f.prerequisites = prerequisites | ||
return f | ||
} | ||
|
||
// Prerequisite is a helper that calls Prerequisites with a single value. | ||
func (f *flagBuilder) Prerequisite(prerequisiteKey string, variation int32) *flagBuilder { | ||
return f.Prerequisites([]ldapi.Prerequisite{{Key: prerequisiteKey, Variation: variation}}) | ||
} | ||
|
||
// OffVariation sets the flag's off variation. | ||
func (f *flagBuilder) OffVariation(v int) *flagBuilder { | ||
f.offVariation = v | ||
return f | ||
} | ||
|
||
// FallthroughVariation sets the flag's fallthrough variation. | ||
func (f *flagBuilder) FallthroughVariation(v int) *flagBuilder { | ||
f.fallthroughVariation = v | ||
return f | ||
} | ||
|
||
// On enables or disables flag targeting. | ||
func (f *flagBuilder) On(on bool) *flagBuilder { | ||
f.on = on | ||
return f | ||
} | ||
|
||
// Create creates the flag using the LD REST API. | ||
func (f *flagBuilder) Create() error { | ||
flagPost := ldapi.FeatureFlagBody{ | ||
Name: f.key, | ||
Key: f.key, | ||
ClientSideAvailability: &f.clientSide, | ||
} | ||
|
||
_, _, err := f.helper.apiClient.FeatureFlagsApi. | ||
PostFeatureFlag(f.helper.apiContext, f.projectKey). | ||
FeatureFlagBody(flagPost). | ||
Execute() | ||
|
||
if err := f.logAPIResult("create flag", err); err != nil { | ||
return err | ||
} | ||
|
||
envPrefix := fmt.Sprintf("/environments/%s", f.envKey) | ||
patch := ldapi.PatchWithComment{ | ||
Patch: []ldapi.PatchOperation{ | ||
makePatch("replace", envPrefix+"/offVariation", f.offVariation), | ||
makePatch("replace", envPrefix+"/fallthrough/variation", f.fallthroughVariation), | ||
makePatch("replace", envPrefix+"/on", f.on), | ||
makePatch("replace", envPrefix+"/prerequisites", f.prerequisites), | ||
}, | ||
} | ||
|
||
_, _, err = f.helper.apiClient.FeatureFlagsApi. | ||
PatchFeatureFlag(f.helper.apiContext, f.projectKey, f.key). | ||
PatchWithComment(patch). | ||
Execute() | ||
|
||
return f.logAPIResult("patch flag", err) | ||
} | ||
|
||
func (f *flagBuilder) logAPIResult(desc string, err error) error { | ||
return f.helper.logResult(f.scopedOp(desc), err) | ||
} | ||
|
||
func (f *flagBuilder) scopedOp(desc string) string { | ||
return fmt.Sprintf("%s %s in %s/%s", desc, f.key, f.projectKey, f.envKey) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This little bug (the error is a pointer) was causing response bodies not to be logged.