github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/license-unregister.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 "fmt" 22 23 "github.com/fatih/color" 24 "github.com/minio/cli" 25 json "github.com/minio/colorjson" 26 "github.com/minio/mc/pkg/probe" 27 "github.com/minio/pkg/v2/console" 28 ) 29 30 const licUnregisterMsgTag = "licenseUnregisterMessage" 31 32 var licenseUnregisterCmd = cli.Command{ 33 Name: "unregister", 34 Usage: "unregister from MinIO Subscription Network", 35 OnUsageError: onUsageError, 36 Action: mainLicenseUnregister, 37 Before: setGlobalsFromContext, 38 Hidden: true, 39 Flags: subnetCommonFlags, 40 CustomHelpTemplate: `NAME: 41 {{.HelpName}} - {{.Usage}} 42 43 USAGE: 44 {{.HelpName}} TARGET 45 46 FLAGS: 47 {{range .VisibleFlags}}{{.}} 48 {{end}} 49 EXAMPLES: 50 1. Unregister MinIO cluster at alias 'myminio' from SUBNET 51 {{.Prompt}} {{.HelpName}} myminio 52 `, 53 } 54 55 type licUnregisterMessage struct { 56 Status string `json:"status"` 57 Alias string `json:"-"` 58 } 59 60 // String colorized license unregister message 61 func (li licUnregisterMessage) String() string { 62 msg := fmt.Sprintf("%s unregistered successfully.", li.Alias) 63 return console.Colorize(licUnregisterMsgTag, msg) 64 } 65 66 // JSON jsonified license unregister message 67 func (li licUnregisterMessage) JSON() string { 68 jsonBytes, e := json.MarshalIndent(li, "", " ") 69 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 70 71 return string(jsonBytes) 72 } 73 74 // checkLicenseUnregisterSyntax - validate arguments passed by a user 75 func checkLicenseUnregisterSyntax(ctx *cli.Context) { 76 if len(ctx.Args()) != 1 { 77 showCommandHelpAndExit(ctx, 1) // last argument is exit code 78 } 79 } 80 81 func mainLicenseUnregister(ctx *cli.Context) error { 82 console.SetColor(licUnregisterMsgTag, color.New(color.FgGreen, color.Bold)) 83 checkLicenseUnregisterSyntax(ctx) 84 85 aliasedURL := ctx.Args().Get(0) 86 alias, apiKey := initSubnetConnectivity(ctx, aliasedURL, true) 87 if len(apiKey) == 0 { 88 // api key not passed as flag. Check that the cluster is registered. 89 apiKey = validateClusterRegistered(alias, true) 90 } 91 92 if !globalAirgapped { 93 info := getAdminInfo(aliasedURL) 94 e := unregisterClusterFromSubnet(info.DeploymentID, apiKey) 95 fatalIf(probe.NewError(e), "Could not unregister cluster from SUBNET:") 96 } 97 98 removeSubnetAuthConfig(alias) 99 100 printMsg(licUnregisterMessage{Status: "success", Alias: alias}) 101 return nil 102 }