2018-05-25 03:48:03 +02:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-09-04 21:38:58 +02:00
|
|
|
package httpstate
|
2018-05-25 03:48:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2020-04-17 20:50:00 +02:00
|
|
|
"os"
|
2018-05-25 03:48:03 +02:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-17 20:50:00 +02:00
|
|
|
const (
|
|
|
|
// ConsoleDomainEnvVar overrides the way we infer the domain we assume the Pulumi Console will
|
|
|
|
// be served from, and instead just use this value. e.g. so links to the stack update go to
|
|
|
|
// https://pulumi.example.com/org/project/stack/updates/2 instead.
|
|
|
|
ConsoleDomainEnvVar = "PULUMI_CONSOLE_DOMAIN"
|
|
|
|
|
|
|
|
// PulumiCloudURL is the Cloud URL used if no environment or explicit cloud is chosen.
|
|
|
|
PulumiCloudURL = "https://" + defaultAPIDomainPrefix + "pulumi.com"
|
|
|
|
|
|
|
|
// defaultAPIDomainPrefix is the assumed Cloud URL prefix for typical Pulumi Cloud API endpoints.
|
|
|
|
defaultAPIDomainPrefix = "api."
|
|
|
|
// defaultConsoleDomainPrefix is the assumed Cloud URL prefix typically used for the Pulumi Console.
|
|
|
|
defaultConsoleDomainPrefix = "app."
|
|
|
|
)
|
|
|
|
|
|
|
|
// cloudConsoleURL returns a URL to the Pulumi Cloud Console, rooted at cloudURL. If there is
|
|
|
|
// an error, returns "".
|
2018-05-25 03:48:03 +02:00
|
|
|
func cloudConsoleURL(cloudURL string, paths ...string) string {
|
|
|
|
u, err := url.Parse(cloudURL)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2020-04-17 20:50:00 +02:00
|
|
|
case os.Getenv(ConsoleDomainEnvVar) != "":
|
|
|
|
// Honor a PULUMI_CONSOLE_DOMAIN environment variable to override the
|
|
|
|
// default behavior. Since we identify a backend by a single URI, we
|
|
|
|
// cannot know what the Pulumi Console is hosted at...
|
|
|
|
u.Host = os.Getenv(ConsoleDomainEnvVar)
|
2018-05-25 03:48:03 +02:00
|
|
|
case strings.HasPrefix(u.Host, defaultAPIDomainPrefix):
|
2020-04-17 20:50:00 +02:00
|
|
|
// ... but if the cloudURL (API domain) is "api.", then we assume the
|
|
|
|
// console is hosted at "app.".
|
2018-05-25 03:48:03 +02:00
|
|
|
u.Host = defaultConsoleDomainPrefix + u.Host[len(defaultAPIDomainPrefix):]
|
|
|
|
case u.Host == "localhost:8080":
|
2020-04-17 20:50:00 +02:00
|
|
|
// ... or when running locally, on port 3000.
|
2018-05-25 03:48:03 +02:00
|
|
|
u.Host = "localhost:3000"
|
|
|
|
default:
|
2020-04-17 20:50:00 +02:00
|
|
|
// We couldn't figure out how to convert the api hostname into a console hostname.
|
|
|
|
// We return "" so that the caller can know to omit the URL rather than just
|
|
|
|
// return an incorrect one.
|
|
|
|
return ""
|
2018-05-25 03:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u.Path = path.Join(paths...)
|
|
|
|
return u.String()
|
|
|
|
}
|