Files
fn-serverless/api/models/app.go
Denis Makogon 3c15ca6ea6 App ID (#641)
* App ID

* Clean-up

* Use ID or name to reference apps

* Can use app by name or ID

* Get rid of AppName for routes API and model

 routes API is completely backwards-compatible
 routes API accepts both app ID and name

* Get rid of AppName from calls API and model

* Fixing tests

* Get rid of AppName from logs API and model

* Restrict API to work with app names only

* Addressing review comments

* Fix for hybrid mode

* Fix rebase problems

* Addressing review comments

* Addressing review comments pt.2

* Fixing test issue

* Addressing review comments pt.3

* Updated docstring

* Adjust UpdateApp SQL implementation to work with app IDs instead of names

* Fixing tests

* fmt after rebase

* Make tests green again!

* Use GetAppByID wherever it is necessary

 - adding new v2 endpoints to keep hybrid api/runner mode working
 - extract CallBase from Call object to expose that to a user
   (it doesn't include any app reference, as we do for all other API objects)

* Get rid of GetAppByName

* Adjusting server router setup

* Make hybrid work again

* Fix datastore tests

* Fixing tests

* Do not ignore app_id

* Resolve issues after rebase

* Updating test to make it work as it was

* Tabula rasa for migrations

* Adding calls API test

 - we need to ensure we give "App not found" for the missing app and missing call in first place
 - making previous test work (request missing call for the existing app)

* Make datastore tests work fine with correctly applied migrations

* Make CallFunction middleware work again

 had to adjust its implementation to set app ID before proceeding

* The biggest rebase ever made

* Fix 8's migration

* Fix tests

* Fix hybrid client

* Fix tests problem

* Increment app ID migration version

* Fixing TestAppUpdate

* Fix rebase issues

* Addressing review comments

* Renew vendor

* Updated swagger doc per recommendations
2018-03-26 11:19:36 -07:00

118 lines
2.8 KiB
Go

package models
import (
"time"
"unicode"
"github.com/fnproject/fn/api/id"
"github.com/go-openapi/strfmt"
)
type App struct {
ID string `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Config Config `json:"config,omitempty" db:"config"`
Annotations Annotations `json:"annotations,omitempty" db:"annotations"`
CreatedAt strfmt.DateTime `json:"created_at,omitempty" db:"created_at"`
UpdatedAt strfmt.DateTime `json:"updated_at,omitempty" db:"updated_at"`
}
func (a *App) SetDefaults() {
if time.Time(a.CreatedAt).IsZero() {
a.CreatedAt = strfmt.DateTime(time.Now())
}
if time.Time(a.UpdatedAt).IsZero() {
a.UpdatedAt = strfmt.DateTime(time.Now())
}
if a.Config == nil {
// keeps the json from being nil
a.Config = map[string]string{}
}
if a.ID == "" {
a.ID = id.New().String()
}
}
func (a *App) Validate() error {
if a.Name == "" {
return ErrAppsMissingName
}
if len(a.Name) > maxAppName {
return ErrAppsTooLongName
}
for _, c := range a.Name {
if !(unicode.IsLetter(c) || unicode.IsNumber(c) || c == '_' || c == '-') {
return ErrAppsInvalidName
}
}
err := a.Annotations.Validate()
if err != nil {
return err
}
return nil
}
func (a *App) Clone() *App {
clone := new(App)
*clone = *a // shallow copy
// now deep copy the map
if a.Config != nil {
clone.Config = make(Config, len(a.Config))
for k, v := range a.Config {
clone.Config[k] = v
}
}
clone.ID = a.ID
return clone
}
func (a1 *App) Equals(a2 *App) bool {
// start off equal, check equivalence of each field.
// the RHS of && won't eval if eq==false so config checking is lazy
eq := true
eq = eq && a1.ID == a2.ID
eq = eq && a1.Name == a2.Name
eq = eq && a1.Config.Equals(a2.Config)
eq = eq && a1.Annotations.Equals(a2.Annotations)
// NOTE: datastore tests are not very fun to write with timestamp checks,
// and these are not values the user may set so we kind of don't care.
//eq = eq && time.Time(a1.CreatedAt).Equal(time.Time(a2.CreatedAt))
//eq = eq && time.Time(a1.UpdatedAt).Equal(time.Time(a2.UpdatedAt))
return eq
}
// Update adds entries from patch to a.Config and a.Annotations, and removes entries with empty values.
func (a *App) Update(patch *App) {
original := a.Clone()
if patch.Config != nil {
if a.Config == nil {
a.Config = make(Config)
}
for k, v := range patch.Config {
if v == "" {
delete(a.Config, k)
} else {
a.Config[k] = v
}
}
}
a.Annotations = a.Annotations.MergeChange(patch.Annotations)
if !a.Equals(original) {
a.UpdatedAt = strfmt.DateTime(time.Now())
}
}
// AppFilter is the filter used for querying apps
type AppFilter struct {
Name string
// NameIn will filter by all names in the list (IN query)
NameIn []string
PerPage int
Cursor string
}