[ISSUE-67015020] address comments, use apid-core utils
diff --git a/api.go b/api.go index 746a11c..4ffc12e 100644 --- a/api.go +++ b/api.go
@@ -23,7 +23,6 @@ "io/ioutil" "net/http" "net/url" - "regexp" "strconv" "sync/atomic" "time" @@ -241,10 +240,6 @@ func (a *apiManager) apiHandleConfigId(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) configId := vars["configId"] - if !isValidUuid(configId) { - a.writeError(w, http.StatusBadRequest, API_ERR_BAD_CONFIG_ID, "configuration id is invalid") - return - } config, err := a.dbMan.getConfigById(configId) if err != nil { if err == sql.ErrNoRows { @@ -433,11 +428,6 @@ return t } -func isValidUuid(uuid string) bool { - r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$") - return r.MatchString(uuid) -} - func getHttpHost() string { configuredEndpoint := config.GetString(configBundleBlobDownloadEndpoint)
diff --git a/api_test.go b/api_test.go index c5454c1..84c1cb5 100644 --- a/api_test.go +++ b/api_test.go
@@ -14,17 +14,16 @@ package apiGatewayConfDeploy import ( - "encoding/json" - "io/ioutil" - "net/http" - "net/url" - - "crypto/rand" "database/sql" + "encoding/json" "fmt" + "github.com/apid/apid-core/util" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "io/ioutil" mathrand "math/rand" + "net/http" + "net/url" "os" "strconv" "strings" @@ -290,7 +289,7 @@ // set test data testFile, err := ioutil.TempFile(bundlePath, "test") - randString := GenerateUUID() + randString := util.GenerateUUID() testFile.Write([]byte(randString)) err = testFile.Close() Expect(err).Should(Succeed()) @@ -370,12 +369,10 @@ //setup test data testData := [][]interface{}{ - {"invalid-uuid", nil}, - {GenerateUUID(), sql.ErrNoRows}, - {GenerateUUID(), fmt.Errorf("test error")}, + {util.GenerateUUID(), sql.ErrNoRows}, + {util.GenerateUUID(), fmt.Errorf("test error")}, } expectedCode := []int{ - http.StatusBadRequest, http.StatusNotFound, http.StatusInternalServerError, } @@ -420,9 +417,9 @@ func makeTestDeployment() *DataDeployment { dep := &DataDeployment{ - ID: GenerateUUID(), - OrgID: GenerateUUID(), - EnvID: GenerateUUID(), + ID: util.GenerateUUID(), + OrgID: util.GenerateUUID(), + EnvID: util.GenerateUUID(), BlobID: testBlobId, BlobResourceID: "", Type: "virtual-host", @@ -504,16 +501,3 @@ func (d *dummyDbManager) getConfigById(id string) (*DataDeployment, error) { return d.configurations[id], d.err } - -func GenerateUUID() string { - - buff := make([]byte, 16) - numRead, err := rand.Read(buff) - if numRead != len(buff) || err != nil { - panic(err) - } - /* uuid v4 spec */ - buff[6] = (buff[6] | 0x40) & 0x4F - buff[8] = (buff[8] | 0x80) & 0xBF - return fmt.Sprintf("%x-%x-%x-%x-%x", buff[0:4], buff[4:6], buff[6:8], buff[8:10], buff[10:]) -}
diff --git a/bundle_test.go b/bundle_test.go index 7946f96..30760cc 100644 --- a/bundle_test.go +++ b/bundle_test.go
@@ -19,6 +19,7 @@ "bytes" "encoding/json" + "github.com/apid/apid-core/util" "github.com/gorilla/mux" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -96,7 +97,7 @@ It("should download blob according to id", func() { // download blob - id := GenerateUUID() + id := util.GenerateUUID() testBundleMan.enqueueRequest(testBundleMan.makeDownloadRequest(id)) received := <-dummyDbMan.fileResponse Expect(received).Should(Equal(id)) @@ -110,7 +111,7 @@ testBundleMan.bundleRetryDelay = 50 * time.Millisecond // download blobs - id := GenerateUUID() + id := util.GenerateUUID() testBundleMan.enqueueRequest(testBundleMan.makeDownloadRequest(id)) received := <-dummyDbMan.fileResponse Expect(received).Should(Equal(id)) @@ -126,7 +127,7 @@ testBundleMan.markDeploymentFailedAfter = 200 * time.Millisecond // download blobs - id := GenerateUUID() + id := util.GenerateUUID() req := testBundleMan.makeDownloadRequest(id) Expect(req.markFailedAt.After(time.Now())).Should(BeTrue()) testBundleMan.enqueueRequest(req)
diff --git a/listener_test.go b/listener_test.go index d270be0..55a71c5 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -17,6 +17,7 @@ import ( "fmt" "github.com/apid/apid-core" + "github.com/apid/apid-core/util" "github.com/apigee-labs/transicator/common" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -61,7 +62,7 @@ unreadyBlobIds := make([]string, 0) blobMap := make(map[string]int) for i := 0; i < 1+rand.Intn(10); i++ { - id := GenerateUUID() + id := util.GenerateUUID() blobMap[id] = 1 unreadyBlobIds = append(unreadyBlobIds, id) } @@ -172,12 +173,12 @@ blobIdOld := make(map[string]int) for i := 0; i < 1+rand.Intn(10); i++ { depNew := makeTestDeployment() - depNew.BlobID = GenerateUUID() - depNew.BlobResourceID = GenerateUUID() + depNew.BlobID = util.GenerateUUID() + depNew.BlobResourceID = util.GenerateUUID() depOld := makeTestDeployment() - depOld.BlobID = GenerateUUID() - depOld.BlobResourceID = GenerateUUID() + depOld.BlobID = util.GenerateUUID() + depOld.BlobResourceID = util.GenerateUUID() change := common.Change{ Operation: common.Update, @@ -221,14 +222,14 @@ for i := 0; i < 1+rand.Intn(10); i++ { depNew := makeTestDeployment() - depNew.BlobID = GenerateUUID() - depNew.BlobResourceID = GenerateUUID() + depNew.BlobID = util.GenerateUUID() + depNew.BlobResourceID = util.GenerateUUID() depOld := makeTestDeployment() if rand.Intn(2) == 0 { // blob id changed - depOld.BlobID = GenerateUUID() + depOld.BlobID = util.GenerateUUID() blobIdChangedNew[depNew.BlobID]++ blobIdChangedOld[depOld.BlobID]++ } else { @@ -238,7 +239,7 @@ if rand.Intn(2) == 0 { // blob id changed - depOld.BlobResourceID = GenerateUUID() + depOld.BlobResourceID = util.GenerateUUID() blobIdChangedNew[depNew.BlobResourceID]++ blobIdChangedOld[depOld.BlobResourceID]++ } else {