github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/applications/applications.go (about) 1 package applications 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/url" 7 "strings" 8 9 . "github.com/cloudfoundry/cli/cf/i18n" 10 11 "github.com/cloudfoundry/cli/cf/api/resources" 12 "github.com/cloudfoundry/cli/cf/configuration/core_config" 13 "github.com/cloudfoundry/cli/cf/errors" 14 "github.com/cloudfoundry/cli/cf/models" 15 "github.com/cloudfoundry/cli/cf/net" 16 ) 17 18 type ApplicationRepository interface { 19 Create(params models.AppParams) (createdApp models.Application, apiErr error) 20 GetApp(appGuid string) (models.Application, error) 21 Read(name string) (app models.Application, apiErr error) 22 ReadFromSpace(name string, spaceGuid string) (app models.Application, apiErr error) 23 Update(appGuid string, params models.AppParams) (updatedApp models.Application, apiErr error) 24 Delete(appGuid string) (apiErr error) 25 ReadEnv(guid string) (*models.Environment, error) 26 CreateRestageRequest(guid string) (apiErr error) 27 } 28 29 type CloudControllerApplicationRepository struct { 30 config core_config.Reader 31 gateway net.Gateway 32 } 33 34 func NewCloudControllerApplicationRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerApplicationRepository) { 35 repo.config = config 36 repo.gateway = gateway 37 return 38 } 39 40 func (repo CloudControllerApplicationRepository) Create(params models.AppParams) (createdApp models.Application, apiErr error) { 41 data, err := repo.formatAppJSON(params) 42 if err != nil { 43 apiErr = errors.NewWithError(T("Failed to marshal JSON"), err) 44 return 45 } 46 47 resource := new(resources.ApplicationResource) 48 apiErr = repo.gateway.CreateResource(repo.config.ApiEndpoint(), "/v2/apps", strings.NewReader(data), resource) 49 if apiErr != nil { 50 return 51 } 52 53 createdApp = resource.ToModel() 54 return 55 } 56 57 func (repo CloudControllerApplicationRepository) GetApp(appGuid string) (app models.Application, apiErr error) { 58 path := fmt.Sprintf("%s/v2/apps/%s", repo.config.ApiEndpoint(), appGuid) 59 appResources := new(resources.ApplicationResource) 60 61 apiErr = repo.gateway.GetResource(path, appResources) 62 if apiErr != nil { 63 return 64 } 65 66 app = appResources.ToModel() 67 return 68 } 69 70 func (repo CloudControllerApplicationRepository) Read(name string) (app models.Application, apiErr error) { 71 return repo.ReadFromSpace(name, repo.config.SpaceFields().Guid) 72 } 73 74 func (repo CloudControllerApplicationRepository) ReadFromSpace(name string, spaceGuid string) (app models.Application, apiErr error) { 75 path := fmt.Sprintf("%s/v2/spaces/%s/apps?q=%s&inline-relations-depth=1", repo.config.ApiEndpoint(), spaceGuid, url.QueryEscape("name:"+name)) 76 appResources := new(resources.PaginatedApplicationResources) 77 apiErr = repo.gateway.GetResource(path, appResources) 78 if apiErr != nil { 79 return 80 } 81 82 if len(appResources.Resources) == 0 { 83 apiErr = errors.NewModelNotFoundError("App", name) 84 return 85 } 86 87 res := appResources.Resources[0] 88 app = res.ToModel() 89 return 90 } 91 92 func (repo CloudControllerApplicationRepository) Update(appGuid string, params models.AppParams) (updatedApp models.Application, apiErr error) { 93 data, err := repo.formatAppJSON(params) 94 if err != nil { 95 apiErr = errors.NewWithError(T("Failed to marshal JSON"), err) 96 return 97 } 98 99 path := fmt.Sprintf("/v2/apps/%s?inline-relations-depth=1", appGuid) 100 resource := new(resources.ApplicationResource) 101 apiErr = repo.gateway.UpdateResource(repo.config.ApiEndpoint(), path, strings.NewReader(data), resource) 102 if apiErr != nil { 103 return 104 } 105 106 updatedApp = resource.ToModel() 107 return 108 } 109 110 func (repo CloudControllerApplicationRepository) formatAppJSON(input models.AppParams) (data string, err error) { 111 appResource := resources.NewApplicationEntityFromAppParams(input) 112 bytes, err := json.Marshal(appResource) 113 data = string(bytes) 114 return 115 } 116 117 func (repo CloudControllerApplicationRepository) Delete(appGuid string) (apiErr error) { 118 path := fmt.Sprintf("/v2/apps/%s?recursive=true", appGuid) 119 return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path) 120 } 121 122 func (repo CloudControllerApplicationRepository) ReadEnv(guid string) (*models.Environment, error) { 123 var ( 124 err error 125 ) 126 127 path := fmt.Sprintf("%s/v2/apps/%s/env", repo.config.ApiEndpoint(), guid) 128 appResource := models.NewEnvironment() 129 130 err = repo.gateway.GetResource(path, appResource) 131 if err != nil { 132 return &models.Environment{}, err 133 } 134 135 return appResource, err 136 } 137 138 func (repo CloudControllerApplicationRepository) CreateRestageRequest(guid string) error { 139 path := fmt.Sprintf("/v2/apps/%s/restage", guid) 140 return repo.gateway.CreateResource(repo.config.ApiEndpoint(), path, strings.NewReader(""), nil) 141 }