Skip to content

Commit

Permalink
Replace enum type for ToolsMode to string
Browse files Browse the repository at this point in the history
  • Loading branch information
vapopov committed Oct 17, 2024
1 parent 1e3b6a4 commit 4544f49
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 165 deletions.
180 changes: 57 additions & 123 deletions api/gen/proto/go/teleport/autoupdate/v1/autoupdate.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions api/proto/teleport/autoupdate/v1/autoupdate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,7 @@ message AutoUpdateConfigSpec {
// AutoUpdateConfigSpecTools encodes the parameters for client tools auto updates.
message AutoUpdateConfigSpecTools {
// Mode defines state of the client tools auto update.
ToolsUpdateMode mode = 1;
}

// ToolsMode type for client tools enabling state.
enum ToolsUpdateMode {
// TOOLS_UPDATE_MODE_UNSPECIFIED is undefined mode.
TOOLS_UPDATE_MODE_UNSPECIFIED = 0;
// TOOLS_UPDATE_MODE_ENABLED client tools auto update is enabled.
TOOLS_UPDATE_MODE_ENABLED = 1;
// TOOLS_UPDATE_MODE_DISABLED client tools auto update is disabled.
TOOLS_UPDATE_MODE_DISABLED = 2;
string mode = 1;
}

// AutoUpdateVersion is a resource singleton with version required for
Expand Down
24 changes: 12 additions & 12 deletions api/types/autoupdate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
"github.com/gravitational/teleport/api/types"
)

const (
// ToolsUpdateModeEnabled enables client tools automatic updates.
ToolsUpdateModeEnabled = "enabled"
// ToolsUpdateModeDisabled disables client tools automatic updates.
ToolsUpdateModeDisabled = "disabled"
)

// NewAutoUpdateConfig creates a new auto update configuration resource.
func NewAutoUpdateConfig(spec *autoupdate.AutoUpdateConfigSpec) (*autoupdate.AutoUpdateConfig, error) {
config := &autoupdate.AutoUpdateConfig{
Expand Down Expand Up @@ -58,18 +65,11 @@ func ValidateAutoUpdateConfig(c *autoupdate.AutoUpdateConfig) error {
if c.Spec == nil {
return trace.BadParameter("Spec is nil")
}
if c.Spec.Tools != nil {
if c.Spec.Tools.Mode != ToolsUpdateModeDisabled && c.Spec.Tools.Mode != ToolsUpdateModeEnabled {
return trace.BadParameter("ToolsMode is not valid")
}
}

return nil
}

// ToolsModeToAPI transforms client tools mode enum to API string representation.
func ToolsModeToAPI(mode autoupdate.ToolsUpdateMode) string {
switch mode {
case autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_ENABLED:
return "enabled"
case autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_DISABLED:
return "disabled"
default:
return ""
}
}
19 changes: 15 additions & 4 deletions api/types/autoupdate/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestNewAutoUpdateConfig(t *testing.T) {
name: "success tools autoupdate disabled",
spec: &autoupdate.AutoUpdateConfigSpec{
Tools: &autoupdate.AutoUpdateConfigSpecTools{
Mode: autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_DISABLED,
Mode: ToolsUpdateModeDisabled,
},
},
assertErr: func(t *testing.T, err error, a ...any) {
Expand All @@ -56,7 +56,7 @@ func TestNewAutoUpdateConfig(t *testing.T) {
},
Spec: &autoupdate.AutoUpdateConfigSpec{
Tools: &autoupdate.AutoUpdateConfigSpecTools{
Mode: autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_DISABLED,
Mode: ToolsUpdateModeDisabled,
},
},
},
Expand All @@ -65,7 +65,7 @@ func TestNewAutoUpdateConfig(t *testing.T) {
name: "success tools autoupdate enabled",
spec: &autoupdate.AutoUpdateConfigSpec{
Tools: &autoupdate.AutoUpdateConfigSpecTools{
Mode: autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_ENABLED,
Mode: ToolsUpdateModeEnabled,
},
},
assertErr: func(t *testing.T, err error, a ...any) {
Expand All @@ -79,7 +79,7 @@ func TestNewAutoUpdateConfig(t *testing.T) {
},
Spec: &autoupdate.AutoUpdateConfigSpec{
Tools: &autoupdate.AutoUpdateConfigSpecTools{
Mode: autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_ENABLED,
Mode: ToolsUpdateModeEnabled,
},
},
},
Expand All @@ -91,6 +91,17 @@ func TestNewAutoUpdateConfig(t *testing.T) {
require.ErrorContains(t, err, "Spec is nil")
},
},
{
name: "invalid tools mode",
spec: &autoupdate.AutoUpdateConfigSpec{
Tools: &autoupdate.AutoUpdateConfigSpecTools{
Mode: "invalid-mode",
},
},
assertErr: func(t *testing.T, err error, a ...any) {
require.ErrorContains(t, err, "ToolsMode is not valid")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3982,7 +3982,7 @@ func newAutoUpdateConfig(t *testing.T) *autoupdate.AutoUpdateConfig {

r, err := update.NewAutoUpdateConfig(&autoupdate.AutoUpdateConfigSpec{
Tools: &autoupdate.AutoUpdateConfigSpecTools{
Mode: autoupdate.ToolsUpdateMode_TOOLS_UPDATE_MODE_ENABLED,
Mode: update.ToolsUpdateModeEnabled,
},
})
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 4544f49

Please sign in to comment.