diff --git a/pkg/api/asset.go b/pkg/api/asset.go index 06296815..b724d273 100644 --- a/pkg/api/asset.go +++ b/pkg/api/asset.go @@ -105,8 +105,8 @@ func (a Asset) Validate() error { return errors.Validation("Identifier is not a valid DomainName") } default: - // If none of the previous case match, force a validation error - return errors.Validation("Asset type not supported") + // If none of the previous case match, should be fine. + return nil } return nil diff --git a/pkg/api/endpoint/assets.go b/pkg/api/endpoint/assets.go index b097a229..abb4329c 100644 --- a/pkg/api/endpoint/assets.go +++ b/pkg/api/endpoint/assets.go @@ -205,7 +205,8 @@ func makeMergeDiscoveredAssetsEndpoint(s api.VulcanitoService, logger kitlog.Log if ar.Identifier == "" || ar.Type == "" { return nil, errors.Validation("Asset identifier and type are required for all the assets") } - if !api.ValidAssetType(ar.Type) { + + if _, err := s.GetAssetType(ctx, ar.Type); err != nil { return nil, errors.Validation(fmt.Errorf("Invalid asset type (%s) for asset (%v)", ar.Type, ar.Identifier)) } diff --git a/pkg/api/service/assets.go b/pkg/api/service/assets.go index 70a4af97..cf524064 100644 --- a/pkg/api/service/assets.go +++ b/pkg/api/service/assets.go @@ -73,13 +73,12 @@ func (s vulcanitoService) CreateAssets(ctx context.Context, assets []api.Asset, // Asset type provided by the user in the request. if asset.AssetType != nil && asset.AssetType.Name != "" { - if !api.ValidAssetType(asset.AssetType.Name) { - return nil, errors.Validation("Asset type not found", "asset", asset.Identifier, asset.AssetType.Name) - } - // Retrieve asset type from its name. assetTypeObj, err := s.GetAssetType(ctx, asset.AssetType.Name) if err != nil { + if errors.IsKind(err, errors.ErrNotFound) { + return nil, errors.Validation("Asset type not found", "asset", asset.Identifier, asset.AssetType.Name) + } return nil, err } @@ -179,18 +178,14 @@ func (s vulcanitoService) CreateAssetsMultiStatus(ctx context.Context, assets [] // If user specified the asset type. if asset.AssetType != nil && asset.AssetType.Name != "" { - // If the asset type is invalid, abort the asset creation. - if !api.ValidAssetType(asset.AssetType.Name) { - response.Status = errors.Validation("Asset type not found", "asset", asset.Identifier, asset.AssetType.Name) - responses = append(responses, response) - continue - } - // Retrieve asset type from its name. assetTypeObj, err := s.GetAssetType(ctx, asset.AssetType.Name) if err != nil { // If there is an error retrieiving the asset type information, abort the asset creation. response.Status = err + if errors.IsKind(err, errors.ErrNotFound) { + response.Status = errors.Validation("Asset type not found", "asset", asset.Identifier, asset.AssetType.Name) + } responses = append(responses, response) continue } @@ -541,13 +536,12 @@ func (s vulcanitoService) detectAssets(ctx context.Context, asset api.Asset) ([] for _, a := range assets { asset := asset - if !api.ValidAssetType(a.assetType) { - return nil, errors.Default("invalid asset type returned by auto-detection routine") - } - // Retrieve asset type from its name. assetTypeObj, err := s.GetAssetType(ctx, a.assetType) if err != nil { + if errors.IsKind(err, errors.ErrNotFound) { + return nil, errors.Default("invalid asset type returned by auto-detection routine") + } return nil, err }