Skip to content

Commit

Permalink
Merge pull request #573 from SanojPunchihewa/mi-activate-deactivate-cmd
Browse files Browse the repository at this point in the history
Add commands to activate and deactivate endpoints, message processors and proxy services in MI instances
  • Loading branch information
uvindra authored Jan 12, 2021
2 parents cdfe040 + a0db0eb commit b0475a4
Show file tree
Hide file tree
Showing 30 changed files with 1,429 additions and 41 deletions.
43 changes: 43 additions & 0 deletions import-export-cli/cmd/mi/activate/activate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package activate

import (
"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

const activateCmdLiteral = "activate"
const activateCmdShortDesc = "Activate artifacts deployed in a Micro Integrator instance"

const activateCmdLongDesc = "Activate artifacts deployed in a Micro Integrator instance in the environment specified by the flag (--environment, -e)"

const activateCmdExamples = utils.ProjectName + " " + utils.MiCmdLiteral + " " + activateCmdLiteral + " " + "endpoint" + " TestEP -e dev"

// ActivateCmd represents the activate command
var ActivateCmd = &cobra.Command{
Use: activateCmdLiteral,
Short: activateCmdShortDesc,
Long: activateCmdLongDesc,
Example: activateCmdExamples,
Run: func(cmd *cobra.Command, args []string) {
utils.Logln(utils.LogPrefixInfo + activateCmdLiteral + " called")
cmd.Help()
},
}
64 changes: 64 additions & 0 deletions import-export-cli/cmd/mi/activate/endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package activate

import (
"fmt"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
impl "github.com/wso2/product-apim-tooling/import-export-cli/mi/impl"
miUtils "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils"
)

var activateEndpointCmdEnvironment string

const artifactEndpoint = "endpoint"
const activateEndpointCmdLiteral = "endpoint [endpoint-name]"

var activateEndpointCmd = &cobra.Command{
Use: activateEndpointCmdLiteral,
Short: generateActivateCmdShortDescForArtifact(artifactEndpoint),
Long: generateActivateCmdLongDescForArtifact(artifactEndpoint, "endpoint-name"),
Example: generateActivateCmdExamplesForArtifact(artifactEndpoint, miUtils.GetTrimmedCmdLiteral(activateEndpointCmdLiteral), "TestEP"),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
handleActivateEndpointCmdArguments(args)
},
}

func init() {
ActivateCmd.AddCommand(activateEndpointCmd)
setEnvFlag(activateEndpointCmd, &activateEndpointCmdEnvironment, artifactEndpoint)
}

func handleActivateEndpointCmdArguments(args []string) {
printActivateCmdVerboseLog(miUtils.GetTrimmedCmdLiteral(activateEndpointCmdLiteral))
credentials.HandleMissingCredentials(activateEndpointCmdEnvironment)
executeActivateEndpoint(args[0])
}

func executeActivateEndpoint(endpointName string) {
resp, err := impl.ActivateEndpoint(activateEndpointCmdEnvironment, endpointName)
if err != nil {
printErrorForArtifact(artifactEndpoint, endpointName, err)
} else {
fmt.Println(resp)
}
}
64 changes: 64 additions & 0 deletions import-export-cli/cmd/mi/activate/messageprocessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package activate

import (
"fmt"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
impl "github.com/wso2/product-apim-tooling/import-export-cli/mi/impl"
miUtils "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils"
)

var activateMessageProcessorCmdEnvironment string

const artifactMessageProcessor = "message processor"
const activateMessageProcessorCmdLiteral = "message-processor [messageprocessor-name]"

var activateMessageProcessorCmd = &cobra.Command{
Use: activateMessageProcessorCmdLiteral,
Short: generateActivateCmdShortDescForArtifact(artifactMessageProcessor),
Long: generateActivateCmdLongDescForArtifact(artifactMessageProcessor, "messageprocessor-name"),
Example: generateActivateCmdExamplesForArtifact(artifactMessageProcessor, miUtils.GetTrimmedCmdLiteral(activateMessageProcessorCmdLiteral), "TestMessageProcessor"),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
handleActivateMessageProcessorCmdArguments(args)
},
}

func init() {
ActivateCmd.AddCommand(activateMessageProcessorCmd)
setEnvFlag(activateMessageProcessorCmd, &activateMessageProcessorCmdEnvironment, artifactMessageProcessor)
}

func handleActivateMessageProcessorCmdArguments(args []string) {
printActivateCmdVerboseLog(miUtils.GetTrimmedCmdLiteral(activateMessageProcessorCmdLiteral))
credentials.HandleMissingCredentials(activateMessageProcessorCmdEnvironment)
executeActivateMessageProcessor(args[0])
}

