github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/backend/swift/config.go (about)

     1  package swift
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/restic/restic/internal/errors"
     8  	"github.com/restic/restic/internal/options"
     9  )
    10  
    11  // Config contains basic configuration needed to specify swift location for a swift server
    12  type Config struct {
    13  	UserName     string
    14  	Domain       string
    15  	APIKey       string
    16  	AuthURL      string
    17  	Region       string
    18  	Tenant       string
    19  	TenantID     string
    20  	TenantDomain string
    21  	TrustID      string
    22  
    23  	StorageURL string
    24  	AuthToken  string
    25  
    26  	Container              string
    27  	Prefix                 string
    28  	DefaultContainerPolicy string
    29  
    30  	Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
    31  }
    32  
    33  func init() {
    34  	options.Register("swift", Config{})
    35  }
    36  
    37  // NewConfig returns a new config with the default values filled in.
    38  func NewConfig() Config {
    39  	return Config{
    40  		Connections: 5,
    41  	}
    42  }
    43  
    44  // ParseConfig parses the string s and extract swift's container name and prefix.
    45  func ParseConfig(s string) (interface{}, error) {
    46  	data := strings.SplitN(s, ":", 3)
    47  	if len(data) != 3 {
    48  		return nil, errors.New("invalid URL, expected: swift:container-name:/[prefix]")
    49  	}
    50  
    51  	scheme, container, prefix := data[0], data[1], data[2]
    52  	if scheme != "swift" {
    53  		return nil, errors.Errorf("unexpected prefix: %s", data[0])
    54  	}
    55  
    56  	if len(prefix) == 0 {
    57  		return nil, errors.Errorf("prefix is empty")
    58  	}
    59  
    60  	if prefix[0] != '/' {
    61  		return nil, errors.Errorf("prefix does not start with slash (/)")
    62  	}
    63  	prefix = prefix[1:]
    64  
    65  	cfg := NewConfig()
    66  	cfg.Container = container
    67  	cfg.Prefix = prefix
    68  
    69  	return cfg, nil
    70  }
    71  
    72  // ApplyEnvironment saves values from the environment to the config.
    73  func ApplyEnvironment(prefix string, cfg interface{}) error {
    74  	c := cfg.(*Config)
    75  	for _, val := range []struct {
    76  		s   *string
    77  		env string
    78  	}{
    79  		// v2/v3 specific
    80  		{&c.UserName, prefix + "OS_USERNAME"},
    81  		{&c.APIKey, prefix + "OS_PASSWORD"},
    82  		{&c.Region, prefix + "OS_REGION_NAME"},
    83  		{&c.AuthURL, prefix + "OS_AUTH_URL"},
    84  
    85  		// v3 specific
    86  		{&c.Domain, prefix + "OS_USER_DOMAIN_NAME"},
    87  		{&c.Tenant, prefix + "OS_PROJECT_NAME"},
    88  		{&c.TenantDomain, prefix + "OS_PROJECT_DOMAIN_NAME"},
    89  
    90  		// v2 specific
    91  		{&c.TenantID, prefix + "OS_TENANT_ID"},
    92  		{&c.Tenant, prefix + "OS_TENANT_NAME"},
    93  
    94  		// v1 specific
    95  		{&c.AuthURL, prefix + "ST_AUTH"},
    96  		{&c.UserName, prefix + "ST_USER"},
    97  		{&c.APIKey, prefix + "ST_KEY"},
    98  
    99  		// Manual authentication
   100  		{&c.StorageURL, prefix + "OS_STORAGE_URL"},
   101  		{&c.AuthToken, prefix + "OS_AUTH_TOKEN"},
   102  
   103  		{&c.DefaultContainerPolicy, prefix + "SWIFT_DEFAULT_CONTAINER_POLICY"},
   104  	} {
   105  		if *val.s == "" {
   106  			*val.s = os.Getenv(val.env)
   107  		}
   108  	}
   109  	return nil
   110  }