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