github.com/sleungcy-sap/cli@v7.1.0+incompatible/actor/v7action/application_summary.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     6  	"code.cloudfoundry.org/cli/resources"
     7  )
     8  
     9  type ApplicationSummary struct {
    10  	resources.Application
    11  	ProcessSummaries ProcessSummaries
    12  	Routes           []resources.Route
    13  }
    14  
    15  // v7action.DetailedApplicationSummary represents an application with its processes and droplet.
    16  type DetailedApplicationSummary struct {
    17  	ApplicationSummary
    18  	CurrentDroplet resources.Droplet
    19  }
    20  
    21  func (a ApplicationSummary) GetIsolationSegmentName() (string, bool) {
    22  	if a.hasIsolationSegment() {
    23  		return a.ProcessSummaries[0].InstanceDetails[0].IsolationSegment, true
    24  	}
    25  	return "", false
    26  }
    27  
    28  func (a ApplicationSummary) hasIsolationSegment() bool {
    29  	return len(a.ProcessSummaries) > 0 &&
    30  		len(a.ProcessSummaries[0].InstanceDetails) > 0 &&
    31  		len(a.ProcessSummaries[0].InstanceDetails[0].IsolationSegment) > 0
    32  }
    33  
    34  func (actor Actor) GetAppSummariesForSpace(spaceGUID string, labelSelector string) ([]ApplicationSummary, Warnings, error) {
    35  	var allWarnings Warnings
    36  	var allSummaries []ApplicationSummary
    37  
    38  	keys := []ccv3.Query{
    39  		{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}},
    40  		{Key: ccv3.OrderBy, Values: []string{ccv3.NameOrder}},
    41  	}
    42  	if len(labelSelector) > 0 {
    43  		keys = append(keys, ccv3.Query{Key: ccv3.LabelSelectorFilter, Values: []string{labelSelector}})
    44  	}
    45  	apps, warnings, err := actor.CloudControllerClient.GetApplications(keys...)
    46  	allWarnings = append(allWarnings, warnings...)
    47  	if err != nil {
    48  		return nil, allWarnings, err
    49  	}
    50  
    51  	processes, warnings, err := actor.CloudControllerClient.GetProcesses(ccv3.Query{
    52  		Key: ccv3.AppGUIDFilter, Values: toAppGUIDs(apps),
    53  	})
    54  	allWarnings = append(allWarnings, warnings...)
    55  	if err != nil {
    56  		return nil, allWarnings, err
    57  	}
    58  
    59  	processSummariesByAppGUID := make(map[string]ProcessSummaries, len(apps))
    60  	for _, process := range processes {
    61  		instances, warnings, err := actor.CloudControllerClient.GetProcessInstances(process.GUID)
    62  		allWarnings = append(allWarnings, Warnings(warnings)...)
    63  		if err != nil {
    64  			return nil, allWarnings, err
    65  		}
    66  
    67  		var instanceDetails []ProcessInstance
    68  		for _, instance := range instances {
    69  			instanceDetails = append(instanceDetails, ProcessInstance(instance))
    70  		}
    71  
    72  		processSummary := ProcessSummary{
    73  			Process:         Process(process),
    74  			InstanceDetails: instanceDetails,
    75  		}
    76  
    77  		processSummariesByAppGUID[process.AppGUID] = append(processSummariesByAppGUID[process.AppGUID], processSummary)
    78  	}
    79  
    80  	routes, warnings, err := actor.CloudControllerClient.GetRoutes(ccv3.Query{
    81  		Key: ccv3.AppGUIDFilter, Values: toAppGUIDs(apps),
    82  	})
    83  	allWarnings = append(allWarnings, Warnings(warnings)...)
    84  	if err != nil {
    85  		return nil, allWarnings, err
    86  	}
    87  
    88  	routesByAppGUID := make(map[string][]resources.Route)
    89  
    90  	for _, route := range routes {
    91  		for _, dest := range route.Destinations {
    92  			routesByAppGUID[dest.App.GUID] = append(routesByAppGUID[dest.App.GUID], route)
    93  		}
    94  	}
    95  
    96  	for _, app := range apps {
    97  		processSummariesByAppGUID[app.GUID].Sort()
    98  
    99  		summary := ApplicationSummary{
   100  			Application:      app,
   101  			ProcessSummaries: processSummariesByAppGUID[app.GUID],
   102  			Routes:           routesByAppGUID[app.GUID],
   103  		}
   104  
   105  		allSummaries = append(allSummaries, summary)
   106  	}
   107  
   108  	return allSummaries, allWarnings, nil
   109  }
   110  
   111  func (actor Actor) GetDetailedAppSummary(appName, spaceGUID string, withObfuscatedValues bool) (DetailedApplicationSummary, Warnings, error) {
   112  	var allWarnings Warnings
   113  
   114  	app, actorWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
   115  	allWarnings = append(allWarnings, actorWarnings...)
   116  	if err != nil {
   117  		return DetailedApplicationSummary{}, actorWarnings, err
   118  	}
   119  
   120  	summary, warnings, err := actor.createSummary(app, withObfuscatedValues)
   121  	allWarnings = append(allWarnings, warnings...)
   122  	if err != nil {
   123  		return DetailedApplicationSummary{}, allWarnings, err
   124  	}
   125  
   126  	detailedSummary, warnings, err := actor.addDroplet(summary)
   127  	allWarnings = append(allWarnings, warnings...)
   128  	if err != nil {
   129  		return DetailedApplicationSummary{}, allWarnings, err
   130  	}
   131  
   132  	return detailedSummary, allWarnings, err
   133  }
   134  
   135  func (actor Actor) createSummary(app resources.Application, withObfuscatedValues bool) (ApplicationSummary, Warnings, error) {
   136  	var allWarnings Warnings
   137  
   138  	processSummaries, processWarnings, err := actor.getProcessSummariesForApp(app.GUID, withObfuscatedValues)
   139  	allWarnings = append(allWarnings, processWarnings...)
   140  	if err != nil {
   141  		return ApplicationSummary{}, allWarnings, err
   142  	}
   143  
   144  	routes, warnings, err := actor.GetApplicationRoutes(app.GUID)
   145  	allWarnings = append(allWarnings, warnings...)
   146  	if err != nil {
   147  		return ApplicationSummary{}, allWarnings, err
   148  	}
   149  
   150  	return ApplicationSummary{
   151  		Application:      app,
   152  		ProcessSummaries: processSummaries,
   153  		Routes:           routes,
   154  	}, allWarnings, nil
   155  }
   156  
   157  func (actor Actor) addDroplet(summary ApplicationSummary) (DetailedApplicationSummary, Warnings, error) {
   158  	var allWarnings Warnings
   159  
   160  	droplet, warnings, err := actor.GetCurrentDropletByApplication(summary.GUID)
   161  	allWarnings = append(allWarnings, warnings...)
   162  	if err != nil {
   163  		if _, ok := err.(actionerror.DropletNotFoundError); !ok {
   164  			return DetailedApplicationSummary{}, allWarnings, err
   165  		}
   166  	}
   167  	return DetailedApplicationSummary{
   168  		ApplicationSummary: summary,
   169  		CurrentDroplet:     droplet,
   170  	}, allWarnings, nil
   171  }
   172  
   173  func toAppGUIDs(apps []resources.Application) []string {
   174  	guids := make([]string, len(apps))
   175  
   176  	for i, app := range apps {
   177  		guids[i] = app.GUID
   178  	}
   179  
   180  	return guids
   181  }