github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/backend/dynamic/dynamic.go (about)

     1  package dynamic
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/rancher/longhorn/types"
     8  )
     9  
    10  type Factory struct {
    11  	factories map[string]types.BackendFactory
    12  }
    13  
    14  func New(factories map[string]types.BackendFactory) types.BackendFactory {
    15  	return &Factory{
    16  		factories: factories,
    17  	}
    18  }
    19  
    20  func (d *Factory) Create(address string) (types.Backend, error) {
    21  	parts := strings.SplitN(address, "://", 2)
    22  
    23  	if len(parts) == 2 {
    24  		if factory, ok := d.factories[parts[0]]; ok {
    25  			return factory.Create(parts[1])
    26  		}
    27  	}
    28  
    29  	return nil, fmt.Errorf("Failed to find factory for %s", address)
    30  }