worried-journalist-71921
07/11/2024, 11:37 AMchilly-king-10285
07/11/2024, 11:37 AMchilly-king-10285
07/11/2024, 11:37 AMdefault_schema_id
of a project using the Go SDK, you can use the PatchIdentity
function provided by the Ory Go SDK. Here is an example of how you can do it: go package main import ( "context" "fmt" client "<http://github.com/ory/client-go|github.com/ory/client-go>" ) const ( YOUR_ORY_API_KEY = "$YOUR_ORY_API_KEY" YOUR_ORY_PROJECT_SLUG = "$PROJECT_SLUG" IDENTITY_ID_TO_UPDATE = "$IDENTITY_ID" NEW_SCHEMA_ID = "$NEW_SCHEMA_ID" ) func main() { if err := updateDefaultSchemaID(NEW_SCHEMA_ID, IDENTITY_ID_TO_UPDATE); err != nil { panic(err) } } func updateDefaultSchemaID(newSchemaID, identityID string) error { // Initialize the client configuration cfg := client.NewConfiguration() cfg.Servers = client.ServerConfigurations{ { URL: fmt.Sprintf("<https://%s.projects.oryapis.com>", YOUR_ORY_PROJECT_SLUG), }, } // Create an instance of the API client ory := client.NewAPIClient(cfg) // Set the access token ctx := context.Background() ctx = context.WithValue(ctx, client.ContextAccessToken, YOUR_ORY_API_KEY) // Prepare the JSON patch for the update jsonPatch := []client.JsonPatch{ {Op: "replace", Path: "/default_schema_id", Value: newSchemaID}, } // Apply the JSON patch to update the identity _, _, err := ory.IdentityApi.PatchIdentity(ctx, identityID).JsonPatch(jsonPatch).Execute() if err != nil { return err } // Verify that the schema was updated updatedIdentity, _, err := ory.IdentityApi.GetIdentity(ctx, identityID).Execute() if err != nil { return err } if updatedIdentity.SchemaId != newSchemaID { return fmt.Errorf("default_schema_id wasn't updated") } fmt.Println("default_schema_id updated 🎉") return nil }
In this code, replace the constants YOUR_ORY_API_KEY
, YOUR_ORY_PROJECT_SLUG
, IDENTITY_ID_TO_UPDATE
, and NEW_SCHEMA_ID
with your actual values. This code will update the default_schema_id
of the specified identity to the new schema ID. Please note that this code assumes that you have the necessary permissions to update the identity and that the new schema ID exists. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#update-identities-to-use-a-new-schema).worried-journalist-71921
07/11/2024, 11:40 AMjsonPatch := []openapiclient.JsonPatch{*openapiclient.NewJsonPatch("replace", "/name")} // []JsonPatch | (optional)
configuration := openapiclient.NewConfiguration()
apiClient := openapiclient.NewAPIClient(configuration)
resp, r, err := apiClient.ProjectAPI.PatchProject(context.Background(), projectId).JsonPatch(jsonPatch).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectAPI.PatchProject``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `PatchProject`: SuccessfulProjectUpdate
fmt.Fprintf(os.Stdout, "Response from `ProjectAPI.PatchProject`: %v\n", resp)
how can I update identity schema with GO SDK?chilly-king-10285
07/11/2024, 11:40 AMchilly-king-10285
07/11/2024, 11:41 AMPatchIdentity
function. Here is an example of how you can do it: go package main import ( "context" "fmt" client "<http://github.com/ory/client-go|github.com/ory/client-go>" ) const ( YOUR_ORY_API_KEY = "$YOUR_ORY_API_KEY" YOUR_ORY_PROJECT_SLUG = "$PROJECT_SLUG" IDENTITY_ID_TO_UPDATE = "$IDENTITY_ID" NEW_SCHEMA_ID = "$NEW_SCHEMA_ID" ) func main() { if err := updateIdentitySchema(NEW_SCHEMA_ID, IDENTITY_ID_TO_UPDATE); err != nil { panic(err) } } func updateIdentitySchema(newSchemaID, identityID string) error { // Initialize the client configuration cfg := client.NewConfiguration() cfg.Servers = client.ServerConfigurations{ { URL: fmt.Sprintf("<https://%s.projects.oryapis.com>", YOUR_ORY_PROJECT_SLUG), }, } // Create an instance of the API client ory := client.NewAPIClient(cfg) // Set the access token ctx := context.Background() ctx = context.WithValue(ctx, client.ContextAccessToken, YOUR_ORY_API_KEY) // Prepare the JSON patch for the update jsonPatch := []client.JsonPatch{ {Op: "replace", Path: "/schema_id", Value: newSchemaID}, } // Apply the JSON patch to update the identity _, _, err := ory.IdentityApi.PatchIdentity(ctx, identityID).JsonPatch(jsonPatch).Execute() if err != nil { return err } // Verify that the schema was updated updatedIdentity, _, err := ory.IdentityApi.GetIdentity(ctx, identityID).Execute() if err != nil { return err } if updatedIdentity.SchemaId != newSchemaID { return fmt.Errorf("schema wasn't updated") } fmt.Println("Identity schema updated 🎉") return nil }
In this code, replace the constants YOUR_ORY_API_KEY
, YOUR_ORY_PROJECT_SLUG
, IDENTITY_ID_TO_UPDATE
, and NEW_SCHEMA_ID
with your actual values. This code will update the schema_id
of the specified identity to the new schema ID. Please note that this code assumes that you have the necessary permissions to update the identity and that the new schema ID exists. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#update-identities-to-use-a-new-schema).worried-journalist-71921
07/11/2024, 11:44 AM"identity": {
"default_schema_id": "<preset://username>",
in the project config using go SDK>?chilly-king-10285
07/11/2024, 11:44 AMchilly-king-10285
07/11/2024, 11:45 AMdefault_schema_id
in the project configuration. The Go SDK provides methods to interact with identities, such as creating, retrieving, updating, and deleting identities, but it does not provide a method to directly update the project configuration. However, you can use the Ory CLI to update the default_schema_id
in the project configuration as shown in the following command: bash ory patch identity-config {your-project-id} \ --replace '/identity/default_schema_id="{unique-schema-id}"' \ --replace '/identity/schemas=[{"id":"{unique-schema-id}","url":"base64://'$schema'"}]'
In this command, replace {your-project-id}
, {unique-schema-id}
, and $schema
with your actual values. This command will update the default_schema_id
in the project configuration. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#create-custom-schema).worried-journalist-71921
07/11/2024, 11:47 AMdefault_schema_id
based on this example:
projectId := "83fa99f0-ecc4-4faf-8131-7be47dd722f4"
// jsonPatch := []openapiclient.JsonPatch{*openapiclient.NewJsonPatch("replace", "/name")} // []JsonPatch | (optional)
jsonPatch := []client.JsonPatch{
{Op: "replace", Path: "/identity/default_schema_id", Value: "<preset://email>"},
}
resp, r, err := k.workspaceClient.ProjectAPI.PatchProject(context.Background(), projectId).JsonPatch(jsonPatch).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ProjectAPI.PatchProject``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
chilly-king-10285
07/11/2024, 11:47 AMchilly-king-10285
07/11/2024, 11:47 AMworried-journalist-71921
07/11/2024, 1:12 PMchilly-king-10285
07/11/2024, 1:12 PMchilly-king-10285
07/11/2024, 1:13 PMdefault_schema_id
in the project configuration. Here is an example of how you can do it using the Ory CLI: bash # Encode your schema to Base64 and export it to a variable. schema=$(cat {path-to-json-with-identity-schema} | base64) # Update your project's configuration. ory patch identity-config {your-project-id} \ --replace '/identity/default_schema_id="<preset://email>"' \ --replace '/identity/schemas=[{"id":"<preset://email>","url":"base64://'$schema'"}]'
In this command, replace {path-to-json-with-identity-schema}
, {your-project-id}
, and $schema
with your actual values. This command will update the default_schema_id
in the project configuration. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#create-custom-schema).