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