Skip to content

Commit

Permalink
remove dependency VPC/Subnets in packer (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sangkyu-kim authored Feb 10, 2022
1 parent 34d436a commit 925ff89
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
66 changes: 50 additions & 16 deletions builder/ncloud/step_validate_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,31 @@ func (s *StepValidateTemplate) validateVpc() error {
return nil
}

if s.Config.VpcNo == "" && s.Config.SubnetNo == "" {
reqParam := &vpc.GetVpcListRequest{
RegionCode: &s.Config.RegionCode,
}

resp, err := s.Conn.vpc.V2Api.GetVpcList(reqParam)
if err != nil {
return err
}

if resp == nil || *resp.TotalRows == 0 {
return fmt.Errorf("could not found VPC in %s region", s.Config.RegionCode)
}

for _, vpc := range resp.VpcList {
s.Config.VpcNo = *vpc.VpcNo
if subnet, err := s.getFirstPublicSubnet(); err == nil {
s.Config.SubnetNo = *subnet.SubnetNo
return nil
}
}

return fmt.Errorf("could not found public subnet in entire VPCs [%s]", s.Config.RegionCode)
}

if s.Config.VpcNo != "" {
reqParam := &vpc.GetVpcDetailRequest{
RegionCode: &s.Config.RegionCode,
Expand All @@ -381,7 +406,7 @@ func (s *StepValidateTemplate) validateVpc() error {
}

if resp == nil || *resp.TotalRows == 0 {
return fmt.Errorf("cloud not found VPC `vpc_no` [%s]", s.Config.VpcNo)
return fmt.Errorf("could not found VPC `vpc_no` [%s]", s.Config.VpcNo)
}
}

Expand All @@ -400,33 +425,41 @@ func (s *StepValidateTemplate) validateVpc() error {
s.Config.VpcNo = *resp.SubnetList[0].VpcNo
s.Say("Set `vpc_no` is " + s.Config.VpcNo)
} else {
return fmt.Errorf("cloud not found public subnet in `subnet_no` [%s]", s.Config.SubnetNo)
return fmt.Errorf("could not found public subnet in `subnet_no` [%s]", s.Config.SubnetNo)
}
}

if s.Config.VpcNo != "" && s.Config.SubnetNo == "" {
reqParam := &vpc.GetSubnetListRequest{
RegionCode: &s.Config.RegionCode,
VpcNo: &s.Config.VpcNo,
SubnetTypeCode: ncloud.String("PUBLIC"),
}

resp, err := s.Conn.vpc.V2Api.GetSubnetList(reqParam)
if err != nil {
if subnet, err := s.getFirstPublicSubnet(); err != nil {
return err
}

if resp != nil && *resp.TotalRows > 0 {
s.Config.SubnetNo = *resp.SubnetList[0].SubnetNo
s.Say("Set `subnet_no` is " + s.Config.SubnetNo)
} else {
return fmt.Errorf("cloud not found public subnet in `vpc_no` [%s]", s.Config.VpcNo)
s.Config.SubnetNo = *subnet.SubnetNo
s.Say("Set `subnet_no` is " + s.Config.SubnetNo)
}
}

return nil
}

func (s *StepValidateTemplate) getFirstPublicSubnet() (*vpc.Subnet, error) {
reqParam := &vpc.GetSubnetListRequest{
RegionCode: &s.Config.RegionCode,
VpcNo: &s.Config.VpcNo,
SubnetTypeCode: ncloud.String("PUBLIC"),
}

resp, err := s.Conn.vpc.V2Api.GetSubnetList(reqParam)
if err != nil {
return nil, err
}

if resp != nil && *resp.TotalRows > 0 {
return resp.SubnetList[0], nil
}

return nil, fmt.Errorf("could not found public subnet in `vpc_no` [%s]", s.Config.VpcNo)
}

// Check ImageName / Product Code / Server Image Product Code / Server Product Code...
func (s *StepValidateTemplate) validateTemplate() error {
// Get RegionNo, ZoneNo
Expand All @@ -449,6 +482,7 @@ func (s *StepValidateTemplate) validateTemplate() error {
return err
}

s.Say(fmt.Sprintf("vpc: %s, subnet: %s", s.Config.VpcNo, s.Config.SubnetNo))
// Validate server_product_code
return s.validateServerProductCode()
}
Expand Down
20 changes: 13 additions & 7 deletions docs/builders/ncloud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ Platform](https://www.ncloud.com/).

- `server_product_code` (string) - Product (spec) code to create.

- `vpc_no` (string) - The ID of the VPC where you want to place the Server Instance. If this field is left blank, Packer will try to get the VPC ID from the `subnet_no`.
(You are required to least one between two parameters if u want using VPC environment: `vpc_no` or `subnet_no`)
- `support_vpc` (bool) - Whether to use VPC without specific vpc or subnet. Packer will try find public subnet in entire vpc if you set this value `true`.
(You are required to least one three parameters if u want using VPC environment: `support_vpc` or `vpc_no` or `subnet_no`)

- `subnet_no` (string) - The ID of the Subnet where you want to place the Server Instance. If this field is left blank, Packer will try to get the Public Subnet ID from the `vpc_no`.
- `vpc_no` (string) - The ID of the VPC where you want to place the Server Instance. If this field is left blank, Packer will try to get the VPC ID from the `subnet_no` or exists public subnet.
(You are required to least one three parameters if u want using VPC environment: `support_vpc` or `vpc_no` or `subnet_no`)

- `subnet_no` (string) - The ID of the Subnet where you want to place the Server Instance. This field is required if you are using an specific subnets.
(You are required to least one three parameters if u want using VPC environment: `support_vpc` or `vpc_no` or `subnet_no`)

- `member_server_image_no` (string) - Previous image code. If there is an
image previously created, it can be used to create a new image.
Expand Down Expand Up @@ -100,8 +104,9 @@ source "ncloud" "example-windows" {
server_image_description = "server image description"
user_data = "CreateObject(\"WScript.Shell\").run(\"cmd.exe /c powershell Set-ExecutionPolicy RemoteSigned & winrm quickconfig -q & sc config WinRM start= auto & winrm set winrm/config/service/auth @{Basic=\"\"true\"\"} & winrm set winrm/config/service @{AllowUnencrypted=\"\"true\"\"} & winrm get winrm/config/service\")"
region = "Korea"
vpc_no = "{{YOUR_VPC_ID}}" // Remove this if you use CLASSIC environment.
subnet_no = "{{YOUR_SUBNET_ID}}" // Remove this if you use CLASSIC environment.
support_vpc = true // Remove this if you use CLASSIC environment.
# vpc_no = "{{YOUR_VPC_ID}}" // Remove this if you use CLASSIC environment.
# subnet_no = "{{YOUR_SUBNET_ID}}" // Remove this if you use CLASSIC environment.
communicator = "winrm"
winrm_username = "Administrator"
# access_control_group_no = "{{YOUR_ACG_ID}}" // Specific ACG ID allowed port (5985) if you use CLASSIC environment.
Expand Down Expand Up @@ -184,8 +189,9 @@ source "ncloud" "example-linux" {
server_image_name = "packer-${local.timestamp}"
server_image_description = "server image description"
region = "Korea"
vpc_no = "{{YOUR_VPC_ID}}" // Remove this if you use CLASSIC environment.
subnet_no = "{{YOUR_SUBNET_ID}}" // Remove this if you use CLASSIC environment.
support_vpc = true // Remove this if you use CLASSIC environment.
# vpc_no = "{{YOUR_VPC_ID}}" // Remove this if you use CLASSIC environment.
# subnet_no = "{{YOUR_SUBNET_ID}}" // Remove this if you use CLASSIC environment.
communicator = "ssh"
ssh_username = "root"
}
Expand Down

0 comments on commit 925ff89

Please sign in to comment.