Skip to content

Commit 99fa35f

Browse files
committed
cloudapi/v2: adapt to new oapi-codegen
Fixed distribution list type, function signatures (later versions parse uuids in the generated code), defaults actually being set and added x-go-type where needed.
1 parent 7c4a766 commit 99fa35f

12 files changed

+511
-554
lines changed

internal/cloudapi/v2/compose.go

+68-50
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (rbp *Blueprint) GetCustomizationsFromBlueprintRequest() (*blueprint.Custom
235235
}
236236

237237
if rbpc.Openscap.PolicyId != nil {
238-
oscap.PolicyID = *rbpc.Openscap.PolicyId
238+
oscap.PolicyID = rbpc.Openscap.PolicyId.String()
239239
}
240240

241241
if rbpc.Openscap.Datastream != nil {
@@ -288,23 +288,27 @@ func (rbp *Blueprint) GetCustomizationsFromBlueprintRequest() (*blueprint.Custom
288288
dirCustomization.Mode = *d.Mode
289289
}
290290
if d.User != nil {
291-
dirCustomization.User = *d.User
292-
if uid, ok := dirCustomization.User.(float64); ok {
293-
// check if uid can be converted to int64
294-
if uid != float64(int64(uid)) {
295-
return nil, fmt.Errorf("invalid user %f: must be an integer", uid)
291+
user0, err := d.User.AsDirectoryUser0()
292+
if err == nil {
293+
dirCustomization.User = user0
294+
} else {
295+
user1, err := d.User.AsDirectoryUser1()
296+
if err != nil {
297+
return nil, fmt.Errorf("invalid user: %w", err)
296298
}
297-
dirCustomization.User = int64(uid)
299+
dirCustomization.User = user1
298300
}
299301
}
300302
if d.Group != nil {
301-
dirCustomization.Group = *d.Group
302-
if gid, ok := dirCustomization.Group.(float64); ok {
303-
// check if gid can be converted to int64
304-
if gid != float64(int64(gid)) {
305-
return nil, fmt.Errorf("invalid group %f: must be an integer", gid)
303+
group0, err := d.Group.AsDirectoryGroup0()
304+
if err == nil {
305+
dirCustomization.Group = group0
306+
} else {
307+
group1, err := d.Group.AsDirectoryGroup1()
308+
if err != nil {
309+
return nil, fmt.Errorf("invalid group: %w", err)
306310
}
307-
dirCustomization.Group = int64(gid)
311+
dirCustomization.Group = group1
308312
}
309313
}
310314
if d.EnsureParents != nil {
@@ -335,23 +339,29 @@ func (rbp *Blueprint) GetCustomizationsFromBlueprintRequest() (*blueprint.Custom
335339
fileCustomization.Mode = *f.Mode
336340
}
337341
if f.User != nil {
338-
fileCustomization.User = *f.User
339-
if uid, ok := fileCustomization.User.(float64); ok {
340-
// check if uid can be converted to int64
341-
if uid != float64(int64(uid)) {
342-
return nil, fmt.Errorf("invalid user %f: must be an integer", uid)
342+
user0, err := f.User.AsBlueprintFileUser0()
343+
if err == nil {
344+
fileCustomization.User = user0
345+
} else {
346+
user1, err := f.User.AsBlueprintFileUser1()
347+
if err != nil {
348+
return nil, fmt.Errorf("invalid user: %w", err)
343349
}
344-
fileCustomization.User = int64(uid)
350+
fileCustomization.User = user1
345351
}
346352
}
347353
if f.Group != nil {
348-
fileCustomization.Group = *f.Group
349-
if gid, ok := fileCustomization.Group.(float64); ok {
350-
// check if gid can be converted to int64
351-
if gid != float64(int64(gid)) {
352-
return nil, fmt.Errorf("invalid group %f: must be an integer", gid)
354+
group0, err := f.Group.AsBlueprintFileGroup0()
355+
if err == nil {
356+
fileCustomization.Group = group0
357+
} else {
358+
group1, err := f.Group.AsBlueprintFileGroup1()
359+
if err != nil {
360+
return nil, fmt.Errorf("invalid group: %w", err)
361+
}
362+
if group1 != 0 {
363+
fileCustomization.Group = group1
353364
}
354-
fileCustomization.Group = int64(gid)
355365
}
356366
}
357367
fileCustomizations = append(fileCustomizations, fileCustomization)
@@ -655,23 +665,27 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
655665
dirCustomization.Mode = *d.Mode
656666
}
657667
if d.User != nil {
658-
dirCustomization.User = *d.User
659-
if uid, ok := dirCustomization.User.(float64); ok {
660-
// check if uid can be converted to int64
661-
if uid != float64(int64(uid)) {
662-
return bp, fmt.Errorf("invalid user %f: must be an integer", uid)
668+
user0, err := d.User.AsDirectoryUser0()
669+
if err == nil {
670+
dirCustomization.User = user0
671+
} else {
672+
user1, err := d.User.AsDirectoryUser1()
673+
if err != nil {
674+
return bp, fmt.Errorf("invalid user: %w", err)
663675
}
664-
dirCustomization.User = int64(uid)
676+
dirCustomization.User = user1
665677
}
666678
}
667679
if d.Group != nil {
668-
dirCustomization.Group = *d.Group
669-
if gid, ok := dirCustomization.Group.(float64); ok {
670-
// check if gid can be converted to int64
671-
if gid != float64(int64(gid)) {
672-
return bp, fmt.Errorf("invalid group %f: must be an integer", gid)
680+
group0, err := d.Group.AsDirectoryGroup0()
681+
if err == nil {
682+
dirCustomization.Group = group0
683+
} else {
684+
group1, err := d.Group.AsDirectoryGroup1()
685+
if err != nil {
686+
return bp, fmt.Errorf("invalid group: %w", err)
673687
}
674-
dirCustomization.Group = int64(gid)
688+
dirCustomization.Group = group1
675689
}
676690
}
677691
if d.EnsureParents != nil {
@@ -702,23 +716,27 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
702716
fileCustomization.Mode = *f.Mode
703717
}
704718
if f.User != nil {
705-
fileCustomization.User = *f.User
706-
if uid, ok := fileCustomization.User.(float64); ok {
707-
// check if uid can be converted to int64
708-
if uid != float64(int64(uid)) {
709-
return bp, fmt.Errorf("invalid user %f: must be an integer", uid)
719+
user0, err := f.User.AsFileUser0()
720+
if err == nil {
721+
fileCustomization.User = user0
722+
} else {
723+
user1, err := f.User.AsFileUser1()
724+
if err != nil {
725+
return bp, fmt.Errorf("invalid user: %w", err)
710726
}
711-
fileCustomization.User = int64(uid)
727+
fileCustomization.User = user1
712728
}
713729
}
714730
if f.Group != nil {
715-
fileCustomization.Group = *f.Group
716-
if gid, ok := fileCustomization.Group.(float64); ok {
717-
// check if gid can be converted to int64
718-
if gid != float64(int64(gid)) {
719-
return bp, fmt.Errorf("invalid group %f: must be an integer", gid)
731+
group0, err := f.Group.AsFileGroup0()
732+
if err == nil {
733+
fileCustomization.Group = group0
734+
} else {
735+
group1, err := f.Group.AsFileGroup1()
736+
if err != nil {
737+
return bp, fmt.Errorf("invalid group: %w", err)
720738
}
721-
fileCustomization.Group = int64(gid)
739+
fileCustomization.Group = group1
722740
}
723741
}
724742
fileCustomizations = append(fileCustomizations, fileCustomization)
@@ -770,7 +788,7 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
770788
}
771789

772790
if request.Customizations.Openscap.PolicyId != nil {
773-
openSCAPCustomization.PolicyID = *request.Customizations.Openscap.PolicyId
791+
openSCAPCustomization.PolicyID = request.Customizations.Openscap.PolicyId.String()
774792
}
775793

776794
if request.Customizations.Openscap.Tailoring != nil && request.Customizations.Openscap.JsonTailoring != nil {

internal/cloudapi/v2/compose_test.go

+27-18
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,14 @@ func TestGetBlueprintFromCustomizations(t *testing.T) {
201201
assert.Equal(t, "0.0.0", bp.Version)
202202
assert.Nil(t, bp.Customizations)
203203

204-
// interface{} is a terrible idea. Work around it...
205-
var rootStr interface{} = "root"
204+
var dirUser Directory_User
205+
require.NoError(t, dirUser.FromDirectoryUser0("root"))
206+
var dirGroup Directory_Group
207+
require.NoError(t, dirGroup.FromDirectoryGroup0("root"))
208+
var fileUser File_User
209+
require.NoError(t, fileUser.FromFileUser0("root"))
210+
var fileGroup File_Group
211+
require.NoError(t, fileGroup.FromFileGroup0("root"))
206212

207213
// Construct the compose request with customizations
208214
cr = ComposeRequest{Customizations: &Customizations{
@@ -230,8 +236,8 @@ func TestGetBlueprintFromCustomizations(t *testing.T) {
230236
Directory{
231237
Path: "/opt/extra",
232238
EnsureParents: common.ToPtr(true),
233-
User: &rootStr,
234-
Group: &rootStr,
239+
User: &dirUser,
240+
Group: &dirGroup,
235241
Mode: common.ToPtr("0755"),
236242
},
237243
},
@@ -240,8 +246,8 @@ func TestGetBlueprintFromCustomizations(t *testing.T) {
240246
Path: "/etc/mad.conf",
241247
Data: common.ToPtr("Alfred E. Neuman was here.\n"),
242248
EnsureParents: common.ToPtr(true),
243-
User: &rootStr,
244-
Group: &rootStr,
249+
User: &fileUser,
250+
Group: &fileGroup,
245251
Mode: common.ToPtr("0644"),
246252
},
247253
},
@@ -379,8 +385,14 @@ func TestGetBlueprintFromCompose(t *testing.T) {
379385
assert.Equal(t, "0.0.0", bp.Version)
380386
assert.Nil(t, bp.Customizations)
381387

382-
// interface{} is a terrible idea. Work around it...
383-
var rootStr interface{} = "root"
388+
var dirUser Directory_User
389+
require.NoError(t, dirUser.FromDirectoryUser0("root"))
390+
var dirGroup Directory_Group
391+
require.NoError(t, dirGroup.FromDirectoryGroup0("root"))
392+
var fileUser BlueprintFile_User
393+
require.NoError(t, fileUser.FromBlueprintFileUser0("root"))
394+
var fileGroup BlueprintFile_Group
395+
require.NoError(t, fileGroup.FromBlueprintFileGroup0("root"))
384396

385397
// Construct the compose request with a blueprint
386398
cr = ComposeRequest{Blueprint: &Blueprint{
@@ -411,17 +423,17 @@ func TestGetBlueprintFromCompose(t *testing.T) {
411423
Directory{
412424
Path: "/opt/extra",
413425
EnsureParents: common.ToPtr(true),
414-
User: &rootStr,
415-
Group: &rootStr,
426+
User: &dirUser,
427+
Group: &dirGroup,
416428
Mode: common.ToPtr("0755"),
417429
},
418430
},
419431
Files: &[]BlueprintFile{
420432
{
421433
Path: "/etc/mad.conf",
422434
Data: common.ToPtr("Alfred E. Neuman was here.\n"),
423-
User: &rootStr,
424-
Group: &rootStr,
435+
User: &fileUser,
436+
Group: &fileGroup,
425437
Mode: common.ToPtr("0644"),
426438
},
427439
},
@@ -800,13 +812,12 @@ func TestGetImageRequests_ImageTypeConversion(t *testing.T) {
800812
for _, tt := range tests {
801813
t.Run(string(tt.requestedImageType), func(t *testing.T) {
802814
for _, d := range tt.requestedDistros {
803-
uo := UploadOptions(struct{}{})
804815
request := &ComposeRequest{
805816
Distribution: d,
806817
ImageRequest: &ImageRequest{
807818
Architecture: "x86_64",
808819
ImageType: tt.requestedImageType,
809-
UploadOptions: &uo,
820+
UploadOptions: &UploadOptions{},
810821
Repositories: []Repository{
811822
Repository{
812823
Baseurl: common.ToPtr("http://example.org/pub/linux/repo"),
@@ -828,13 +839,12 @@ func TestGetImageRequests_ImageTypeConversion(t *testing.T) {
828839
}
829840

830841
func TestGetImageRequests_NoRepositories(t *testing.T) {
831-
uo := UploadOptions(struct{}{})
832842
request := &ComposeRequest{
833843
Distribution: TEST_DISTRO_NAME,
834844
ImageRequest: &ImageRequest{
835845
Architecture: "x86_64",
836846
ImageType: ImageTypesAws,
837-
UploadOptions: &uo,
847+
UploadOptions: &UploadOptions{},
838848
Repositories: []Repository{},
839849
},
840850
}
@@ -849,13 +859,12 @@ func TestGetImageRequests_NoRepositories(t *testing.T) {
849859

850860
// TestGetImageRequests_BlueprintDistro test to make sure blueprint distro overrides request distro
851861
func TestGetImageRequests_BlueprintDistro(t *testing.T) {
852-
uo := UploadOptions(struct{}{})
853862
request := &ComposeRequest{
854863
Distribution: TEST_DISTRO_NAME,
855864
ImageRequest: &ImageRequest{
856865
Architecture: "x86_64",
857866
ImageType: ImageTypesAws,
858-
UploadOptions: &uo,
867+
UploadOptions: &UploadOptions{},
859868
Repositories: []Repository{},
860869
},
861870
Blueprint: &Blueprint{

internal/cloudapi/v2/errors.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252
ErrorBlueprintOrCustomNotBoth ServiceErrorCode = 39
5353
ErrorMismatchedDistribution ServiceErrorCode = 40
5454
ErrorMismatchedArchitecture ServiceErrorCode = 41
55+
ErrorBadRequest ServiceErrorCode = 42
5556

5657
// Internal errors, these are bugs
5758
ErrorFailedToInitializeBlueprint ServiceErrorCode = 1000
@@ -136,8 +137,9 @@ func getServiceErrors() serviceErrors {
136137
serviceError{ErrorInvalidPartitioningMode, http.StatusBadRequest, "Requested partitioning mode is invalid"},
137138
serviceError{ErrorInvalidUploadTarget, http.StatusBadRequest, "Invalid upload target for image type"},
138139
serviceError{ErrorBlueprintOrCustomNotBoth, http.StatusBadRequest, "Invalid request, include blueprint or customizations, not both"},
139-
serviceError{ErrorMismatchedDistribution, http.StatusBadRequest, "Invalid request, Blueprint and Cloud API request Distribution must match."},
140-
serviceError{ErrorMismatchedArchitecture, http.StatusBadRequest, "Invalid request, Blueprint and Cloud API request Architecture must match."},
140+
serviceError{ErrorMismatchedDistribution, http.StatusBadRequest, "Invalid request, Blueprint and Cloud API request Distribution must match"},
141+
serviceError{ErrorMismatchedArchitecture, http.StatusBadRequest, "Invalid request, Blueprint and Cloud API request Architecture must match"},
142+
serviceError{ErrorBadRequest, http.StatusBadRequest, "Invalid request, see details for more information"},
141143

142144
serviceError{ErrorFailedToInitializeBlueprint, http.StatusInternalServerError, "Failed to initialize blueprint"},
143145
serviceError{ErrorFailedToGenerateManifestSeed, http.StatusInternalServerError, "Failed to generate manifest seed"},
@@ -221,11 +223,9 @@ func APIError(serviceError *serviceError, c echo.Context, details interface{}) *
221223
}
222224

223225
apiErr := &Error{
224-
ObjectReference: ObjectReference{
225-
Href: fmt.Sprintf("%s/%d", ErrorHREF, serviceError.code),
226-
Id: fmt.Sprintf("%d", serviceError.code),
227-
Kind: "Error",
228-
},
226+
Href: fmt.Sprintf("%s/%d", ErrorHREF, serviceError.code),
227+
Id: fmt.Sprintf("%d", serviceError.code),
228+
Kind: "Error",
229229
Code: fmt.Sprintf("%s%d", ErrorCodePrefix, serviceError.code),
230230
OperationId: operationID, // set operation id from context
231231
Reason: serviceError.reason,
@@ -239,12 +239,10 @@ func APIError(serviceError *serviceError, c echo.Context, details interface{}) *
239239
// Helper to make the ErrorList as defined in openapi.v2.yml
240240
func APIErrorList(page int, pageSize int, c echo.Context) *ErrorList {
241241
list := &ErrorList{
242-
List: List{
243-
Kind: "ErrorList",
244-
Page: page,
245-
Size: 0,
246-
Total: len(getServiceErrors()),
247-
},
242+
Kind: "ErrorList",
243+
Page: page,
244+
Size: 0,
245+
Total: len(getServiceErrors()),
248246
Items: []Error{},
249247
}
250248

@@ -273,6 +271,8 @@ func apiErrorFromEchoError(echoError *echo.HTTPError) ServiceErrorCode {
273271
switch echoError.Code {
274272
case http.StatusNotFound:
275273
return ErrorResourceNotFound
274+
case http.StatusBadRequest:
275+
return ErrorBadRequest
276276
case http.StatusMethodNotAllowed:
277277
return ErrorMethodNotAllowed
278278
case http.StatusNotAcceptable:
@@ -322,7 +322,7 @@ func HTTPErrorHandler(echoError error, c echo.Context) {
322322

323323
err, ok := he.Message.(detailsError)
324324
if !ok {
325-
// No service code was set, so Echo threw this error
325+
// No service code was set, so Echo or the generated code threw this error
326326
doResponse(he.Error(), apiErrorFromEchoError(he), c, he.Internal)
327327
return
328328
}

0 commit comments

Comments
 (0)