Skip to content

Commit

Permalink
Merge pull request #36 from napptive/PG-134/Remove_app_command
Browse files Browse the repository at this point in the history
Add application commands
  • Loading branch information
Carmendelope authored Aug 11, 2021
2 parents 8279eb5 + e944027 commit 79d25e2
Show file tree
Hide file tree
Showing 24 changed files with 481 additions and 49 deletions.
82 changes: 82 additions & 0 deletions cmd/catalog-manager/commands/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright 2021 Napptive
*
* Licensed 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
*
* https://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 commands

import (
"github.com/napptive/catalog-manager/internal/app/cli"
"github.com/spf13/cobra"
)

var adminCmdLongHelp = `admin commands`
var adminCmdShortHelp = `admin commands`

var adminCmd = &cobra.Command{
Use: "admin",
Long: adminCmdLongHelp,
Short: adminCmdShortHelp,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

var listCmdLongHelp = `list the catalog applications`
var listCmdShortHelp = `list the catalog applications`
var listCmd = &cobra.Command{
Use: "list [namespace]",
Long: listCmdLongHelp,
Short: listCmdShortHelp,
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
namespace := ""
if len(args) > 0 {
namespace = args[0]
}
op, err := cli.NewApplicationCli(cfg.AdminGRPCPort)
if err != nil {
return err
}
return op.List(namespace)
},
}

var deleteAppCmdLongHelp = `Delete applications from catalog.
You can delete a namespace indicating the name as arg or delete only one application by passing the name as namespace/applicationName`
var deleteAppCmdShortHelp = `Delete catalog application`

var deleteAppCmd = &cobra.Command{
Use: "delete <applicationName>",
Long: deleteAppCmdLongHelp,
Short: deleteAppCmdShortHelp,
Aliases: []string{"rm", "remove"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
op, err := cli.NewApplicationCli(cfg.AdminGRPCPort)
if err != nil {
return err
}
return op.Delete(args[0])
},
}

