Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(cmd): add more advanced handling of app urls #110

Merged
merged 1 commit into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/deis/controller-sdk-go/api"
"github.com/deis/controller-sdk-go/apps"
"github.com/deis/controller-sdk-go/config"
"github.com/deis/controller-sdk-go/domains"
"github.com/deis/workflow-cli/pkg/git"
"github.com/deis/workflow-cli/pkg/webbrowser"
"github.com/deis/workflow-cli/settings"
Expand Down Expand Up @@ -104,11 +105,20 @@ func AppInfo(appID string) error {
return err
}

url, err := appURL(s, appID)
if err != nil {
return err
}

if url == "" {
url = fmt.Sprintf(noDomainAssignedMsg, appID)
}

fmt.Printf("=== %s Application\n", app.ID)
fmt.Println("updated: ", app.Updated)
fmt.Println("uuid: ", app.UUID)
fmt.Println("created: ", app.Created)
fmt.Println("url: ", app.URL)
fmt.Println("url: ", url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we shouldn't be changing this because the server fills out this field for us. If the server is returning us false information when the app has no domain, then we should fix this in the controller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, this adds an additional API request to this command which we try to avoid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't from the controller.... it's from the SDK, which is making the URL assumption of appname.cluster.com

Copy link
Contributor Author

@Joshua-Anderson Joshua-Anderson Jul 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend to remove app.URL from the SDK after this is merged.

fmt.Println("owner: ", app.Owner)
fmt.Println("id: ", app.ID)

Expand Down Expand Up @@ -137,12 +147,15 @@ func AppOpen(appID string) error {
return err
}

app, err := apps.Get(s.Client, appID)
if checkAPICompatibility(s.Client, err) != nil {
u, err := appURL(s, appID)
if err != nil {
return err
}

u := app.URL
if u == "" {
return fmt.Errorf(noDomainAssignedMsg, appID)
}

if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) {
u = "http://" + u
}
Expand Down Expand Up @@ -276,3 +289,32 @@ func AppTransfer(appID, username string) error {

return nil
}

const noDomainAssignedMsg = "No domain assigned to %s"

// appURL grabs the first domain an app has and returns this.
func appURL(s *settings.Settings, appID string) (string, error) {
domains, _, err := domains.List(s.Client, appID, 1)
if checkAPICompatibility(s.Client, err) != nil {
return "", err
}

if len(domains) == 0 {
return "", nil
}

return expandURL(s.Client.ControllerURL.Host, domains[0].Domain), nil
}

// expandURL expands an app url if neccessary.
func expandURL(host, u string) string {
if strings.Contains(u, ".") {
// If domain is a full url.
return u
}

// If domain is a subdomain, look up the controller url and replace the subdomain.
parts := strings.Split(host, ".")
parts[0] = u
return strings.Join(parts, ".")
}
26 changes: 26 additions & 0 deletions cmd/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,29 @@ func TestPrintLogLinesBadLine(t *testing.T) {
t.Fatal(err)
}
}

type expandURLCases struct {
Input string
Expected string
}

func TestExpandUrl(t *testing.T) {
checks := []expandURLCases{
expandURLCases{
Input: "test.com",
Expected: "test.com",
},
expandURLCases{
Input: "test",
Expected: "test.foo.com",
},
}

for _, check := range checks {
out := expandURL("deis.foo.com", check.Input)

if out != check.Expected {
t.Errorf("Expected %s, Got %s", check.Expected, out)
}
}
}