|
| 1 | +/* |
| 2 | +* Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org) All Rights Reserved. |
| 3 | +* |
| 4 | +* WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | +* Version 2.0 (the "License"); you may not use this file except |
| 6 | +* in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, |
| 12 | +* software distributed under the License is distributed on an |
| 13 | +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +* KIND, either express or implied. See the License for the |
| 15 | +* specific language governing permissions and limitations |
| 16 | +* under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package cmd |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "net/http" |
| 25 | + |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "github.com/wso2/product-apim-tooling/import-export-cli/credentials" |
| 28 | + "github.com/wso2/product-apim-tooling/import-export-cli/impl" |
| 29 | + "github.com/wso2/product-apim-tooling/import-export-cli/utils" |
| 30 | +) |
| 31 | + |
| 32 | +var mcpServerStateChangeEnvironment string |
| 33 | +var mcpServerNameForStateChange string |
| 34 | +var mcpServerVersionForStateChange string |
| 35 | +var mcpServerProviderForStateChange string |
| 36 | +var mcpServerStateChangeAction string |
| 37 | + |
| 38 | +// ChangeMCPServerStatus command related usage info |
| 39 | +const changeMCPServerStatusCmdLiteral = "mcp-server" |
| 40 | +const changeMCPServerStatusCmdShortDesc = "Change Status of an MCP Server" |
| 41 | +const changeMCPServerStatusCmdLongDesc = "Change the lifecycle status of an MCP Server in an environment" |
| 42 | + |
| 43 | +const changeMCPServerStatusCmdExamples = utils.ProjectName + ` ` + changeStatusCmdLiteral + ` ` + changeMCPServerStatusCmdLiteral + ` -a Publish -n MyMCPServer -v 1.0.0 -r admin -e dev |
| 44 | +` + utils.ProjectName + ` ` + changeStatusCmdLiteral + ` ` + changeMCPServerStatusCmdLiteral + ` -a Publish -n MyMCPServer -v 2.1.0 -e production |
| 45 | +NOTE: The 4 flags (--action (-a), --name (-n), --version (-v), and --environment (-e)) are mandatory.` |
| 46 | + |
| 47 | +// changeMCPServerStatusCmd represents change-status mcp-server command |
| 48 | +var ChangeMCPServerStatusCmd = &cobra.Command{ |
| 49 | + Use: changeMCPServerStatusCmdLiteral + " (--action <action-of-the-mcpserver-state-change> --name <name-of-the-mcpserver> --version <version-of-the-mcpserver> --provider " + |
| 50 | + "<provider-of-the-mcpserver> --environment <environment-from-which-the-mcpserver-state-should-be-changed>)", |
| 51 | + Short: changeMCPServerStatusCmdShortDesc, |
| 52 | + Long: changeMCPServerStatusCmdLongDesc, |
| 53 | + Example: changeMCPServerStatusCmdExamples, |
| 54 | + Run: func(cmd *cobra.Command, args []string) { |
| 55 | + utils.Logln(utils.LogPrefixInfo + changeMCPServerStatusCmdLiteral + " called") |
| 56 | + cred, err := GetCredentials(mcpServerStateChangeEnvironment) |
| 57 | + if err != nil { |
| 58 | + utils.HandleErrorAndExit("Error getting credentials ", err) |
| 59 | + } |
| 60 | + executeChangeMCPServerStatusCmd(cred) |
| 61 | + }, |
| 62 | +} |
| 63 | + |
| 64 | +// executeChangeMCPServerStatusCmd executes the change mcp server status command |
| 65 | +func executeChangeMCPServerStatusCmd(credential credentials.Credential) { |
| 66 | + accessToken, preCommandErr := credentials.GetOAuthAccessToken(credential, mcpServerStateChangeEnvironment) |
| 67 | + if preCommandErr == nil { |
| 68 | + resp, err := impl.ChangeMCPServerStatusInEnv(accessToken, mcpServerStateChangeEnvironment, mcpServerStateChangeAction, |
| 69 | + mcpServerNameForStateChange, mcpServerVersionForStateChange, mcpServerProviderForStateChange) |
| 70 | + if err != nil { |
| 71 | + utils.HandleErrorAndExit("Error while changing the MCP Server status", err) |
| 72 | + } |
| 73 | + // Print info on response |
| 74 | + utils.Logf(utils.LogPrefixInfo+"ResponseStatus: %v\n", resp.Status()) |
| 75 | + if resp.StatusCode() == http.StatusOK { |
| 76 | + // 200 OK |
| 77 | + fmt.Println(mcpServerNameForStateChange + " MCP Server state changed successfully!") |
| 78 | + } else if resp.StatusCode() == http.StatusInternalServerError { |
| 79 | + // 500 Internal Server Error |
| 80 | + fmt.Println(string(resp.Body())) |
| 81 | + } else { |
| 82 | + // Neither 200 nor 500 |
| 83 | + fmt.Println("Error while changing MCP Server Status: ", resp.Status(), "\n", string(resp.Body())) |
| 84 | + } |
| 85 | + } else { |
| 86 | + // Error changing the MCP Server status |
| 87 | + fmt.Println("Error getting OAuth tokens while changing status of the MCP Server:" + preCommandErr.Error()) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func init() { |
| 92 | + ChangeStatusCmd.AddCommand(ChangeMCPServerStatusCmd) |
| 93 | + ChangeMCPServerStatusCmd.Flags().StringVarP(&mcpServerStateChangeAction, "action", "a", "", |
| 94 | + "Action to be taken to change the status of the MCP Server") |
| 95 | + ChangeMCPServerStatusCmd.Flags().StringVarP(&mcpServerNameForStateChange, "name", "n", "", |
| 96 | + "Name of the MCP Server to be state changed") |
| 97 | + ChangeMCPServerStatusCmd.Flags().StringVarP(&mcpServerVersionForStateChange, "version", "v", "", |
| 98 | + "Version of the MCP Server to be state changed") |
| 99 | + ChangeMCPServerStatusCmd.Flags().StringVarP(&mcpServerProviderForStateChange, "provider", "r", "", |
| 100 | + "Provider of the MCP Server") |
| 101 | + ChangeMCPServerStatusCmd.Flags().StringVarP(&mcpServerStateChangeEnvironment, "environment", "e", |
| 102 | + "", "Environment of which the MCP Server state should be changed") |
| 103 | + // Mark required flags |
| 104 | + _ = ChangeMCPServerStatusCmd.MarkFlagRequired("action") |
| 105 | + _ = ChangeMCPServerStatusCmd.MarkFlagRequired("name") |
| 106 | + _ = ChangeMCPServerStatusCmd.MarkFlagRequired("version") |
| 107 | + _ = ChangeMCPServerStatusCmd.MarkFlagRequired("environment") |
| 108 | +} |
0 commit comments