//
// AddService adds a new services to store. It validates the name
// uniqueness and the IPVS uniqueness by saving the IPVS key
// in the store, which consists in a combination of address, port
// and protocol.
AddService adds a new services to store. It validates the name
uniqueness and the IPVS uniqueness by saving the IPVS key
in the store, which consists in a combination of address, port
and protocol.
// // AddService adds a new services to store . It validates the name // uniqueness and the IPVS uniqueness by saving the IPVS key // in the store which consists in a combination of address port // and protocol .
func (s *FusisStore) AddService(svc *types.Service) error {
	svcKey := s.key("services", svc.GetId())
	ipvsKey := s.key("ipvs-ids", "services", svc.IpvsId())
	if err := s.validateService(svc); err != nil {
		return err
	}
	value, err := json.Marshal(svc)
	if err != nil {
		return errors.Wrapf(err, "[store] Error marshaling service: %v", svc)
	}
	svcCmp := notFound(svcKey)
	ipvsCmp := notFound(ipvsKey)
	svcPut := clientv3.OpPut(svcKey, string(value))
	ipvsPut := clientv3.OpPut(ipvsKey, "true")
	resp, err := s.kv.Txn(context.TODO()).
		If(svcCmp, ipvsCmp).
		Then(svcPut, ipvsPut).
		Commit()
	if err != nil {
		return errors.Wrapf(err, "[store] Error sending service to Etcd: %v", svc)
	}
	if resp.Succeeded == false {
		return types.ErrValidation{Type: "service", Errors: map[string]string{"ipvs": "service must be unique"}}
	}
	return nil
}
func ( s * FusisStore ) AddService ( svc * types . Service ) error { svcKey := s . key ( " " , svc . GetId ( ) ) ipvsKey := s . key ( " " , " " , svc . IpvsId ( ) ) if err := s . validateService ( svc ) ; err != nil { return err } value , err := json . Marshal ( svc ) if err != nil { return errors . Wrapf ( err , " " , svc ) } svcCmp := notFound ( svcKey ) ipvsCmp := notFound ( ipvsKey ) svcPut := clientv3 . OpPut ( svcKey , string ( value ) ) ipvsPut := clientv3 . OpPut ( ipvsKey , " " ) resp , err := s . kv . Txn ( context . TODO ( ) ) . If ( svcCmp , ipvsCmp ) . Then ( svcPut , ipvsPut ) . Commit ( ) if err != nil { return errors . Wrapf ( err , " " , svc ) } if resp . Succeeded == false { return types . ErrValidation { Type : " " , Errors : map [ string ] string { " " : " " } } } return nil }
AddService adds a new services to store.
AddService adds a new services to store .
// Validating service // resp.Succeeded means the compare clause was true