From f30c95a301d14d0aa91ba32ea06dc7a3ec906422 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Tue, 1 May 2018 23:06:37 +0530 Subject: [PATCH] Add etcd handling for web-handler methods --- cmd/generic-handlers.go | 2 +- cmd/typed-errors.go | 3 ++ cmd/web-handlers.go | 63 +++++++++++++++++++++++++++----- docs/federation/lookup/README.md | 2 +- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/cmd/generic-handlers.go b/cmd/generic-handlers.go index 3c7d447f9..fe13f60b7 100644 --- a/cmd/generic-handlers.go +++ b/cmd/generic-handlers.go @@ -624,7 +624,7 @@ type bucketForwardingHandler struct { } func (f bucketForwardingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if globalDNSConfig == nil || globalDomainName == "" { + if globalDNSConfig == nil || globalDomainName == "" || guessIsBrowserReq(r) || guessIsHealthCheckReq(r) || guessIsMetricsReq(r) || guessIsRPCReq(r) { f.handler.ServeHTTP(w, r) return } diff --git a/cmd/typed-errors.go b/cmd/typed-errors.go index af2de7b6b..f261f2ffc 100644 --- a/cmd/typed-errors.go +++ b/cmd/typed-errors.go @@ -67,3 +67,6 @@ var errNotFirstDisk = errors.New("Not first disk") // error returned by first disk waiting to initialize other servers. var errFirstDiskWait = errors.New("Waiting on other disks") + +// error returned when a bucket already exists +var errBucketAlreadyExists = errors.New("Your previous request to create the named bucket succeeded and you already own it") diff --git a/cmd/web-handlers.go b/cmd/web-handlers.go index 330122f2d..21e18e7f4 100644 --- a/cmd/web-handlers.go +++ b/cmd/web-handlers.go @@ -31,6 +31,7 @@ import ( "strings" "time" + etcd "github.com/coreos/etcd/client" humanize "github.com/dustin/go-humanize" "github.com/gorilla/mux" "github.com/gorilla/rpc/v2/json2" @@ -135,6 +136,26 @@ func (web *webAPIHandlers) MakeBucket(r *http.Request, args *MakeBucketArgs, rep return toJSONError(errInvalidBucketName) } + if globalDNSConfig != nil { + if _, err := globalDNSConfig.Get(args.BucketName); err != nil { + if etcd.IsKeyNotFound(err) { + // Proceed to creating a bucket. + if err = objectAPI.MakeBucketWithLocation(context.Background(), args.BucketName, globalServerConfig.GetRegion()); err != nil { + return toJSONError(err) + } + if err = globalDNSConfig.Put(args.BucketName); err != nil { + objectAPI.DeleteBucket(context.Background(), args.BucketName) + return toJSONError(err) + } + + reply.UIVersion = browser.UIVersion + return nil + } + return toJSONError(err) + } + return toJSONError(errBucketAlreadyExists) + } + if err := objectAPI.MakeBucketWithLocation(context.Background(), args.BucketName, globalServerConfig.GetRegion()); err != nil { return toJSONError(err, args.BucketName) } @@ -176,6 +197,14 @@ func (web *webAPIHandlers) DeleteBucket(r *http.Request, args *RemoveBucketArgs, logger.LogIf(ctx, nerr.Err) } + if globalDNSConfig != nil { + if err := globalDNSConfig.Delete(args.BucketName); err != nil { + // Deleting DNS entry failed, attempt to create the bucket again. + objectAPI.MakeBucketWithLocation(ctx, args.BucketName, "") + return toJSONError(err) + } + } + reply.UIVersion = browser.UIVersion return nil } @@ -208,16 +237,32 @@ func (web *webAPIHandlers) ListBuckets(r *http.Request, args *WebGenericArgs, re if authErr != nil { return toJSONError(authErr) } - buckets, err := listBuckets(context.Background()) - if err != nil { - return toJSONError(err) - } - for _, bucket := range buckets { - reply.Buckets = append(reply.Buckets, WebBucketInfo{ - Name: bucket.Name, - CreationDate: bucket.Created, - }) + // If etcd, dns federation configured list buckets from etcd. + if globalDNSConfig != nil { + dnsBuckets, err := globalDNSConfig.List() + if err != nil { + return toJSONError(err) + } + for _, dnsRecord := range dnsBuckets { + bucketName := strings.Trim(dnsRecord.Key, "/") + reply.Buckets = append(reply.Buckets, WebBucketInfo{ + Name: bucketName, + CreationDate: dnsRecord.CreationDate, + }) + } + } else { + buckets, err := listBuckets(context.Background()) + if err != nil { + return toJSONError(err) + } + for _, bucket := range buckets { + reply.Buckets = append(reply.Buckets, WebBucketInfo{ + Name: bucket.Name, + CreationDate: bucket.Created, + }) + } } + reply.UIVersion = browser.UIVersion return nil } diff --git a/docs/federation/lookup/README.md b/docs/federation/lookup/README.md index 2cf7ee553..64bb3e8f1 100644 --- a/docs/federation/lookup/README.md +++ b/docs/federation/lookup/README.md @@ -42,7 +42,7 @@ NOTE: `mybucket` only exists on one cluster either `cluster1` or `cluster2` this is decided by how `domain.com` gets resolved, if there is a round-robin DNS on `domain.com` then it is randomized which cluster might provision the bucket. -TODO: For now the control to create the bucket from a client to the right cluster using `region` paramter +TODO: For now the control to create the bucket from a client to the right cluster using `region` parameter is not implemented yet.