Skip to content

Commit

Permalink
vip
Browse files Browse the repository at this point in the history
  • Loading branch information
vhvb1989 committed Oct 19, 2023
1 parent 3b9bc3c commit c8b5794
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 29 deletions.
87 changes: 66 additions & 21 deletions cli/azd/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type showAction struct {
deploymentOperations azapi.DeploymentOperations
azdCtx *azdcontext.AzdContext
flags *showFlags
serviceManager project.ServiceManager
resourceManager project.ResourceManager
}

func newShowAction(
Expand All @@ -72,6 +74,8 @@ func newShowAction(
projectConfig *project.ProjectConfig,
azdCtx *azdcontext.AzdContext,
flags *showFlags,
serviceManager project.ServiceManager,
resourceManager project.ResourceManager,
) actions.Action {
return &showAction{
projectConfig: projectConfig,
Expand All @@ -83,6 +87,8 @@ func newShowAction(
deploymentOperations: deploymentOperations,
azdCtx: azdCtx,
flags: flags,
serviceManager: serviceManager,
resourceManager: resourceManager,
}
}

Expand Down Expand Up @@ -125,18 +131,18 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}

}

var subId, rgName string
if env, err := s.envManager.Get(ctx, environmentName); err != nil {
log.Printf("could not load environment: %s, resource ids will not be available", err)
} else {
if subId := env.GetSubscriptionId(); subId == "" {
if subId = env.GetSubscriptionId(); subId == "" {
log.Printf("provision has not been run, resource ids will not be available")
} else {
azureResourceManager := infra.NewAzureResourceManager(s.azCli, s.deploymentOperations)
resourceManager := project.NewResourceManager(env, s.azCli, s.deploymentOperations)
envName := env.GetEnvName()

rgName, err := azureResourceManager.FindResourceGroupForEnvironment(ctx, subId, envName)
rgName, err = azureResourceManager.FindResourceGroupForEnvironment(ctx, subId, envName)
if err == nil {
for svcName, serviceConfig := range s.projectConfig.Services {
resources, err := resourceManager.GetServiceResources(ctx, subId, rgName, serviceConfig)
Expand All @@ -150,6 +156,7 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
resSvc.Target = &contracts.ShowTargetArm{
ResourceIds: resourceIds,
}
resSvc.IngresUrl = s.serviceEndpoint(ctx, subId, serviceConfig, env)
res.Services[svcName] = resSvc
} else {
log.Printf("ignoring error determining resource id for service %s: %v", svcName, err)
Expand All @@ -168,30 +175,68 @@ func (s *showAction) Run(ctx context.Context) (*actions.ActionResult, error) {
return nil, s.formatter.Format(res, s.writer, nil)
}

appEnvironments, err := s.envManager.List(ctx)
if err != nil {
return nil, err
}
uxEnvironments := make([]*ux.ShowEnvironment, len(appEnvironments))
for index, environment := range appEnvironments {
uxEnvironments[index] = &ux.ShowEnvironment{
Name: environment.Name,
IsDefault: environment.IsDefault,
}
}

uxServices := make([]*ux.ShowService, len(res.Services))
var index int
for serviceName, service := range res.Services {
uxServices[index] = &ux.ShowService{
Name: serviceName,
IngresUrl: service.IngresUrl,
}
index++
}

s.console.MessageUxItem(ctx, &ux.Show{
AppName: "Foo",
Services: []*ux.ShowService{
{
Name: "xx",
IngresUrl: "bar",
},
},
Environments: []*ux.ShowEnvironment{
{
Name: "foo",
IsDefault: true,
},
{
Name: "Bar",
IsDefault: false,
},
},
AzurePortalLink: "foo.com",
AppName: s.azdCtx.GetDefaultProjectName(),
Services: uxServices,
Environments: uxEnvironments,
AzurePortalLink: azurePortalLink(subId, rgName),
})

return nil, nil
}

func (s *showAction) serviceEndpoint(
ctx context.Context, subId string, serviceConfig *project.ServiceConfig, env *environment.Environment) string {
targetResource, err := s.resourceManager.GetTargetResource(ctx, subId, serviceConfig)
if err != nil {
log.Printf("error: getting target-resource. Endpoints will be empty: %v", err)
return ""
}

st, err := s.serviceManager.GetServiceTarget(ctx, serviceConfig)
if err != nil {
log.Printf("error: getting service target. Endpoints will be empty: %v", err)
return ""
}
endpoints, err := st.Endpoints(ctx, serviceConfig, targetResource)
if err != nil {
log.Printf("error: getting service endpoints. Endpoints might be empty: %v", err)
}

overriddenEndpoints := project.OverriddenEndpoints(ctx, serviceConfig, env)
if len(overriddenEndpoints) > 0 {
endpoints = overriddenEndpoints
}

if len(endpoints) == 0 {
return ""
}

return endpoints[0]
}

func showTypeFromLanguage(language project.ServiceLanguageKind) contracts.ShowType {
switch language {
case project.ServiceLanguageDotNet, project.ServiceLanguageCsharp, project.ServiceLanguageFsharp:
Expand Down
15 changes: 11 additions & 4 deletions cli/azd/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,22 @@ func getResourceGroupFollowUp(
}
followUp = fmt.Sprintf("%s\n%s",
defaultFollowUpText,
output.WithLinkFormat(fmt.Sprintf(
"https://portal.azure.com/#@/resource/subscriptions/%s/resourceGroups/%s/overview",
subscriptionId,
resourceGroupName)))
azurePortalLink(subscriptionId, resourceGroupName))
}

return followUp
}

func azurePortalLink(subscriptionId, resourceGroupName string) string {
if subscriptionId == "" || resourceGroupName == "" {
return "Application is not yet provisioned. Run azd provision first."
}
return output.WithLinkFormat(fmt.Sprintf(
"https://portal.azure.com/#@/resource/subscriptions/%s/resourceGroups/%s/overview",
subscriptionId,
resourceGroupName))
}

func serviceNameWarningCheck(console input.Console, serviceNameFlag string, commandName string) {
if serviceNameFlag == "" {
return
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/pkg/contracts/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type ShowService struct {
Project ShowServiceProject `json:"project"`
// Target contains information about the resource that the service is deployed
// to.
Target *ShowTargetArm `json:"target,omitempty"`
Target *ShowTargetArm `json:"target,omitempty"`
IngresUrl string
}

// ShowServiceProject is the contract for a service's project as returned by `azd show`
Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/project/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (sm *serviceManager) Deploy(

// Allow users to specify their own endpoints, in cases where they've configured their own front-end load balancers,
// reverse proxies or DNS host names outside of the service target (and prefer that to be used instead).
overriddenEndpoints := sm.getOverriddenEndpoints(ctx, serviceConfig)
overriddenEndpoints := OverriddenEndpoints(ctx, serviceConfig, sm.env)
if len(overriddenEndpoints) > 0 {
deployResult.Endpoints = overriddenEndpoints
}
Expand Down Expand Up @@ -537,8 +537,8 @@ func (sm *serviceManager) GetFrameworkService(ctx context.Context, serviceConfig
return frameworkService, nil
}

func (sm *serviceManager) getOverriddenEndpoints(ctx context.Context, serviceConfig *ServiceConfig) []string {
overriddenEndpoints := sm.env.GetServiceProperty(serviceConfig.Name, "ENDPOINTS")
func OverriddenEndpoints(ctx context.Context, serviceConfig *ServiceConfig, env *environment.Environment) []string {
overriddenEndpoints := env.GetServiceProperty(serviceConfig.Name, "ENDPOINTS")
if overriddenEndpoints != "" {
var endpoints []string
err := json.Unmarshal([]byte(overriddenEndpoints), &endpoints)
Expand Down

0 comments on commit c8b5794

Please sign in to comment.