Generates & import data into Keycloak for performance tests
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

/*
* This file is part of the keycloak-import-realm distribution
* (https://github.com/nmasse-itix/keycloak-import-realm).
* Copyright (c) 2021 Nicolas Massé <nicolas.masse@itix.fr>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path"
"text/template"
kcimport "github.com/nmasse-itix/keycloak-realm-import"
"github.com/spf13/cobra"
)
var realmCount, clientCount, userCount int
var targetDir, customTemplateFile string
// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate Keycloak realms",
Long: `TODO`,
Run: func(cmd *cobra.Command, args []string) {
err := os.MkdirAll(targetDir, 0777)
if err != nil {
logger.Fatal(err)
}
var customTemplate *template.Template
if customTemplateFile != "" {
b, err := ioutil.ReadFile(customTemplateFile)
if err != nil {
logger.Fatal(err)
}
content := string(b)
customTemplate, err = kcimport.GetRealmTemplate(content)
}
realms := kcimport.GenerateRealms(realmCount, clientCount, userCount)
for _, realm := range realms {
f, err := os.OpenFile(path.Join(targetDir, fmt.Sprintf("realm-%s.json", realm.ID)), os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
logger.Fatal(err)
}
defer f.Close()
if customTemplate != nil {
err = kcimport.WriteRealmFileWithTemplate(realm, f, customTemplate)
} else {
err = kcimport.WriteRealmFile(realm, f)
}
if err != nil {
logger.Fatal(err)
}
}
},
}
func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().IntVar(&realmCount, "realms", 1, "number of realms to generate")
generateCmd.Flags().IntVar(&clientCount, "clients", 10, "number of clients to generate per realm")
generateCmd.Flags().IntVar(&userCount, "users", 10, "number of users to generate per realm")
generateCmd.Flags().StringVar(&targetDir, "target", ".", "target directory")
generateCmd.Flags().StringVar(&customTemplateFile, "template", "", "go template used to generate the realm")
}