-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add implementation of variable set
- Loading branch information
Showing
31 changed files
with
4,369 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package constant | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrInvalidVariableName = errors.New("variable name can only have alphanumeric characters and underscores with [a-zA-Z0-9_]") | ||
ErrInvalidVariableType = errors.New("invalid variable type, only PlainText and CipherText supported") | ||
ErrEmptyVariableSet = errors.New("variable set should not be empty") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package constant | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrInvalidVariableSetName = errors.New("variable set name can only have alphanumeric characters and underscores with [a-zA-Z0-9_]") | ||
ErrEmptyVariableSetLabels = errors.New("variable set labels should not be empty") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package entity | ||
|
||
import "errors" | ||
|
||
type VariableType string | ||
|
||
const ( | ||
PlainTextType VariableType = "PlainText" | ||
CipherTextType VariableType = "CipherText" | ||
) | ||
|
||
// Variable represents a specific configuration code variable, | ||
// which usually includes the global configuration for Terraform providers like | ||
// api host, access key and secret key. | ||
type Variable struct { | ||
// Name is the name of the variable. | ||
Name string `yaml:"name,omitempty" json:"name,omitempty"` | ||
// Value is the value of the variable. | ||
Value string `yaml:"value,omitempty" json:"value,omitempty"` | ||
// Type is the text type of the variable. | ||
Type VariableType `yaml:"type,omitempty" json:"type,omitempty"` | ||
// VariableSet is the variable set to which the variable belongs. | ||
VariableSet string `yaml:"variableSet,omitempty" json:"variableSet,omitempty"` | ||
} | ||
|
||
// VariableFilter represents the filter conditions to list variables. | ||
type VariableFilter struct { | ||
Name string | ||
VariableSet string | ||
Pagination *Pagination | ||
FetchAll bool | ||
} | ||
|
||
// VariableListResult represents the result of listing variables. | ||
type VariableListResult struct { | ||
Variables []*Variable | ||
Total int | ||
} | ||
|
||
// Validate checks if the variable is valid. | ||
func (v *Variable) Validate() error { | ||
if v == nil { | ||
return errors.New("variable is nil") | ||
} | ||
|
||
if v.Name == "" { | ||
return errors.New("empty variable name") | ||
} | ||
|
||
if v.Type != PlainTextType && v.Type != CipherTextType { | ||
return errors.New("invalid variable type") | ||
} | ||
|
||
if v.VariableSet == "" { | ||
return errors.New("empty variable set name") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package entity | ||
|
||
import "errors" | ||
|
||
// VariableSet represents a set of the global configuration variables. | ||
type VariableSet struct { | ||
// Name is the name of the variable set. | ||
Name string `yaml:"name,omitempty" json:"name,omitempty"` | ||
// Labels clarifies the scope of the variable set. | ||
Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"` | ||
} | ||
|
||
// VariableSetFilter represents the filter conditions to list variable sets. | ||
type VariableSetFilter struct { | ||
Name string | ||
Pagination *Pagination | ||
FetchAll bool | ||
} | ||
|
||
// VariableSetListResult represents the result of listing variable sets. | ||
type VariableSetListResult struct { | ||
VariableSets []*VariableSet | ||
Total int | ||
} | ||
|
||
// Validate checks if the variable set is valid. | ||
func (vs *VariableSet) Validate() error { | ||
if vs == nil { | ||
return errors.New("variable set is nil") | ||
} | ||
|
||
if vs.Name == "" { | ||
return errors.New("empty variable set name") | ||
} | ||
|
||
if len(vs.Labels) == 0 { | ||
return errors.New("empty variable set labels") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package request | ||
|
||
import ( | ||
"net/http" | ||
|
||
"kusionstack.io/kusion/pkg/domain/constant" | ||
"kusionstack.io/kusion/pkg/domain/entity" | ||
) | ||
|
||
// CreateVariableRequest represents the create request structure | ||
// for a variable. | ||
type CreateVariableRequest struct { | ||
// Name is the name of the variable. | ||
Name string `json:"name" binding:"required"` | ||
// Value is the value of the variable. | ||
Value string `json:"value"` | ||
// Type is the type of the variable. | ||
Type entity.VariableType `json:"type"` | ||
// VariableSet is the variable set to which the variable belongs. | ||
VariableSet string `json:"variableSet" binding:"required"` | ||
} | ||
|
||
// UpdateVariableRequest represents the update request structure | ||
// for a variable. | ||
type UpdateVariableRequest struct { | ||
// Name is the name of the variable. | ||
Name string `json:"name" binding:"required"` | ||
// Value is the value of the variable. | ||
Value string `json:"value"` | ||
// Type is the type of the variable. | ||
Type entity.VariableType `json:"type"` | ||
// VariableSet is the variable set to which the variable belongs. | ||
VariableSet string `json:"variableSet" binding:"required"` | ||
} | ||
|
||
func (payload *CreateVariableRequest) Validate() error { | ||
// Validate variable name. | ||
if validName(payload.Name) { | ||
return constant.ErrInvalidVariableName | ||
} | ||
|
||
// Validate variable set name. . | ||
if validName(payload.VariableSet) { | ||
return constant.ErrInvalidVariableSetName | ||
} | ||
|
||
// Validate variable type. | ||
if payload.Type != "" && | ||
payload.Type != entity.PlainTextType && payload.Type != entity.CipherTextType { | ||
return constant.ErrInvalidVariableType | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (payload *UpdateVariableRequest) Validate() error { | ||
// Validate variable name. | ||
if validName(payload.Name) { | ||
return constant.ErrInvalidVariableName | ||
} | ||
|
||
// Validate variable set name. . | ||
if validName(payload.VariableSet) { | ||
return constant.ErrInvalidVariableSetName | ||
} | ||
|
||
// Validate variable type. | ||
if payload.Type != "" && | ||
payload.Type != entity.PlainTextType && payload.Type != entity.CipherTextType { | ||
return constant.ErrInvalidVariableType | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (payload *CreateVariableRequest) Decode(r *http.Request) error { | ||
return decode(r, payload) | ||
} | ||
|
||
func (payload *UpdateVariableRequest) Decode(r *http.Request) error { | ||
return decode(r, payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package request | ||
|
||
import ( | ||
"net/http" | ||
|
||
"kusionstack.io/kusion/pkg/domain/constant" | ||
) | ||
|
||
// CreateVariableSetRequest represents the create request structure | ||
// for a variable set. | ||
type CreateVariableSetRequest struct { | ||
// Name is the name of the variable set. | ||
Name string `json:"name" binding:"required"` | ||
// Labels clarifies the scope of the variable set. | ||
Labels map[string]string `json:"labels" binding:"required"` | ||
} | ||
|
||
// UpdateVariableSetRequest represents the update request structure | ||
// for a variable set. | ||
type UpdateVariableSetRequest struct { | ||
// Name is the name of the variable set. | ||
Name string `json:"name" binding:"required"` | ||
// Labels clarifies the scope of the variable set. | ||
Labels map[string]string `json:"labels" binding:"required"` | ||
} | ||
|
||
func (payload *CreateVariableSetRequest) Validate() error { | ||
// Validate variable set name. | ||
if validName(payload.Name) { | ||
return constant.ErrInvalidVariableSetName | ||
} | ||
|
||
if len(payload.Labels) == 0 { | ||
return constant.ErrEmptyVariableSetLabels | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (payload *UpdateVariableSetRequest) Validate() error { | ||
// Validate variable set name. | ||
if payload.Name != "" && validName(payload.Name) { | ||
return constant.ErrInvalidVariableSetName | ||
} | ||
|
||
if len(payload.Labels) == 0 { | ||
return constant.ErrEmptyVariableSetLabels | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (payload *CreateVariableSetRequest) Decode(r *http.Request) error { | ||
return decode(r, payload) | ||
} | ||
|
||
func (payload *UpdateVariableSetRequest) Decode(r *http.Request) error { | ||
return decode(r, payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package response | ||
|
||
import "kusionstack.io/kusion/pkg/domain/entity" | ||
|
||
type PaginatedVariableResponse struct { | ||
Variables []*entity.Variable `json:"variable"` | ||
Total int `json:"total"` | ||
CurrentPage int `json:"currentPage"` | ||
PageSize int `json:"pageSize"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package response | ||
|
||
import "kusionstack.io/kusion/pkg/domain/entity" | ||
|
||
type PaginatedVariableSetResponse struct { | ||
VariableSets []*entity.VariableSet `json:"variableSets"` | ||
Total int `json:"total"` | ||
CurrentPage int `json:"currentPage"` | ||
PageSize int `json:"pageSize"` | ||
} | ||
|
||
type SelectedVariableSetResponse struct { | ||
VariableSets []*entity.VariableSet `json:"variableSets"` | ||
Total int `json:"total"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.