Skip to content

Commit

Permalink
Expose ServicePlan SB schema via ui-api-layer (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
polskikiel authored Sep 28, 2018
1 parent 62d32c0 commit 1d385c0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ func (p *servicePlanConverter) ToGQL(item *v1beta1.ServicePlan) (*gqlschema.Serv

var instanceCreateParameterSchema *gqlschema.JSON
if item.Spec.ServiceInstanceCreateParameterSchema != nil {
unpackedSchema, err := p.unpackInstanceCreateParameterSchema(item.Spec.ServiceInstanceCreateParameterSchema.Raw)
unpackedSchema, err := p.unpackCreateParameterSchema(item.Spec.ServiceInstanceCreateParameterSchema.Raw)
if err != nil {
return nil, p.wrapConversionError(err, item.Name)
return nil, errors.Wrapf(err, "while unpacking service instance create parameter schema from ServicePlan [%s]", item.Name)
}
instanceCreateParameterSchema = &unpackedSchema
}
var bindingCreateParameterSchema *gqlschema.JSON
if item.Spec.ServiceBindingCreateParameterSchema != nil {
unpackedSchema, err := p.unpackCreateParameterSchema(item.Spec.ServiceBindingCreateParameterSchema.Raw)
if err != nil {
return nil, errors.Wrapf(err, "while unpacking service binding create parameter schema from ServicePlan [%s]", item.Name)
}
bindingCreateParameterSchema = &unpackedSchema
}

displayName := resource.ToStringPtr(externalMetadata["displayName"])
plan := gqlschema.ServicePlan{
Expand All @@ -44,6 +52,7 @@ func (p *servicePlanConverter) ToGQL(item *v1beta1.ServicePlan) (*gqlschema.Serv
Description: item.Spec.Description,
RelatedServiceClassName: item.Spec.ServiceClassRef.Name,
InstanceCreateParameterSchema: instanceCreateParameterSchema,
BindingCreateParameterSchema: bindingCreateParameterSchema,
}

return &plan, nil
Expand All @@ -68,26 +77,26 @@ func (p *servicePlanConverter) wrapConversionError(err error, name string) error
return errors.Wrapf(err, "while converting item %s to ServicePlan", name)
}

func (p *servicePlanConverter) unpackInstanceCreateParameterSchema(raw []byte) (gqlschema.JSON, error) {
func (p *servicePlanConverter) unpackCreateParameterSchema(raw []byte) (gqlschema.JSON, error) {
if len(raw) == 0 {
return nil, nil
}

decoded := make([]byte, base64.StdEncoding.DecodedLen(len(raw)))
_, err := base64.StdEncoding.Decode(decoded, raw)
if err != nil {
return p.extractInstanceCreateSchema(raw)
return p.extractCreateSchema(raw)
}

return p.extractInstanceCreateSchema(decoded)
return p.extractCreateSchema(decoded)
}

func (p *servicePlanConverter) extractInstanceCreateSchema(raw []byte) (map[string]interface{}, error) {
func (p *servicePlanConverter) extractCreateSchema(raw []byte) (map[string]interface{}, error) {
extracted := make(map[string]interface{})

err := json.Unmarshal(raw, &extracted)
if err != nil {
return nil, errors.Wrap(err, "while extracting instance creation parameter schema")
return nil, errors.Wrap(err, "while extracting creation parameter schema")
}

return extracted, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func TestServicePlanConverter_ToGQL(t *testing.T) {
ServiceInstanceCreateParameterSchema: &runtime.RawExtension{
Raw: encodedParameterSchemaBytes,
},
ServiceBindingCreateParameterSchema: &runtime.RawExtension{
Raw: encodedParameterSchemaBytes,
},
},
ServiceClassRef: v1beta1.LocalObjectReference{
Name: "serviceClassRef",
Expand All @@ -65,6 +68,7 @@ func TestServicePlanConverter_ToGQL(t *testing.T) {
DisplayName: &displayName,
ExternalName: "ExampleExternalName",
InstanceCreateParameterSchema: parameterSchemaJSON,
BindingCreateParameterSchema: parameterSchemaJSON,
}

result, err := converter.ToGQL(&clusterServicePlan)
Expand Down Expand Up @@ -156,6 +160,9 @@ func fixServicePlan(t require.TestingT) *v1beta1.ServicePlan {
ServiceInstanceCreateParameterSchema: &runtime.RawExtension{
Raw: encodedParameterSchemaBytes,
},
ServiceBindingCreateParameterSchema: &runtime.RawExtension{
Raw: encodedParameterSchemaBytes,
},
},
ServiceClassRef: v1beta1.LocalObjectReference{
Name: "serviceClassRef",
Expand Down
1 change: 1 addition & 0 deletions components/ui-api-layer/internal/gqlschema/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/ui-api-layer/internal/gqlschema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type ServicePlan {
description: String!
relatedServiceClassName: String!
instanceCreateParameterSchema: JSON
bindingCreateParameterSchema: JSON
}

type ClusterServicePlan {
Expand Down
34 changes: 34 additions & 0 deletions components/ui-api-layer/internal/gqlschema/schema_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d385c0

Please sign in to comment.