From ec69501e94d98b7e8d2ae6c62823e245d71d2f9f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 24 Nov 2022 19:36:01 +0100 Subject: [PATCH] api/types: move ServiceCreateResponse, and generate from swagger Signed-off-by: Sebastiaan van Stijn --- api/server/router/swarm/backend.go | 2 +- api/swagger.yaml | 39 ++++++++++++++-------- api/types/client.go | 9 ----- api/types/swarm/service_create_response.go | 20 +++++++++++ api/types/types_deprecated.go | 6 ++++ client/interface.go | 2 +- client/service_create.go | 4 +-- client/service_create_test.go | 6 ++-- daemon/cluster/services.go | 6 ++-- hack/generate-swagger-api.sh | 1 + 10 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 api/types/swarm/service_create_response.go diff --git a/api/server/router/swarm/backend.go b/api/server/router/swarm/backend.go index 0c3daf9995..3a1de28fc2 100644 --- a/api/server/router/swarm/backend.go +++ b/api/server/router/swarm/backend.go @@ -19,7 +19,7 @@ type Backend interface { UnlockSwarm(req swarm.UnlockRequest) error GetServices(types.ServiceListOptions) ([]swarm.Service, error) GetService(idOrName string, insertDefaults bool) (swarm.Service, error) - CreateService(swarm.ServiceSpec, string, bool) (*types.ServiceCreateResponse, error) + CreateService(swarm.ServiceSpec, string, bool) (*swarm.ServiceCreateResponse, error) UpdateService(string, uint64, swarm.ServiceSpec, types.ServiceUpdateOptions, bool) (*swarm.ServiceUpdateResponse, error) RemoveService(string) error ServiceLogs(context.Context, *backend.LogSelector, *types.ContainerLogsOptions) (<-chan *backend.LogMessage, error) diff --git a/api/swagger.yaml b/api/swagger.yaml index 18c2414a2f..3c3d83fb9a 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -4487,6 +4487,29 @@ definitions: description: "The image ID of an image that was deleted" type: "string" + ServiceCreateResponse: + type: "object" + description: | + contains the information returned to a client on the + creation of a new service. + properties: + ID: + description: "The ID of the created service." + type: "string" + x-nullable: false + example: "ak7w3gjqoa3kuz8xcpnyy0pvl" + Warnings: + description: | + Optional warning message. + + FIXME(thaJeztah): this should have "omitempty" in the generated type. + type: "array" + x-nullable: true + items: + type: "string" + example: + - "unable to pin image doesnotexist:latest to digest: image library/doesnotexist:latest not found" + ServiceUpdateResponse: type: "object" properties: @@ -4496,7 +4519,8 @@ definitions: items: type: "string" example: - Warning: "unable to pin image doesnotexist:latest to digest: image library/doesnotexist:latest not found" + Warnings: + - "unable to pin image doesnotexist:latest to digest: image library/doesnotexist:latest not found" ContainerSummary: type: "object" @@ -11099,18 +11123,7 @@ paths: 201: description: "no error" schema: - type: "object" - title: "ServiceCreateResponse" - properties: - ID: - description: "The ID of the created service." - type: "string" - Warning: - description: "Optional warning message" - type: "string" - example: - ID: "ak7w3gjqoa3kuz8xcpnyy0pvl" - Warning: "unable to pin image doesnotexist:latest to digest: image library/doesnotexist:latest not found" + $ref: "#/definitions/ServiceCreateResponse" 400: description: "bad parameter" schema: diff --git a/api/types/client.go b/api/types/client.go index 80691034e8..b14bac1115 100644 --- a/api/types/client.go +++ b/api/types/client.go @@ -322,15 +322,6 @@ type ServiceCreateOptions struct { QueryRegistry bool } -// ServiceCreateResponse contains the information returned to a client -// on the creation of a new service. -type ServiceCreateResponse struct { - // ID is the ID of the created service. - ID string - // Warnings is a set of non-fatal warning messages to pass on to the user. - Warnings []string `json:",omitempty"` -} - // Values for RegistryAuthFrom in ServiceUpdateOptions const ( RegistryAuthFromSpec = "spec" diff --git a/api/types/swarm/service_create_response.go b/api/types/swarm/service_create_response.go new file mode 100644 index 0000000000..9a268ff1b9 --- /dev/null +++ b/api/types/swarm/service_create_response.go @@ -0,0 +1,20 @@ +package swarm + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +// ServiceCreateResponse contains the information returned to a client on the +// creation of a new service. +// +// swagger:model ServiceCreateResponse +type ServiceCreateResponse struct { + + // The ID of the created service. + ID string `json:"ID,omitempty"` + + // Optional warning message. + // + // FIXME(thaJeztah): this should have "omitempty" in the generated type. + // + Warnings []string `json:"Warnings"` +} diff --git a/api/types/types_deprecated.go b/api/types/types_deprecated.go index 53d4a4a4ab..c6463fc96c 100644 --- a/api/types/types_deprecated.go +++ b/api/types/types_deprecated.go @@ -80,6 +80,12 @@ type ImageSummary = image.Summary // Deprecated: use [image.Metadata]. type ImageMetadata = image.Metadata +// ServiceCreateResponse contains the information returned to a client +// on the creation of a new service. +// +// Deprecated: use [swarm.ServiceCreateResponse]. +type ServiceCreateResponse = swarm.ServiceCreateResponse + // ServiceUpdateResponse service update response. // // Deprecated: use [swarm.ServiceUpdateResponse]. diff --git a/client/interface.go b/client/interface.go index 95ad6e90be..ffdb0de1e3 100644 --- a/client/interface.go +++ b/client/interface.go @@ -141,7 +141,7 @@ type PluginAPIClient interface { // ServiceAPIClient defines API client methods for the services type ServiceAPIClient interface { - ServiceCreate(ctx context.Context, service swarm.ServiceSpec, options types.ServiceCreateOptions) (types.ServiceCreateResponse, error) + ServiceCreate(ctx context.Context, service swarm.ServiceSpec, options types.ServiceCreateOptions) (swarm.ServiceCreateResponse, error) ServiceInspectWithRaw(ctx context.Context, serviceID string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) ServiceRemove(ctx context.Context, serviceID string) error diff --git a/client/service_create.go b/client/service_create.go index 7f54e83487..2ebb5ee3a5 100644 --- a/client/service_create.go +++ b/client/service_create.go @@ -17,8 +17,8 @@ import ( ) // ServiceCreate creates a new service. -func (cli *Client) ServiceCreate(ctx context.Context, service swarm.ServiceSpec, options types.ServiceCreateOptions) (types.ServiceCreateResponse, error) { - var response types.ServiceCreateResponse +func (cli *Client) ServiceCreate(ctx context.Context, service swarm.ServiceSpec, options types.ServiceCreateOptions) (swarm.ServiceCreateResponse, error) { + var response swarm.ServiceCreateResponse // Make sure we negotiated (if the client is configured to do so), // as code below contains API-version specific handling of options. diff --git a/client/service_create_test.go b/client/service_create_test.go index 752604ffde..0bbd0bc283 100644 --- a/client/service_create_test.go +++ b/client/service_create_test.go @@ -38,7 +38,7 @@ func TestServiceCreate(t *testing.T) { if req.Method != http.MethodPost { return nil, fmt.Errorf("expected POST method, got %s", req.Method) } - b, err := json.Marshal(types.ServiceCreateResponse{ + b, err := json.Marshal(swarm.ServiceCreateResponse{ ID: "service_id", }) if err != nil { @@ -77,7 +77,7 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) { assert.Check(t, is.Len(serviceSpec.TaskTemplate.Placement.Platforms, 1)) p := serviceSpec.TaskTemplate.Placement.Platforms[0] - b, err := json.Marshal(types.ServiceCreateResponse{ + b, err := json.Marshal(swarm.ServiceCreateResponse{ ID: "service_" + p.OS + "_" + p.Architecture, }) if err != nil { @@ -153,7 +153,7 @@ func TestServiceCreateDigestPinning(t *testing.T) { } serviceCreateImage = service.TaskTemplate.ContainerSpec.Image - b, err := json.Marshal(types.ServiceCreateResponse{ + b, err := json.Marshal(swarm.ServiceCreateResponse{ ID: "service_id", }) if err != nil { diff --git a/daemon/cluster/services.go b/daemon/cluster/services.go index c93c2df41b..f05f23073c 100644 --- a/daemon/cluster/services.go +++ b/daemon/cluster/services.go @@ -180,8 +180,8 @@ func (c *Cluster) GetService(input string, insertDefaults bool) (swarm.Service, } // CreateService creates a new service in a managed swarm cluster. -func (c *Cluster) CreateService(s swarm.ServiceSpec, encodedAuth string, queryRegistry bool) (*types.ServiceCreateResponse, error) { - var resp *types.ServiceCreateResponse +func (c *Cluster) CreateService(s swarm.ServiceSpec, encodedAuth string, queryRegistry bool) (*swarm.ServiceCreateResponse, error) { + var resp *swarm.ServiceCreateResponse err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error { err := c.populateNetworkID(ctx, state.controlClient, &s) if err != nil { @@ -193,7 +193,7 @@ func (c *Cluster) CreateService(s swarm.ServiceSpec, encodedAuth string, queryRe return errdefs.InvalidParameter(err) } - resp = &types.ServiceCreateResponse{} + resp = &swarm.ServiceCreateResponse{} switch serviceSpec.Task.Runtime.(type) { case *swarmapi.TaskSpec_Attachment: diff --git a/hack/generate-swagger-api.sh b/hack/generate-swagger-api.sh index c7399eae60..2868d8804e 100755 --- a/hack/generate-swagger-api.sh +++ b/hack/generate-swagger-api.sh @@ -42,4 +42,5 @@ swagger generate operation -f api/swagger.yaml \ swagger generate model -f api/swagger.yaml \ -t api -m types/swarm --skip-validator -C api/swagger-gen.yaml \ + -n ServiceCreateResponse \ -n ServiceUpdateResponse