github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/backend/httpstate/console.go (about) 1 // Copyright 2016-2018, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package httpstate 16 17 import ( 18 "net/url" 19 "os" 20 "path" 21 "strings" 22 ) 23 24 const ( 25 // ConsoleDomainEnvVar overrides the way we infer the domain we assume the Pulumi Console will 26 // be served from, and instead just use this value. e.g. so links to the stack update go to 27 // https://pulumi.example.com/org/project/stack/updates/2 instead. 28 ConsoleDomainEnvVar = "PULUMI_CONSOLE_DOMAIN" 29 30 // PulumiCloudURL is the Cloud URL used if no environment or explicit cloud is chosen. 31 PulumiCloudURL = "https://" + defaultAPIDomainPrefix + "pulumi.com" 32 33 // defaultAPIDomainPrefix is the assumed Cloud URL prefix for typical Pulumi Cloud API endpoints. 34 defaultAPIDomainPrefix = "api." 35 // defaultConsoleDomainPrefix is the assumed Cloud URL prefix typically used for the Pulumi Console. 36 defaultConsoleDomainPrefix = "app." 37 ) 38 39 // cloudConsoleURL returns a URL to the Pulumi Cloud Console, rooted at cloudURL. If there is 40 // an error, returns "". 41 func cloudConsoleURL(cloudURL string, paths ...string) string { 42 u, err := url.Parse(cloudURL) 43 if err != nil { 44 return "" 45 } 46 47 switch { 48 case os.Getenv(ConsoleDomainEnvVar) != "": 49 // Honor a PULUMI_CONSOLE_DOMAIN environment variable to override the 50 // default behavior. Since we identify a backend by a single URI, we 51 // cannot know what the Pulumi Console is hosted at... 52 u.Host = os.Getenv(ConsoleDomainEnvVar) 53 case strings.HasPrefix(u.Host, defaultAPIDomainPrefix): 54 // ... but if the cloudURL (API domain) is "api.", then we assume the 55 // console is hosted at "app.". 56 u.Host = defaultConsoleDomainPrefix + u.Host[len(defaultAPIDomainPrefix):] 57 case u.Host == "localhost:8080": 58 // ... or when running locally, on port 3000. 59 u.Host = "localhost:3000" 60 default: 61 // We couldn't figure out how to convert the api hostname into a console hostname. 62 // We return "" so that the caller can know to omit the URL rather than just 63 // return an incorrect one. 64 return "" 65 } 66 67 u.Path = path.Join(paths...) 68 return u.String() 69 }