github.com/cloudfoundry/cli@v7.1.0+incompatible/resources/application_resource.go (about) 1 package resources 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 8 ) 9 10 // Application represents a Cloud Controller V3 Application. 11 type Application struct { 12 // GUID is the unique application identifier. 13 GUID string 14 // StackName is the name of the stack on which the application runs. 15 StackName string 16 // LifecycleBuildpacks is a list of the names of buildpacks. 17 LifecycleBuildpacks []string 18 // LifecycleType is the type of the lifecycle. 19 LifecycleType constant.AppLifecycleType 20 // Metadata is used for custom tagging of API resources 21 Metadata *Metadata 22 // Name is the name given to the application. 23 Name string 24 // SpaceGUID is the unique identifier of the parent space. 25 SpaceGUID string 26 // State is the desired state of the application. 27 State constant.ApplicationState 28 } 29 30 // MarshalJSON converts an Application into a Cloud Controller Application. 31 func (a Application) MarshalJSON() ([]byte, error) { 32 ccApp := ccApplication{ 33 Name: a.Name, 34 Metadata: a.Metadata, 35 } 36 37 if a.SpaceGUID != "" { 38 ccApp.Relationships = Relationships{ 39 constant.RelationshipTypeSpace: Relationship{GUID: a.SpaceGUID}, 40 } 41 } 42 43 if a.LifecycleType == constant.AppLifecycleTypeDocker { 44 ccApp.setDockerLifecycle() 45 } else if a.LifecycleType == constant.AppLifecycleTypeBuildpack { 46 if len(a.LifecycleBuildpacks) > 0 || a.StackName != "" { 47 if a.hasAutodetectedBuildpack() { 48 ccApp.setAutodetectedBuildpackLifecycle(a) 49 } else { 50 ccApp.setBuildpackLifecycle(a) 51 } 52 } 53 } 54 55 return json.Marshal(ccApp) 56 } 57 58 // UnmarshalJSON helps unmarshal a Cloud Controller Application response. 59 func (a *Application) UnmarshalJSON(data []byte) error { 60 lifecycle := ccLifecycle{} 61 ccApp := ccApplication{ 62 Lifecycle: &lifecycle, 63 } 64 65 err := cloudcontroller.DecodeJSON(data, &ccApp) 66 if err != nil { 67 return err 68 } 69 70 a.GUID = ccApp.GUID 71 a.StackName = lifecycle.Data.Stack 72 a.LifecycleBuildpacks = lifecycle.Data.Buildpacks 73 a.LifecycleType = lifecycle.Type 74 a.Name = ccApp.Name 75 a.SpaceGUID = ccApp.Relationships[constant.RelationshipTypeSpace].GUID 76 a.State = ccApp.State 77 a.Metadata = ccApp.Metadata 78 79 return nil 80 } 81 82 func (a Application) Started() bool { 83 return a.State == constant.ApplicationStarted 84 } 85 86 func (a Application) Stopped() bool { 87 return a.State == constant.ApplicationStopped 88 } 89 90 func (a Application) hasAutodetectedBuildpack() bool { 91 if len(a.LifecycleBuildpacks) == 0 { 92 return false 93 } 94 return a.LifecycleBuildpacks[0] == constant.AutodetectBuildpackValueDefault || a.LifecycleBuildpacks[0] == constant.AutodetectBuildpackValueNull 95 } 96 97 type ccLifecycle struct { 98 Type constant.AppLifecycleType `json:"type,omitempty"` 99 Data struct { 100 Buildpacks []string `json:"buildpacks,omitempty"` 101 Stack string `json:"stack,omitempty"` 102 } `json:"data"` 103 } 104 105 type ccApplication struct { 106 Name string `json:"name,omitempty"` 107 Relationships Relationships `json:"relationships,omitempty"` 108 Lifecycle interface{} `json:"lifecycle,omitempty"` 109 GUID string `json:"guid,omitempty"` 110 State constant.ApplicationState `json:"state,omitempty"` 111 Metadata *Metadata `json:"metadata,omitempty"` 112 } 113 114 func (ccApp *ccApplication) setAutodetectedBuildpackLifecycle(a Application) { 115 var nullBuildpackLifecycle struct { 116 Type constant.AppLifecycleType `json:"type,omitempty"` 117 Data struct { 118 Buildpacks []string `json:"buildpacks"` 119 Stack string `json:"stack,omitempty"` 120 } `json:"data"` 121 } 122 nullBuildpackLifecycle.Type = constant.AppLifecycleTypeBuildpack 123 nullBuildpackLifecycle.Data.Stack = a.StackName 124 ccApp.Lifecycle = nullBuildpackLifecycle 125 } 126 127 func (ccApp *ccApplication) setBuildpackLifecycle(a Application) { 128 var lifecycle ccLifecycle 129 lifecycle.Type = a.LifecycleType 130 lifecycle.Data.Buildpacks = a.LifecycleBuildpacks 131 lifecycle.Data.Stack = a.StackName 132 ccApp.Lifecycle = lifecycle 133 } 134 135 func (ccApp *ccApplication) setDockerLifecycle() { 136 ccApp.Lifecycle = ccLifecycle{ 137 Type: constant.AppLifecycleTypeDocker, 138 } 139 }