Esempio n. 1
0
// title: service instance info
// path: /services/{service}/instances/{instance}
// method: GET
// produce: application/json
// responses:
//   200: OK
//   401: Unauthorized
//   404: Service instance not found
func serviceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	instanceName := r.URL.Query().Get(":instance")
	serviceName := r.URL.Query().Get(":service")
	serviceInstance, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	allowed := permission.Check(t, permission.PermServiceInstanceRead,
		contextsForServiceInstance(serviceInstance, serviceName)...,
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	requestIDHeader, _ := config.GetString("request-id-header")
	requestID := context.GetRequestID(r, requestIDHeader)
	info, err := serviceInstance.Info(requestID)
	if err != nil {
		return err
	}
	plan, err := service.GetPlanByServiceNameAndPlanName(serviceName, serviceInstance.PlanName, requestID)
	if err != nil {
		return err
	}
	sInfo := serviceInstanceInfo{
		Apps:            serviceInstance.Apps,
		Teams:           serviceInstance.Teams,
		TeamOwner:       serviceInstance.TeamOwner,
		Description:     serviceInstance.Description,
		PlanName:        plan.Name,
		PlanDescription: plan.Description,
		CustomInfo:      info,
	}
	w.Header().Set("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(sInfo)
}
Esempio n. 2
0
func serviceInstanceInfo(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	instanceName := r.URL.Query().Get(":instance")
	serviceName := r.URL.Query().Get(":service")
	serviceInstance, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	permissionValue := serviceName + "/" + instanceName
	allowed := permission.Check(t, permission.PermServiceInstanceRead,
		append(permission.Contexts(permission.CtxTeam, serviceInstance.Teams),
			permission.Context(permission.CtxServiceInstance, permissionValue),
		)...,
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	rec.Log(t.GetUserName(), "service-instance-info", serviceName, instanceName)
	info, err := serviceInstance.Info()
	if err != nil {
		return err
	}
	plan, err := service.GetPlanByServiceNameAndPlanName(serviceName, serviceInstance.PlanName)
	if err != nil {
		return err
	}
	sInfo := ServiceInstanceInfo{
		Apps:            serviceInstance.Apps,
		Teams:           serviceInstance.Teams,
		TeamOwner:       serviceInstance.TeamOwner,
		Description:     serviceInstance.Description,
		PlanName:        plan.Name,
		PlanDescription: plan.Description,
		CustomInfo:      info,
	}
	b, err := json.Marshal(sInfo)
	if err != nil {
		return nil
	}
	w.Write(b)
	return nil
}