github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/broker.go (about)

     1  package broker
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"strings"
     7  
     8  	"gopkg.in/yaml.v2"
     9  
    10  	"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
    11  	"github.com/kyma-project/kyma-environment-broker/internal"
    12  )
    13  
    14  const (
    15  	KymaServiceID   = "47c9dcbf-ff30-448e-ab36-d3bad66ba281"
    16  	KymaServiceName = "kymaruntime"
    17  )
    18  
    19  type PlanDefaults func(planID string, platformProvider internal.CloudProvider, parametersProvider *internal.CloudProvider) (*gqlschema.ClusterConfigInput, error)
    20  
    21  type KymaEnvironmentBroker struct {
    22  	*ServicesEndpoint
    23  	*ProvisionEndpoint
    24  	*DeprovisionEndpoint
    25  	*UpdateEndpoint
    26  	*GetInstanceEndpoint
    27  	*LastOperationEndpoint
    28  	*BindEndpoint
    29  	*UnbindEndpoint
    30  	*GetBindingEndpoint
    31  	*LastBindingOperationEndpoint
    32  }
    33  
    34  // Config represents configuration for broker
    35  type Config struct {
    36  	EnablePlans                             EnablePlans `envconfig:"default=azure"`
    37  	OnlySingleTrialPerGA                    bool        `envconfig:"default=true"`
    38  	URL                                     string
    39  	EnableKubeconfigURLLabel                bool   `envconfig:"default=false"`
    40  	IncludeAdditionalParamsInSchema         bool   `envconfig:"default=false"`
    41  	ShowTrialExpirationInfo                 bool   `envconfig:"default=false"`
    42  	SubaccountsIdsToShowTrialExpirationInfo string `envconfig:"default="`
    43  	TrialDocsURL                            string `envconfig:"default="`
    44  
    45  	AllowNetworkingParameters      bool `envconfig:"default=false"`
    46  	RegionParameterIsRequired      bool `envconfig:"default=false"`
    47  	ExposeSchemaWithRegionRequired bool `envconfig:"default=false"`
    48  	AllowModulesParameters         bool `envconfig:"default=false"`
    49  }
    50  
    51  type ServicesConfig map[string]Service
    52  
    53  func NewServicesConfigFromFile(path string) (ServicesConfig, error) {
    54  	yamlFile, err := ioutil.ReadFile(path)
    55  	if err != nil {
    56  		return nil, fmt.Errorf("while reading YAML file with managed components list: %w", err)
    57  	}
    58  	var servicesConfig struct {
    59  		Services ServicesConfig `yaml:"services"`
    60  	}
    61  	err = yaml.Unmarshal(yamlFile, &servicesConfig)
    62  	if err != nil {
    63  		return nil, fmt.Errorf("while unmarshaling YAML file with managed components list: %w", err)
    64  	}
    65  	return servicesConfig.Services, nil
    66  }
    67  
    68  func (s ServicesConfig) DefaultPlansConfig() (PlansConfig, error) {
    69  	cfg, ok := s[KymaServiceName]
    70  	if !ok {
    71  		return nil, fmt.Errorf("while getting data about %s plans", KymaServiceName)
    72  	}
    73  	return cfg.Plans, nil
    74  }
    75  
    76  type Service struct {
    77  	Description string          `yaml:"description"`
    78  	Metadata    ServiceMetadata `yaml:"metadata"`
    79  	Plans       PlansConfig     `yaml:"plans"`
    80  }
    81  
    82  type ServiceMetadata struct {
    83  	DisplayName         string `yaml:"displayName"`
    84  	ImageUrl            string `yaml:"imageUrl"`
    85  	LongDescription     string `yaml:"longDescription"`
    86  	ProviderDisplayName string `yaml:"providerDisplayName"`
    87  	DocumentationUrl    string `yaml:"documentationUrl"`
    88  	SupportUrl          string `yaml:"supportUrl"`
    89  }
    90  
    91  type PlansConfig map[string]PlanData
    92  
    93  type PlanData struct {
    94  	Description string       `yaml:"description"`
    95  	Metadata    PlanMetadata `yaml:"metadata"`
    96  }
    97  type PlanMetadata struct {
    98  	DisplayName string `yaml:"displayName"`
    99  }
   100  
   101  // EnablePlans defines the plans that should be available for provisioning
   102  type EnablePlans []string
   103  
   104  // Unmarshal provides custom parsing of enabled plans.
   105  // Implements envconfig.Unmarshal interface.
   106  func (m *EnablePlans) Unmarshal(in string) error {
   107  	plans := strings.Split(in, ",")
   108  	for _, name := range plans {
   109  		if _, exists := PlanIDsMapping[name]; !exists {
   110  			return fmt.Errorf("unrecognized %v plan name", name)
   111  		}
   112  	}
   113  
   114  	*m = plans
   115  	return nil
   116  }