github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/ilm-rule-import.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 "os" 23 24 "github.com/minio/cli" 25 json "github.com/minio/colorjson" 26 "github.com/minio/mc/pkg/probe" 27 "github.com/minio/minio-go/v7/pkg/lifecycle" 28 "github.com/minio/pkg/v2/console" 29 ) 30 31 var ilmImportCmd = cli.Command{ 32 Name: "import", 33 Usage: "import lifecycle configuration in JSON format", 34 Action: mainILMImport, 35 OnUsageError: onUsageError, 36 Before: setGlobalsFromContext, 37 Flags: globalFlags, 38 CustomHelpTemplate: `NAME: 39 {{.HelpName}} - {{.Usage}} 40 41 USAGE: 42 {{.HelpName}} TARGET 43 44 DESCRIPTION: 45 Import entire lifecycle configuration from STDIN, input file is expected to be in JSON format. 46 47 EXAMPLES: 48 1. Set lifecycle configuration for the mybucket on alias 'myminio' to the rules imported from lifecycle.json 49 {{.Prompt}} {{.HelpName}} myminio/mybucket < lifecycle.json 50 51 2. Set lifecycle configuration for the mybucket on alias 'myminio'. User is expected to enter the JSON contents on STDIN 52 {{.Prompt}} {{.HelpName}} myminio/mybucket 53 `, 54 } 55 56 type ilmImportMessage struct { 57 Status string `json:"status"` 58 Target string `json:"target"` 59 } 60 61 func (i ilmImportMessage) String() string { 62 return console.Colorize(ilmThemeResultSuccess, "Lifecycle configuration imported successfully to `"+i.Target+"`.") 63 } 64 65 func (i ilmImportMessage) JSON() string { 66 msgBytes, e := json.MarshalIndent(i, "", " ") 67 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 68 return string(msgBytes) 69 } 70 71 // readILMConfig read from stdin, returns XML. 72 func readILMConfig() (*lifecycle.Configuration, *probe.Error) { 73 // User is expected to enter the lifecycleConfiguration instance contents in JSON format 74 cfg := lifecycle.NewConfiguration() 75 76 // Consume json from STDIN 77 dec := json.NewDecoder(os.Stdin) 78 if e := dec.Decode(cfg); e != nil { 79 return cfg, probe.NewError(e) 80 } 81 82 return cfg, nil 83 } 84 85 // checkILMImportSyntax - validate arguments passed by user 86 func checkILMImportSyntax(ctx *cli.Context) { 87 if len(ctx.Args()) != 1 { 88 showCommandHelpAndExit(ctx, globalErrorExitStatus) 89 } 90 } 91 92 func mainILMImport(cliCtx *cli.Context) error { 93 ctx, cancelILMImport := context.WithCancel(globalContext) 94 defer cancelILMImport() 95 96 checkILMImportSyntax(cliCtx) 97 setILMDisplayColorScheme() 98 99 args := cliCtx.Args() 100 urlStr := args.Get(0) 101 102 client, err := newClient(urlStr) 103 fatalIf(err.Trace(urlStr), "Unable to initialize client for "+urlStr) 104 105 ilmCfg, err := readILMConfig() 106 fatalIf(err.Trace(args...), "Unable to read ILM configuration") 107 108 if len(ilmCfg.Rules) == 0 { 109 // Abort here, otherwise client.SetLifecycle will remove the lifecycle configuration 110 // since no rules are provided and we will show a success message. 111 fatalIf(errDummy(), "The provided ILM configuration does not contain any rule, aborting.") 112 } 113 114 fatalIf(client.SetLifecycle(ctx, ilmCfg).Trace(urlStr), "Unable to set new lifecycle rules") 115 116 printMsg(ilmImportMessage{ 117 Status: "success", 118 Target: urlStr, 119 }) 120 return nil 121 }