github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/instance_get.go (about) 1 package broker 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "strings" 8 9 "github.com/kyma-project/kyma-environment-broker/internal" 10 11 "github.com/kyma-project/kyma-environment-broker/internal/storage" 12 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 13 14 "github.com/pivotal-cf/brokerapi/v8/domain" 15 "github.com/pivotal-cf/brokerapi/v8/domain/apiresponses" 16 "github.com/sirupsen/logrus" 17 ) 18 19 const allSubaccountsIDs = "all" 20 21 type GetInstanceEndpoint struct { 22 config Config 23 instancesStorage storage.Instances 24 operationsStorage storage.Provisioning 25 brokerURL string 26 27 log logrus.FieldLogger 28 } 29 30 func NewGetInstance(cfg Config, 31 instancesStorage storage.Instances, 32 operationsStorage storage.Provisioning, 33 log logrus.FieldLogger, 34 ) *GetInstanceEndpoint { 35 return &GetInstanceEndpoint{ 36 config: cfg, 37 instancesStorage: instancesStorage, 38 operationsStorage: operationsStorage, 39 log: log.WithField("service", "GetInstanceEndpoint"), 40 } 41 } 42 43 // GetInstance fetches information about a service instance 44 // GET /v2/service_instances/{instance_id} 45 func (b *GetInstanceEndpoint) GetInstance(_ context.Context, instanceID string, _ domain.FetchInstanceDetails) (domain.GetInstanceDetailsSpec, error) { 46 logger := b.log.WithField("instanceID", instanceID) 47 logger.Infof("GetInstance called") 48 49 instance, err := b.instancesStorage.GetByID(instanceID) 50 if err != nil { 51 statusCode := http.StatusNotFound 52 if !dberr.IsNotFound(err) { 53 statusCode = http.StatusInternalServerError 54 return domain.GetInstanceDetailsSpec{}, apiresponses.NewFailureResponse(fmt.Errorf("failed to get instanceID %s", instanceID), statusCode, fmt.Sprintf("failed to get instanceID %s", instanceID)) 55 } 56 return domain.GetInstanceDetailsSpec{}, apiresponses.NewFailureResponse(fmt.Errorf("instance with instanceID %s does not exist", instanceID), statusCode, fmt.Sprintf("instance with instanceID %s does not exist", instanceID)) 57 } 58 59 // check if provisioning still in progress 60 op, err := b.operationsStorage.GetProvisioningOperationByInstanceID(instanceID) 61 if err != nil { 62 return domain.GetInstanceDetailsSpec{}, apiresponses.NewFailureResponse(fmt.Errorf("failed to get operation for instanceID %s", instanceID), http.StatusNotFound, fmt.Sprintf("failed to get operation for instanceID %s", instanceID)) 63 } else if op.State == domain.InProgress || op.State == domain.Failed { 64 err = fmt.Errorf("provisioning of instanceID %s %s", instanceID, op.State) 65 return domain.GetInstanceDetailsSpec{}, apiresponses.NewFailureResponse(err, http.StatusNotFound, err.Error()) 66 } 67 68 if !instance.DeletedAt.IsZero() { 69 return domain.GetInstanceDetailsSpec{}, apiresponses.NewFailureResponse(fmt.Errorf("instance with instanceID %s does not exist", instanceID), http.StatusNotFound, fmt.Sprintf("instance with instanceID %s does not exist", instanceID)) 70 } 71 72 parameters := b.prepareParametersToReturn(instance.Parameters) 73 74 spec := domain.GetInstanceDetailsSpec{ 75 ServiceID: instance.ServiceID, 76 PlanID: instance.ServicePlanID, 77 DashboardURL: instance.DashboardURL, 78 Parameters: parameters, 79 Metadata: domain.InstanceMetadata{ 80 Labels: ResponseLabels(*op, *instance, b.config.URL, b.config.EnableKubeconfigURLLabel), 81 }, 82 } 83 84 if b.config.ShowTrialExpirationInfo && 85 instance.ServicePlanID == TrialPlanID && 86 (b.config.SubaccountsIdsToShowTrialExpirationInfo == allSubaccountsIDs || 87 strings.Contains(b.config.SubaccountsIdsToShowTrialExpirationInfo, instance.SubAccountID)) { 88 spec.Metadata.Labels = ResponseLabelsWithExpirationInfo(*op, *instance, b.config.URL, b.config.TrialDocsURL, b.config.EnableKubeconfigURLLabel) 89 } 90 91 return spec, nil 92 } 93 94 func (b *GetInstanceEndpoint) prepareParametersToReturn(parameters internal.ProvisioningParameters) internal.ProvisioningParameters { 95 parameters.Parameters.Kubeconfig = "" 96 parameters.ErsContext.SMOperatorCredentials = nil 97 return parameters 98 }