Improves flexibility of configuration handling for S3 driver
Treats nil parameters the same as unprovided parameters (fixes issues where certain parameters are printed to "<nil>"). Accepts "true" and "false" string values for boolean parameters. Signed-off-by: Brian Bland <brian.bland@docker.com>master
							parent
							
								
									3f97c258a6
								
							
						
					
					
						commit
						52b4a328dd
					
				| 
						 | 
					@ -107,17 +107,18 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
 | 
				
			||||||
	// Providing no values for these is valid in case the user is authenticating
 | 
						// Providing no values for these is valid in case the user is authenticating
 | 
				
			||||||
	// with an IAM on an ec2 instance (in which case the instance credentials will
 | 
						// with an IAM on an ec2 instance (in which case the instance credentials will
 | 
				
			||||||
	// be summoned when GetAuth is called)
 | 
						// be summoned when GetAuth is called)
 | 
				
			||||||
	accessKey, ok := parameters["accesskey"]
 | 
						accessKey := parameters["accesskey"]
 | 
				
			||||||
	if !ok {
 | 
						if accessKey == nil {
 | 
				
			||||||
		accessKey = ""
 | 
							accessKey = ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	secretKey, ok := parameters["secretkey"]
 | 
					
 | 
				
			||||||
	if !ok {
 | 
						secretKey := parameters["secretkey"]
 | 
				
			||||||
 | 
						if secretKey == nil {
 | 
				
			||||||
		secretKey = ""
 | 
							secretKey = ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	regionName, ok := parameters["region"]
 | 
						regionName := parameters["region"]
 | 
				
			||||||
	if !ok || fmt.Sprint(regionName) == "" {
 | 
						if regionName == nil || fmt.Sprint(regionName) == "" {
 | 
				
			||||||
		return nil, fmt.Errorf("No region parameter provided")
 | 
							return nil, fmt.Errorf("No region parameter provided")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	region := aws.GetRegion(fmt.Sprint(regionName))
 | 
						region := aws.GetRegion(fmt.Sprint(regionName))
 | 
				
			||||||
| 
						 | 
					@ -125,41 +126,64 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
 | 
				
			||||||
		return nil, fmt.Errorf("Invalid region provided: %v", region)
 | 
							return nil, fmt.Errorf("Invalid region provided: %v", region)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bucket, ok := parameters["bucket"]
 | 
						bucket := parameters["bucket"]
 | 
				
			||||||
	if !ok || fmt.Sprint(bucket) == "" {
 | 
						if bucket == nil || fmt.Sprint(bucket) == "" {
 | 
				
			||||||
		return nil, fmt.Errorf("No bucket parameter provided")
 | 
							return nil, fmt.Errorf("No bucket parameter provided")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	encryptBool := false
 | 
						encryptBool := false
 | 
				
			||||||
	encrypt, ok := parameters["encrypt"]
 | 
						encrypt := parameters["encrypt"]
 | 
				
			||||||
	if ok {
 | 
						switch encrypt := encrypt.(type) {
 | 
				
			||||||
		encryptBool, ok = encrypt.(bool)
 | 
						case string:
 | 
				
			||||||
		if !ok {
 | 
							b, err := strconv.ParseBool(encrypt)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
			return nil, fmt.Errorf("The encrypt parameter should be a boolean")
 | 
								return nil, fmt.Errorf("The encrypt parameter should be a boolean")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							encryptBool = b
 | 
				
			||||||
 | 
						case bool:
 | 
				
			||||||
 | 
							encryptBool = encrypt
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							// do nothing
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("The encrypt parameter should be a boolean")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	secureBool := true
 | 
						secureBool := true
 | 
				
			||||||
	secure, ok := parameters["secure"]
 | 
						secure := parameters["secure"]
 | 
				
			||||||
	if ok {
 | 
						switch secure := secure.(type) {
 | 
				
			||||||
		secureBool, ok = secure.(bool)
 | 
						case string:
 | 
				
			||||||
		if !ok {
 | 
							b, err := strconv.ParseBool(secure)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
			return nil, fmt.Errorf("The secure parameter should be a boolean")
 | 
								return nil, fmt.Errorf("The secure parameter should be a boolean")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							secureBool = b
 | 
				
			||||||
 | 
						case bool:
 | 
				
			||||||
 | 
							secureBool = secure
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							// do nothing
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("The secure parameter should be a boolean")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	v4AuthBool := false
 | 
						v4AuthBool := false
 | 
				
			||||||
	v4Auth, ok := parameters["v4auth"]
 | 
						v4Auth := parameters["v4auth"]
 | 
				
			||||||
	if ok {
 | 
						switch v4Auth := v4Auth.(type) {
 | 
				
			||||||
		v4AuthBool, ok = v4Auth.(bool)
 | 
						case string:
 | 
				
			||||||
		if !ok {
 | 
							b, err := strconv.ParseBool(v4Auth)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
			return nil, fmt.Errorf("The v4auth parameter should be a boolean")
 | 
								return nil, fmt.Errorf("The v4auth parameter should be a boolean")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							v4AuthBool = b
 | 
				
			||||||
 | 
						case bool:
 | 
				
			||||||
 | 
							v4AuthBool = v4Auth
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							// do nothing
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("The v4auth parameter should be a boolean")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	chunkSize := int64(defaultChunkSize)
 | 
						chunkSize := int64(defaultChunkSize)
 | 
				
			||||||
	chunkSizeParam, ok := parameters["chunksize"]
 | 
						chunkSizeParam := parameters["chunksize"]
 | 
				
			||||||
	if ok {
 | 
					 | 
				
			||||||
	switch v := chunkSizeParam.(type) {
 | 
						switch v := chunkSizeParam.(type) {
 | 
				
			||||||
	case string:
 | 
						case string:
 | 
				
			||||||
		vv, err := strconv.ParseInt(v, 0, 64)
 | 
							vv, err := strconv.ParseInt(v, 0, 64)
 | 
				
			||||||
| 
						 | 
					@ -171,6 +195,8 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
 | 
				
			||||||
		chunkSize = v
 | 
							chunkSize = v
 | 
				
			||||||
	case int, uint, int32, uint32, uint64:
 | 
						case int, uint, int32, uint32, uint64:
 | 
				
			||||||
		chunkSize = reflect.ValueOf(v).Convert(reflect.TypeOf(chunkSize)).Int()
 | 
							chunkSize = reflect.ValueOf(v).Convert(reflect.TypeOf(chunkSize)).Int()
 | 
				
			||||||
 | 
						case nil:
 | 
				
			||||||
 | 
							// do nothing
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		return nil, fmt.Errorf("invalid value for chunksize: %#v", chunkSizeParam)
 | 
							return nil, fmt.Errorf("invalid value for chunksize: %#v", chunkSizeParam)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -178,16 +204,15 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
 | 
				
			||||||
	if chunkSize < minChunkSize {
 | 
						if chunkSize < minChunkSize {
 | 
				
			||||||
		return nil, fmt.Errorf("The chunksize %#v parameter should be a number that is larger than or equal to %d", chunkSize, minChunkSize)
 | 
							return nil, fmt.Errorf("The chunksize %#v parameter should be a number that is larger than or equal to %d", chunkSize, minChunkSize)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rootDirectory, ok := parameters["rootdirectory"]
 | 
						rootDirectory := parameters["rootdirectory"]
 | 
				
			||||||
	if !ok {
 | 
						if rootDirectory == nil {
 | 
				
			||||||
		rootDirectory = ""
 | 
							rootDirectory = ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	storageClass := s3.StandardStorage
 | 
						storageClass := s3.StandardStorage
 | 
				
			||||||
	storageClassParam, ok := parameters["storageclass"]
 | 
						storageClassParam := parameters["storageclass"]
 | 
				
			||||||
	if ok {
 | 
						if storageClassParam != nil {
 | 
				
			||||||
		storageClassString, ok := storageClassParam.(string)
 | 
							storageClassString, ok := storageClassParam.(string)
 | 
				
			||||||
		if !ok {
 | 
							if !ok {
 | 
				
			||||||
			return nil, fmt.Errorf("The storageclass parameter must be one of %v, %v invalid", []s3.StorageClass{s3.StandardStorage, s3.ReducedRedundancy}, storageClassParam)
 | 
								return nil, fmt.Errorf("The storageclass parameter must be one of %v, %v invalid", []s3.StorageClass{s3.StandardStorage, s3.ReducedRedundancy}, storageClassParam)
 | 
				
			||||||
| 
						 | 
					@ -200,8 +225,8 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
 | 
				
			||||||
		storageClass = storageClassCasted
 | 
							storageClass = storageClassCasted
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	userAgent, ok := parameters["useragent"]
 | 
						userAgent := parameters["useragent"]
 | 
				
			||||||
	if !ok {
 | 
						if userAgent == nil {
 | 
				
			||||||
		userAgent = ""
 | 
							userAgent = ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue