Skip to content

Commit

Permalink
AutoScale: ScheduleTrigger (#186)
Browse files Browse the repository at this point in the history
* types.AutoScaleTriggerType

* AutoScale: ScheduleTrigger
  • Loading branch information
yamamoto-febc authored Mar 24, 2023
1 parent b341d3c commit a036f57
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 24 deletions.
4 changes: 4 additions & 0 deletions internal/define/auto_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ var (
fields.AutoScaleTriggerType(),
fields.AutoScaleCPUThreshold(),
fields.AutoScaleRouterThreshold(),
fields.AutoScaleScheduleScaling(),
fields.SettingsHash(),

// status
Expand Down Expand Up @@ -161,6 +162,7 @@ var (
fields.AutoScaleTriggerType(),
fields.AutoScaleCPUThreshold(),
fields.AutoScaleRouterThreshold(),
fields.AutoScaleScheduleScaling(),

// status
fields.AutoScaleAPIKeyID(),
Expand All @@ -184,6 +186,7 @@ var (
fields.AutoScaleTriggerType(),
fields.AutoScaleCPUThreshold(),
fields.AutoScaleRouterThreshold(),
fields.AutoScaleScheduleScaling(),
// settings hash
fields.SettingsHash(),
},
Expand All @@ -200,6 +203,7 @@ var (
fields.AutoScaleTriggerType(),
fields.AutoScaleCPUThreshold(),
fields.AutoScaleRouterThreshold(),
fields.AutoScaleScheduleScaling(),
// settings hash
fields.SettingsHash(),
},
Expand Down
22 changes: 21 additions & 1 deletion internal/define/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -2916,7 +2916,7 @@ func (f *fieldsDef) AutoScaleDisabled() *dsl.FieldDesc {
func (f *fieldsDef) AutoScaleTriggerType() *dsl.FieldDesc {
return &dsl.FieldDesc{
Name: "TriggerType",
Type: meta.TypeString,
Type: meta.Static(types.EAutoScaleTriggerType("")),
Tags: &dsl.FieldTags{
MapConv: "Settings.TriggerType",
},
Expand Down Expand Up @@ -2958,3 +2958,23 @@ func (f *fieldsDef) AutoScaleRouterThreshold() *dsl.FieldDesc {
},
}
}

func (f *fieldsDef) AutoScaleScheduleScaling() *dsl.FieldDesc {
return &dsl.FieldDesc{
Name: "ScheduleScaling",
Type: &dsl.Model{
Name: "AutoScaleScheduleScaling",
IsArray: true,
NakedType: meta.Static(naked.AutoScaleScheduleScaling{}),
Fields: []*dsl.FieldDesc{
fields.Def("Action", meta.Static(types.EAutoScaleAction(""))),
fields.Def("Hour", meta.TypeInt),
fields.Def("Minute", meta.TypeInt),
fields.Def("DayOfWeek", meta.Static([]types.EDayOfTheWeek{})),
},
},
Tags: &dsl.FieldTags{
MapConv: "Settings.ScheduleScaling,recursive",
},
}
}
14 changes: 11 additions & 3 deletions naked/auto_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ type AutoScaleSettingsUpdate struct {

// AutoScaleSettings セッティング
type AutoScaleSettings struct {
TriggerType string
TriggerType types.EAutoScaleTriggerType
CPUThresholdScaling *AutoScaleCPUThresholdScaling `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
RouterThresholdScaling *AutoScaleRouterThresholdScaling `json:",omitempty" yaml:",omitempty" structs:",omitempty"`
Zones []string `json:"SakuraCloudZones"`
Config string `json:",omitempty" yaml:",omitempty"`
ScheduleScaling []*AutoScaleScheduleScaling
Zones []string `json:"SakuraCloudZones"`
Config string `json:",omitempty" yaml:",omitempty"`
Disabled bool
}

Expand All @@ -65,6 +66,13 @@ type AutoScaleRouterThresholdScaling struct {
Mbps int `json:",omitempty" yaml:",omitempty"`
}

type AutoScaleScheduleScaling struct {
Action types.EAutoScaleAction
Hour int
Minute int
DayOfWeek []types.EDayOfTheWeek
}

// AutoScaleStatus ステータス
type AutoScaleStatus struct {
RegisteredBy string `json:",omitempty" yaml:",omitempty"`
Expand Down
73 changes: 69 additions & 4 deletions test/auto_scale_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func TestAutoScaleOp_CRUD(t *testing.T) {
IgnoreFields: ignoreAutoScaleFields,
}),
},
{
Func: testAutoScaleUpdateTriggerType,
CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
ExpectValue: updateAutoScaleTriggerTypeExpected,
IgnoreFields: ignoreAutoScaleFields,
}),
},
},
Delete: &testutil.CRUDTestDeleteFunc{
Func: testAutoScaleDelete,
Expand All @@ -83,6 +90,7 @@ var (
"SettingsHash",
"CreatedAt",
"ModifiedAt",
"ScheduleScaling",
}
createAutoScaleParam = &iaas.AutoScaleCreateRequest{
Name: testutil.ResourceName("auto-scale"),
Expand All @@ -91,7 +99,7 @@ var (

Config: fmt.Sprintf(autoScaleConfigTemplate, autoScaleTestServerName, testutil.TestZone()),
Zones: []string{testutil.TestZone()},
TriggerType: "cpu",
TriggerType: types.AutoScaleTriggerTypes.CPU,
Disabled: true,
CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{
ServerPrefix: autoScaleTestServerName,
Expand All @@ -106,7 +114,7 @@ var (
Tags: createAutoScaleParam.Tags,
Availability: types.Availabilities.Available,

TriggerType: "cpu",
TriggerType: types.AutoScaleTriggerTypes.CPU,
Disabled: true,
Config: fmt.Sprintf(autoScaleConfigTemplate, autoScaleTestServerName, testutil.TestZone()),
Zones: []string{testutil.TestZone()},
Expand All @@ -125,7 +133,7 @@ var (

Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()),
Zones: []string{testutil.TestZone()},
TriggerType: "cpu",
TriggerType: types.AutoScaleTriggerTypes.CPU,
Disabled: false,
CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{
ServerPrefix: autoScaleTestServerName,
Expand All @@ -142,7 +150,7 @@ var (

Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()),
Zones: []string{testutil.TestZone()},
TriggerType: "cpu",
TriggerType: types.AutoScaleTriggerTypes.CPU,
Disabled: false,
CPUThresholdScaling: &iaas.AutoScaleCPUThresholdScaling{
ServerPrefix: autoScaleTestServerName,
Expand All @@ -151,6 +159,58 @@ var (
},
APIKeyID: os.Getenv("SAKURACLOUD_API_KEY_ID"),
}
updateAutoScaleTriggerTypeParam = &iaas.AutoScaleUpdateRequest{
Name: testutil.ResourceName("auto-scale-upd"),
Description: "desc-upd",
Tags: []string{"tag1-upd", "tag2-upd"},
IconID: testIconID,

Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()),
Zones: []string{testutil.TestZone()},
TriggerType: types.AutoScaleTriggerTypes.Schedule,
Disabled: false,
ScheduleScaling: []*iaas.AutoScaleScheduleScaling{
{
Action: types.AutoScaleActions.Up,
Hour: 10,
Minute: 15,
DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday},
},
{
Action: types.AutoScaleActions.Down,
Hour: 18,
Minute: 15,
DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday},
},
},
}
updateAutoScaleTriggerTypeExpected = &iaas.AutoScale{
Name: updateAutoScaleParam.Name,
Description: updateAutoScaleParam.Description,
Tags: updateAutoScaleParam.Tags,
Availability: types.Availabilities.Available,
IconID: testIconID,

Config: fmt.Sprintf(autoScaleConfigTemplateUpd, autoScaleTestServerName, testutil.TestZone()),
Zones: []string{testutil.TestZone()},
TriggerType: types.AutoScaleTriggerTypes.Schedule,
Disabled: false,
ScheduleScaling: []*iaas.AutoScaleScheduleScaling{
{
Action: types.AutoScaleActions.Up,
Hour: 10,
Minute: 15,
DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday},
},
{
Action: types.AutoScaleActions.Down,
Hour: 18,
Minute: 15,
DayOfWeek: []types.EDayOfTheWeek{types.DaysOfTheWeek.Monday},
},
},
APIKeyID: os.Getenv("SAKURACLOUD_API_KEY_ID"),
}
)

func testAutoScaleCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
Expand All @@ -168,6 +228,11 @@ func testAutoScaleUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (
return client.Update(ctx, ctx.ID, updateAutoScaleParam)
}

func testAutoScaleUpdateTriggerType(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
client := iaas.NewAutoScaleOp(caller)
return client.Update(ctx, ctx.ID, updateAutoScaleTriggerTypeParam)
}

func testAutoScaleDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
client := iaas.NewAutoScaleOp(caller)
return client.Delete(ctx, ctx.ID)
Expand Down
36 changes: 36 additions & 0 deletions types/auto_scale_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022-2023 The sacloud/iaas-api-go Authors
//
// 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.

package types

type EAutoScaleAction string

// AutoScaleActions サーバプランCPUコミットメント
var AutoScaleActions = struct {
// Up .
Up EAutoScaleAction
// Down .
Down EAutoScaleAction
}{
Up: EAutoScaleAction("up"),
Down: EAutoScaleAction("down"),
}

// String EAutoScaleActionの文字列表現
func (c EAutoScaleAction) String() string {
return string(c)
}

// AutoScaleActionStrings サーバプランCPUコミットメントを表す文字列
var AutoScaleActionStrings = []string{"cpu", "router", "schedule"}
42 changes: 42 additions & 0 deletions types/auto_scale_trigger_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022-2023 The sacloud/iaas-api-go Authors
//
// 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.

package types

type EAutoScaleTriggerType string

// AutoScaleTriggerTypes サーバプランCPUコミットメント
var AutoScaleTriggerTypes = struct {
// Default CPUトリガーがデフォルト
Default EAutoScaleTriggerType
// Standard 通常
CPU EAutoScaleTriggerType
// DedicatedCPU コア専有
Router EAutoScaleTriggerType
// DedicatedCPU コア専有
Schedule EAutoScaleTriggerType
}{
Default: EAutoScaleTriggerType(""),
CPU: EAutoScaleTriggerType("cpu"),
Router: EAutoScaleTriggerType("router"),
Schedule: EAutoScaleTriggerType("schedule"),
}

// String EAutoScaleTriggerTypeの文字列表現
func (c EAutoScaleTriggerType) String() string {
return string(c)
}

// AutoScaleTriggerTypeStrings サーバプランCPUコミットメントを表す文字列
var AutoScaleTriggerTypeStrings = []string{"cpu", "router", "schedule"}
Loading

0 comments on commit a036f57

Please sign in to comment.