func executeActivateMessageProcessor(messageProcessorName string) {
resp, err := impl.ActivateMessageProcessor(activateMessageProcessorCmdEnvironment, messageProcessorName)
if err != nil {
printErrorForArtifact(artifactMessageProcessor, messageProcessorName, err)
} else {
fmt.Println(resp)
}
}
64 changes: 64 additions & 0 deletions import-export-cli/cmd/mi/activate/proxyservice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package activate

import (
"fmt"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
impl "github.com/wso2/product-apim-tooling/import-export-cli/mi/impl"
miUtils "github.com/wso2/product-apim-tooling/import-export-cli/mi/utils"
)

var activateProxyCmdEnvironment string

const artifactProxy = "proxy service"
const activateProxyCmdLiteral = "proxy-service [proxy-name]"

var activateProxyCmd = &cobra.Command{
Use: activateProxyCmdLiteral,
Short: generateActivateCmdShortDescForArtifact(artifactProxy),
Long: generateActivateCmdLongDescForArtifact(artifactProxy, "proxy-name"),
Example: generateActivateCmdExamplesForArtifact(artifactProxy, miUtils.GetTrimmedCmdLiteral(activateProxyCmdLiteral), "SampleProxy"),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
handleActivateProxyCmdArguments(args)
},
}

func init() {
ActivateCmd.AddCommand(activateProxyCmd)
setEnvFlag(activateProxyCmd, &activateProxyCmdEnvironment, artifactProxy)
}

func handleActivateProxyCmdArguments(args []string) {
printActivateCmdVerboseLog(miUtils.GetTrimmedCmdLiteral(activateProxyCmdLiteral))
credentials.HandleMissingCredentials(activateProxyCmdEnvironment)
executeActivateProxy(args[0])
}

func executeActivateProxy(proxyName string) {
resp, err := impl.ActivateProxy(activateProxyCmdEnvironment, proxyName)
if err != nil {
printErrorForArtifact(artifactProxy, proxyName, err)
} else {
fmt.Println(resp)
}
}
53 changes: 53 additions & 0 deletions import-export-cli/cmd/mi/activate/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package activate

import (
"fmt"

"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

func generateActivateCmdShortDescForArtifact(artifact string) string {
return "Activate a " + artifact + " deployed in a Micro Integrator"
}

func generateActivateCmdLongDescForArtifact(artifact, argument string) string {
return "Activate the " + artifact + " specified by the command line argument [" + argument + "] deployed in a Micro Integrator in the environment specified by the flag --environment, -e"
}

func generateActivateCmdExamplesForArtifact(artifact, cmdLiteral, sampleResourceName string) string {
return "To activate a " + artifact + "\n" +
" " + utils.ProjectName + " " + utils.MiCmdLiteral + " " + activateCmdLiteral + " " + cmdLiteral + " " + sampleResourceName + " -e dev\n" +
"NOTE: The flag (--environment (-e)) is mandatory"
}

func printErrorForArtifact(artifactType, artifactName string, err error) {
fmt.Println(utils.LogPrefixError+"Activating "+artifactType+" [ "+artifactName+" ]", err)
}

func printActivateCmdVerboseLog(cmd string) {
utils.Logln(utils.LogPrefixInfo + activateCmdLiteral + " " + cmd + " called")
}

func setEnvFlag(cmd *cobra.Command, param *string, artifactType string) {
cmd.Flags().StringVarP(param, "environment", "e", "", "Environment of the micro integrator in which the "+artifactType+" should be activated")
cmd.MarkFlagRequired("environment")
}
43 changes: 43 additions & 0 deletions import-export-cli/cmd/mi/deactivate/deactivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package deactivate

import (
"github.com/spf13/cobra"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

const deactivateCmdLiteral = "deactivate"
const deactivateCmdShortDesc = "Deactivate artifacts deployed in a Micro Integrator instance"

const deactivateCmdLongDesc = "Deactivate artifacts deployed in a Micro Integrator instance in the environment specified by the flag (--environment, -e)"

const deactivateCmdExamples = utils.ProjectName + " " + utils.MiCmdLiteral + " " + deactivateCmdLiteral + " " + "endpoint" + " TestEP -e dev"

// DeactivateCmd represents the deactivate command
var DeactivateCmd = &cobra.Command{
Use: deactivateCmdLiteral,
Short: deactivateCmdShortDesc,
Long: deactivateCmdLongDesc,
Example: deactivateCmdExamples,
Run: func(cmd *cobra.Command, args []string) {
utils.Logln(utils.LogPrefixInfo + deactivateCmdLiteral + " called")
cmd.Help()
},
}
Loading

0 comments on commit b0475a4

Please sign in to comment.