|
| 1 | +package subcmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "path/filepath" |
| 9 | + "syscall" |
| 10 | + |
| 11 | + "github.com/segmentio/topicctl/pkg/admin" |
| 12 | + "github.com/segmentio/topicctl/pkg/cli" |
| 13 | + "github.com/segmentio/topicctl/pkg/config" |
| 14 | + "github.com/segmentio/topicctl/pkg/create" |
| 15 | + log "github.com/sirupsen/logrus" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +var createCmd = &cobra.Command{ |
| 20 | + Use: "create [resource type]", |
| 21 | + Short: "creates one or more resources", |
| 22 | + PersistentPreRunE: createPreRun, |
| 23 | +} |
| 24 | + |
| 25 | +type createCmdConfig struct { |
| 26 | + dryRun bool |
| 27 | + pathPrefix string |
| 28 | + skipConfirm bool |
| 29 | + |
| 30 | + shared sharedOptions |
| 31 | +} |
| 32 | + |
| 33 | +var createConfig createCmdConfig |
| 34 | + |
| 35 | +func init() { |
| 36 | + createCmd.PersistentFlags().BoolVar( |
| 37 | + &createConfig.dryRun, |
| 38 | + "dry-run", |
| 39 | + false, |
| 40 | + "Do a dry-run", |
| 41 | + ) |
| 42 | + createCmd.PersistentFlags().StringVar( |
| 43 | + &createConfig.pathPrefix, |
| 44 | + "path-prefix", |
| 45 | + os.Getenv("TOPICCTL_ACL_PATH_PREFIX"), |
| 46 | + "Prefix for ACL config paths", |
| 47 | + ) |
| 48 | + createCmd.PersistentFlags().BoolVar( |
| 49 | + &createConfig.skipConfirm, |
| 50 | + "skip-confirm", |
| 51 | + false, |
| 52 | + "Skip confirmation prompts during creation process", |
| 53 | + ) |
| 54 | + |
| 55 | + addSharedFlags(createCmd, &createConfig.shared) |
| 56 | + createCmd.AddCommand( |
| 57 | + createACLsCmd(), |
| 58 | + ) |
| 59 | + RootCmd.AddCommand(createCmd) |
| 60 | +} |
| 61 | + |
| 62 | +func createPreRun(cmd *cobra.Command, args []string) error { |
| 63 | + if err := RootCmd.PersistentPreRunE(cmd, args); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + return createConfig.shared.validate() |
| 67 | +} |
| 68 | + |
| 69 | +func createACLsCmd() *cobra.Command { |
| 70 | + cmd := &cobra.Command{ |
| 71 | + Use: "acls [acl configs]", |
| 72 | + Short: "creates ACLs from configuration files", |
| 73 | + Args: cobra.MinimumNArgs(1), |
| 74 | + RunE: createACLRun, |
| 75 | + PreRunE: createPreRun, |
| 76 | + } |
| 77 | + |
| 78 | + return cmd |
| 79 | +} |
| 80 | + |
| 81 | +func createACLRun(cmd *cobra.Command, args []string) error { |
| 82 | + ctx, cancel := context.WithCancel(context.Background()) |
| 83 | + defer cancel() |
| 84 | + |
| 85 | + sigChan := make(chan os.Signal, 1) |
| 86 | + signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) |
| 87 | + go func() { |
| 88 | + <-sigChan |
| 89 | + cancel() |
| 90 | + }() |
| 91 | + |
| 92 | + // Keep a cache of the admin clients with the cluster config path as the key |
| 93 | + adminClients := map[string]admin.Client{} |
| 94 | + |
| 95 | + defer func() { |
| 96 | + for _, adminClient := range adminClients { |
| 97 | + adminClient.Close() |
| 98 | + } |
| 99 | + }() |
| 100 | + |
| 101 | + matchCount := 0 |
| 102 | + |
| 103 | + for _, arg := range args { |
| 104 | + if createConfig.pathPrefix != "" && !filepath.IsAbs(arg) { |
| 105 | + arg = filepath.Join(createConfig.pathPrefix, arg) |
| 106 | + } |
| 107 | + |
| 108 | + matches, err := filepath.Glob(arg) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + for _, match := range matches { |
| 114 | + matchCount++ |
| 115 | + if err := createACL(ctx, match, adminClients); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if matchCount == 0 { |
| 122 | + return fmt.Errorf("No ACL configs match the provided args (%+v)", args) |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func createACL( |
| 129 | + ctx context.Context, |
| 130 | + aclConfigPath string, |
| 131 | + adminClients map[string]admin.Client, |
| 132 | +) error { |
| 133 | + clusterConfigPath, err := clusterConfigForACLCreate(aclConfigPath) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + aclConfigs, err := config.LoadACLsFile(aclConfigPath) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + clusterConfig, err := config.LoadClusterFile(clusterConfigPath, createConfig.shared.expandEnv) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + adminClient, ok := adminClients[clusterConfigPath] |
| 149 | + if !ok { |
| 150 | + adminClient, err = clusterConfig.NewAdminClient( |
| 151 | + ctx, |
| 152 | + nil, |
| 153 | + createConfig.dryRun, |
| 154 | + createConfig.shared.saslUsername, |
| 155 | + createConfig.shared.saslPassword, |
| 156 | + ) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + adminClients[clusterConfigPath] = adminClient |
| 161 | + } |
| 162 | + |
| 163 | + cliRunner := cli.NewCLIRunner(adminClient, log.Infof, false) |
| 164 | + |
| 165 | + for _, aclConfig := range aclConfigs { |
| 166 | + aclConfig.SetDefaults() |
| 167 | + log.Infof( |
| 168 | + "Processing ACL %s in config %s with cluster config %s", |
| 169 | + aclConfig.Meta.Name, |
| 170 | + aclConfigPath, |
| 171 | + clusterConfigPath, |
| 172 | + ) |
| 173 | + |
| 174 | + creatorConfig := create.ACLCreatorConfig{ |
| 175 | + DryRun: createConfig.dryRun, |
| 176 | + SkipConfirm: createConfig.skipConfirm, |
| 177 | + ACLConfig: aclConfig, |
| 178 | + ClusterConfig: clusterConfig, |
| 179 | + } |
| 180 | + |
| 181 | + if err := cliRunner.CreateACL(ctx, creatorConfig); err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func clusterConfigForACLCreate(aclConfigPath string) (string, error) { |
| 190 | + if createConfig.shared.clusterConfig != "" { |
| 191 | + return createConfig.shared.clusterConfig, nil |
| 192 | + } |
| 193 | + |
| 194 | + return filepath.Abs( |
| 195 | + filepath.Join( |
| 196 | + filepath.Dir(aclConfigPath), |
| 197 | + "..", |
| 198 | + "cluster.yaml", |
| 199 | + ), |
| 200 | + ) |
| 201 | +} |
0 commit comments