func init() {
rootCmd.AddCommand(adminCmd)

adminCmd.AddCommand(deleteAppCmd)
adminCmd.AddCommand(listCmd)

adminCmd.PersistentFlags().IntVar(&cfg.CatalogManager.AdminGRPCPort, "adminGRPCPort", 7062, "gRPC Port to connect the Catalog-manager admin API")
}
2 changes: 1 addition & 1 deletion cmd/catalog-manager/commands/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var loginUserCmd = &cobra.Command{

func init() {

userCmd.PersistentFlags().StringVarP(&connString, "connectionString", "c", "host=postgres user=postgres password=postgres port=5432","connection string to connect postgres database")
userCmd.PersistentFlags().StringVarP(&connString, "connectionString", "c", "host=postgres user=postgres password=postgres port=5432", "connection string to connect postgres database")

rootCmd.AddCommand(userCmd)
userCmd.AddCommand(createUserCmd)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/napptive/analytics v1.0.1
github.com/napptive/grpc-catalog-common-go v0.2.0
github.com/napptive/grpc-catalog-go v0.16.0
github.com/napptive/grpc-catalog-go v0.17.0
github.com/napptive/mockup-generator v1.0.1-0.20210630142914-ae5eac8f9f80
github.com/napptive/nerrors v1.0.0
github.com/napptive/njwt v1.0.1-0.20210709073938-ea6f9de62a71
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ github.com/napptive/grpc-authx-go v0.9.0 h1:hU4LP5l+bbj7eGQu/uElDZZUUG44q9jTxiZ4
github.com/napptive/grpc-authx-go v0.9.0/go.mod h1:BHBradRIe4tKea4ze9ju9F1NWTX+c9NGBEh3xV9bpK0=
github.com/napptive/grpc-catalog-common-go v0.2.0 h1:BdIYVOEmzIZbzbVvjKnd9/8T8nQfNqZeEzzVCXjWhvI=
github.com/napptive/grpc-catalog-common-go v0.2.0/go.mod h1:bnedZvTuFnhO2q1u66EUseAy3k0/N5MW/BFvb8S23bA=
github.com/napptive/grpc-catalog-go v0.16.0 h1:u832OVhpHojFqkM5nK0kK4QY4bMa0U3MPpzlUJj9Eyw=
github.com/napptive/grpc-catalog-go v0.16.0/go.mod h1:BoOdDpZZlW8rWNlSe2/mjG9JC6+UCLlZkUNovwMFxY0=
github.com/napptive/grpc-catalog-go v0.17.0 h1:y2m+9LER//Hu2RvXlKuyA7E23AxCWR1KxI7cmbeuk34=
github.com/napptive/grpc-catalog-go v0.17.0/go.mod h1:BoOdDpZZlW8rWNlSe2/mjG9JC6+UCLlZkUNovwMFxY0=
github.com/napptive/grpc-common-go v0.2.0/go.mod h1:Q896cZY+yIkted9zYw3jtguVDdfL1bqTHjjiirBTjnw=
github.com/napptive/grpc-common-go v0.4.0 h1:uN6sxr8tidDEPwhVLA6UwnGpNHwuI+QTwWdfTseoiQc=
github.com/napptive/grpc-common-go v0.4.0/go.mod h1:Q896cZY+yIkted9zYw3jtguVDdfL1bqTHjjiirBTjnw=
Expand Down
2 changes: 1 addition & 1 deletion internal/app/catalog-manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (s *Service) LaunchHTTPService() {
var grpcOptions []grpc.DialOption
if s.cfg.TLSConfig.LaunchSecureService {
tlsConfig := &tls.Config{
// Since the proxy may not be seeing the same DNS address, skip the verification for now.
// Since the proxy may not be seeing the same DNS address, skip the verification for now.
InsecureSkipVerify: true,
}
tlsCredentials := credentials.NewTLS(tlsConfig)
Expand Down
83 changes: 83 additions & 0 deletions internal/app/cli/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2021 Napptive
*
* Licensed 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
*
* https://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 cli

import (
"fmt"
grpc_catalog_go "github.com/napptive/grpc-catalog-go"
"github.com/napptive/nerrors/pkg/nerrors"
"github.com/rs/zerolog/log"
"golang.org/x/net/context"
"google.golang.org/grpc"
"strings"
"time"
)

type ApplicationCli struct {
// adminClient to connect to Admin interface
adminClient grpc_catalog_go.NamespaceAdministrationClient
}

func NewApplicationCli(adminPort int) (*ApplicationCli, error) {
dir := fmt.Sprintf(":%d", adminPort)
log.Info().Str("dir", dir).Msg("admin direction")
conn, err := grpc.Dial(dir, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := grpc_catalog_go.NewNamespaceAdministrationClient(conn)
return &ApplicationCli{
adminClient: client,
}, nil
}

// Delete removes applications from the catalog
// if app is an applicationName removes it and
// if app is a namespace removes all the namespace applications
func (ac *ApplicationCli) Delete(app string) error {
log.Debug().Str("appName", app).Msg("Delete")

if app == "" {
return nerrors.NewFailedPreconditionError("applicationName or namespace mut be filled")
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

// check if app is an applicationName or is a namespace
// applicationName -> namespace/appName
// namespace -> namespace
if strings.Contains(app, "/") {
response, err := ac.adminClient.DeleteApplication(ctx, &grpc_catalog_go.RemoveApplicationRequest{ApplicationId: app})
PrintResultOrError(response, err)
} else {
// is a namespace
response, err := ac.adminClient.Delete(ctx, &grpc_catalog_go.DeleteNamespaceRequest{Namespace: app})
PrintResultOrError(response, err)
}

return nil
}

func (ac *ApplicationCli) List(namespace string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

response, err := ac.adminClient.List(ctx, &grpc_catalog_go.ListApplicationsRequest{Namespace: namespace})
PrintResultOrError(response, err)

return nil
}
43 changes: 43 additions & 0 deletions internal/app/cli/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2021 Napptive
*
* Licensed 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
*
* https://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 cli

import (
"fmt"
"github.com/napptive/catalog-manager/internal/pkg/printer"

"github.com/napptive/nerrors/pkg/nerrors"
"github.com/rs/zerolog"
)

// PrintResultOrError prints the result using a given printer or the error.
func PrintResultOrError(result interface{}, err error) {
if err != nil {
if zerolog.GlobalLevel() == zerolog.DebugLevel {
fmt.Println(nerrors.FromError(err).StackTraceToString())
} else {
fmt.Println(err.Error())
}
} else {
if pErr := printer.NewTablePrinter().Print(result); pErr != nil {
if zerolog.GlobalLevel() == zerolog.DebugLevel {
fmt.Println(nerrors.FromError(pErr).StackTraceToString())
} else {
fmt.Println(pErr.Error())
}
}
}
}
6 changes: 3 additions & 3 deletions internal/pkg/config/bq_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type BQConfig struct {
Config bqconfig.BigQueryConfig
}

func (bq *BQConfig) IsValid () error {
func (bq *BQConfig) IsValid() error {
if bq.Enabled {
return bq.Config.IsValid()
}
return nil
}

func (bq *BQConfig) Print () {
func (bq *BQConfig) Print() {
if bq.Enabled {
bq.Config.Print()
}
}
}
4 changes: 2 additions & 2 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type Config struct {
// Version of the application.
Version string
// Commit related to this built.
Commit string
Commit string
// Debug flag.
Debug bool
Debug bool
}

// IsValid checks if the configuration options are valid.
Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/config/jwtconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type JWTConfig struct {
nwjtConfig.JWTConfig
}


// IsValid checks if the configuration options are valid.
func (c *JWTConfig) IsValid() error {
if c.AuthEnabled {
Expand All @@ -32,4 +31,4 @@ func (c *JWTConfig) Print() error {
}

return nil
}
}
2 changes: 1 addition & 1 deletion internal/pkg/config/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ package config
type UserSQL struct {
// ConnStr with the connection string to connect to database
ConnStr string
}
}
12 changes: 6 additions & 6 deletions internal/pkg/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ func (a *AppSummary) ToApplicationSummary() *grpc_catalog_go.ApplicationSummary

// map[string][]ApplicationLogo TO map[string]* Logo []*ApplicationLogo

logoSummary := make (map[string]*grpc_catalog_go.ApplicationLogoList)
logoSummary := make(map[string]*grpc_catalog_go.ApplicationLogoList)
for key, value := range a.MetadataLogo {
logoList := make ([]*grpc_catalog_go.ApplicationLogo, 0)
logoList := make([]*grpc_catalog_go.ApplicationLogo, 0)
for _, logo := range value {
logoList = append(logoList, logo.ToGRPC())
}
logoSummary[key] = &grpc_catalog_go.ApplicationLogoList{Logo: logoList}
}

return &grpc_catalog_go.ApplicationSummary{
Namespace: a.Namespace,
ApplicationName: a.ApplicationName,
TagMetadataName: a.TagMetadataName,
Namespace: a.Namespace,
ApplicationName: a.ApplicationName,
TagMetadataName: a.TagMetadataName,
SummaryApplicationLogo: logoSummary,
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ type ExtendedAppSummary struct {
// -- ApplicationMetadata

// ApplicationInfo with the metadata of application, this will be the application info showed
type ApplicationInfo struct {
type ApplicationInfo struct {
// CatalogID with an internal identifier
CatalogID string
// Namespace where the application is located.
Expand Down
23 changes: 23 additions & 0 deletions internal/pkg/printer/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2021 Napptive
*
* Licensed 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
*
* https://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 printer

// ResultPrinter defines the operations that a printer must define. Multiple printer are
// offered depending on the desired output format.
type ResultPrinter interface {
// Print the result.
Print(result interface{}) error
}
Loading

0 comments on commit 79d25e2

Please sign in to comment.