Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GetMetricConfiguration description #815

Merged
merged 23 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 75 additions & 2 deletions api/assessment/metric.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
// Copyright 2022 Fraunhofer AISEC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.

package assessment

import (
Expand All @@ -8,11 +33,14 @@ import (
"fmt"

"clouditor.io/clouditor/persistence"
"github.com/google/uuid"
)

var (
ErrMetricNameMissing = errors.New("metric name is missing")
ErrMetricEmpty = errors.New("metric is missing or empty")
ErrMetricNameMissing = errors.New("metric name is missing")
ErrMetricEmpty = errors.New("metric is missing or empty")
ErrCloudServiceIDIsMissing = errors.New("cloud service id is missing")
ErrCloudServiceIDIsInvalid = errors.New("cloud service id is invalid")
)

func (r *Range) UnmarshalJSON(b []byte) (err error) {
Expand Down Expand Up @@ -142,8 +170,53 @@ func (m *Metric) Validate(opts ...MetricValidationOption) (err error) {
return nil
}

// Validate validates the metric configuration
func (c *MetricConfiguration) Validate() (err error) {

// isDefault does not need to be checked.
// UpdatedAt does not need to be checked.

if c == nil {
return ErrMetricConfigurationMissing
}

if c.Operator == "" {
return ErrMetricConfigurationOperatorMissing
}

if c.TargetValue == nil {
return ErrMetricConfigurationTargetValueMissing
}

if c.MetricId == "" {
return ErrMetricIdMissing
}

err = CheckCloudServiceID(c.CloudServiceId)
if err != nil {
return fmt.Errorf("%w", err)
anatheka marked this conversation as resolved.
Show resolved Hide resolved
}

return
}

// Hash provides a simple string based hash for this metric configuration. It can be used
// to provide a key for a map or a cache.
func (x *MetricConfiguration) Hash() string {
return base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%v-%v", x.Operator, x.TargetValue)))
}

// CheckCloudServiceID checks if serviceID is available and in the valid UUID format.
func CheckCloudServiceID(serviceID string) error {
// Check if ServiceId is missing
if serviceID == "" {
return ErrCloudServiceIDIsMissing
}

// Check if ServiceId is valid
if _, err := uuid.Parse(serviceID); err != nil {
return ErrCloudServiceIDIsInvalid
}

return nil
}