edge-connect-client/cmd/instance.go
Daniel Sy c539eb2210
feat(cli): Implement Edge Connect CLI tool
Creates a new command-line interface for managing Edge Connect applications and instances with the following features:

- Configuration management via YAML files and environment variables
- Application lifecycle commands (create, show, list, delete)
- Instance management with cloudlet support
- Improved error handling and authentication flow
- Comprehensive documentation with usage examples

The CLI provides a user-friendly interface for managing Edge Connect resources while following best practices for command-line tool development using Cobra and Viper.
2025-09-18 13:51:09 +02:00

159 lines
4.5 KiB
Go

package cmd
import (
"context"
"fmt"
"os"
"edp.buildth.ing/DevFW-CICD/edge-connect-client/client"
"github.com/spf13/cobra"
)
var (
cloudletName string
cloudletOrg string
instanceName string
flavorName string
)
var appInstanceCmd = &cobra.Command{
Use: "instance",
Short: "Manage Edge Connect application instances",
Long: `Create, show, list, and delete Edge Connect application instances.`,
}
var createInstanceCmd = &cobra.Command{
Use: "create",
Short: "Create a new Edge Connect application instance",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
input := client.NewAppInstanceInput{
Region: region,
AppInst: client.AppInstance{
Key: client.AppInstanceKey{
Organization: organization,
Name: instanceName,
CloudletKey: client.CloudletKey{
Organization: cloudletOrg,
Name: cloudletName,
},
},
AppKey: client.AppKey{
Organization: organization,
Name: appName,
Version: appVersion,
},
Flavor: client.Flavor{
Name: flavorName,
},
},
}
err := c.CreateAppInstance(context.Background(), input)
if err != nil {
fmt.Printf("Error creating app instance: %v\n", err)
os.Exit(1)
}
fmt.Println("Application instance created successfully")
},
}
var showInstanceCmd = &cobra.Command{
Use: "show",
Short: "Show details of an Edge Connect application instance",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
instanceKey := client.AppInstanceKey{
Organization: organization,
Name: instanceName,
CloudletKey: client.CloudletKey{
Organization: cloudletOrg,
Name: cloudletName,
},
}
instance, err := c.ShowAppInstance(context.Background(), instanceKey, region)
if err != nil {
fmt.Printf("Error showing app instance: %v\n", err)
os.Exit(1)
}
fmt.Printf("Application instance details:\n%+v\n", instance)
},
}
var listInstancesCmd = &cobra.Command{
Use: "list",
Short: "List Edge Connect application instances",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
instanceKey := client.AppInstanceKey{
Organization: organization,
Name: instanceName,
CloudletKey: client.CloudletKey{
Organization: cloudletOrg,
Name: cloudletName,
},
}
instances, err := c.ShowAppInstances(context.Background(), instanceKey, region)
if err != nil {
fmt.Printf("Error listing app instances: %v\n", err)
os.Exit(1)
}
fmt.Println("Application instances:")
for _, instance := range instances {
fmt.Printf("%+v\n", instance)
}
},
}
var deleteInstanceCmd = &cobra.Command{
Use: "delete",
Short: "Delete an Edge Connect application instance",
Run: func(cmd *cobra.Command, args []string) {
c := newClient()
instanceKey := client.AppInstanceKey{
Organization: organization,
Name: instanceName,
CloudletKey: client.CloudletKey{
Organization: cloudletOrg,
Name: cloudletName,
},
}
err := c.DeleteAppInstance(context.Background(), instanceKey, region)
if err != nil {
fmt.Printf("Error deleting app instance: %v\n", err)
os.Exit(1)
}
fmt.Println("Application instance deleted successfully")
},
}
func init() {
rootCmd.AddCommand(appInstanceCmd)
appInstanceCmd.AddCommand(createInstanceCmd, showInstanceCmd, listInstancesCmd, deleteInstanceCmd)
// Add flags to all instance commands
instanceCmds := []*cobra.Command{createInstanceCmd, showInstanceCmd, listInstancesCmd, deleteInstanceCmd}
for _, cmd := range instanceCmds {
cmd.Flags().StringVarP(&organization, "org", "o", "", "organization name (required)")
cmd.Flags().StringVarP(&instanceName, "name", "n", "", "instance name (required)")
cmd.Flags().StringVarP(&cloudletName, "cloudlet", "c", "", "cloudlet name (required)")
cmd.Flags().StringVarP(&cloudletOrg, "cloudlet-org", "", "", "cloudlet organization (required)")
cmd.Flags().StringVarP(&region, "region", "r", "", "region (required)")
cmd.MarkFlagRequired("org")
cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("cloudlet")
cmd.MarkFlagRequired("cloudlet-org")
cmd.MarkFlagRequired("region")
}
// Add additional flags for create command
createInstanceCmd.Flags().StringVarP(&appName, "app", "a", "", "application name (required)")
createInstanceCmd.Flags().StringVarP(&appVersion, "version", "v", "", "application version")
createInstanceCmd.Flags().StringVarP(&flavorName, "flavor", "f", "", "flavor name (required)")
createInstanceCmd.MarkFlagRequired("app")
createInstanceCmd.MarkFlagRequired("flavor")
}