github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+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 . "github.com/liamawhite/cli-with-i18n/cf/i18n" 11 12 "github.com/liamawhite/cli-with-i18n/cf/api/resources" 13 "github.com/liamawhite/cli-with-i18n/cf/configuration/coreconfig" 14 "github.com/liamawhite/cli-with-i18n/cf/errors" 15 "github.com/liamawhite/cli-with-i18n/cf/models" 16 "github.com/liamawhite/cli-with-i18n/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 GetApp(appGUID string) (models.Application, error) 24 Read(name string) (app models.Application, apiErr error) 25 ReadFromSpace(name string, spaceGUID string) (app models.Application, apiErr error) 26 Update(appGUID string, params models.AppParams) (updatedApp models.Application, apiErr error) 27 Delete(appGUID string) (apiErr error) 28 ReadEnv(guid string) (*models.Environment, error) 29 CreateRestageRequest(guid string) (apiErr error) 30 } 31 32 type CloudControllerRepository struct { 33 config coreconfig.Reader 34 gateway net.Gateway 35 } 36 37 func NewCloudControllerRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerRepository) { 38 repo.config = config 39 repo.gateway = gateway 40 return 41 } 42 43 func (repo CloudControllerRepository) Create(params models.AppParams) (models.Application, error) { 44 appResource := resources.NewApplicationEntityFromAppParams(params) 45 data, err := json.Marshal(appResource) 46 if err != nil { 47 return models.Application{}, fmt.Errorf("%s: %s", T("Failed to marshal JSON"), err.Error()) 48 } 49 50 resource := new(resources.ApplicationResource) 51 err = repo.gateway.CreateResource(repo.config.APIEndpoint(), "/v2/apps", bytes.NewReader(data), resource) 52 if err != nil { 53 return models.Application{}, err 54 } 55 56 return resource.ToModel(), nil 57 } 58 59 func (repo CloudControllerRepository) GetApp(appGUID string) (app models.Application, apiErr error) { 60 path := fmt.Sprintf("%s/v2/apps/%s", repo.config.APIEndpoint(), appGUID) 61 appResources := new(resources.ApplicationResource) 62 63 apiErr = repo.gateway.GetResource(path, appResources) 64 if apiErr != nil { 65 return 66 } 67 68 app = appResources.ToModel() 69 return 70 } 71 72 func (repo CloudControllerRepository) Read(name string) (app models.Application, apiErr error) { 73 return repo.ReadFromSpace(name, repo.config.SpaceFields().GUID) 74 } 75 76 func (repo CloudControllerRepository) ReadFromSpace(name string, spaceGUID string) (app models.Application, apiErr error) { 77 path := fmt.Sprintf("%s/v2/spaces/%s/apps?q=%s&inline-relations-depth=1", repo.config.APIEndpoint(), spaceGUID, url.QueryEscape("name:"+name)) 78 appResources := new(resources.PaginatedApplicationResources) 79 apiErr = repo.gateway.GetResource(path, appResources) 80 if apiErr != nil { 81 return 82 } 83 84 if len(appResources.Resources) == 0 { 85 apiErr = errors.NewModelNotFoundError("App", name) 86 return 87 } 88 89 res := appResources.Resources[0] 90 app = res.ToModel() 91 return 92 } 93 94 func (repo CloudControllerRepository) Update(appGUID string, params models.AppParams) (updatedApp models.Application, apiErr error) { 95 appResource := resources.NewApplicationEntityFromAppParams(params) 96 data, err := json.Marshal(appResource) 97 if err != nil { 98 return models.Application{}, fmt.Errorf("%s: %s", T("Failed to marshal JSON"), err.Error()) 99 } 100 101 path := fmt.Sprintf("/v2/apps/%s?inline-relations-depth=1", appGUID) 102 resource := new(resources.ApplicationResource) 103 apiErr = repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(data), resource) 104 if apiErr != nil { 105 return 106 } 107 108 updatedApp = resource.ToModel() 109 return 110 } 111 112 func (repo CloudControllerRepository) Delete(appGUID string) (apiErr error) { 113 path := fmt.Sprintf("/v2/apps/%s?recursive=true", appGUID) 114 return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path) 115 } 116 117 func (repo CloudControllerRepository) ReadEnv(guid string) (*models.Environment, error) { 118 var ( 119 err error 120 ) 121 122 path := fmt.Sprintf("%s/v2/apps/%s/env", repo.config.APIEndpoint(), guid) 123 appResource := models.NewEnvironment() 124 125 err = repo.gateway.GetResource(path, appResource) 126 if err != nil { 127 return &models.Environment{}, err 128 } 129 130 return appResource, err 131 } 132 133 func (repo CloudControllerRepository) CreateRestageRequest(guid string) error { 134 path := fmt.Sprintf("/v2/apps/%s/restage", guid) 135 return repo.gateway.CreateResource(repo.config.APIEndpoint(), path, strings.NewReader(""), nil) 136 }