This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add export importers to the admin console (#1085)
* Add export importers to the admin console * Remove unused error * Add and fix tests * Tick ticks
- Loading branch information
Showing
17 changed files
with
677 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, softwar | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package exportimporters is part of the admin system. | ||
package exportimporters | ||
|
||
import ( | ||
"github.com/google/exposure-notifications-server/internal/admin" | ||
"github.com/google/exposure-notifications-server/internal/exportimport/model" | ||
) | ||
|
||
type formData struct { | ||
IndexFile string `form:"index_file"` | ||
ExportRoot string `form:"export_root"` | ||
Region string `form:"region"` | ||
|
||
// FromDate and FromTime are combined into FromTimestamp. | ||
FromDate string `form:"from_date"` | ||
FromTime string `form:"from_time"` | ||
|
||
// ThruDate and ThruTime are combined into ThruTimestamp. | ||
ThruDate string `form:"thru_date"` | ||
ThruTime string `form:"thru_time"` | ||
} | ||
|
||
// BuildExportImporterModel populates and mutates the given model with form | ||
// data. It overwrites any form data that's present. | ||
func (f *formData) BuildExportImporterModel(c *model.ExportImport) error { | ||
from, err := admin.CombineDateAndTime(f.FromDate, f.FromTime) | ||
if err != nil { | ||
return err | ||
} | ||
thru, err := admin.CombineDateAndTime(f.ThruDate, f.ThruTime) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if val := f.IndexFile; val != "" { | ||
c.IndexFile = val | ||
} | ||
|
||
if val := f.ExportRoot; val != "" { | ||
c.ExportRoot = val | ||
} | ||
|
||
if val := f.Region; val != "" { | ||
c.Region = val | ||
} | ||
|
||
if !from.IsZero() { | ||
c.From = from | ||
} | ||
|
||
if !thru.IsZero() { | ||
c.Thru = &thru | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, softwar | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package exportimporters | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/google/exposure-notifications-server/internal/admin" | ||
exportimportdatabase "github.com/google/exposure-notifications-server/internal/exportimport/database" | ||
exportimportmodel "github.com/google/exposure-notifications-server/internal/exportimport/model" | ||
"github.com/google/exposure-notifications-server/internal/serverenv" | ||
) | ||
|
||
type saveController struct { | ||
config *admin.Config | ||
env *serverenv.ServerEnv | ||
} | ||
|
||
func NewSave(c *admin.Config, env *serverenv.ServerEnv) admin.Controller { | ||
return &saveController{config: c, env: env} | ||
} | ||
|
||
func (v *saveController) Execute(c *gin.Context) { | ||
var form formData | ||
if err := c.Bind(&form); err != nil { | ||
admin.ErrorPage(c, err.Error()) | ||
return | ||
} | ||
|
||
ctx := c.Request.Context() | ||
m := admin.TemplateMap{} | ||
|
||
db := exportimportdatabase.New(v.env.Database()) | ||
model := new(exportimportmodel.ExportImport) | ||
|
||
idRaw := c.Param("id") | ||
if idRaw != "" && idRaw != "0" { | ||
id, err := strconv.ParseInt(idRaw, 10, 64) | ||
if err != nil { | ||
admin.ErrorPage(c, "failed to to parse `id` param.") | ||
return | ||
} | ||
|
||
model, err = db.GetConfig(ctx, id) | ||
if err != nil { | ||
admin.ErrorPage(c, fmt.Sprintf("failed to load export importer config: %s", err)) | ||
return | ||
} | ||
} | ||
|
||
if err := form.BuildExportImporterModel(model); err != nil { | ||
admin.ErrorPage(c, fmt.Sprintf("failed to build export importer config: %s", err)) | ||
return | ||
} | ||
|
||
fn := db.AddConfig | ||
if model.ID != 0 { | ||
fn = db.UpdateConfig | ||
} | ||
|
||
if err := fn(ctx, model); err != nil { | ||
admin.ErrorPage(c, fmt.Sprintf("failed to write export importer config: %s", err)) | ||
return | ||
} | ||
|
||
m.AddSuccess("Successfully updated export importer config!") | ||
m["model"] = model | ||
c.HTML(http.StatusOK, "export-importer", m) | ||
c.Abort() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, softwar | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package exportimporters | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/google/exposure-notifications-server/internal/admin" | ||
exportimportdatabase "github.com/google/exposure-notifications-server/internal/exportimport/database" | ||
exportimportmodel "github.com/google/exposure-notifications-server/internal/exportimport/model" | ||
"github.com/google/exposure-notifications-server/internal/serverenv" | ||
) | ||
|
||
type viewController struct { | ||
config *admin.Config | ||
env *serverenv.ServerEnv | ||
} | ||
|
||
func NewView(c *admin.Config, env *serverenv.ServerEnv) admin.Controller { | ||
return &viewController{config: c, env: env} | ||
} | ||
|
||
func (v *viewController) Execute(c *gin.Context) { | ||
ctx := c.Request.Context() | ||
|
||
db := exportimportdatabase.New(v.env.Database()) | ||
model := new(exportimportmodel.ExportImport) | ||
|
||
if idRaw := c.Param("id"); idRaw != "" && idRaw != "0" { | ||
id, err := strconv.ParseInt(idRaw, 10, 64) | ||
if err != nil { | ||
admin.ErrorPage(c, fmt.Sprintf("Failed to parse `id` param: %s", err)) | ||
return | ||
} | ||
|
||
model, err = db.GetConfig(ctx, id) | ||
if err != nil { | ||
admin.ErrorPage(c, fmt.Sprintf("Failed to load export importer config: %s", err)) | ||
return | ||
} | ||
} | ||
|
||
m := make(admin.TemplateMap) | ||
m["model"] = model | ||
c.HTML(http.StatusOK, "export-importer", m) | ||
c.Abort() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, softwar | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package exportimporters | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/exposure-notifications-server/internal/admin" | ||
exportimportmodel "github.com/google/exposure-notifications-server/internal/exportimport/model" | ||
) | ||
|
||
func TestRenderSignatureInfo(t *testing.T) { | ||
// Hello developer! | ||
// If this test fails, it's likely that you changed something in | ||
// internal/authorizedapp/model/ | ||
// And whatever you changed is used in the | ||
// tools/admin-console/templates/siginfo.html | ||
// That is what caused the test failure. | ||
m := admin.TemplateMap{} | ||
model := new(exportimportmodel.ExportImport) | ||
m["model"] = model | ||
|
||
recorder := httptest.NewRecorder() | ||
config := admin.Config{ | ||
TemplatePath: "../../../tools/admin-console/templates", | ||
TopFile: "top", | ||
BotFile: "bottom", | ||
} | ||
err := config.RenderTemplate(recorder, "export-importer", m) | ||
if err != nil { | ||
t.Fatalf("error rendering template: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.