2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/ucloud/ucloud-sdk-go/ucloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigSecurityGroup struct {
|
|
|
|
SecurityGroupId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigSecurityGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*UCloudClient)
|
|
|
|
conn := client.unetconn
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if len(s.SecurityGroupId) != 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Trying to use specified security group %q...", s.SecurityGroupId))
|
|
|
|
securityGroupSet, err := client.describeFirewallById(s.SecurityGroupId)
|
|
|
|
if err != nil {
|
|
|
|
if isNotFoundError(err) {
|
2019-06-28 00:01:47 -04:00
|
|
|
err = fmt.Errorf("the specified security group %q does not exist", s.SecurityGroupId)
|
2019-06-13 03:16:49 -04:00
|
|
|
return halt(state, err, "")
|
|
|
|
}
|
2019-06-19 09:32:33 -04:00
|
|
|
return 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-06-19 09:32:33 -04:00
|
|
|
return 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-06-13 08:17:08 -04:00
|
|
|
if item.Type == 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 == "" {
|
|
|
|
return halt(state, fmt.Errorf("the default security group 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) {
|
|
|
|
}
|