From 6e49e4991ea9748b955fa0a676775b7ad9c03938 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 3 Aug 2022 15:05:28 -0400 Subject: [PATCH] basic overview implementation --- controller/list.go | 2 +- controller/overview.go | 43 ++++++++++- .../metadata/overview_responses.go | 8 +- rest_model_zrok/environment_services_list.go | 73 +++++++++++++++++++ rest_server_zrok/embedded_spec.go | 16 +++- .../operations/metadata/overview_responses.go | 19 +++-- specs/zrok.yml | 6 +- 7 files changed, 149 insertions(+), 18 deletions(-) create mode 100644 rest_model_zrok/environment_services_list.go diff --git a/controller/list.go b/controller/list.go index 8945b3a1..c92a9217 100644 --- a/controller/list.go +++ b/controller/list.go @@ -16,7 +16,7 @@ func listEnvironmentsHandler(_ metadata.ListEnvironmentsParams, principal *rest_ defer func() { _ = tx.Rollback() }() envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx) if err != nil { - logrus.Errorf("error finding identities for '%v': %v", principal.Username, err) + logrus.Errorf("error finding environments for '%v': %v", principal.Username, err) return metadata.NewListEnvironmentsInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) } var out rest_model_zrok.Environments diff --git a/controller/overview.go b/controller/overview.go index 772c37c7..dbf95a1d 100644 --- a/controller/overview.go +++ b/controller/overview.go @@ -4,8 +4,49 @@ import ( "github.com/go-openapi/runtime/middleware" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/metadata" + "github.com/sirupsen/logrus" ) func overviewHandler(_ metadata.OverviewParams, principal *rest_model_zrok.Principal) middleware.Responder { - return nil + tx, err := str.Begin() + if err != nil { + logrus.Errorf("error starting transaction: %v", err) + return metadata.NewOverviewInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + defer func() { _ = tx.Rollback() }() + envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx) + if err != nil { + logrus.Errorf("error finding environments for '%v': %v", principal.Username, err) + return metadata.NewOverviewInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + var out rest_model_zrok.EnvironmentServicesList + for _, env := range envs { + svcs, err := str.FindServicesForEnvironment(env.Id, tx) + if err != nil { + logrus.Errorf("error finding services for environment '%v': %v", env.ZitiIdentityId, err) + return metadata.NewOverviewInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + es := &rest_model_zrok.EnvironmentServices{ + Environment: &rest_model_zrok.Environment{ + Active: env.Active, + Address: env.Address, + CreatedAt: env.CreatedAt.String(), + Description: env.Description, + Host: env.Host, + UpdatedAt: env.UpdatedAt.String(), + ZitiIdentityID: env.ZitiIdentityId, + }, + } + for _, svc := range svcs { + es.Services = append(es.Services, &rest_model_zrok.Service{ + Active: svc.Active, + CreatedAt: svc.CreatedAt.String(), + Endpoint: svc.Endpoint, + UpdatedAt: svc.UpdatedAt.String(), + ZitiServiceID: svc.ZitiServiceId, + }) + } + out = append(out, es) + } + return metadata.NewOverviewOK().WithPayload(out) } diff --git a/rest_client_zrok/metadata/overview_responses.go b/rest_client_zrok/metadata/overview_responses.go index ff1545ad..797c3f5c 100644 --- a/rest_client_zrok/metadata/overview_responses.go +++ b/rest_client_zrok/metadata/overview_responses.go @@ -50,22 +50,20 @@ func NewOverviewOK() *OverviewOK { overview returned */ type OverviewOK struct { - Payload *rest_model_zrok.EnvironmentServices + Payload rest_model_zrok.EnvironmentServicesList } func (o *OverviewOK) Error() string { return fmt.Sprintf("[GET /overview][%d] overviewOK %+v", 200, o.Payload) } -func (o *OverviewOK) GetPayload() *rest_model_zrok.EnvironmentServices { +func (o *OverviewOK) GetPayload() rest_model_zrok.EnvironmentServicesList { return o.Payload } func (o *OverviewOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { - o.Payload = new(rest_model_zrok.EnvironmentServices) - // response payload - if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + if err := consumer.Consume(response.Body(), &o.Payload); err != nil && err != io.EOF { return err } diff --git a/rest_model_zrok/environment_services_list.go b/rest_model_zrok/environment_services_list.go new file mode 100644 index 00000000..26bbc2f1 --- /dev/null +++ b/rest_model_zrok/environment_services_list.go @@ -0,0 +1,73 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package rest_model_zrok + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "strconv" + + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" +) + +// EnvironmentServicesList environment services list +// +// swagger:model environmentServicesList +type EnvironmentServicesList []*EnvironmentServices + +// Validate validates this environment services list +func (m EnvironmentServicesList) Validate(formats strfmt.Registry) error { + var res []error + + for i := 0; i < len(m); i++ { + if swag.IsZero(m[i]) { // not required + continue + } + + if m[i] != nil { + if err := m[i].Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName(strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName(strconv.Itoa(i)) + } + return err + } + } + + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +// ContextValidate validate this environment services list based on the context it is used +func (m EnvironmentServicesList) ContextValidate(ctx context.Context, formats strfmt.Registry) error { + var res []error + + for i := 0; i < len(m); i++ { + + if m[i] != nil { + if err := m[i].ContextValidate(ctx, formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName(strconv.Itoa(i)) + } else if ce, ok := err.(*errors.CompositeError); ok { + return ce.ValidateName(strconv.Itoa(i)) + } + return err + } + } + + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/rest_server_zrok/embedded_spec.go b/rest_server_zrok/embedded_spec.go index a7460684..de6703e5 100644 --- a/rest_server_zrok/embedded_spec.go +++ b/rest_server_zrok/embedded_spec.go @@ -184,7 +184,7 @@ func init() { "200": { "description": "overview returned", "schema": { - "$ref": "#/definitions/environmentServices" + "$ref": "#/definitions/environmentServicesList" } }, "500": { @@ -373,6 +373,12 @@ func init() { } } }, + "environmentServicesList": { + "type": "array", + "items": { + "$ref": "#/definitions/environmentServices" + } + }, "environments": { "type": "array", "items": { @@ -645,7 +651,7 @@ func init() { "200": { "description": "overview returned", "schema": { - "$ref": "#/definitions/environmentServices" + "$ref": "#/definitions/environmentServicesList" } }, "500": { @@ -834,6 +840,12 @@ func init() { } } }, + "environmentServicesList": { + "type": "array", + "items": { + "$ref": "#/definitions/environmentServices" + } + }, "environments": { "type": "array", "items": { diff --git a/rest_server_zrok/operations/metadata/overview_responses.go b/rest_server_zrok/operations/metadata/overview_responses.go index 95d49ce5..79105e46 100644 --- a/rest_server_zrok/operations/metadata/overview_responses.go +++ b/rest_server_zrok/operations/metadata/overview_responses.go @@ -25,7 +25,7 @@ type OverviewOK struct { /* In: Body */ - Payload *rest_model_zrok.EnvironmentServices `json:"body,omitempty"` + Payload rest_model_zrok.EnvironmentServicesList `json:"body,omitempty"` } // NewOverviewOK creates OverviewOK with default headers values @@ -35,13 +35,13 @@ func NewOverviewOK() *OverviewOK { } // WithPayload adds the payload to the overview o k response -func (o *OverviewOK) WithPayload(payload *rest_model_zrok.EnvironmentServices) *OverviewOK { +func (o *OverviewOK) WithPayload(payload rest_model_zrok.EnvironmentServicesList) *OverviewOK { o.Payload = payload return o } // SetPayload sets the payload to the overview o k response -func (o *OverviewOK) SetPayload(payload *rest_model_zrok.EnvironmentServices) { +func (o *OverviewOK) SetPayload(payload rest_model_zrok.EnvironmentServicesList) { o.Payload = payload } @@ -49,11 +49,14 @@ func (o *OverviewOK) SetPayload(payload *rest_model_zrok.EnvironmentServices) { func (o *OverviewOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { rw.WriteHeader(200) - if o.Payload != nil { - payload := o.Payload - if err := producer.Produce(rw, payload); err != nil { - panic(err) // let the recovery middleware deal with this - } + payload := o.Payload + if payload == nil { + // return empty array + payload = rest_model_zrok.EnvironmentServicesList{} + } + + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this } } diff --git a/specs/zrok.yml b/specs/zrok.yml index a77da042..a724c63a 100644 --- a/specs/zrok.yml +++ b/specs/zrok.yml @@ -108,7 +108,7 @@ paths: 200: description: overview returned schema: - $ref: "#/definitions/environmentServices" + $ref: "#/definitions/environmentServicesList" 500: description: internal server error schema: @@ -224,6 +224,10 @@ definitions: updatedAt: type: string + environmentServicesList: + type: array + items: + $ref: "#/definitions/environmentServices" environmentServices: type: object properties: