diff --git a/builder/ucloud/uhost/consts.go b/builder/ucloud/uhost/consts.go index b2a99ff65..c95ef1735 100644 --- a/builder/ucloud/uhost/consts.go +++ b/builder/ucloud/uhost/consts.go @@ -7,6 +7,16 @@ const ( defaultPasswordSpe = "-_" ) +const ( + osTypeWindows = "Windows" + securityGroupNonWeb = "recommend non web" + instanceStateRunning = "Running" + instanceStateStopped = "Stopped" + bootDiskStateInitializing = "Initializing" + bootDiskStateNormal = "Normal" + imageStateAvailable = "Available" +) + var bootDiskTypeMap = map[string]string{ "cloud_ssd": "CLOUD_SSD", "local_normal": "LOCAL_NORMAL", diff --git a/builder/ucloud/uhost/step_check_source_image.go b/builder/ucloud/uhost/step_check_source_image.go index e0774f9c3..a8302a870 100644 --- a/builder/ucloud/uhost/step_check_source_image.go +++ b/builder/ucloud/uhost/step_check_source_image.go @@ -24,8 +24,8 @@ func (s *stepCheckSourceImageId) Run(ctx context.Context, state multistep.StateB return halt(state, err, "Error on querying source_image_id") } - if imageSet.OsType == "Windows" { - return halt(state, err, "The builder of ucloud-uhost not support make Windows image yet") + if imageSet.OsType == osTypeWindows { + return halt(state, err, "The builder of ucloud-uhost not support build Windows image yet") } state.Put("source_image", imageSet) diff --git a/builder/ucloud/uhost/step_config_security_group.go b/builder/ucloud/uhost/step_config_security_group.go index 533ce4904..713d4be11 100644 --- a/builder/ucloud/uhost/step_config_security_group.go +++ b/builder/ucloud/uhost/step_config_security_group.go @@ -52,7 +52,7 @@ func (s *stepConfigSecurityGroup) Run(ctx context.Context, state multistep.State } for _, item := range resp.DataSet { - if item.Type == "recommend non web" { + if item.Type == securityGroupNonWeb { securityGroupId = item.FWId break } diff --git a/builder/ucloud/uhost/step_copy_image.go b/builder/ucloud/uhost/step_copy_image.go index fac0c517b..7bd818c3d 100644 --- a/builder/ucloud/uhost/step_copy_image.go +++ b/builder/ucloud/uhost/step_copy_image.go @@ -70,7 +70,7 @@ func (s *stepCopyUCloudImage) Run(ctx context.Context, state multistep.StateBag) return err } - if imageSet.State == "Available" { + if imageSet.State == imageStateAvailable { expectedImages.Remove(v.Id()) continue } diff --git a/builder/ucloud/uhost/step_create_image.go b/builder/ucloud/uhost/step_create_image.go index 1845ba50c..48268af2f 100644 --- a/builder/ucloud/uhost/step_create_image.go +++ b/builder/ucloud/uhost/step_create_image.go @@ -46,7 +46,7 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul if err != nil { return err } - if inst == nil || inst.State != "Available" { + if inst == nil || inst.State != imageStateAvailable { return newExpectedStateError("image", resp.ImageId) } diff --git a/builder/ucloud/uhost/step_create_instance.go b/builder/ucloud/uhost/step_create_instance.go index 3ad1f75d2..e797d5fea 100644 --- a/builder/ucloud/uhost/step_create_instance.go +++ b/builder/ucloud/uhost/step_create_instance.go @@ -53,7 +53,7 @@ func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) if err != nil { return err } - if inst == nil || inst.State != "Running" { + if inst == nil || inst.State != instanceStateRunning { return newExpectedStateError("instance", instanceId) } @@ -73,7 +73,7 @@ func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) s.instanceId = instanceId state.Put("instance", instance) - if instance.BootDiskState == "Initializing" { + if instance.BootDiskState == bootDiskStateInitializing { ui.Say(fmt.Sprintf("Waiting for boot disk of instance initialized when boot_disk_type is %q", s.BootDiskType)) err = retry.Config{ @@ -87,7 +87,7 @@ func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) if err != nil { return err } - if inst.BootDiskState != "Normal" { + if inst.BootDiskState != bootDiskStateNormal { return newExpectedStateError("boot_disk of instance", instanceId) } @@ -136,7 +136,7 @@ func (s *stepCreateInstance) Cleanup(state multistep.StateBag) { return } - if instance.State != "Stopped" { + if instance.State != instanceStateStopped { err = retry.Config{ Tries: 5, ShouldRetry: func(err error) bool { @@ -168,7 +168,7 @@ func (s *stepCreateInstance) Cleanup(state multistep.StateBag) { return err } - if instance.State != "Stopped" { + if instance.State != instanceStateStopped { return newExpectedStateError("instance", s.instanceId) } diff --git a/builder/ucloud/uhost/step_stop_instance.go b/builder/ucloud/uhost/step_stop_instance.go index ebc645da5..621633b3c 100644 --- a/builder/ucloud/uhost/step_stop_instance.go +++ b/builder/ucloud/uhost/step_stop_instance.go @@ -26,7 +26,7 @@ func (s *stepStopInstance) Run(ctx context.Context, state multistep.StateBag) mu return halt(state, err, fmt.Sprintf("Error on reading instance when stop %q", instance.UHostId)) } - if instance.State != "Stopped" { + if instance.State != instanceStateStopped { stopReq := conn.NewPoweroffUHostInstanceRequest() stopReq.UHostId = ucloud.String(instance.UHostId) ui.Say(fmt.Sprintf("Stopping instance %q", instance.UHostId)) @@ -59,7 +59,7 @@ func (s *stepStopInstance) Run(ctx context.Context, state multistep.StateBag) mu return err } - if instance.State != "Stopped" { + if instance.State != instanceStateStopped { return newExpectedStateError("instance", instance.UHostId) } diff --git a/builder/ucloud/uhost/utils.go b/builder/ucloud/uhost/utils.go index 1d2ed14b4..55f43a510 100644 --- a/builder/ucloud/uhost/utils.go +++ b/builder/ucloud/uhost/utils.go @@ -8,28 +8,28 @@ import ( "strings" ) -func checkStringIn(val string, availables []string) error { - for _, choice := range availables { +func checkStringIn(val string, available []string) error { + for _, choice := range available { if val == choice { return nil } } - return fmt.Errorf("should be one of %q, got %q", strings.Join(availables, ","), val) + return fmt.Errorf("should be one of %q, got %q", strings.Join(available, ","), val) } -func checkIntIn(val int, availables []int) error { - for _, choice := range availables { +func checkIntIn(val int, available []int) error { + for _, choice := range available { if val == choice { return nil } } - return fmt.Errorf("should be one of %v, got %d", availables, val) + return fmt.Errorf("should be one of %v, got %d", available, val) } -func isStringIn(val string, availables []string) bool { - for _, choice := range availables { +func isStringIn(val string, available []string) bool { + for _, choice := range available { if val == choice { return true }