github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/connectors/connectors.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package connectors
    20  
    21  import (
    22  	"github.com/rs/zerolog"
    23  
    24  	"github.com/cs3org/reva/v2/pkg/mentix/config"
    25  	"github.com/cs3org/reva/v2/pkg/mentix/entity"
    26  )
    27  
    28  var (
    29  	registeredConnectors = entity.NewRegistry()
    30  )
    31  
    32  // Collection represents a collection of connectors.
    33  type Collection struct {
    34  	Connectors []Connector
    35  }
    36  
    37  // Entities gets the entities in this collection.
    38  func (collection *Collection) Entities() []entity.Entity {
    39  	entities := make([]entity.Entity, 0, len(collection.Connectors))
    40  	for _, connector := range collection.Connectors {
    41  		entities = append(entities, connector)
    42  	}
    43  	return entities
    44  }
    45  
    46  // ActivateAll activates all entities in the collection.
    47  func (collection *Collection) ActivateAll(conf *config.Configuration, log *zerolog.Logger) error {
    48  	return entity.ActivateEntities(collection, conf, log)
    49  }
    50  
    51  // AvailableConnectors returns a collection of all connectors that are enabled in the configuration.
    52  func AvailableConnectors(conf *config.Configuration) (*Collection, error) {
    53  	entities, err := registeredConnectors.FindEntities(conf.EnabledConnectors, true, true)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	connectors := make([]Connector, 0, len(entities))
    59  	for _, entry := range entities {
    60  		connectors = append(connectors, entry.(Connector))
    61  	}
    62  
    63  	return &Collection{Connectors: connectors}, nil
    64  }
    65  
    66  func registerConnector(connector Connector) {
    67  	registeredConnectors.Register(connector)
    68  }