Skip to content

Commit 492f866

Browse files
authored
fix review comments (#1)
1 parent 1347bc9 commit 492f866

File tree

4 files changed

+399
-87
lines changed

4 files changed

+399
-87
lines changed

v1/client.go

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -97,45 +97,33 @@ func (a *VMCloudAPIClient) GetDeploymentDetails(ctx context.Context, deploymentI
9797

9898
// CreateDeployment creates a new deployment in VictoriaMetrics Cloud based on the provided deployment configuration.
9999
func (a *VMCloudAPIClient) CreateDeployment(ctx context.Context, deployment DeploymentCreationRequest) (DeploymentInfo, error) {
100-
if deployment.Name == "" {
101-
return DeploymentInfo{}, fmt.Errorf("deployment name cannot be empty")
102-
}
103-
if deployment.Type != DeploymentTypeSingleNode && deployment.Type != DeploymentTypeCluster {
104-
return DeploymentInfo{}, fmt.Errorf("invalid deployment type: %s", deployment.Type)
105-
}
106-
if deployment.Region == "" {
107-
return DeploymentInfo{}, fmt.Errorf("deployment region cannot be empty")
108-
}
109-
if deployment.Tier == 0 {
110-
return DeploymentInfo{}, fmt.Errorf("deployment tier cannot be empty")
111-
}
112-
if deployment.Provider != DeploymentCloudProviderAWS {
113-
return DeploymentInfo{}, fmt.Errorf("unsupported deployment cloud provider: %s", deployment.Provider)
114-
}
115-
if deployment.MaintenanceWindow != MaintenanceWindowWeekendDays && deployment.MaintenanceWindow != MaintenanceWindowBusinessDays {
116-
return DeploymentInfo{}, fmt.Errorf("invalid maintenance window: %s", deployment.MaintenanceWindow)
117-
}
118-
if deployment.StorageSize == 0 {
119-
return DeploymentInfo{}, fmt.Errorf("deployment storage size cannot be zero")
120-
}
121-
if deployment.StorageSizeUnit != StorageUnitGB && deployment.StorageSizeUnit != StorageUnitTB {
122-
return DeploymentInfo{}, fmt.Errorf("invalid storage size unit: %s", deployment.StorageSizeUnit)
123-
}
124-
if deployment.StorageSizeUnit == StorageUnitGB && deployment.StorageSize < 10 {
125-
return DeploymentInfo{}, fmt.Errorf("deployment storage size must be at least 10 GB")
126-
}
127-
if deployment.Type == DeploymentTypeSingleNode && deployment.StorageSizeUnit == StorageUnitTB && deployment.StorageSize > 16 {
128-
return DeploymentInfo{}, fmt.Errorf("single-node deployments cannot have more than 16 TB of storage")
129-
}
130-
if deployment.Retention == 0 {
131-
return DeploymentInfo{}, fmt.Errorf("deployment retention cannot be zero")
132-
}
133-
if deployment.RetentionUnit != DurationUnitDay && deployment.RetentionUnit != DurationUnitMonth {
134-
return DeploymentInfo{}, fmt.Errorf("invalid retention unit: %s, only days and months are supported", deployment.RetentionUnit)
100+
// Validate common parameters
101+
err := validateCommonDeploymentParams(
102+
deployment.Name,
103+
deployment.Tier,
104+
deployment.MaintenanceWindow,
105+
deployment.StorageSize,
106+
deployment.StorageSizeUnit,
107+
deployment.Retention,
108+
deployment.RetentionUnit,
109+
deployment.DeduplicationUnit,
110+
)
111+
if err != nil {
112+
return DeploymentInfo{}, err
135113
}
136-
if deployment.DeduplicationUnit != DurationUnitSecond && deployment.DeduplicationUnit != DurationUnitMillisecond {
137-
return DeploymentInfo{}, fmt.Errorf("invalid deduplication unit: %s, only seconds and milliseconds are supported", deployment.DeduplicationUnit)
114+
115+
// Validate creation-specific parameters
116+
err = validateCreateDeploymentParams(
117+
deployment.Type,
118+
deployment.Region,
119+
deployment.Provider,
120+
deployment.StorageSize,
121+
deployment.StorageSizeUnit,
122+
)
123+
if err != nil {
124+
return DeploymentInfo{}, err
138125
}
126+
139127
body, err := json.Marshal(deployment)
140128
if err != nil {
141129
return DeploymentInfo{}, fmt.Errorf("failed to marshal deployment create request: %w", err)
@@ -148,33 +136,22 @@ func (a *VMCloudAPIClient) UpdateDeployment(ctx context.Context, deploymentID st
148136
if err := checkDeploymentID(deploymentID); err != nil {
149137
return DeploymentInfo{}, err
150138
}
151-
if deployment.Name == "" {
152-
return DeploymentInfo{}, fmt.Errorf("deployment name cannot be empty")
153-
}
154-
if deployment.Tier == 0 {
155-
return DeploymentInfo{}, fmt.Errorf("deployment tier cannot be empty")
156-
}
157-
if deployment.MaintenanceWindow != MaintenanceWindowWeekendDays && deployment.MaintenanceWindow != MaintenanceWindowBusinessDays {
158-
return DeploymentInfo{}, fmt.Errorf("invalid maintenance window: %s", deployment.MaintenanceWindow)
159-
}
160-
if deployment.StorageSize == 0 {
161-
return DeploymentInfo{}, fmt.Errorf("deployment storage size cannot be zero")
162-
}
163-
if deployment.StorageSizeUnit != StorageUnitGB && deployment.StorageSizeUnit != StorageUnitTB {
164-
return DeploymentInfo{}, fmt.Errorf("invalid storage size unit: %s", deployment.StorageSizeUnit)
165-
}
166-
if deployment.StorageSizeUnit == StorageUnitGB && deployment.StorageSize < 10 {
167-
return DeploymentInfo{}, fmt.Errorf("deployment storage size must be at least 10 GB")
168-
}
169-
if deployment.Retention == 0 {
170-
return DeploymentInfo{}, fmt.Errorf("deployment retention cannot be zero")
171-
}
172-
if deployment.RetentionUnit != DurationUnitDay && deployment.RetentionUnit != DurationUnitMonth {
173-
return DeploymentInfo{}, fmt.Errorf("invalid retention unit: %s, only days and months are supported", deployment.RetentionUnit)
174-
}
175-
if deployment.DeduplicationUnit != DurationUnitSecond && deployment.DeduplicationUnit != DurationUnitMillisecond {
176-
return DeploymentInfo{}, fmt.Errorf("invalid deduplication unit: %s, only seconds and milliseconds are supported", deployment.DeduplicationUnit)
139+
140+
// Validate common parameters
141+
err := validateCommonDeploymentParams(
142+
deployment.Name,
143+
deployment.Tier,
144+
deployment.MaintenanceWindow,
145+
deployment.StorageSize,
146+
deployment.StorageSizeUnit,
147+
deployment.Retention,
148+
deployment.RetentionUnit,
149+
deployment.DeduplicationUnit,
150+
)
151+
if err != nil {
152+
return DeploymentInfo{}, err
177153
}
154+
178155
body, err := json.Marshal(deployment)
179156
if err != nil {
180157
return DeploymentInfo{}, fmt.Errorf("failed to marshal deployment update request: %w", err)

v1/utils.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9-
"regexp"
109
)
1110

1211
func requestAPI[R any](ctx context.Context, a *VMCloudAPIClient, method string, body io.Reader, path ...string) (R, error) {
@@ -33,7 +32,7 @@ func requestAPI[R any](ctx context.Context, a *VMCloudAPIClient, method string,
3332
}
3433
if len(respBodyBytes) > 0 {
3534
// Special case for string type - just return the response body as a string
36-
if stringResult, ok := any(&result).(*(string)); ok {
35+
if stringResult, ok := any(&result).(*string); ok {
3736
*stringResult = string(respBodyBytes)
3837
} else {
3938
// For other types, unmarshal as JSON
@@ -44,25 +43,3 @@ func requestAPI[R any](ctx context.Context, a *VMCloudAPIClient, method string,
4443
}
4544
return result, nil
4645
}
47-
48-
var uuidRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
49-
50-
func isValidUUID(uuid string) bool {
51-
return uuidRegex.MatchString(uuid)
52-
}
53-
54-
func checkDeploymentID(deploymentID string) error {
55-
if deploymentID == "" {
56-
return fmt.Errorf("deployment ID cannot be empty")
57-
}
58-
if !isValidUUID(deploymentID) {
59-
return fmt.Errorf("invalid deployment ID format: %s", deploymentID)
60-
}
61-
return nil
62-
}
63-
64-
var tenantIDRegex = regexp.MustCompile(`^(\d+)(:\d+)?$`)
65-
66-
func isValidTenantID(tenantID string) bool {
67-
return tenantIDRegex.MatchString(tenantID)
68-
}

v1/validation.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package v1
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
var uuidRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
9+
10+
func isValidUUID(uuid string) bool {
11+
return uuidRegex.MatchString(uuid)
12+
}
13+
14+
func checkDeploymentID(deploymentID string) error {
15+
if deploymentID == "" {
16+
return fmt.Errorf("deployment ID cannot be empty")
17+
}
18+
if !isValidUUID(deploymentID) {
19+
return fmt.Errorf("invalid deployment ID format: %s", deploymentID)
20+
}
21+
return nil
22+
}
23+
24+
var tenantIDRegex = regexp.MustCompile(`^(\d+)(:\d+)?$`)
25+
26+
func isValidTenantID(tenantID string) bool {
27+
return tenantIDRegex.MatchString(tenantID)
28+
}
29+
30+
// validateCommonDeploymentParams validates parameters common to both create and update operations
31+
func validateCommonDeploymentParams(
32+
name string,
33+
tier uint32,
34+
maintenanceWindow MaintenanceWindow,
35+
storageSize uint64,
36+
storageSizeUnit StorageUnit,
37+
retention uint32,
38+
retentionUnit DurationUnit,
39+
deduplicationUnit DurationUnit,
40+
) error {
41+
if name == "" {
42+
return fmt.Errorf("deployment name cannot be empty")
43+
}
44+
if tier == 0 {
45+
return fmt.Errorf("deployment tier cannot be empty")
46+
}
47+
if maintenanceWindow != MaintenanceWindowWeekendDays &&
48+
maintenanceWindow != MaintenanceWindowBusinessDays {
49+
return fmt.Errorf("invalid maintenance window: %s", maintenanceWindow)
50+
}
51+
if storageSize == 0 {
52+
return fmt.Errorf("deployment storage size cannot be zero")
53+
}
54+
if storageSizeUnit != StorageUnitGB && storageSizeUnit != StorageUnitTB {
55+
return fmt.Errorf("invalid storage size unit: %s", storageSizeUnit)
56+
}
57+
if storageSizeUnit == StorageUnitGB && storageSize < 10 {
58+
return fmt.Errorf("deployment storage size must be at least 10 GB")
59+
}
60+
if retention == 0 {
61+
return fmt.Errorf("deployment retention cannot be zero")
62+
}
63+
if retentionUnit != DurationUnitDay && retentionUnit != DurationUnitMonth {
64+
return fmt.Errorf("invalid retention unit: %s, only days and months are supported", retentionUnit)
65+
}
66+
if deduplicationUnit != DurationUnitSecond && deduplicationUnit != DurationUnitMillisecond {
67+
return fmt.Errorf("invalid deduplication unit: %s, only seconds and milliseconds are supported", deduplicationUnit)
68+
}
69+
return nil
70+
}
71+
72+
// validateCreateDeploymentParams validates parameters specific to deployment creation
73+
func validateCreateDeploymentParams(
74+
deploymentType DeploymentType,
75+
region string,
76+
provider DeploymentCloudProvider,
77+
deploymentStorageSize uint64,
78+
storageSizeUnit StorageUnit,
79+
) error {
80+
if deploymentType != DeploymentTypeSingleNode && deploymentType != DeploymentTypeCluster {
81+
return fmt.Errorf("invalid deployment type: %s", deploymentType)
82+
}
83+
if region == "" {
84+
return fmt.Errorf("deployment region cannot be empty")
85+
}
86+
if provider != DeploymentCloudProviderAWS {
87+
return fmt.Errorf("unsupported deployment cloud provider: %s", provider)
88+
}
89+
if deploymentType == DeploymentTypeSingleNode &&
90+
storageSizeUnit == StorageUnitTB && deploymentStorageSize > 16 {
91+
return fmt.Errorf("single-node deployments cannot have more than 16 TB of storage")
92+
}
93+
return nil
94+
}

0 commit comments

Comments
 (0)