Skip to content
Snippets Groups Projects
Commit 6ff55def authored by Jon Liu's avatar Jon Liu
Browse files

feat: setModuleProperties mutation

parent 89ce0c1b
No related branches found
No related tags found
1 merge request!6feat: get and set property
Pipeline #57135550 failed
......@@ -83,7 +83,7 @@ type Mutation {
# Set a module's property
setModuleProperty(serviceName: String!, moduleName: String!, property: PropertyInput!): Property
# Set a module's properties
# setModuleProperties(moduleName: String!, properties: [PropertyInput]): [Property]
setModuleProperties(serviceName: String!, moduleName: String!, properties: [PropertyInput]): [Property]
}
input ConfigurationInput {
......
......@@ -3,6 +3,7 @@ package resolver
import (
"errors"
"fmt"
"strings"
//"encoding/json"
//"log"
......@@ -79,3 +80,42 @@ func (r *Resolver) SetModuleProperty(ctx context.Context, args struct {
return &propertyResolver{property}, nil
}
func (r *Resolver) SetModuleProperties(ctx context.Context, args struct {
ServiceName string
ModuleName string
Properties *[]*propertyInput
}) (*[]*propertyResolver, error) {
if isAuthorized := ctx.Value("is_authorized").(bool); !isAuthorized {
return nil, errors.New(gcontext.CredentialsError)
}
// Convert the properties key and value into comma delimited strings
var bufKey strings.Builder
var bufValue strings.Builder
reqProperties := *(args.Properties)
for i := range reqProperties {
bufKey.WriteString((*reqProperties[i]).Key)
bufKey.WriteByte(',')
bufValue.WriteString((*reqProperties[i]).Value)
bufValue.WriteByte(',')
}
// Take the output and remove the last comma delimiter
reqKeys := strings.TrimSuffix(bufKey.String(), ",")
reqValues := strings.TrimSuffix(bufValue.String(), ",")
// Use the broker service
properties, err := ctx.Value("brokerService").(*service.BrokerService).SetModuleProperties(args.ServiceName, args.ModuleName, reqKeys, reqValues)
if err != nil {
fmt.Errorf("SetModuleProperties error: ", err)
return nil, err
}
// Create property resolvers for each property
var _properties []*propertyResolver
for _, property := range properties {
_properties = append(_properties, &propertyResolver{property})
}
return &_properties, nil
}
......@@ -361,15 +361,16 @@ func (b *BrokerService) GetModuleProperty(serviceName string, moduleID string, p
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create the property request
request := &pb.PropertyRequest{
Id: moduleID,
Key: propertyName,
}
// Send the request to the specified service
response, err := b.clients[serviceName].GetModuleProperty(ctx, request)
if err != nil {
return nil, err
}
// Convert the protobuf property into a model property
_property := &model.Property{
Key: response.GetProperty().GetKey(),
......@@ -416,16 +417,17 @@ func (b *BrokerService) SetModuleProperty(serviceName string, moduleID string, p
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create the property request
request := &pb.PropertyRequest{
Id: moduleID,
Key: property.GetKey(),
Value: property.GetValue(),
}
// Send the request to the specified service
response, err := b.clients[serviceName].SetModuleProperty(ctx, request)
if err != nil {
return nil, err
}
// Convert the protobuf property into a model property
_property := &model.Property{
Key: response.GetProperty().GetKey(),
......@@ -434,3 +436,35 @@ func (b *BrokerService) SetModuleProperty(serviceName string, moduleID string, p
return _property, nil
}
// rpc SetModuleProperties(PropertiesRequest) returns (PropertiesResponse);
func (b *BrokerService) SetModuleProperties(serviceName string, moduleID string, propertyKeys string, propertyValues string) ([]*model.Property, error) {
b.log.Debugf("Sending gRPC request for SetModuleProperties to %s/%s for %s/%s", serviceName, moduleID, propertyKeys, propertyValues)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create the properties request
request := &pb.PropertiesRequest{
Id: moduleID,
Keys: propertyKeys,
Values: propertyValues,
}
// Send the request to the specified service
response, err := b.clients[serviceName].SetModuleProperties(ctx, request)
if err != nil {
return nil, err
}
// Convert the protobuf properties into an array of model property
var convertedProperties []*model.Property
responseProperties := response.GetProperties()
for _, property := range responseProperties {
log.Print(*property)
tempProperty := &model.Property{
Key: property.GetKey(),
Value: property.GetValue(),
}
convertedProperties = append(convertedProperties, tempProperty)
}
return convertedProperties, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment