From 79ffa4edc64c631fac896d54cb3ef47eca2a6955 Mon Sep 17 00:00:00 2001 From: Vasil Averyanau Date: Fri, 20 Dec 2024 13:15:05 +0100 Subject: [PATCH] feat(swagger): adds /cloud/metadata to agent api definition. This adds /cloud/metadata api call to agent which should return cloud instance metadata, such as instance_type and cloud_provider. Refs: #4130 --- v3/swagger/agent.json | 49 +++++++ .../client/operations/metadata_parameters.go | 113 +++++++++++++++ .../client/operations/metadata_responses.go | 135 ++++++++++++++++++ .../client/operations/operations_client.go | 37 +++++ .../gen/agent/models/instance_metadata.go | 111 ++++++++++++++ 5 files changed, 445 insertions(+) create mode 100644 v3/swagger/gen/agent/client/operations/metadata_parameters.go create mode 100644 v3/swagger/gen/agent/client/operations/metadata_responses.go create mode 100644 v3/swagger/gen/agent/models/instance_metadata.go diff --git a/v3/swagger/agent.json b/v3/swagger/agent.json index 028871831..af87e1652 100644 --- a/v3/swagger/agent.json +++ b/v3/swagger/agent.json @@ -966,6 +966,34 @@ }, "security": [] } + }, + "/cloud/metadata": { + "get": { + "description": "Collect instance metadata on a node", + "summary": "Return instance metadata", + "operationId": "Metadata", + "produces": [ + "application/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Instance metadata", + "schema": { + "$ref": "#/definitions/InstanceMetadata" + }, + "headers": {} + }, + "default": { + "description": "Server error", + "schema": { + "$ref": "#/definitions/ErrorResponse" + }, + "headers": {} + } + }, + "security": [] + } } }, "definitions": { @@ -1694,6 +1722,27 @@ "format": "uint64" } } + }, + "InstanceMetadata": { + "title": "instance metadata", + "description": "Information about instance on which agent is running", + "type": "object", + "properties": { + "instance_type": { + "description": "Cloud machine type, e.g. Standard-A3", + "type": "string" + }, + "cloud_provider": { + "description": "Cloud provider", + "type": "string", + "enum": [ + "aws", + "gcp", + "azure", + "" + ] + } + } } }, "tags": [] diff --git a/v3/swagger/gen/agent/client/operations/metadata_parameters.go b/v3/swagger/gen/agent/client/operations/metadata_parameters.go new file mode 100644 index 000000000..aede911a5 --- /dev/null +++ b/v3/swagger/gen/agent/client/operations/metadata_parameters.go @@ -0,0 +1,113 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "context" + "net/http" + "time" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + cr "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" +) + +// NewMetadataParams creates a new MetadataParams object +// with the default values initialized. +func NewMetadataParams() *MetadataParams { + + return &MetadataParams{ + + timeout: cr.DefaultTimeout, + } +} + +// NewMetadataParamsWithTimeout creates a new MetadataParams object +// with the default values initialized, and the ability to set a timeout on a request +func NewMetadataParamsWithTimeout(timeout time.Duration) *MetadataParams { + + return &MetadataParams{ + + timeout: timeout, + } +} + +// NewMetadataParamsWithContext creates a new MetadataParams object +// with the default values initialized, and the ability to set a context for a request +func NewMetadataParamsWithContext(ctx context.Context) *MetadataParams { + + return &MetadataParams{ + + Context: ctx, + } +} + +// NewMetadataParamsWithHTTPClient creates a new MetadataParams object +// with the default values initialized, and the ability to set a custom HTTPClient for a request +func NewMetadataParamsWithHTTPClient(client *http.Client) *MetadataParams { + + return &MetadataParams{ + HTTPClient: client, + } +} + +/* +MetadataParams contains all the parameters to send to the API endpoint +for the metadata operation typically these are written to a http.Request +*/ +type MetadataParams struct { + timeout time.Duration + Context context.Context + HTTPClient *http.Client +} + +// WithTimeout adds the timeout to the metadata params +func (o *MetadataParams) WithTimeout(timeout time.Duration) *MetadataParams { + o.SetTimeout(timeout) + return o +} + +// SetTimeout adds the timeout to the metadata params +func (o *MetadataParams) SetTimeout(timeout time.Duration) { + o.timeout = timeout +} + +// WithContext adds the context to the metadata params +func (o *MetadataParams) WithContext(ctx context.Context) *MetadataParams { + o.SetContext(ctx) + return o +} + +// SetContext adds the context to the metadata params +func (o *MetadataParams) SetContext(ctx context.Context) { + o.Context = ctx +} + +// WithHTTPClient adds the HTTPClient to the metadata params +func (o *MetadataParams) WithHTTPClient(client *http.Client) *MetadataParams { + o.SetHTTPClient(client) + return o +} + +// SetHTTPClient adds the HTTPClient to the metadata params +func (o *MetadataParams) SetHTTPClient(client *http.Client) { + o.HTTPClient = client +} + +// WriteToRequest writes these params to a swagger request +func (o *MetadataParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { + + if err := r.SetTimeout(o.timeout); err != nil { + return err + } + var res []error + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} diff --git a/v3/swagger/gen/agent/client/operations/metadata_responses.go b/v3/swagger/gen/agent/client/operations/metadata_responses.go new file mode 100644 index 000000000..31089cc9e --- /dev/null +++ b/v3/swagger/gen/agent/client/operations/metadata_responses.go @@ -0,0 +1,135 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package operations + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "fmt" + "io" + "strconv" + "strings" + + "github.com/go-openapi/runtime" + "github.com/go-openapi/strfmt" + + "github.com/scylladb/scylla-manager/v3/swagger/gen/agent/models" +) + +// MetadataReader is a Reader for the Metadata structure. +type MetadataReader struct { + formats strfmt.Registry +} + +// ReadResponse reads a server response into the received o. +func (o *MetadataReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { + switch response.Code() { + case 200: + result := NewMetadataOK() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return result, nil + default: + result := NewMetadataDefault(response.Code()) + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + if response.Code()/100 == 2 { + return result, nil + } + return nil, result + } +} + +// NewMetadataOK creates a MetadataOK with default headers values +func NewMetadataOK() *MetadataOK { + return &MetadataOK{} +} + +/* +MetadataOK handles this case with default header values. + +Instance metadata +*/ +type MetadataOK struct { + Payload *models.InstanceMetadata + JobID int64 +} + +func (o *MetadataOK) GetPayload() *models.InstanceMetadata { + return o.Payload +} + +func (o *MetadataOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.InstanceMetadata) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + if jobIDHeader := response.GetHeader("x-rclone-jobid"); jobIDHeader != "" { + jobID, err := strconv.ParseInt(jobIDHeader, 10, 64) + if err != nil { + return err + } + + o.JobID = jobID + } + return nil +} + +// NewMetadataDefault creates a MetadataDefault with default headers values +func NewMetadataDefault(code int) *MetadataDefault { + return &MetadataDefault{ + _statusCode: code, + } +} + +/* +MetadataDefault handles this case with default header values. + +Server error +*/ +type MetadataDefault struct { + _statusCode int + + Payload *models.ErrorResponse + JobID int64 +} + +// Code gets the status code for the metadata default response +func (o *MetadataDefault) Code() int { + return o._statusCode +} + +func (o *MetadataDefault) GetPayload() *models.ErrorResponse { + return o.Payload +} + +func (o *MetadataDefault) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + o.Payload = new(models.ErrorResponse) + + // response payload + if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF { + return err + } + + if jobIDHeader := response.GetHeader("x-rclone-jobid"); jobIDHeader != "" { + jobID, err := strconv.ParseInt(jobIDHeader, 10, 64) + if err != nil { + return err + } + + o.JobID = jobID + } + return nil +} + +func (o *MetadataDefault) Error() string { + return fmt.Sprintf("agent [HTTP %d] %s", o._statusCode, strings.TrimRight(o.Payload.Message, ".")) +} diff --git a/v3/swagger/gen/agent/client/operations/operations_client.go b/v3/swagger/gen/agent/client/operations/operations_client.go index d64a8fe64..82b098751 100644 --- a/v3/swagger/gen/agent/client/operations/operations_client.go +++ b/v3/swagger/gen/agent/client/operations/operations_client.go @@ -43,6 +43,8 @@ type ClientService interface { JobStop(params *JobStopParams) (*JobStopOK, error) + Metadata(params *MetadataParams) (*MetadataOK, error) + NodeInfo(params *NodeInfoParams) (*NodeInfoOK, error) OperationsAbout(params *OperationsAboutParams) (*OperationsAboutOK, error) @@ -393,6 +395,41 @@ func (a *Client) JobStop(params *JobStopParams) (*JobStopOK, error) { return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) } +/* +Metadata returns instance metadata + +Collect instance metadata on a node +*/ +func (a *Client) Metadata(params *MetadataParams) (*MetadataOK, error) { + // TODO: Validate the params before sending + if params == nil { + params = NewMetadataParams() + } + + result, err := a.transport.Submit(&runtime.ClientOperation{ + ID: "Metadata", + Method: "GET", + PathPattern: "/cloud/metadata", + ProducesMediaTypes: []string{"application/json"}, + ConsumesMediaTypes: []string{"application/json"}, + Schemes: []string{"http"}, + Params: params, + Reader: &MetadataReader{formats: a.formats}, + Context: params.Context, + Client: params.HTTPClient, + }) + if err != nil { + return nil, err + } + success, ok := result.(*MetadataOK) + if ok { + return success, nil + } + // unexpected success response + unexpectedSuccess := result.(*MetadataDefault) + return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) +} + /* NodeInfo gets information about scylla node diff --git a/v3/swagger/gen/agent/models/instance_metadata.go b/v3/swagger/gen/agent/models/instance_metadata.go new file mode 100644 index 000000000..ca21a3c35 --- /dev/null +++ b/v3/swagger/gen/agent/models/instance_metadata.go @@ -0,0 +1,111 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "encoding/json" + + "github.com/go-openapi/errors" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" + "github.com/go-openapi/validate" +) + +// InstanceMetadata instance metadata +// +// # Information about instance on which agent is running +// +// swagger:model InstanceMetadata +type InstanceMetadata struct { + + // Cloud provider + // Enum: [aws gcp azure ] + CloudProvider string `json:"cloud_provider,omitempty"` + + // Cloud machine type, e.g. Standard-A3 + InstanceType string `json:"instance_type,omitempty"` +} + +// Validate validates this instance metadata +func (m *InstanceMetadata) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateCloudProvider(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +var instanceMetadataTypeCloudProviderPropEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["aws","gcp","azure",""]`), &res); err != nil { + panic(err) + } + for _, v := range res { + instanceMetadataTypeCloudProviderPropEnum = append(instanceMetadataTypeCloudProviderPropEnum, v) + } +} + +const ( + + // InstanceMetadataCloudProviderAws captures enum value "aws" + InstanceMetadataCloudProviderAws string = "aws" + + // InstanceMetadataCloudProviderGcp captures enum value "gcp" + InstanceMetadataCloudProviderGcp string = "gcp" + + // InstanceMetadataCloudProviderAzure captures enum value "azure" + InstanceMetadataCloudProviderAzure string = "azure" + + // InstanceMetadataCloudProviderEmpty captures enum value "" + InstanceMetadataCloudProviderEmpty string = "" +) + +// prop value enum +func (m *InstanceMetadata) validateCloudProviderEnum(path, location string, value string) error { + if err := validate.EnumCase(path, location, value, instanceMetadataTypeCloudProviderPropEnum, true); err != nil { + return err + } + return nil +} + +func (m *InstanceMetadata) validateCloudProvider(formats strfmt.Registry) error { + + if swag.IsZero(m.CloudProvider) { // not required + return nil + } + + // value enum + if err := m.validateCloudProviderEnum("cloud_provider", "body", m.CloudProvider); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (m *InstanceMetadata) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *InstanceMetadata) UnmarshalBinary(b []byte) error { + var res InstanceMetadata + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +}