Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OpenFGAContainer ¶
type OpenFGAContainer struct {
testcontainers.Container
}
OpenFGAContainer represents the OpenFGA container type used in the module
func Run ¶ added in v0.32.0
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*OpenFGAContainer, error)
Run creates an instance of the OpenFGA container type
Example ¶
// runOpenFGAContainer {
ctx := context.Background()
openfgaContainer, err := openfga.Run(ctx, "openfga/openfga:v1.5.0")
defer func() {
if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// }
state, err := openfgaContainer.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.Running)
Output: true
Example (ConnectToPlayground) ¶
openfgaContainer, err := openfga.Run(context.Background(), "openfga/openfga:v1.5.0")
defer func() {
if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// playgroundEndpoint {
playgroundEndpoint, err := openfgaContainer.PlaygroundEndpoint(context.Background())
if err != nil {
log.Printf("failed to get playground endpoint: %s", err)
return
}
// }
httpClient := http.Client{}
resp, err := httpClient.Get(playgroundEndpoint)
if err != nil {
log.Printf("failed to get playground endpoint: %s", err)
return
}
fmt.Println(resp.StatusCode)
Output: 200
Example (ConnectWithSDKClient) ¶
openfgaContainer, err := openfga.Run(context.Background(), "openfga/openfga:v1.5.0")
defer func() {
if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// httpEndpoint {
httpEndpoint, err := openfgaContainer.HttpEndpoint(context.Background())
if err != nil {
log.Printf("failed to get HTTP endpoint: %s", err)
return
}
// }
// StoreId is not required for listing and creating stores
fgaClient, err := client.NewSdkClient(&client.ClientConfiguration{
ApiUrl: httpEndpoint, // required
})
if err != nil {
log.Printf("failed to create SDK client: %s", err)
return
}
list, err := fgaClient.ListStores(context.Background()).Execute()
if err != nil {
log.Printf("failed to list stores: %s", err)
return
}
fmt.Println(len(list.Stores))
store, err := fgaClient.CreateStore(context.Background()).Body(client.ClientCreateStoreRequest{Name: "test"}).Execute()
if err != nil {
log.Printf("failed to create store: %s", err)
return
}
fmt.Println(store.Name)
list, err = fgaClient.ListStores(context.Background()).Execute()
if err != nil {
log.Printf("failed to list stores: %s", err)
return
}
fmt.Println(len(list.Stores))
Output: 0 test 1
Example (WriteModel) ¶
// openFGAwriteModel {
secret := "openfga-secret"
openfgaContainer, err := openfga.Run(
context.Background(),
"openfga/openfga:v1.5.0",
testcontainers.WithEnv(map[string]string{
"OPENFGA_LOG_LEVEL": "warn",
"OPENFGA_AUTHN_METHOD": "preshared",
"OPENFGA_AUTHN_PRESHARED_KEYS": secret,
}),
)
defer func() {
if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
httpEndpoint, err := openfgaContainer.HttpEndpoint(context.Background())
if err != nil {
log.Printf("failed to get HTTP endpoint: %s", err)
return
}
fgaClient, err := client.NewSdkClient(&client.ClientConfiguration{
ApiUrl: httpEndpoint,
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
ApiToken: secret,
},
},
// because we are going to write an authorization model,
// we need to specify a store id. Else, it will fail with
// "Configuration.StoreId is required and must be specified to call this method"
// In this example, it's an arbitrary store id, that will be created
// on the fly.
StoreId: "11111111111111111111111111",
})
if err != nil {
log.Printf("failed to create openfga client: %v", err)
return
}
f, err := os.Open(filepath.Join("testdata", "authorization_model.json"))
if err != nil {
log.Printf("failed to open file: %v", err)
return
}
defer f.Close()
bs, err := io.ReadAll(f)
if err != nil {
log.Printf("failed to read file: %v", err)
return
}
var body client.ClientWriteAuthorizationModelRequest
if err := json.Unmarshal(bs, &body); err != nil {
log.Printf("failed to unmarshal json: %v", err)
return
}
resp, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(body).Execute()
if err != nil {
log.Printf("failed to write authorization model: %v", err)
return
}
// }
value, ok := resp.GetAuthorizationModelIdOk()
fmt.Println(ok)
fmt.Println(*value != "")
Output: true true
func RunContainer
deprecated
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*OpenFGAContainer, error)
Deprecated: use Run instead RunContainer creates an instance of the OpenFGA container type
func (*OpenFGAContainer) GrpcEndpoint ¶
func (c *OpenFGAContainer) GrpcEndpoint(ctx context.Context) (string, error)
GrpcEndpoint returns the gRPC endpoint for the OpenFGA container, which uses the 8081/tcp port.
func (*OpenFGAContainer) HttpEndpoint ¶
func (c *OpenFGAContainer) HttpEndpoint(ctx context.Context) (string, error)
HttpEndpoint returns the HTTP endpoint for the OpenFGA container, which uses the 8080/tcp port.
func (*OpenFGAContainer) PlaygroundEndpoint ¶
func (c *OpenFGAContainer) PlaygroundEndpoint(ctx context.Context) (string, error)
PlaygroundEndpoint returns the playground endpoint for the OpenFGA container, which is the HTTP endpoint with the path /playground in the port 3000/tcp.
Click to show internal directories.
Click to hide internal directories.