// GetServiceClassAndPlans returns the service class details with the associated plans
// serviceName is the name of the service class
// the first parameter returned is the ServiceClass object
// the second parameter returned is the array of ServicePlans associated with the service class
GetServiceClassAndPlans returns the service class details with the associated plans
serviceName is the name of the service class
the first parameter returned is the ServiceClass object
the second parameter returned is the array of ServicePlans associated with the service class
// GetServiceClassAndPlans returns the service class details with the associated plans // serviceName is the name of the service class // the first parameter returned is the ServiceClass object // the second parameter returned is the array of ServicePlans associated with the service class
func GetServiceClassAndPlans(client *occlient.Client, serviceName string) (ServiceClass, []ServicePlans, error) {
	result, err := client.GetClusterServiceClass(serviceName)
	if err != nil {
		return ServiceClass{}, nil, errors.Wrap(err, "unable to get the given service")
	}
	var meta map[string]interface{}
	err = json.Unmarshal(result.Spec.ExternalMetadata.Raw, &meta)
	if err != nil {
		return ServiceClass{}, nil, errors.Wrap(err, "unable to unmarshal data the given service")
	}
	service := ServiceClass{
		Name:              result.Spec.ExternalName,
		Bindable:          result.Spec.Bindable,
		ShortDescription:  result.Spec.Description,
		Tags:              result.Spec.Tags,
		ServiceBrokerName: result.Spec.ClusterServiceBrokerName,
	}
	if val, ok := meta["longDescription"]; ok {
		service.LongDescription = val.(string)
	}
	if val, ok := meta["dependencies"]; ok {
		versions := fmt.Sprint(val)
		versions = strings.Replace(versions, "[", "", -1)
		versions = strings.Replace(versions, "]", "", -1)
		service.VersionsAvailable = strings.Split(versions, " ")
	}
	planResults, err := client.GetClusterPlansFromServiceName(result.Name)
	if err != nil {
		return ServiceClass{}, nil, errors.Wrap(err, "unable to get plans for the given service")
	}
	var plans []ServicePlans
	for _, result := range planResults {
		plan := ServicePlans{
			Name:        result.Spec.ExternalName,
			Description: result.Spec.Description,
		}
		var externalMetaData map[string]interface{}
		err = json.Unmarshal(result.Spec.ExternalMetadata.Raw, &externalMetaData)
		if err != nil {
			return ServiceClass{}, nil, errors.Wrap(err, "unable to unmarshal data the given service")
		}
		if val, ok := externalMetaData["displayName"]; ok {
			plan.DisplayName = val.(string)
		}
		var createParameter map[string]interface{}
		err = json.Unmarshal(result.Spec.ServiceInstanceCreateParameterSchema.Raw, &createParameter)
		if err != nil {
			return ServiceClass{}, nil, errors.Wrap(err, "unable to unmarshal data the given service")
		}
		requiredParameterNames := []string{}
		if val, ok := createParameter["required"]; ok {
			required := fmt.Sprint(val)
			required = strings.Replace(required, "[", "", -1)
			required = strings.Replace(required, "]", "", -1)
			requiredParameterNames = strings.Split(required, " ")
		}
		if val, ok := createParameter["properties"]; ok {
			propertiesMap, ok := val.(map[string]interface{})
			if ok {
				allServicePlanParameters := make([]ServicePlanParameter, 0, len(propertiesMap))
				for propertyName, propertyValues := range propertiesMap {
					servicePlanParameter := ServicePlanParameter{
						Name: propertyName,
					}
					for _, name := range requiredParameterNames {
						if name == propertyName {
							servicePlanParameter.Required = true
						}
					}
					propertyValuesMap, ok := propertyValues.(map[string]interface{})
					if ok {
						propertyDefaultValue, hasDefaultValue := propertyValuesMap["default"]
						if hasDefaultValue {
							servicePlanParameter.HasDefaultValue = true
							servicePlanParameter.DefaultValue = propertyDefaultValue
						}
						if propertyType, ok := propertyValuesMap["type"]; ok {
							servicePlanParameter.Type = fmt.Sprintf("%v", propertyType)
						}
					}
					allServicePlanParameters = append(allServicePlanParameters, servicePlanParameter)
				}
				plan.Parameters = allServicePlanParameters
			}
		}
		plans = append(plans, plan)
	}
	return service, plans, nil
}
func GetServiceClassAndPlans ( client * occlient . Client , serviceName string ) ( ServiceClass , [ ] ServicePlans , error ) { result , err := client . GetClusterServiceClass ( serviceName ) if err != nil { return ServiceClass { } , nil , errors . Wrap ( err , " " ) } var meta map [ string ] interface { } err = json . Unmarshal ( result . Spec . ExternalMetadata . Raw , & meta ) if err != nil { return ServiceClass { } , nil , errors . Wrap ( err , " " ) } service := ServiceClass { Name : result . Spec . ExternalName , Bindable : result . Spec . Bindable , ShortDescription : result . Spec . Description , Tags : result . Spec . Tags , ServiceBrokerName : result . Spec . ClusterServiceBrokerName , } if val , ok := meta [ " " ] ; ok { service . LongDescription = val . ( string ) } if val , ok := meta [ " " ] ; ok { versions := fmt . Sprint ( val ) versions = strings . Replace ( versions , " " , " " , - 1 ) versions = strings . Replace ( versions , " " , " " , - 1 ) service . VersionsAvailable = strings . Split ( versions , " " ) } planResults , err := client . GetClusterPlansFromServiceName ( result . Name ) if err != nil { return ServiceClass { } , nil , errors . Wrap ( err , " " ) } var plans [ ] ServicePlans for _ , result := range planResults { plan := ServicePlans { Name : result . Spec . ExternalName , Description : result . Spec . Description , } var externalMetaData map [ string ] interface { } err = json . Unmarshal ( result . Spec . ExternalMetadata . Raw , & externalMetaData ) if err != nil { return ServiceClass { } , nil , errors . Wrap ( err , " " ) } if val , ok := externalMetaData [ " " ] ; ok { plan . DisplayName = val . ( string ) } var createParameter map [ string ] interface { } err = json . Unmarshal ( result . Spec . ServiceInstanceCreateParameterSchema . Raw , & createParameter ) if err != nil { return ServiceClass { } , nil , errors . Wrap ( err , " " ) } requiredParameterNames := [ ] string { } if val , ok := createParameter [ " " ] ; ok { required := fmt . Sprint ( val ) required = strings . Replace ( required , " " , " " , - 1 ) required = strings . Replace ( required , " " , " " , - 1 ) requiredParameterNames = strings . Split ( required , " " ) } if val , ok := createParameter [ " " ] ; ok { propertiesMap , ok := val . ( map [ string ] interface { } ) if ok { allServicePlanParameters := make ( [ ] ServicePlanParameter , 0 , len ( propertiesMap ) ) for propertyName , propertyValues := range propertiesMap { servicePlanParameter := ServicePlanParameter { Name : propertyName , } for _ , name := range requiredParameterNames { if name == propertyName { servicePlanParameter . Required = true } } propertyValuesMap , ok := propertyValues . ( map [ string ] interface { } ) if ok { propertyDefaultValue , hasDefaultValue := propertyValuesMap [ " " ] if hasDefaultValue { servicePlanParameter . HasDefaultValue = true servicePlanParameter . DefaultValue = propertyDefaultValue } if propertyType , ok := propertyValuesMap [ " " ] ; ok { servicePlanParameter . Type = fmt . Sprintf ( " " , propertyType ) } } allServicePlanParameters = append ( allServicePlanParameters , servicePlanParameter ) } plan . Parameters = allServicePlanParameters } } plans = append ( plans , plan ) } return service , plans , nil }
GetServiceClassAndPlans returns the service class details with the associated plans
serviceName is the name of the service class
the first parameter returned is the ServiceClass object
the second parameter returned is the array of ServicePlans associated with the service class
GetServiceClassAndPlans returns the service class details with the associated plans serviceName is the name of the service class the first parameter returned is the ServiceClass object the second parameter returned is the array of ServicePlans associated with the service class
// get the plans according to the service name // get the display name from the external meta data // get the create parameters // record the values specified in the "required" field // these parameters are not trully required from a user perspective // since some of the parameters might have default values // set the optional properties by inspecting additionalProperties // and if it's true, then // we set the Required flag is the name of parameter // is one of the parameters indicated as required // the type of the "type" value is always a string, so we just convert it