2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-17 05:25:56 -05:00
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2019-06-13 03:16:49 -04:00
|
|
|
"github.com/ucloud/ucloud-sdk-go/ucloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigSecurityGroup struct {
|
|
|
|
SecurityGroupId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigSecurityGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-10-12 04:46:21 -04:00
|
|
|
client := state.Get("client").(*ucloudcommon.UCloudClient)
|
|
|
|
conn := client.UNetConn
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
if len(s.SecurityGroupId) != 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Trying to use specified security group %q...", s.SecurityGroupId))
|
2019-10-12 04:46:21 -04:00
|
|
|
securityGroupSet, err := client.DescribeFirewallById(s.SecurityGroupId)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
if ucloudcommon.IsNotFoundError(err) {
|
2019-06-28 00:01:47 -04:00
|
|
|
err = fmt.Errorf("the specified security group %q does not exist", s.SecurityGroupId)
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, "")
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified security group %q", s.SecurityGroupId))
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("security_group_id", securityGroupSet.FWId)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Trying to use default security group...")
|
|
|
|
var securityGroupId string
|
|
|
|
var limit = 100
|
|
|
|
var offset int
|
|
|
|
|
|
|
|
for {
|
|
|
|
req := conn.NewDescribeFirewallRequest()
|
|
|
|
req.Limit = ucloud.Int(limit)
|
|
|
|
req.Offset = ucloud.Int(offset)
|
|
|
|
|
|
|
|
resp, err := conn.DescribeFirewall(req)
|
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, "Error on querying default security group")
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.DataSet) < 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range resp.DataSet {
|
2019-10-12 04:46:21 -04:00
|
|
|
if item.Type == ucloudcommon.SecurityGroupNonWeb {
|
2019-06-13 03:16:49 -04:00
|
|
|
securityGroupId = item.FWId
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.DataSet) < limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = offset + limit
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:32:33 -04:00
|
|
|
if securityGroupId == "" {
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, fmt.Errorf("the default security group does not exist"), "")
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-06-19 09:32:33 -04:00
|
|
|
|
|
|
|
state.Put("security_group_id", securityGroupId)
|
2019-06-13 03:16:49 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigSecurityGroup) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|