Fix issue with JSON formatting, redo main cmd to simply accept a file containing expected deployments
diff --git a/README.md b/README.md index aef30fa..c5bf99a 100644 --- a/README.md +++ b/README.md
@@ -30,10 +30,35 @@ Command line options: -* -bundle <file path> -config <file path> +* -deployments <file path> -If you use the `-bundle` option, the server will start using a clean database that contains only the - deployment bundle specified. +If you use the `-deployments` option, the server will start using a clean database that contains only the + deployments contained in the file specified. + +The file should be the JSON for an array of deployments. JSON format is: + + [ + { + "id": "", + "scopeId": "", + "created": "", + "createdBy": "", + "updated": "", + "updatedBy": "", + "configuration": { + "key": "value" + }, + "bundleConfiguration": { + "key": "value" + }, + "displayName": "", + "uri": "" + } + ] + +Notes: +* id must be unique +* uri should point to a bundle file Once the process is running, you should be able to manually give the plugin's API a whirl...
diff --git a/api.go b/api.go index fed9e55..8a54a31 100644 --- a/api.go +++ b/api.go
@@ -28,20 +28,21 @@ Reason string `json:"reason"` } -type apiDeployment struct { - ID string `json:"id"` - ScopeId string `json:"scopeId"` - Created string `json:"created"` - CreatedBy string `json:"createdBy"` - Updated string `json:"updated"` - UpdatedBy string `json:"updatedBy"` - ConfigurationJson string `json:"configurationJson"` - DisplayName string `json:"displayName"` - URI string `json:"uri"` +type ApiDeployment struct { + ID string `json:"id"` + ScopeId string `json:"scopeId"` + Created string `json:"created"` + CreatedBy string `json:"createdBy"` + Updated string `json:"updated"` + UpdatedBy string `json:"updatedBy"` + ConfigJson json.RawMessage `json:"configuration"` + BundleConfigJson json.RawMessage `json:"bundleConfiguration"` + DisplayName string `json:"displayName"` + URI string `json:"uri"` } // sent to client -type apiDeploymentResponse []apiDeployment +type ApiDeploymentResponse []ApiDeployment type apiDeploymentResult struct { ID string `json:"id"` @@ -184,17 +185,18 @@ func sendDeployments(w http.ResponseWriter, dataDeps []DataDeployment, eTag string) { - var apiDeps apiDeploymentResponse + var apiDeps ApiDeploymentResponse for _, d := range dataDeps { - apiDeps = append(apiDeps, apiDeployment{ + apiDeps = append(apiDeps, ApiDeployment{ ID: d.ID, ScopeId: d.DataScopeID, Created: d.Created, CreatedBy: d.CreatedBy, Updated: d.Updated, UpdatedBy: d.UpdatedBy, - ConfigurationJson: d.ConfigJSON, + BundleConfigJson: []byte(d.BundleConfigJSON), + ConfigJson: []byte(d.ConfigJSON), DisplayName: d.BundleName, URI: d.LocalBundleURI, })
diff --git a/api_test.go b/api_test.go index be29bd1..cb8ec38 100644 --- a/api_test.go +++ b/api_test.go
@@ -44,7 +44,7 @@ Expect(err).ShouldNot(HaveOccurred()) defer res.Body.Close() - var depRes apiDeploymentResponse + var depRes ApiDeploymentResponse body, err := ioutil.ReadAll(res.Body) Expect(err).ShouldNot(HaveOccurred()) json.Unmarshal(body, &depRes) @@ -57,8 +57,15 @@ Expect(dep.ScopeId).To(Equal(deploymentID)) Expect(dep.DisplayName).To(Equal(deploymentID)) - // todo: more tests - //dep.ConfigurationJson + var config bundleConfigJson + + err = json.Unmarshal(dep.ConfigJson, &config) + Expect(err).ShouldNot(HaveOccurred()) + Expect(config.Name).To(Equal("/bundles/1")) + + err = json.Unmarshal(dep.BundleConfigJson, &config) + Expect(err).ShouldNot(HaveOccurred()) + Expect(config.Name).To(Equal("/bundles/1")) }) It("should get 304 for no change", func() { @@ -124,7 +131,7 @@ defer res.Body.Close() Expect(res.StatusCode).To(Equal(http.StatusOK)) - var depRes apiDeploymentResponse + var depRes ApiDeploymentResponse body, err := ioutil.ReadAll(res.Body) Expect(err).ShouldNot(HaveOccurred()) json.Unmarshal(body, &depRes) @@ -315,7 +322,7 @@ ApidClusterID: deploymentID, DataScopeID: deploymentID, BundleConfigJSON: string(bundleJson), - ConfigJSON: "", + ConfigJSON: string(bundleJson), Status: "", Created: "", CreatedBy: "",
diff --git a/cmd/apidGatewayDeploy/deployments.json b/cmd/apidGatewayDeploy/deployments.json new file mode 100644 index 0000000..72efd9f --- /dev/null +++ b/cmd/apidGatewayDeploy/deployments.json
@@ -0,0 +1,24 @@ +[ + { + "id": "api_no_change_blocking", + "scopeId": "api_no_change_blocking", + "created": "", + "createdBy": "", + "updated": "", + "updatedBy": "", + "configuration": { + "name": "/bundles/1", + "uri": "http://127.0.0.1:53589/bundles/1", + "checksumType": "crc-32", + "checksum": "c65a0f2a" + }, + "bundleConfiguration": { + "name": "/bundles/1", + "uri": "http://127.0.0.1:53589/bundles/1", + "checksumType": "crc-32", + "checksum": "c65a0f2a" + }, + "displayName": "api_no_change_blocking", + "uri": "x" + } +] \ No newline at end of file
diff --git a/cmd/apidGatewayDeploy/main.go b/cmd/apidGatewayDeploy/main.go index 32de317..63961f5 100644 --- a/cmd/apidGatewayDeploy/main.go +++ b/cmd/apidGatewayDeploy/main.go
@@ -2,24 +2,19 @@ import ( "flag" - "io/ioutil" - "os" - "strings" - - "fmt" - "github.com/30x/apid" "github.com/30x/apid/factory" - "github.com/30x/apidGatewayDeploy" _ "github.com/30x/apidGatewayDeploy" + "io/ioutil" + "github.com/30x/apidGatewayDeploy" + "os" + "encoding/json" ) func main() { - bundleFlag := flag.String("bundle", "", "file path to a bundle file (for testing)") - configFlag := flag.String("config", "", "file path to a bundle config file (for testing)") + deploymentsFlag := flag.String("deployments", "", "file path to a deployments file (for testing)") flag.Parse() - bundleFile := *bundleFlag - configFile := *configFlag + deploymentsFile := *deploymentsFlag apid.Initialize(factory.DefaultServicesFactory()) @@ -28,10 +23,9 @@ configService := apid.Config() - // if bundle is specified, start in a temp dir for testing - var bundleConfig string - if bundleFile != "" { - log.Printf("Running in temp dir with bundle file: %s", bundleFile) + var deployments apiGatewayDeploy.ApiDeploymentResponse + if deploymentsFile != "" { + log.Printf("Running in temp dir using deployments file: %s", deploymentsFile) tmpDir, err := ioutil.TempDir("", "apidGatewayDeploy") if err != nil { log.Panicf("ERROR: Unable to create temp dir", err) @@ -39,37 +33,27 @@ defer os.RemoveAll(tmpDir) configService.Set("data_path", tmpDir) - configService.Set("gatewaydeploy_bundle_dir", tmpDir) + configService.Set("gatewaydeploy_bundle_dir", tmpDir) // todo: legacy? - if configFile != "" { - bundleConfigBytes, err := ioutil.ReadFile(configFile) + if deploymentsFile != "" { + bytes, err := ioutil.ReadFile(deploymentsFile) if err != nil { - log.Errorf("ERROR: Unable to read bundle config file at %s", configFile) + log.Errorf("ERROR: Unable to read bundle config file at %s", deploymentsFile) return } - bundleConfig = string(bundleConfigBytes) + + err = json.Unmarshal(bytes, &deployments) + if err != nil { + log.Errorf("ERROR: Unable to parse deployments %v", err) + return + } } } apid.InitializePlugins() - if bundleFile != "" { - if strings.ContainsAny(bundleFile, ",") { - for deploymentCount, singleBundleFile := range strings.Split(bundleFile, ",") { - deployment := fmt.Sprintf("testDeployment-%d", deploymentCount) - err := insertTestDeployment(singleBundleFile, bundleConfig, deployment) - if err != nil { - log.Fatal(err) - } - } - } else { - err := insertTestDeployment(bundleFile, bundleConfig, "testDeployment") - if err != nil { - log.Fatal(err) - } - } - } + insertTestDeployments(deployments) // print the base url to the console basePath := "/deployments" @@ -84,25 +68,10 @@ log.Fatalf("Error. Is something already running on port %d? %s", port, err) } -func insertTestDeployment(bundleFile, bundleConfig string, deploymentID string) error { +func insertTestDeployments(deployments apiGatewayDeploy.ApiDeploymentResponse) error { - dep := apiGatewayDeploy.DataDeployment{ - ID: deploymentID, - BundleConfigID: deploymentID, - ApidClusterID: deploymentID, - DataScopeID: deploymentID, - BundleConfigJSON: bundleConfig, - ConfigJSON: "", - Status: "", - Created: "", - CreatedBy: "", - Updated: "", - UpdatedBy: "", - BundleName: deploymentID, - BundleURI: bundleFile, - BundleChecksum: "", - BundleChecksumType: "", - LocalBundleURI: bundleFile, + if len(deployments) == 0 { + return nil } log := apid.Log() @@ -123,10 +92,34 @@ return err } - err = apiGatewayDeploy.InsertDeployment(tx, dep) - if err != nil { - log.Error("Unable to insert deployment") - return err + for _, ad := range deployments { + + dep := apiGatewayDeploy.DataDeployment{ + ID: ad.ID, + BundleConfigID: ad.ID, + ApidClusterID: ad.ID, + DataScopeID: ad.ScopeId, + BundleConfigJSON: string(ad.BundleConfigJson), + ConfigJSON: string(ad.ConfigJson), + Status: "", + Created: "", + CreatedBy: "", + Updated: "", + UpdatedBy: "", + BundleName: deploymentID, + BundleURI: bundleFile, + BundleChecksum: "", + BundleChecksumType: "", + LocalBundleURI: bundleFile, + } + + + err = apiGatewayDeploy.InsertDeployment(tx, dep) + if err != nil { + log.Error("Unable to insert deployment") + return err + } + } err = tx.Commit() @@ -137,4 +130,4 @@ apiGatewayDeploy.InitAPI() return nil -} +} \ No newline at end of file
diff --git a/data.go b/data.go index d21070a..45ad641 100644 --- a/data.go +++ b/data.go
@@ -2,9 +2,9 @@ import ( "database/sql" + "fmt" "github.com/30x/apid" "sync" - "fmt" ) var ( @@ -13,22 +13,22 @@ ) type DataDeployment struct { - ID string - BundleConfigID string - ApidClusterID string - DataScopeID string - BundleConfigJSON string - ConfigJSON string - Status string - Created string - CreatedBy string - Updated string - UpdatedBy string - BundleName string - BundleURI string - BundleChecksum string + ID string + BundleConfigID string + ApidClusterID string + DataScopeID string + BundleConfigJSON string + ConfigJSON string + Status string + Created string + CreatedBy string + Updated string + UpdatedBy string + BundleName string + BundleURI string + BundleChecksum string BundleChecksumType string - LocalBundleURI string + LocalBundleURI string } type SQLExec interface { @@ -171,7 +171,7 @@ return err } - deploymentsChanged<- depID + deploymentsChanged <- depID log.Debugf("deleteDeployment %s succeeded", depID) return err @@ -190,7 +190,7 @@ WHERE local_bundle_uri != "" `) if err != nil { - if err == sql.ErrNoRows{ + if err == sql.ErrNoRows { return deployments, nil } log.Errorf("Error querying deployments: %v", err)