Skip to content

Commit

Permalink
a bunch of refactors on app list implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
drgarcia1986 committed Aug 7, 2017
1 parent b153e33 commit 9883c2f
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 225 deletions.
17 changes: 7 additions & 10 deletions cmd/client/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ var appListCmd = &cobra.Command{
}

func appList(cmd *cobra.Command, args []string) {

conn, err := connection.New(cfgFile)
if err != nil {
client.PrintErrorAndExit("Error connecting to server: %v", err)
Expand All @@ -171,12 +170,11 @@ func appList(cmd *cobra.Command, args []string) {
cli := appb.NewAppClient(conn)
resp, err := cli.List(context.Background(), &appb.Empty{})
if err != nil {
fmt.Fprintln(os.Stderr, client.GetErrorMsg(err))
return
client.PrintErrorAndExit("Error listing apps: %v", err)
}

if len(resp.Apps) == 0 {
fmt.Println("You do not have to any app")
fmt.Println("You don't have any app")
return
}
// rendering app list in a table view
Expand All @@ -186,11 +184,12 @@ func appList(cmd *cobra.Command, args []string) {
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetRowSeparator("-")
table.SetAutoWrapText(false)
for _, t := range resp.Apps {
if t.Urls == "" {
t.Urls = "n/a"
for _, a := range resp.Apps {
urls := strings.Join(a.Urls, ",")
if urls == "" {
urls = "n/a"
}
r := []string{t.Team, t.App, t.Urls}
r := []string{a.Team, a.Name, urls}
table.Append(r)
}
table.Render()
Expand Down Expand Up @@ -455,8 +454,6 @@ func init() {
// App logs
appLogsCmd.Flags().Int64("lines", 10, "number of lines")
appLogsCmd.Flags().Bool("follow", false, "follow logs")
// App list
appListCmd.Flags().Bool("show-apps", false, "Show team's app(s)")
}

func appLogs(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/cmd/teresaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func NewTeresa() TeresaClient {

tc := TeresaClient{teresa: apiclient.Default}
log.Debugf(`Setting new teresa client. server: %s, api suffix: %s`, cluster.Server, suffix)
ts, err := ParseServerURL(cluster.Server)

ts, err := ParseServerURL(cluster.Server)
if err != nil {
log.Fatal(err)
}
Expand Down
128 changes: 65 additions & 63 deletions pkg/protobuf/app/app.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/protobuf/app/app.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ message CreateRequest {
}

message ListResponse {

message App {
string team = 1;
string app = 2;
string urls = 3;
string name = 2;
repeated string urls = 3;
}
repeated App apps = 1;

Expand Down
25 changes: 12 additions & 13 deletions pkg/server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Operations interface {
HasPermission(user *storage.User, appName string) bool
SetEnv(user *storage.User, appName string, evs []*EnvVar) error
UnsetEnv(user *storage.User, appName string, evs []string) error
List(user *storage.User) ([]*List, error)
List(user *storage.User) ([]*AppListItem, error)
}

type K8sOperations interface {
Expand All @@ -48,7 +48,7 @@ type K8sOperations interface {
DeleteDeployEnvVars(namespace, name string, evNames []string) error
CreateOrUpdateDeployEnvVars(namespace, name string, evs []*EnvVar) error
DeleteNamespace(namespace string) error
ListNamespaceByLabel(label string) ([]string, error)
NamespaceListByLabel(label, value string) ([]string, error)
}

type AppOperations struct {
Expand Down Expand Up @@ -337,31 +337,30 @@ func checkForProtectedEnvVars(evsNames []string) error {
return nil
}

func (ops *AppOperations) List(user *storage.User) ([]*List, error) {
func (ops *AppOperations) List(user *storage.User) ([]*AppListItem, error) {
teams, err := ops.tops.ListByUser(user.Email)
if err != nil {
return nil, err
}
result := make([]*List, 0)
items := make([]*AppListItem, 0)
for _, team := range teams {
appNames, err := ops.kops.ListNamespaceByLabel(team.Name)
apps, err := ops.kops.NamespaceListByLabel(TeresaTeamLabel, team.Name)
if err != nil {
return nil, err
}
for _, app := range appNames {
appAdd, err := ops.kops.AddressList(app)
for _, a := range apps {
addrs, err := ops.kops.AddressList(a)
if err != nil {
return nil, err
}
list := &List{
items = append(items, &AppListItem{
Team: team.Name,
Addresses: appAdd,
Name: string(app),
}
result = append(result, list)
Name: a,
Addresses: addrs,
})
}
}
return result, nil
return items, nil
}

func NewOperations(tops team.Operations, kops K8sOperations, st st.Storage) Operations {
Expand Down
37 changes: 24 additions & 13 deletions pkg/server/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
)

type fakeK8sOperations struct {
Namespaces map[string]struct{}
ErrNotFound error
Namespaces map[string]struct{}
}

type errK8sOperations struct {
Expand Down Expand Up @@ -130,8 +129,12 @@ func (f *fakeK8sOperations) DeleteNamespace(namespace string) error {
return nil
}

func (*fakeK8sOperations) ListNamespaceByLabel(label string) ([]string, error) {
return nil, nil
func (f *fakeK8sOperations) NamespaceListByLabel(label, value string) ([]string, error) {
ns := make([]string, 0)
for s, _ := range f.Namespaces {
ns = append(ns, s)
}
return ns, nil
}

func (e *errK8sOperations) CreateNamespace(app *App, user string) error {
Expand Down Expand Up @@ -207,7 +210,7 @@ func (e *errK8sOperations) DeleteNamespace(namespace string) error {
return e.DeleteNamespaceErr
}

func (e *errK8sOperations) ListNamespaceByLabel(label string) ([]string, error) {
func (e *errK8sOperations) NamespaceListByLabel(label, value string) ([]string, error) {
return nil, e.Err
}

Expand Down Expand Up @@ -529,27 +532,35 @@ func TestAppOperationsInfoErrNotFound(t *testing.T) {

func TestAppOperationsList(t *testing.T) {
tops := team.NewFakeOperations()
ops := NewOperations(tops, &fakeK8sOperations{}, nil)
appName := "teresa"
teamName := "luizalabs"

user := &storage.User{Email: "teresa@luizalabs.com"}
app := &App{Name: "teresa", Team: teamName}
tops.(*team.FakeOperations).Storage[app.Name] = &storage.Team{
fk8s := &fakeK8sOperations{Namespaces: map[string]struct{}{appName: struct{}{}}}

ops := NewOperations(tops, fk8s, nil)
tops.(*team.FakeOperations).Storage[appName] = &storage.Team{
Name: teamName,
Users: []storage.User{*user},
}

list, err := ops.List(user)
apps, err := ops.List(user)
if err != nil {
t.Fatal("error getting app list: ", err)
t.Fatal("error getting app list:", err)
}

for _, a := range list {
if len(apps) == 0 {
t.Fatal("expected at least one app")
}
for _, a := range apps {
if a.Name != appName {
t.Errorf("expected %s, got %s", appName, a.Name)
}
if a.Team != teamName {
t.Errorf("expected %s, got %s", teamName, a.Team)
}

if len(a.Addresses) != 1 { // see fakeK8sOperations.AddressList
t.Errorf("expected 2, got %d", len(a.Addresses))
t.Errorf("expected 1 address, got %d", len(a.Addresses))
}
}
}
Expand Down
Loading

0 comments on commit 9883c2f

Please sign in to comment.