From 7ef91b35f72a38fdf8da1e0ac04a288fa7db091e Mon Sep 17 00:00:00 2001 From: sthwang-metal <126002920+sthwang-metal@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:25:15 -0700 Subject: [PATCH] add origin weights (#251) * Add ability to set weight for an origin Signed-off-by: Tyler Auerbeck * Linters gonna lint Signed-off-by: Tyler Auerbeck * add weight to origin node Signed-off-by: Stephen Hwang <126002920+sthwang-metal@users.noreply.github.com> * add migration to add origin weight column Signed-off-by: Stephen Hwang <126002920+sthwang-metal@users.noreply.github.com> * postgres compatible Signed-off-by: Stephen Hwang <126002920+sthwang-metal@users.noreply.github.com> * change to int instead of bigint Signed-off-by: Stephen Hwang <126002920+sthwang-metal@users.noreply.github.com> --------- Signed-off-by: Tyler Auerbeck Signed-off-by: Stephen Hwang <126002920+sthwang-metal@users.noreply.github.com> Co-authored-by: Tyler Auerbeck --- .../20231017005257_add_origin_weight.sql | 7 + db/migrations/atlas.sum | 3 +- go.sum | 8 + internal/ent/generated/gql_collection.go | 5 + internal/ent/generated/gql_mutation_input.go | 8 + internal/ent/generated/gql_pagination.go | 18 ++ internal/ent/generated/gql_where_input.go | 34 +++ internal/ent/generated/migrate/schema.go | 5 +- internal/ent/generated/mutation.go | 89 ++++++- internal/ent/generated/origin.go | 13 +- internal/ent/generated/origin/origin.go | 10 + internal/ent/generated/origin/where.go | 45 ++++ internal/ent/generated/origin_create.go | 25 ++ internal/ent/generated/origin_update.go | 54 ++++ internal/ent/generated/runtime.go | 12 +- internal/ent/schema/origin.go | 7 + internal/graphapi/gen_server.go | 243 +++++++++++++++++- internal/graphapi/loadbalancer.resolvers.go | 7 +- internal/graphapi/origin.resolvers.go | 3 +- internal/graphapi/origin_test.go | 37 +++ internal/graphapi/pool.resolvers.go | 5 +- internal/graphapi/port.resolvers.go | 5 +- internal/graphapi/provider.resolvers.go | 3 +- internal/graphclient/gen_models.go | 16 +- internal/graphclient/schema/schema.graphql | 13 + pkg/client/types.go | 1 + schema.graphql | 13 + schema/ent.graphql | 13 + 28 files changed, 675 insertions(+), 27 deletions(-) create mode 100644 db/migrations/20231017005257_add_origin_weight.sql diff --git a/db/migrations/20231017005257_add_origin_weight.sql b/db/migrations/20231017005257_add_origin_weight.sql new file mode 100644 index 000000000..6bbfef024 --- /dev/null +++ b/db/migrations/20231017005257_add_origin_weight.sql @@ -0,0 +1,7 @@ +-- +goose Up +-- modify "origins" table +ALTER TABLE "origins" ADD COLUMN "weight" int NOT NULL DEFAULT 100; + +-- +goose Down +-- reverse: modify "origins" table +ALTER TABLE "origins" DROP COLUMN "weight"; diff --git a/db/migrations/atlas.sum b/db/migrations/atlas.sum index fbb7f23ed..ff1077165 100644 --- a/db/migrations/atlas.sum +++ b/db/migrations/atlas.sum @@ -1,4 +1,5 @@ -h1:c+ziReSuevRk7RBOpYWo0gncdcwco4V61ROlPMPe5EE= +h1:C2ooKhLvT6ZWxsvbhzwIlh+LWFh+wU+nRN1onsCHlkw= 20230503185445_initial-migration.sql h1:4pqNp2MDBBRdGxU/H5mmZui9oi1SyjIiMVGatajrBeY= 20230615194819_drop_tenant_add_owner.sql h1:KGCsItU0NYhxYEkhZOaMQjfIrBMnek5rxC6D/LhnyCk= 20230629085916_drop_status_and_annotations.sql h1:kvDMoaMEjyoj/aRi6rw4XvCLxGH09vGGLbL0/p5tpPo= +20231017005257_add_origin_weight.sql h1:G0SKQBweZg4S9IvpnoIeVJ05aLSiNb3PWnJcZyo+2fg= diff --git a/go.sum b/go.sum index a4fbe7157..2d91bae55 100644 --- a/go.sum +++ b/go.sum @@ -108,6 +108,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E= github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= @@ -474,6 +475,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y= @@ -542,6 +545,9 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8= +github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= +github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= @@ -557,6 +563,8 @@ github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vb github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= github.com/wundergraph/graphql-go-tools v1.66.4 h1:yRvXYi0jjTghi5zimTluqHXAmyS7JVlGzTlxY6aL0sI= github.com/wundergraph/graphql-go-tools v1.66.4/go.mod h1:obaEJWub7088qodhKbSGHyhRVnHlBP5M9HigN/oalLE= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/internal/ent/generated/gql_collection.go b/internal/ent/generated/gql_collection.go index 8e12e89f9..9547396c8 100644 --- a/internal/ent/generated/gql_collection.go +++ b/internal/ent/generated/gql_collection.go @@ -279,6 +279,11 @@ func (o *OriginQuery) collectField(ctx context.Context, opCtx *graphql.Operation selectedFields = append(selectedFields, origin.FieldName) fieldSeen[origin.FieldName] = struct{}{} } + case "weight": + if _, ok := fieldSeen[origin.FieldWeight]; !ok { + selectedFields = append(selectedFields, origin.FieldWeight) + fieldSeen[origin.FieldWeight] = struct{}{} + } case "target": if _, ok := fieldSeen[origin.FieldTarget]; !ok { selectedFields = append(selectedFields, origin.FieldTarget) diff --git a/internal/ent/generated/gql_mutation_input.go b/internal/ent/generated/gql_mutation_input.go index 326fb1711..ede04cd8c 100644 --- a/internal/ent/generated/gql_mutation_input.go +++ b/internal/ent/generated/gql_mutation_input.go @@ -86,6 +86,7 @@ func (c *LoadBalancerUpdateOne) SetInput(i UpdateLoadBalancerInput) *LoadBalance // CreateLoadBalancerOriginInput represents a mutation input for creating loadbalancerorigins. type CreateLoadBalancerOriginInput struct { Name string + Weight *int32 Target string PortNumber int Active *bool @@ -95,6 +96,9 @@ type CreateLoadBalancerOriginInput struct { // Mutate applies the CreateLoadBalancerOriginInput on the OriginMutation builder. func (i *CreateLoadBalancerOriginInput) Mutate(m *OriginMutation) { m.SetName(i.Name) + if v := i.Weight; v != nil { + m.SetWeight(*v) + } m.SetTarget(i.Target) m.SetPortNumber(i.PortNumber) if v := i.Active; v != nil { @@ -112,6 +116,7 @@ func (c *OriginCreate) SetInput(i CreateLoadBalancerOriginInput) *OriginCreate { // UpdateLoadBalancerOriginInput represents a mutation input for updating loadbalancerorigins. type UpdateLoadBalancerOriginInput struct { Name *string + Weight *int32 Target *string PortNumber *int Active *bool @@ -122,6 +127,9 @@ func (i *UpdateLoadBalancerOriginInput) Mutate(m *OriginMutation) { if v := i.Name; v != nil { m.SetName(*v) } + if v := i.Weight; v != nil { + m.SetWeight(*v) + } if v := i.Target; v != nil { m.SetTarget(*v) } diff --git a/internal/ent/generated/gql_pagination.go b/internal/ent/generated/gql_pagination.go index 29b377b81..b692adbb5 100644 --- a/internal/ent/generated/gql_pagination.go +++ b/internal/ent/generated/gql_pagination.go @@ -733,6 +733,20 @@ var ( } }, } + // OriginOrderFieldWeight orders Origin by weight. + OriginOrderFieldWeight = &LoadBalancerOriginOrderField{ + Value: func(o *LoadBalancerOrigin) (ent.Value, error) { + return o.Weight, nil + }, + column: origin.FieldWeight, + toTerm: origin.ByWeight, + toCursor: func(o *LoadBalancerOrigin) Cursor { + return Cursor{ + ID: o.ID, + Value: o.Weight, + } + }, + } // OriginOrderFieldTarget orders Origin by target. OriginOrderFieldTarget = &LoadBalancerOriginOrderField{ Value: func(o *LoadBalancerOrigin) (ent.Value, error) { @@ -787,6 +801,8 @@ func (f LoadBalancerOriginOrderField) String() string { str = "UPDATED_AT" case OriginOrderFieldName.column: str = "name" + case OriginOrderFieldWeight.column: + str = "weight" case OriginOrderFieldTarget.column: str = "target" case OriginOrderFieldPortNumber.column: @@ -815,6 +831,8 @@ func (f *LoadBalancerOriginOrderField) UnmarshalGQL(v interface{}) error { *f = *OriginOrderFieldUpdatedAt case "name": *f = *OriginOrderFieldName + case "weight": + *f = *OriginOrderFieldWeight case "target": *f = *OriginOrderFieldTarget case "number": diff --git a/internal/ent/generated/gql_where_input.go b/internal/ent/generated/gql_where_input.go index c5c1ee66c..631c26348 100644 --- a/internal/ent/generated/gql_where_input.go +++ b/internal/ent/generated/gql_where_input.go @@ -372,6 +372,16 @@ type LoadBalancerOriginWhereInput struct { NameEqualFold *string `json:"nameEqualFold,omitempty"` NameContainsFold *string `json:"nameContainsFold,omitempty"` + // "weight" field predicates. + Weight *int32 `json:"weight,omitempty"` + WeightNEQ *int32 `json:"weightNEQ,omitempty"` + WeightIn []int32 `json:"weightIn,omitempty"` + WeightNotIn []int32 `json:"weightNotIn,omitempty"` + WeightGT *int32 `json:"weightGT,omitempty"` + WeightGTE *int32 `json:"weightGTE,omitempty"` + WeightLT *int32 `json:"weightLT,omitempty"` + WeightLTE *int32 `json:"weightLTE,omitempty"` + // "target" field predicates. Target *string `json:"target,omitempty"` TargetNEQ *string `json:"targetNEQ,omitempty"` @@ -588,6 +598,30 @@ func (i *LoadBalancerOriginWhereInput) P() (predicate.Origin, error) { if i.NameContainsFold != nil { predicates = append(predicates, origin.NameContainsFold(*i.NameContainsFold)) } + if i.Weight != nil { + predicates = append(predicates, origin.WeightEQ(*i.Weight)) + } + if i.WeightNEQ != nil { + predicates = append(predicates, origin.WeightNEQ(*i.WeightNEQ)) + } + if len(i.WeightIn) > 0 { + predicates = append(predicates, origin.WeightIn(i.WeightIn...)) + } + if len(i.WeightNotIn) > 0 { + predicates = append(predicates, origin.WeightNotIn(i.WeightNotIn...)) + } + if i.WeightGT != nil { + predicates = append(predicates, origin.WeightGT(*i.WeightGT)) + } + if i.WeightGTE != nil { + predicates = append(predicates, origin.WeightGTE(*i.WeightGTE)) + } + if i.WeightLT != nil { + predicates = append(predicates, origin.WeightLT(*i.WeightLT)) + } + if i.WeightLTE != nil { + predicates = append(predicates, origin.WeightLTE(*i.WeightLTE)) + } if i.Target != nil { predicates = append(predicates, origin.TargetEQ(*i.Target)) } diff --git a/internal/ent/generated/migrate/schema.go b/internal/ent/generated/migrate/schema.go index 9baf0ef8e..3af7f748a 100644 --- a/internal/ent/generated/migrate/schema.go +++ b/internal/ent/generated/migrate/schema.go @@ -79,6 +79,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "name", Type: field.TypeString}, + {Name: "weight", Type: field.TypeInt32, Default: 100}, {Name: "target", Type: field.TypeString}, {Name: "port_number", Type: field.TypeInt}, {Name: "active", Type: field.TypeBool, Default: true}, @@ -92,7 +93,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "origins_pools_pool", - Columns: []*schema.Column{OriginsColumns[7]}, + Columns: []*schema.Column{OriginsColumns[8]}, RefColumns: []*schema.Column{PoolsColumns[0]}, OnDelete: schema.NoAction, }, @@ -111,7 +112,7 @@ var ( { Name: "origin_pool_id", Unique: false, - Columns: []*schema.Column{OriginsColumns[7]}, + Columns: []*schema.Column{OriginsColumns[8]}, }, }, } diff --git a/internal/ent/generated/mutation.go b/internal/ent/generated/mutation.go index f0874ac93..404173fcf 100644 --- a/internal/ent/generated/mutation.go +++ b/internal/ent/generated/mutation.go @@ -800,6 +800,8 @@ type OriginMutation struct { created_at *time.Time updated_at *time.Time name *string + weight *int32 + addweight *int32 target *string port_number *int addport_number *int @@ -1024,6 +1026,62 @@ func (m *OriginMutation) ResetName() { m.name = nil } +// SetWeight sets the "weight" field. +func (m *OriginMutation) SetWeight(i int32) { + m.weight = &i + m.addweight = nil +} + +// Weight returns the value of the "weight" field in the mutation. +func (m *OriginMutation) Weight() (r int32, exists bool) { + v := m.weight + if v == nil { + return + } + return *v, true +} + +// OldWeight returns the old "weight" field's value of the Origin entity. +// If the Origin object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *OriginMutation) OldWeight(ctx context.Context) (v int32, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldWeight is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldWeight requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldWeight: %w", err) + } + return oldValue.Weight, nil +} + +// AddWeight adds i to the "weight" field. +func (m *OriginMutation) AddWeight(i int32) { + if m.addweight != nil { + *m.addweight += i + } else { + m.addweight = &i + } +} + +// AddedWeight returns the value that was added to the "weight" field in this mutation. +func (m *OriginMutation) AddedWeight() (r int32, exists bool) { + v := m.addweight + if v == nil { + return + } + return *v, true +} + +// ResetWeight resets all changes to the "weight" field. +func (m *OriginMutation) ResetWeight() { + m.weight = nil + m.addweight = nil +} + // SetTarget sets the "target" field. func (m *OriginMutation) SetTarget(s string) { m.target = &s @@ -1249,7 +1307,7 @@ func (m *OriginMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *OriginMutation) Fields() []string { - fields := make([]string, 0, 7) + fields := make([]string, 0, 8) if m.created_at != nil { fields = append(fields, origin.FieldCreatedAt) } @@ -1259,6 +1317,9 @@ func (m *OriginMutation) Fields() []string { if m.name != nil { fields = append(fields, origin.FieldName) } + if m.weight != nil { + fields = append(fields, origin.FieldWeight) + } if m.target != nil { fields = append(fields, origin.FieldTarget) } @@ -1285,6 +1346,8 @@ func (m *OriginMutation) Field(name string) (ent.Value, bool) { return m.UpdatedAt() case origin.FieldName: return m.Name() + case origin.FieldWeight: + return m.Weight() case origin.FieldTarget: return m.Target() case origin.FieldPortNumber: @@ -1308,6 +1371,8 @@ func (m *OriginMutation) OldField(ctx context.Context, name string) (ent.Value, return m.OldUpdatedAt(ctx) case origin.FieldName: return m.OldName(ctx) + case origin.FieldWeight: + return m.OldWeight(ctx) case origin.FieldTarget: return m.OldTarget(ctx) case origin.FieldPortNumber: @@ -1346,6 +1411,13 @@ func (m *OriginMutation) SetField(name string, value ent.Value) error { } m.SetName(v) return nil + case origin.FieldWeight: + v, ok := value.(int32) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetWeight(v) + return nil case origin.FieldTarget: v, ok := value.(string) if !ok { @@ -1382,6 +1454,9 @@ func (m *OriginMutation) SetField(name string, value ent.Value) error { // this mutation. func (m *OriginMutation) AddedFields() []string { var fields []string + if m.addweight != nil { + fields = append(fields, origin.FieldWeight) + } if m.addport_number != nil { fields = append(fields, origin.FieldPortNumber) } @@ -1393,6 +1468,8 @@ func (m *OriginMutation) AddedFields() []string { // was not set, or was not defined in the schema. func (m *OriginMutation) AddedField(name string) (ent.Value, bool) { switch name { + case origin.FieldWeight: + return m.AddedWeight() case origin.FieldPortNumber: return m.AddedPortNumber() } @@ -1404,6 +1481,13 @@ func (m *OriginMutation) AddedField(name string) (ent.Value, bool) { // type. func (m *OriginMutation) AddField(name string, value ent.Value) error { switch name { + case origin.FieldWeight: + v, ok := value.(int32) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddWeight(v) + return nil case origin.FieldPortNumber: v, ok := value.(int) if !ok { @@ -1447,6 +1531,9 @@ func (m *OriginMutation) ResetField(name string) error { case origin.FieldName: m.ResetName() return nil + case origin.FieldWeight: + m.ResetWeight() + return nil case origin.FieldTarget: m.ResetTarget() return nil diff --git a/internal/ent/generated/origin.go b/internal/ent/generated/origin.go index 6bdefcc7e..f22c9bedd 100644 --- a/internal/ent/generated/origin.go +++ b/internal/ent/generated/origin.go @@ -39,6 +39,8 @@ type Origin struct { UpdatedAt time.Time `json:"updated_at,omitempty"` // Name holds the value of the "name" field. Name string `json:"name,omitempty"` + // Weight holds the value of the "weight" field. + Weight int32 `json:"weight,omitempty"` // Target holds the value of the "target" field. Target string `json:"target,omitempty"` // PortNumber holds the value of the "port_number" field. @@ -86,7 +88,7 @@ func (*Origin) scanValues(columns []string) ([]any, error) { values[i] = new(gidx.PrefixedID) case origin.FieldActive: values[i] = new(sql.NullBool) - case origin.FieldPortNumber: + case origin.FieldWeight, origin.FieldPortNumber: values[i] = new(sql.NullInt64) case origin.FieldName, origin.FieldTarget: values[i] = new(sql.NullString) @@ -131,6 +133,12 @@ func (o *Origin) assignValues(columns []string, values []any) error { } else if value.Valid { o.Name = value.String } + case origin.FieldWeight: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field weight", values[i]) + } else if value.Valid { + o.Weight = int32(value.Int64) + } case origin.FieldTarget: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field target", values[i]) @@ -205,6 +213,9 @@ func (o *Origin) String() string { builder.WriteString("name=") builder.WriteString(o.Name) builder.WriteString(", ") + builder.WriteString("weight=") + builder.WriteString(fmt.Sprintf("%v", o.Weight)) + builder.WriteString(", ") builder.WriteString("target=") builder.WriteString(o.Target) builder.WriteString(", ") diff --git a/internal/ent/generated/origin/origin.go b/internal/ent/generated/origin/origin.go index c65d0fc50..e62502948 100644 --- a/internal/ent/generated/origin/origin.go +++ b/internal/ent/generated/origin/origin.go @@ -35,6 +35,8 @@ const ( FieldUpdatedAt = "updated_at" // FieldName holds the string denoting the name field in the database. FieldName = "name" + // FieldWeight holds the string denoting the weight field in the database. + FieldWeight = "weight" // FieldTarget holds the string denoting the target field in the database. FieldTarget = "target" // FieldPortNumber holds the string denoting the port_number field in the database. @@ -62,6 +64,7 @@ var Columns = []string{ FieldCreatedAt, FieldUpdatedAt, FieldName, + FieldWeight, FieldTarget, FieldPortNumber, FieldActive, @@ -87,6 +90,8 @@ var ( UpdateDefaultUpdatedAt func() time.Time // NameValidator is a validator for the "name" field. It is called by the builders before save. NameValidator func(string) error + // DefaultWeight holds the default value on creation for the "weight" field. + DefaultWeight int32 // TargetValidator is a validator for the "target" field. It is called by the builders before save. TargetValidator func(string) error // PortNumberValidator is a validator for the "port_number" field. It is called by the builders before save. @@ -122,6 +127,11 @@ func ByName(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldName, opts...).ToFunc() } +// ByWeight orders the results by the weight field. +func ByWeight(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldWeight, opts...).ToFunc() +} + // ByTarget orders the results by the target field. func ByTarget(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldTarget, opts...).ToFunc() diff --git a/internal/ent/generated/origin/where.go b/internal/ent/generated/origin/where.go index 0af4d03fb..2bc8efc51 100644 --- a/internal/ent/generated/origin/where.go +++ b/internal/ent/generated/origin/where.go @@ -85,6 +85,11 @@ func Name(v string) predicate.Origin { return predicate.Origin(sql.FieldEQ(FieldName, v)) } +// Weight applies equality check predicate on the "weight" field. It's identical to WeightEQ. +func Weight(v int32) predicate.Origin { + return predicate.Origin(sql.FieldEQ(FieldWeight, v)) +} + // Target applies equality check predicate on the "target" field. It's identical to TargetEQ. func Target(v string) predicate.Origin { return predicate.Origin(sql.FieldEQ(FieldTarget, v)) @@ -250,6 +255,46 @@ func NameContainsFold(v string) predicate.Origin { return predicate.Origin(sql.FieldContainsFold(FieldName, v)) } +// WeightEQ applies the EQ predicate on the "weight" field. +func WeightEQ(v int32) predicate.Origin { + return predicate.Origin(sql.FieldEQ(FieldWeight, v)) +} + +// WeightNEQ applies the NEQ predicate on the "weight" field. +func WeightNEQ(v int32) predicate.Origin { + return predicate.Origin(sql.FieldNEQ(FieldWeight, v)) +} + +// WeightIn applies the In predicate on the "weight" field. +func WeightIn(vs ...int32) predicate.Origin { + return predicate.Origin(sql.FieldIn(FieldWeight, vs...)) +} + +// WeightNotIn applies the NotIn predicate on the "weight" field. +func WeightNotIn(vs ...int32) predicate.Origin { + return predicate.Origin(sql.FieldNotIn(FieldWeight, vs...)) +} + +// WeightGT applies the GT predicate on the "weight" field. +func WeightGT(v int32) predicate.Origin { + return predicate.Origin(sql.FieldGT(FieldWeight, v)) +} + +// WeightGTE applies the GTE predicate on the "weight" field. +func WeightGTE(v int32) predicate.Origin { + return predicate.Origin(sql.FieldGTE(FieldWeight, v)) +} + +// WeightLT applies the LT predicate on the "weight" field. +func WeightLT(v int32) predicate.Origin { + return predicate.Origin(sql.FieldLT(FieldWeight, v)) +} + +// WeightLTE applies the LTE predicate on the "weight" field. +func WeightLTE(v int32) predicate.Origin { + return predicate.Origin(sql.FieldLTE(FieldWeight, v)) +} + // TargetEQ applies the EQ predicate on the "target" field. func TargetEQ(v string) predicate.Origin { return predicate.Origin(sql.FieldEQ(FieldTarget, v)) diff --git a/internal/ent/generated/origin_create.go b/internal/ent/generated/origin_create.go index d98fa4659..24b7dd441 100644 --- a/internal/ent/generated/origin_create.go +++ b/internal/ent/generated/origin_create.go @@ -70,6 +70,20 @@ func (oc *OriginCreate) SetName(s string) *OriginCreate { return oc } +// SetWeight sets the "weight" field. +func (oc *OriginCreate) SetWeight(i int32) *OriginCreate { + oc.mutation.SetWeight(i) + return oc +} + +// SetNillableWeight sets the "weight" field if the given value is not nil. +func (oc *OriginCreate) SetNillableWeight(i *int32) *OriginCreate { + if i != nil { + oc.SetWeight(*i) + } + return oc +} + // SetTarget sets the "target" field. func (oc *OriginCreate) SetTarget(s string) *OriginCreate { oc.mutation.SetTarget(s) @@ -164,6 +178,10 @@ func (oc *OriginCreate) defaults() { v := origin.DefaultUpdatedAt() oc.mutation.SetUpdatedAt(v) } + if _, ok := oc.mutation.Weight(); !ok { + v := origin.DefaultWeight + oc.mutation.SetWeight(v) + } if _, ok := oc.mutation.Active(); !ok { v := origin.DefaultActive oc.mutation.SetActive(v) @@ -190,6 +208,9 @@ func (oc *OriginCreate) check() error { return &ValidationError{Name: "name", err: fmt.Errorf(`generated: validator failed for field "Origin.name": %w`, err)} } } + if _, ok := oc.mutation.Weight(); !ok { + return &ValidationError{Name: "weight", err: errors.New(`generated: missing required field "Origin.weight"`)} + } if _, ok := oc.mutation.Target(); !ok { return &ValidationError{Name: "target", err: errors.New(`generated: missing required field "Origin.target"`)} } @@ -267,6 +288,10 @@ func (oc *OriginCreate) createSpec() (*Origin, *sqlgraph.CreateSpec) { _spec.SetField(origin.FieldName, field.TypeString, value) _node.Name = value } + if value, ok := oc.mutation.Weight(); ok { + _spec.SetField(origin.FieldWeight, field.TypeInt32, value) + _node.Weight = value + } if value, ok := oc.mutation.Target(); ok { _spec.SetField(origin.FieldTarget, field.TypeString, value) _node.Target = value diff --git a/internal/ent/generated/origin_update.go b/internal/ent/generated/origin_update.go index 1fc2fa8ce..bdddf0551 100644 --- a/internal/ent/generated/origin_update.go +++ b/internal/ent/generated/origin_update.go @@ -47,6 +47,27 @@ func (ou *OriginUpdate) SetName(s string) *OriginUpdate { return ou } +// SetWeight sets the "weight" field. +func (ou *OriginUpdate) SetWeight(i int32) *OriginUpdate { + ou.mutation.ResetWeight() + ou.mutation.SetWeight(i) + return ou +} + +// SetNillableWeight sets the "weight" field if the given value is not nil. +func (ou *OriginUpdate) SetNillableWeight(i *int32) *OriginUpdate { + if i != nil { + ou.SetWeight(*i) + } + return ou +} + +// AddWeight adds i to the "weight" field. +func (ou *OriginUpdate) AddWeight(i int32) *OriginUpdate { + ou.mutation.AddWeight(i) + return ou +} + // SetTarget sets the "target" field. func (ou *OriginUpdate) SetTarget(s string) *OriginUpdate { ou.mutation.SetTarget(s) @@ -162,6 +183,12 @@ func (ou *OriginUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := ou.mutation.Name(); ok { _spec.SetField(origin.FieldName, field.TypeString, value) } + if value, ok := ou.mutation.Weight(); ok { + _spec.SetField(origin.FieldWeight, field.TypeInt32, value) + } + if value, ok := ou.mutation.AddedWeight(); ok { + _spec.AddField(origin.FieldWeight, field.TypeInt32, value) + } if value, ok := ou.mutation.Target(); ok { _spec.SetField(origin.FieldTarget, field.TypeString, value) } @@ -200,6 +227,27 @@ func (ouo *OriginUpdateOne) SetName(s string) *OriginUpdateOne { return ouo } +// SetWeight sets the "weight" field. +func (ouo *OriginUpdateOne) SetWeight(i int32) *OriginUpdateOne { + ouo.mutation.ResetWeight() + ouo.mutation.SetWeight(i) + return ouo +} + +// SetNillableWeight sets the "weight" field if the given value is not nil. +func (ouo *OriginUpdateOne) SetNillableWeight(i *int32) *OriginUpdateOne { + if i != nil { + ouo.SetWeight(*i) + } + return ouo +} + +// AddWeight adds i to the "weight" field. +func (ouo *OriginUpdateOne) AddWeight(i int32) *OriginUpdateOne { + ouo.mutation.AddWeight(i) + return ouo +} + // SetTarget sets the "target" field. func (ouo *OriginUpdateOne) SetTarget(s string) *OriginUpdateOne { ouo.mutation.SetTarget(s) @@ -345,6 +393,12 @@ func (ouo *OriginUpdateOne) sqlSave(ctx context.Context) (_node *Origin, err err if value, ok := ouo.mutation.Name(); ok { _spec.SetField(origin.FieldName, field.TypeString, value) } + if value, ok := ouo.mutation.Weight(); ok { + _spec.SetField(origin.FieldWeight, field.TypeInt32, value) + } + if value, ok := ouo.mutation.AddedWeight(); ok { + _spec.AddField(origin.FieldWeight, field.TypeInt32, value) + } if value, ok := ouo.mutation.Target(); ok { _spec.SetField(origin.FieldTarget, field.TypeString, value) } diff --git a/internal/ent/generated/runtime.go b/internal/ent/generated/runtime.go index d55136b6c..461f5059f 100644 --- a/internal/ent/generated/runtime.go +++ b/internal/ent/generated/runtime.go @@ -86,8 +86,12 @@ func init() { originDescName := originFields[1].Descriptor() // origin.NameValidator is a validator for the "name" field. It is called by the builders before save. origin.NameValidator = originDescName.Validators[0].(func(string) error) + // originDescWeight is the schema descriptor for weight field. + originDescWeight := originFields[2].Descriptor() + // origin.DefaultWeight holds the default value on creation for the weight field. + origin.DefaultWeight = originDescWeight.Default.(int32) // originDescTarget is the schema descriptor for target field. - originDescTarget := originFields[2].Descriptor() + originDescTarget := originFields[3].Descriptor() // origin.TargetValidator is a validator for the "target" field. It is called by the builders before save. origin.TargetValidator = func() func(string) error { validators := originDescTarget.Validators @@ -105,7 +109,7 @@ func init() { } }() // originDescPortNumber is the schema descriptor for port_number field. - originDescPortNumber := originFields[3].Descriptor() + originDescPortNumber := originFields[4].Descriptor() // origin.PortNumberValidator is a validator for the "port_number" field. It is called by the builders before save. origin.PortNumberValidator = func() func(int) error { validators := originDescPortNumber.Validators @@ -123,11 +127,11 @@ func init() { } }() // originDescActive is the schema descriptor for active field. - originDescActive := originFields[4].Descriptor() + originDescActive := originFields[5].Descriptor() // origin.DefaultActive holds the default value on creation for the active field. origin.DefaultActive = originDescActive.Default.(bool) // originDescPoolID is the schema descriptor for pool_id field. - originDescPoolID := originFields[5].Descriptor() + originDescPoolID := originFields[6].Descriptor() // origin.PoolIDValidator is a validator for the "pool_id" field. It is called by the builders before save. origin.PoolIDValidator = originDescPoolID.Validators[0].(func(string) error) // originDescID is the schema descriptor for id field. diff --git a/internal/ent/schema/origin.go b/internal/ent/schema/origin.go index 66d38755c..1c56e388a 100644 --- a/internal/ent/schema/origin.go +++ b/internal/ent/schema/origin.go @@ -15,6 +15,8 @@ import ( "go.infratographer.com/load-balancer-api/x/pubsubinfo" ) +var defaultOriginWeight int32 = 100 + // Origin holds the schema definition for the Origin entity. type Origin struct { ent.Schema @@ -40,6 +42,11 @@ func (Origin) Fields() []ent.Field { Annotations( entgql.OrderField("name"), ), + field.Int32("weight"). + Default(defaultOriginWeight). + Annotations( + entgql.OrderField("weight"), + ), field.String("target"). NotEmpty(). Validate(validations.IPAddress). diff --git a/internal/graphapi/gen_server.go b/internal/graphapi/gen_server.go index eb6e27153..a9a6bbd4e 100644 --- a/internal/graphapi/gen_server.go +++ b/internal/graphapi/gen_server.go @@ -106,6 +106,7 @@ type ComplexityRoot struct { PortNumber func(childComplexity int) int Target func(childComplexity int) int UpdatedAt func(childComplexity int) int + Weight func(childComplexity int) int } LoadBalancerOriginConnection struct { @@ -610,6 +611,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.LoadBalancerOrigin.UpdatedAt(childComplexity), true + case "LoadBalancerOrigin.weight": + if e.complexity.LoadBalancerOrigin.Weight == nil { + break + } + + return e.complexity.LoadBalancerOrigin.Weight(childComplexity), true + case "LoadBalancerOriginConnection.edges": if e.complexity.LoadBalancerOriginConnection.Edges == nil { break @@ -1500,6 +1508,7 @@ Input was generated by ent. """ input CreateLoadBalancerOriginInput { name: String! + weight: Int target: String! portNumber: Int! active: Boolean @@ -1605,6 +1614,7 @@ type LoadBalancerOrigin implements Node @key(fields: "id") @prefixedID(prefix: " createdAt: Time! updatedAt: Time! name: String! + weight: Int! target: String! portNumber: Int! active: Boolean! @@ -1639,6 +1649,7 @@ enum LoadBalancerOriginOrderField { CREATED_AT UPDATED_AT name + weight target number active @@ -1692,6 +1703,15 @@ input LoadBalancerOriginWhereInput { nameHasSuffix: String nameEqualFold: String nameContainsFold: String + """weight field predicates""" + weight: Int + weightNEQ: Int + weightIn: [Int!] + weightNotIn: [Int!] + weightGT: Int + weightGTE: Int + weightLT: Int + weightLTE: Int """target field predicates""" target: String targetNEQ: String @@ -2184,6 +2204,7 @@ Input was generated by ent. """ input UpdateLoadBalancerOriginInput { name: String + weight: Int target: String portNumber: Int active: Boolean @@ -3843,6 +3864,8 @@ func (ec *executionContext) fieldContext_Entity_findLoadBalancerOriginByID(ctx c return ec.fieldContext_LoadBalancerOrigin_updatedAt(ctx, field) case "name": return ec.fieldContext_LoadBalancerOrigin_name(ctx, field) + case "weight": + return ec.fieldContext_LoadBalancerOrigin_weight(ctx, field) case "target": return ec.fieldContext_LoadBalancerOrigin_target(ctx, field) case "portNumber": @@ -5145,6 +5168,50 @@ func (ec *executionContext) fieldContext_LoadBalancerOrigin_name(ctx context.Con return fc, nil } +func (ec *executionContext) _LoadBalancerOrigin_weight(ctx context.Context, field graphql.CollectedField, obj *generated.Origin) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_LoadBalancerOrigin_weight(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Weight, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(int32) + fc.Result = res + return ec.marshalNInt2int32(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_LoadBalancerOrigin_weight(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "LoadBalancerOrigin", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _LoadBalancerOrigin_target(ctx context.Context, field graphql.CollectedField, obj *generated.Origin) (ret graphql.Marshaler) { fc, err := ec.fieldContext_LoadBalancerOrigin_target(ctx, field) if err != nil { @@ -5577,6 +5644,8 @@ func (ec *executionContext) fieldContext_LoadBalancerOriginCreatePayload_loadBal return ec.fieldContext_LoadBalancerOrigin_updatedAt(ctx, field) case "name": return ec.fieldContext_LoadBalancerOrigin_name(ctx, field) + case "weight": + return ec.fieldContext_LoadBalancerOrigin_weight(ctx, field) case "target": return ec.fieldContext_LoadBalancerOrigin_target(ctx, field) case "portNumber": @@ -5682,6 +5751,8 @@ func (ec *executionContext) fieldContext_LoadBalancerOriginEdge_node(ctx context return ec.fieldContext_LoadBalancerOrigin_updatedAt(ctx, field) case "name": return ec.fieldContext_LoadBalancerOrigin_name(ctx, field) + case "weight": + return ec.fieldContext_LoadBalancerOrigin_weight(ctx, field) case "target": return ec.fieldContext_LoadBalancerOrigin_target(ctx, field) case "portNumber": @@ -5790,6 +5861,8 @@ func (ec *executionContext) fieldContext_LoadBalancerOriginUpdatePayload_loadBal return ec.fieldContext_LoadBalancerOrigin_updatedAt(ctx, field) case "name": return ec.fieldContext_LoadBalancerOrigin_name(ctx, field) + case "weight": + return ec.fieldContext_LoadBalancerOrigin_weight(ctx, field) case "target": return ec.fieldContext_LoadBalancerOrigin_target(ctx, field) case "portNumber": @@ -12021,7 +12094,7 @@ func (ec *executionContext) unmarshalInputCreateLoadBalancerOriginInput(ctx cont asMap[k] = v } - fieldsInOrder := [...]string{"name", "target", "portNumber", "active", "poolID"} + fieldsInOrder := [...]string{"name", "weight", "target", "portNumber", "active", "poolID"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -12037,6 +12110,15 @@ func (ec *executionContext) unmarshalInputCreateLoadBalancerOriginInput(ctx cont return it, err } it.Name = data + case "weight": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weight")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.Weight = data case "target": var err error @@ -12329,7 +12411,7 @@ func (ec *executionContext) unmarshalInputLoadBalancerOriginWhereInput(ctx conte asMap[k] = v } - fieldsInOrder := [...]string{"not", "and", "or", "id", "idNEQ", "idIn", "idNotIn", "idGT", "idGTE", "idLT", "idLTE", "createdAt", "createdAtNEQ", "createdAtIn", "createdAtNotIn", "createdAtGT", "createdAtGTE", "createdAtLT", "createdAtLTE", "updatedAt", "updatedAtNEQ", "updatedAtIn", "updatedAtNotIn", "updatedAtGT", "updatedAtGTE", "updatedAtLT", "updatedAtLTE", "name", "nameNEQ", "nameIn", "nameNotIn", "nameGT", "nameGTE", "nameLT", "nameLTE", "nameContains", "nameHasPrefix", "nameHasSuffix", "nameEqualFold", "nameContainsFold", "target", "targetNEQ", "targetIn", "targetNotIn", "targetGT", "targetGTE", "targetLT", "targetLTE", "targetContains", "targetHasPrefix", "targetHasSuffix", "targetEqualFold", "targetContainsFold", "portNumber", "portNumberNEQ", "portNumberIn", "portNumberNotIn", "portNumberGT", "portNumberGTE", "portNumberLT", "portNumberLTE", "active", "activeNEQ", "hasPool", "hasPoolWith"} + fieldsInOrder := [...]string{"not", "and", "or", "id", "idNEQ", "idIn", "idNotIn", "idGT", "idGTE", "idLT", "idLTE", "createdAt", "createdAtNEQ", "createdAtIn", "createdAtNotIn", "createdAtGT", "createdAtGTE", "createdAtLT", "createdAtLTE", "updatedAt", "updatedAtNEQ", "updatedAtIn", "updatedAtNotIn", "updatedAtGT", "updatedAtGTE", "updatedAtLT", "updatedAtLTE", "name", "nameNEQ", "nameIn", "nameNotIn", "nameGT", "nameGTE", "nameLT", "nameLTE", "nameContains", "nameHasPrefix", "nameHasSuffix", "nameEqualFold", "nameContainsFold", "weight", "weightNEQ", "weightIn", "weightNotIn", "weightGT", "weightGTE", "weightLT", "weightLTE", "target", "targetNEQ", "targetIn", "targetNotIn", "targetGT", "targetGTE", "targetLT", "targetLTE", "targetContains", "targetHasPrefix", "targetHasSuffix", "targetEqualFold", "targetContainsFold", "portNumber", "portNumberNEQ", "portNumberIn", "portNumberNotIn", "portNumberGT", "portNumberGTE", "portNumberLT", "portNumberLTE", "active", "activeNEQ", "hasPool", "hasPoolWith"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -12696,6 +12778,78 @@ func (ec *executionContext) unmarshalInputLoadBalancerOriginWhereInput(ctx conte return it, err } it.NameContainsFold = data + case "weight": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weight")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.Weight = data + case "weightNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightNEQ")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.WeightNEQ = data + case "weightIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightIn")) + data, err := ec.unmarshalOInt2ᚕint32ᚄ(ctx, v) + if err != nil { + return it, err + } + it.WeightIn = data + case "weightNotIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightNotIn")) + data, err := ec.unmarshalOInt2ᚕint32ᚄ(ctx, v) + if err != nil { + return it, err + } + it.WeightNotIn = data + case "weightGT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightGT")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.WeightGT = data + case "weightGTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightGTE")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.WeightGTE = data + case "weightLT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightLT")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.WeightLT = data + case "weightLTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weightLTE")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.WeightLTE = data case "target": var err error @@ -14870,7 +15024,7 @@ func (ec *executionContext) unmarshalInputUpdateLoadBalancerOriginInput(ctx cont asMap[k] = v } - fieldsInOrder := [...]string{"name", "target", "portNumber", "active"} + fieldsInOrder := [...]string{"name", "weight", "target", "portNumber", "active"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -14886,6 +15040,15 @@ func (ec *executionContext) unmarshalInputUpdateLoadBalancerOriginInput(ctx cont return it, err } it.Name = data + case "weight": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("weight")) + data, err := ec.unmarshalOInt2ᚖint32(ctx, v) + if err != nil { + return it, err + } + it.Weight = data case "target": var err error @@ -15827,6 +15990,11 @@ func (ec *executionContext) _LoadBalancerOrigin(ctx context.Context, sel ast.Sel if out.Values[i] == graphql.Null { atomic.AddUint32(&out.Invalids, 1) } + case "weight": + out.Values[i] = ec._LoadBalancerOrigin_weight(ctx, field, obj) + if out.Values[i] == graphql.Null { + atomic.AddUint32(&out.Invalids, 1) + } case "target": out.Values[i] = ec._LoadBalancerOrigin_target(ctx, field, obj) if out.Values[i] == graphql.Null { @@ -18237,6 +18405,21 @@ func (ec *executionContext) marshalNInt2int(ctx context.Context, sel ast.Selecti return res } +func (ec *executionContext) unmarshalNInt2int32(ctx context.Context, v interface{}) (int32, error) { + res, err := graphql.UnmarshalInt32(v) + return res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalNInt2int32(ctx context.Context, sel ast.SelectionSet, v int32) graphql.Marshaler { + res := graphql.MarshalInt32(v) + if res == graphql.Null { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + } + return res +} + func (ec *executionContext) marshalNLoadBalancer2goᚗinfratographerᚗcomᚋloadᚑbalancerᚑapiᚋinternalᚋentᚋgeneratedᚐLoadBalancer(ctx context.Context, sel ast.SelectionSet, v generated.LoadBalancer) graphql.Marshaler { return ec._LoadBalancer(ctx, sel, &v) } @@ -19235,6 +19418,44 @@ func (ec *executionContext) marshalOID2ᚖgoᚗinfratographerᚗcomᚋxᚋgidx return v } +func (ec *executionContext) unmarshalOInt2ᚕint32ᚄ(ctx context.Context, v interface{}) ([]int32, error) { + if v == nil { + return nil, nil + } + var vSlice []interface{} + if v != nil { + vSlice = graphql.CoerceList(v) + } + var err error + res := make([]int32, len(vSlice)) + for i := range vSlice { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) + res[i], err = ec.unmarshalNInt2int32(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + +func (ec *executionContext) marshalOInt2ᚕint32ᚄ(ctx context.Context, sel ast.SelectionSet, v []int32) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + for i := range v { + ret[i] = ec.marshalNInt2int32(ctx, sel, v[i]) + } + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + func (ec *executionContext) unmarshalOInt2ᚕintᚄ(ctx context.Context, v interface{}) ([]int, error) { if v == nil { return nil, nil @@ -19289,6 +19510,22 @@ func (ec *executionContext) marshalOInt2ᚖint(ctx context.Context, sel ast.Sele return res } +func (ec *executionContext) unmarshalOInt2ᚖint32(ctx context.Context, v interface{}) (*int32, error) { + if v == nil { + return nil, nil + } + res, err := graphql.UnmarshalInt32(v) + return &res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalOInt2ᚖint32(ctx context.Context, sel ast.SelectionSet, v *int32) graphql.Marshaler { + if v == nil { + return graphql.Null + } + res := graphql.MarshalInt32(*v) + return res +} + func (ec *executionContext) marshalOLoadBalancer2ᚖgoᚗinfratographerᚗcomᚋloadᚑbalancerᚑapiᚋinternalᚋentᚋgeneratedᚐLoadBalancer(ctx context.Context, sel ast.SelectionSet, v *generated.LoadBalancer) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/internal/graphapi/loadbalancer.resolvers.go b/internal/graphapi/loadbalancer.resolvers.go index 7362ec994..64a073a79 100644 --- a/internal/graphapi/loadbalancer.resolvers.go +++ b/internal/graphapi/loadbalancer.resolvers.go @@ -8,13 +8,12 @@ import ( "context" "database/sql" - "go.infratographer.com/permissions-api/pkg/permissions" - "go.infratographer.com/x/events" - "go.infratographer.com/x/gidx" - "go.infratographer.com/load-balancer-api/internal/ent/generated" "go.infratographer.com/load-balancer-api/internal/ent/generated/port" "go.infratographer.com/load-balancer-api/internal/ent/generated/predicate" + "go.infratographer.com/permissions-api/pkg/permissions" + "go.infratographer.com/x/events" + "go.infratographer.com/x/gidx" ) // LoadBalancerCreate is the resolver for the loadBalancerCreate field. diff --git a/internal/graphapi/origin.resolvers.go b/internal/graphapi/origin.resolvers.go index 4088c69a6..650f063da 100644 --- a/internal/graphapi/origin.resolvers.go +++ b/internal/graphapi/origin.resolvers.go @@ -7,10 +7,9 @@ package graphapi import ( "context" + "go.infratographer.com/load-balancer-api/internal/ent/generated" "go.infratographer.com/permissions-api/pkg/permissions" "go.infratographer.com/x/gidx" - - "go.infratographer.com/load-balancer-api/internal/ent/generated" ) // LoadBalancerOriginCreate is the resolver for the loadBalancerOriginCreate field. diff --git a/internal/graphapi/origin_test.go b/internal/graphapi/origin_test.go index c9984f724..d2795467d 100644 --- a/internal/graphapi/origin_test.go +++ b/internal/graphapi/origin_test.go @@ -162,6 +162,43 @@ func TestMutate_OriginCreate(t *testing.T) { }, errorMsg: "invalid ip address", }, + { + TestName: "Default weight to 100", + Input: graphclient.CreateLoadBalancerOriginInput{ + Name: "weightless", + Target: "1.2.3.4", + PortNumber: 22, + PoolID: pool1.ID, + Active: newBool(false), + }, + ExpectedOrigin: ent.LoadBalancerOrigin{ + Name: "weightless", + Target: "1.2.3.4", + PortNumber: 22, + PoolID: pool1.ID, + Weight: 100, + Active: false, + }, + }, + { + TestName: "Set specific weight", + Input: graphclient.CreateLoadBalancerOriginInput{ + Name: "weighted", + Target: "1.2.3.4", + PortNumber: 22, + PoolID: pool1.ID, + Active: newBool(false), + Weight: newInt64(50), + }, + ExpectedOrigin: ent.LoadBalancerOrigin{ + Name: "weighted", + Target: "1.2.3.4", + PortNumber: 22, + PoolID: pool1.ID, + Weight: 50, + Active: false, + }, + }, } for _, tt := range testCases { diff --git a/internal/graphapi/pool.resolvers.go b/internal/graphapi/pool.resolvers.go index 6e9df7680..890308bfa 100644 --- a/internal/graphapi/pool.resolvers.go +++ b/internal/graphapi/pool.resolvers.go @@ -8,14 +8,13 @@ import ( "context" "database/sql" - "go.infratographer.com/permissions-api/pkg/permissions" - "go.infratographer.com/x/gidx" - "go.infratographer.com/load-balancer-api/internal/ent/generated" "go.infratographer.com/load-balancer-api/internal/ent/generated/loadbalancer" "go.infratographer.com/load-balancer-api/internal/ent/generated/origin" "go.infratographer.com/load-balancer-api/internal/ent/generated/port" "go.infratographer.com/load-balancer-api/internal/ent/generated/predicate" + "go.infratographer.com/permissions-api/pkg/permissions" + "go.infratographer.com/x/gidx" ) // LoadBalancerPoolCreate is the resolver for the LoadBalancerPoolCreate field. diff --git a/internal/graphapi/port.resolvers.go b/internal/graphapi/port.resolvers.go index e973aea36..28fe4ddef 100644 --- a/internal/graphapi/port.resolvers.go +++ b/internal/graphapi/port.resolvers.go @@ -8,11 +8,10 @@ import ( "context" "strings" - "go.infratographer.com/permissions-api/pkg/permissions" - "go.infratographer.com/x/gidx" - "go.infratographer.com/load-balancer-api/internal/ent/generated" "go.infratographer.com/load-balancer-api/internal/ent/generated/pool" + "go.infratographer.com/permissions-api/pkg/permissions" + "go.infratographer.com/x/gidx" ) // LoadBalancerPortCreate is the resolver for the loadBalancerPortCreate field. diff --git a/internal/graphapi/provider.resolvers.go b/internal/graphapi/provider.resolvers.go index f6365ed0f..5f98234b9 100644 --- a/internal/graphapi/provider.resolvers.go +++ b/internal/graphapi/provider.resolvers.go @@ -7,10 +7,9 @@ package graphapi import ( "context" + "go.infratographer.com/load-balancer-api/internal/ent/generated" "go.infratographer.com/permissions-api/pkg/permissions" "go.infratographer.com/x/gidx" - - "go.infratographer.com/load-balancer-api/internal/ent/generated" ) // LoadBalancerProviderCreate is the resolver for the loadBalancerProviderCreate field. diff --git a/internal/graphclient/gen_models.go b/internal/graphclient/gen_models.go index e093e54ac..f05f180d9 100644 --- a/internal/graphclient/gen_models.go +++ b/internal/graphclient/gen_models.go @@ -49,6 +49,7 @@ type CreateLoadBalancerInput struct { // Input was generated by ent. type CreateLoadBalancerOriginInput struct { Name string `json:"name"` + Weight *int64 `json:"weight,omitempty"` Target string `json:"target"` PortNumber int64 `json:"portNumber"` Active *bool `json:"active,omitempty"` @@ -152,6 +153,7 @@ type LoadBalancerOrigin struct { CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` Name string `json:"name"` + Weight int64 `json:"weight"` Target string `json:"target"` PortNumber int64 `json:"portNumber"` Active bool `json:"active"` @@ -257,6 +259,15 @@ type LoadBalancerOriginWhereInput struct { NameHasSuffix *string `json:"nameHasSuffix,omitempty"` NameEqualFold *string `json:"nameEqualFold,omitempty"` NameContainsFold *string `json:"nameContainsFold,omitempty"` + // weight field predicates + Weight *int64 `json:"weight,omitempty"` + WeightNeq *int64 `json:"weightNEQ,omitempty"` + WeightIn []int64 `json:"weightIn,omitempty"` + WeightNotIn []int64 `json:"weightNotIn,omitempty"` + WeightGt *int64 `json:"weightGT,omitempty"` + WeightGte *int64 `json:"weightGTE,omitempty"` + WeightLt *int64 `json:"weightLT,omitempty"` + WeightLte *int64 `json:"weightLTE,omitempty"` // target field predicates Target *string `json:"target,omitempty"` TargetNeq *string `json:"targetNEQ,omitempty"` @@ -756,6 +767,7 @@ type UpdateLoadBalancerInput struct { // Input was generated by ent. type UpdateLoadBalancerOriginInput struct { Name *string `json:"name,omitempty"` + Weight *int64 `json:"weight,omitempty"` Target *string `json:"target,omitempty"` PortNumber *int64 `json:"portNumber,omitempty"` Active *bool `json:"active,omitempty"` @@ -849,6 +861,7 @@ const ( LoadBalancerOriginOrderFieldCreatedAt LoadBalancerOriginOrderField = "CREATED_AT" LoadBalancerOriginOrderFieldUpdatedAt LoadBalancerOriginOrderField = "UPDATED_AT" LoadBalancerOriginOrderFieldName LoadBalancerOriginOrderField = "name" + LoadBalancerOriginOrderFieldWeight LoadBalancerOriginOrderField = "weight" LoadBalancerOriginOrderFieldTarget LoadBalancerOriginOrderField = "target" LoadBalancerOriginOrderFieldNumber LoadBalancerOriginOrderField = "number" LoadBalancerOriginOrderFieldActive LoadBalancerOriginOrderField = "active" @@ -858,6 +871,7 @@ var AllLoadBalancerOriginOrderField = []LoadBalancerOriginOrderField{ LoadBalancerOriginOrderFieldCreatedAt, LoadBalancerOriginOrderFieldUpdatedAt, LoadBalancerOriginOrderFieldName, + LoadBalancerOriginOrderFieldWeight, LoadBalancerOriginOrderFieldTarget, LoadBalancerOriginOrderFieldNumber, LoadBalancerOriginOrderFieldActive, @@ -865,7 +879,7 @@ var AllLoadBalancerOriginOrderField = []LoadBalancerOriginOrderField{ func (e LoadBalancerOriginOrderField) IsValid() bool { switch e { - case LoadBalancerOriginOrderFieldCreatedAt, LoadBalancerOriginOrderFieldUpdatedAt, LoadBalancerOriginOrderFieldName, LoadBalancerOriginOrderFieldTarget, LoadBalancerOriginOrderFieldNumber, LoadBalancerOriginOrderFieldActive: + case LoadBalancerOriginOrderFieldCreatedAt, LoadBalancerOriginOrderFieldUpdatedAt, LoadBalancerOriginOrderFieldName, LoadBalancerOriginOrderFieldWeight, LoadBalancerOriginOrderFieldTarget, LoadBalancerOriginOrderFieldNumber, LoadBalancerOriginOrderFieldActive: return true } return false diff --git a/internal/graphclient/schema/schema.graphql b/internal/graphclient/schema/schema.graphql index 191e853d2..654c61ad1 100644 --- a/internal/graphclient/schema/schema.graphql +++ b/internal/graphclient/schema/schema.graphql @@ -28,6 +28,7 @@ Input was generated by ent. """ input CreateLoadBalancerOriginInput { name: String! + weight: Int target: String! portNumber: Int! active: Boolean @@ -151,6 +152,7 @@ type LoadBalancerOrigin implements Node @key(fields: "id") @prefixedID(prefix: " createdAt: Time! updatedAt: Time! name: String! + weight: Int! target: String! portNumber: Int! active: Boolean! @@ -195,6 +197,7 @@ enum LoadBalancerOriginOrderField { CREATED_AT UPDATED_AT name + weight target number active @@ -253,6 +256,15 @@ input LoadBalancerOriginWhereInput { nameHasSuffix: String nameEqualFold: String nameContainsFold: String + """weight field predicates""" + weight: Int + weightNEQ: Int + weightIn: [Int!] + weightNotIn: [Int!] + weightGT: Int + weightGTE: Int + weightLT: Int + weightLTE: Int """target field predicates""" target: String targetNEQ: String @@ -933,6 +945,7 @@ Input was generated by ent. """ input UpdateLoadBalancerOriginInput { name: String + weight: Int target: String portNumber: Int active: Boolean diff --git a/pkg/client/types.go b/pkg/client/types.go index 94ca4cc3a..dd4033463 100644 --- a/pkg/client/types.go +++ b/pkg/client/types.go @@ -6,6 +6,7 @@ type OriginNode struct { Name string Target string PortNumber int64 + Weight int64 Active bool } diff --git a/schema.graphql b/schema.graphql index 62956ba4d..8fef4e101 100644 --- a/schema.graphql +++ b/schema.graphql @@ -16,6 +16,7 @@ Input was generated by ent. """ input CreateLoadBalancerOriginInput { name: String! + weight: Int target: String! portNumber: Int! active: Boolean @@ -138,6 +139,7 @@ type LoadBalancerOrigin implements Node @key(fields: "id") @prefixedID(prefix: " createdAt: Time! updatedAt: Time! name: String! + weight: Int! target: String! portNumber: Int! active: Boolean! @@ -182,6 +184,7 @@ enum LoadBalancerOriginOrderField { CREATED_AT UPDATED_AT name + weight target number active @@ -240,6 +243,15 @@ input LoadBalancerOriginWhereInput { nameHasSuffix: String nameEqualFold: String nameContainsFold: String + """weight field predicates""" + weight: Int + weightNEQ: Int + weightIn: [Int!] + weightNotIn: [Int!] + weightGT: Int + weightGTE: Int + weightLT: Int + weightLTE: Int """target field predicates""" target: String targetNEQ: String @@ -920,6 +932,7 @@ Input was generated by ent. """ input UpdateLoadBalancerOriginInput { name: String + weight: Int target: String portNumber: Int active: Boolean diff --git a/schema/ent.graphql b/schema/ent.graphql index 16e646950..29c303ef2 100644 --- a/schema/ent.graphql +++ b/schema/ent.graphql @@ -17,6 +17,7 @@ Input was generated by ent. """ input CreateLoadBalancerOriginInput { name: String! + weight: Int target: String! portNumber: Int! active: Boolean @@ -122,6 +123,7 @@ type LoadBalancerOrigin implements Node @key(fields: "id") @prefixedID(prefix: " createdAt: Time! updatedAt: Time! name: String! + weight: Int! target: String! portNumber: Int! active: Boolean! @@ -156,6 +158,7 @@ enum LoadBalancerOriginOrderField { CREATED_AT UPDATED_AT name + weight target number active @@ -209,6 +212,15 @@ input LoadBalancerOriginWhereInput { nameHasSuffix: String nameEqualFold: String nameContainsFold: String + """weight field predicates""" + weight: Int + weightNEQ: Int + weightIn: [Int!] + weightNotIn: [Int!] + weightGT: Int + weightGTE: Int + weightLT: Int + weightLTE: Int """target field predicates""" target: String targetNEQ: String @@ -701,6 +713,7 @@ Input was generated by ent. """ input UpdateLoadBalancerOriginInput { name: String + weight: Int target: String portNumber: Int active: Boolean