Esempio n. 1
0
File: rpc.go Progetto: jxaas/jxaas
// update_relation_properties RPC handler
func (self *EndpointRpcUpdateRelationProperties) HttpPost(huddle *core.Huddle, request *RpcUpdateRelationPropertiesRequest) (*RpcUpdateRelationPropertiesResponse, error) {
	// TODO: Validate that this is coming from one of our machines?

	log.Info("Got RPC request: UpdateRelationProperties: %v", request)

	response := &RpcUpdateRelationPropertiesResponse{}

	// Sanitize
	if request.Properties == nil {
		request.Properties = make(map[string]string)
	}

	localUnit := request.ServiceName
	if localUnit == "" {
		return nil, fmt.Errorf("ServiceName is required")
	}
	tenant, bundleTypeName, instanceId, _, _, err := core.ParseUnit(localUnit)
	if err != nil {
		return nil, err
	}

	bundleType := huddle.System.GetBundleType(bundleTypeName)
	if bundleType == nil {
		return nil, fmt.Errorf("Unknown bundle type: %v", bundleTypeName)
	}

	primaryService := bundleType.PrimaryJujuService()

	//	remoteUnit := request.RemoteName
	//	if remoteUnit == "" {
	//		// We're a bit stuck here.  We do have the relationId and other info,
	//		// we just don't have the remote relation, and we're storing the attributes on the remote relation
	//		// TODO: Infer the remote relation? (-stubclient to -primary)?
	//		log.Warn("No remote unit; can't remove relations")
	//		return response, nil
	//	}
	//
	//	_, _, remoteInstanceId, _, remoteUnitId, err := core.ParseUnit(remoteUnit)
	//	if err != nil {
	//		return nil, err
	//	}

	instance := huddle.NewInstance(tenant, bundleType, instanceId)

	relationId := request.RelationId

	if request.Action == "broken" {
		err = instance.DeleteRelationInfo(primaryService, relationId)
	} else {
		err = instance.SetRelationInfo(primaryService, relationId, request.Properties)
	}

	if err != nil {
		return nil, err
	}

	return response, nil
}