|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/MakeNowJust/heredoc" |
| 8 | + "github.com/raystack/frontier/internal/reconcile" |
| 9 | + cli "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +func ReconcileCommand(cliConfig *Config) *cli.Command { |
| 13 | + var ( |
| 14 | + filePath string |
| 15 | + dryRun bool |
| 16 | + header string |
| 17 | + ) |
| 18 | + cmd := &cli.Command{ |
| 19 | + Use: "reconcile", |
| 20 | + Short: "Reconcile platform configuration to a desired-state file", |
| 21 | + Long: heredoc.Doc(` |
| 22 | + Make platform resources match a desired-state YAML file, through the admin API. |
| 23 | +
|
| 24 | + Supports the PlatformUser kind (platform admins and members) for now. The file |
| 25 | + decides who has access: anyone listed is added, anyone not listed is removed. |
| 26 | + Log in as a superuser (for example the bootstrap service account) with --header. |
| 27 | + `), |
| 28 | + Example: heredoc.Doc(` |
| 29 | + $ frontier reconcile -f platform-users.yaml --dry-run -H "Authorization:Basic <base64>" |
| 30 | + $ frontier reconcile -f platform-users.yaml -H "Authorization:Basic <base64>" |
| 31 | + `), |
| 32 | + Annotations: map[string]string{ |
| 33 | + "group": "core", |
| 34 | + "client": "true", |
| 35 | + }, |
| 36 | + RunE: func(cmd *cli.Command, args []string) error { |
| 37 | + data, err := os.ReadFile(filePath) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("read desired-state file: %w", err) |
| 40 | + } |
| 41 | + adminClient, err := createAdminClient(cliConfig.Host) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + registry := map[string]reconcile.Reconciler{ |
| 46 | + reconcile.KindPlatformUser: reconcile.NewPlatformUserReconciler(adminClient, header), |
| 47 | + } |
| 48 | + reports, runErr := reconcile.Run(cmd.Context(), registry, data, dryRun) |
| 49 | + for _, rep := range reports { |
| 50 | + printReconcileReport(cmd, rep) |
| 51 | + } |
| 52 | + return runErr |
| 53 | + }, |
| 54 | + } |
| 55 | + cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the desired-state YAML file") |
| 56 | + cmd.MarkFlagRequired("file") |
| 57 | + cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the plan without applying changes") |
| 58 | + cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value> for auth, e.g. 'Authorization:Basic <base64>'") |
| 59 | + bindFlagsFromClientConfig(cmd) |
| 60 | + return cmd |
| 61 | +} |
| 62 | + |
| 63 | +func printReconcileReport(cmd *cli.Command, rep reconcile.Report) { |
| 64 | + if len(rep.Planned) == 0 { |
| 65 | + cmd.Printf("%s: no changes\n", rep.Kind) |
| 66 | + return |
| 67 | + } |
| 68 | + verb, count := "applied", rep.Applied |
| 69 | + if rep.DryRun { |
| 70 | + verb, count = "planned", len(rep.Planned) |
| 71 | + } |
| 72 | + cmd.Printf("%s (%s %d):\n", rep.Kind, verb, count) |
| 73 | + for _, p := range rep.Planned { |
| 74 | + cmd.Printf(" - %s\n", p) |
| 75 | + } |
| 76 | +} |
0 commit comments