Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SourcePort, DestinationPort to NSG rule resource #192

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.4
require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20241117121028-a3be206688b3
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113
github.com/IBM-Cloud/power-go-client v1.9.0
github.com/IBM-Cloud/power-go-client v1.10.0
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.4.4
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20241117121028-a3be206688b3/go.mod h1:/7h
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113 h1:f2Erqfea1dKpaTFagTJM6W/wnD3JGq/Vn9URh8nuRwk=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
github.com/IBM-Cloud/power-go-client v1.9.0 h1:nnErpb/7TJQe8P7OfIlJPhSJVq5oyuCJlMje9Ry6XEY=
github.com/IBM-Cloud/power-go-client v1.9.0/go.mod h1:UDyXeIKEp6r7yWUXYu3r0ZnFSlNZ2YeQTHwM2Tmlgv0=
github.com/IBM-Cloud/power-go-client v1.10.0 h1:yBUHWwvNBmLkWpbZJQJEXoxBa1Dm+eJgMSbk9ljmXUU=
github.com/IBM-Cloud/power-go-client v1.10.0/go.mod h1:UDyXeIKEp6r7yWUXYu3r0ZnFSlNZ2YeQTHwM2Tmlgv0=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf h1:koUAyF9b6X78lLLruGYPSOmrfY2YcGYKOj/Ug9nbKNw=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf/go.mod h1:6HepcfAXROz0Rf63krk5hPZyHT6qyx2MNvYyHof7ik4=
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca h1:crniVcf+YcmgF03NmmfonXwSQ73oJF+IohFYBwknMxs=
Expand Down Expand Up @@ -150,8 +150,6 @@ github.com/IBM/keyprotect-go-client v0.15.1 h1:m4qzqF5zOumRxKZ8s7vtK7A/UV/D278L8
github.com/IBM/keyprotect-go-client v0.15.1/go.mod h1:asXtHwL/4uCHA221Vd/7SkXEi2pcRHDzPyyksc1DthE=
github.com/IBM/logs-go-sdk v0.4.0 h1:CyUjm19EUtcJjf4mxsj6Rc7gkZDT8JEY5rLUIz8Eoag=
github.com/IBM/logs-go-sdk v0.4.0/go.mod h1:yv/GCXC4/p+MZEeXl4xjZAOMvDAVRwu61WyHZFKFXQM=
github.com/IBM/logs-router-go-sdk v1.0.5 h1:r0kC1+HfmSeQCD6zQTUp4PDI/zp4Ueo1Zo19ipHuNlw=
github.com/IBM/logs-router-go-sdk v1.0.5/go.mod h1:tCN2vFgu5xG0ob9iJcxi5M4bJ6mWmu3nhmRPnvlwev0=
github.com/IBM/logs-router-go-sdk v1.0.7 h1:uQjQAAcQdo3XvhY6MC7HakhZaXIUsGfUmKj2d5vkjnY=
github.com/IBM/logs-router-go-sdk v1.0.7/go.mod h1:tCN2vFgu5xG0ob9iJcxi5M4bJ6mWmu3nhmRPnvlwev0=
github.com/IBM/mqcloud-go-sdk v0.2.0 h1:QOWk8ZGk0QfIL0MOGTKzNdM3Qe0Hk+ifAFtNSFQo5HU=
Expand Down
2 changes: 2 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
Arg_DeploymentTarget = "pi_deployment_target"
Arg_DeploymentType = "pi_deployment_type"
Arg_Description = "pi_description"
Arg_DestinationPort = "pi_destination_port"
Arg_DestinationPorts = "pi_destination_ports"
Arg_DhcpID = "pi_dhcp_id"
Arg_DhcpName = "pi_dhcp_name"
Expand Down Expand Up @@ -129,6 +130,7 @@ const (
Arg_SnapShotName = "pi_snap_shot_name"
Arg_SnapshotName = "pi_snapshot_name"
Arg_SourceCRN = "pi_source_crn"
Arg_SourcePort = "pi_source_port"
Arg_SourcePorts = "pi_source_ports"
Arg_SPPPlacementGroupID = "pi_spp_placement_group_id"
Arg_SPPPlacementGroupName = "pi_spp_placement_group_name"
Expand Down
2 changes: 1 addition & 1 deletion ibm/service/power/resource_ibm_pi_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ func createPVMInstance(d *schema.ResourceData, client *instance.IBMPIInstanceCli
SysType: systype,
ImageID: flex.PtrToString(imageid),
ProcType: flex.PtrToString(processortype),
Replicants: replicants,
Replicants: &replicants,
UserData: encodeBase64(userData),
ReplicantNamingScheme: flex.PtrToString(replicationNamingScheme),
ReplicantAffinityPolicy: flex.PtrToString(replicationpolicy),
Expand Down
74 changes: 67 additions & 7 deletions ibm/service/power/resource_ibm_pi_network_security_group_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,33 @@ func ResourceIBMPINetworkSecurityGroupRule() *schema.Resource {
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_DestinationPort: {
ConflictsWith: []string{Arg_DestinationPorts, Arg_NetworkSecurityGroupRuleID},
Description: "Destination port ranges.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Maximum: {
Default: 65535,
Description: "The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.",
Optional: true,
Type: schema.TypeInt,
},
Attr_Minimum: {
Default: 1,
Description: "The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.",
Optional: true,
Type: schema.TypeInt,
},
},
},
ForceNew: true,
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
Arg_DestinationPorts: {
ConflictsWith: []string{Arg_NetworkSecurityGroupRuleID},
ConflictsWith: []string{Arg_DestinationPort, Arg_NetworkSecurityGroupRuleID},
Deprecated: "This field is deprecated. Please use 'pi_destination_port' instead.",
Description: "Destination port ranges.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -80,7 +105,7 @@ func ResourceIBMPINetworkSecurityGroupRule() *schema.Resource {
Type: schema.TypeString,
},
Arg_NetworkSecurityGroupRuleID: {
ConflictsWith: []string{Arg_Action, Arg_DestinationPorts, Arg_Protocol, Arg_Remote, Arg_SourcePorts},
ConflictsWith: []string{Arg_Action, Arg_DestinationPort, Arg_DestinationPorts, Arg_Protocol, Arg_Remote, Arg_SourcePort, Arg_SourcePorts},
Description: "The network security group rule id to remove.",
ForceNew: true,
Optional: true,
Expand Down Expand Up @@ -147,8 +172,33 @@ func ResourceIBMPINetworkSecurityGroupRule() *schema.Resource {
Optional: true,
Type: schema.TypeList,
},
Arg_SourcePort: {
ConflictsWith: []string{Arg_NetworkSecurityGroupRuleID, Arg_SourcePorts},
Description: "Source port ranges.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Maximum: {
Default: 65535,
Description: "The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.",
Optional: true,
Type: schema.TypeInt,
},
Attr_Minimum: {
Default: 1,
Description: "The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.",
Optional: true,
Type: schema.TypeInt,
},
},
},
ForceNew: true,
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
Arg_SourcePorts: {
ConflictsWith: []string{Arg_NetworkSecurityGroupRuleID},
ConflictsWith: []string{Arg_NetworkSecurityGroupRuleID, Arg_SourcePort},
Deprecated: "This field is deprecated. 'Please use pi_source_port' instead.",
Description: "Source port ranges.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -378,11 +428,21 @@ func resourceIBMPINetworkSecurityGroupRuleCreate(ctx context.Context, d *schema.
networkSecurityGroupAddRule.Remote = networkSecurityGroupRuleMapToRemote(remote)

// Optional fields
destinationPort := d.Get(Arg_DestinationPorts + ".0").(map[string]interface{})
networkSecurityGroupAddRule.DestinationPorts = networkSecurityGroupRuleMapToPort(destinationPort)
if _, ok := d.GetOk(Arg_DestinationPorts); ok {
destinationPort := d.Get(Arg_DestinationPorts + ".0").(map[string]interface{})
networkSecurityGroupAddRule.DestinationPorts = networkSecurityGroupRuleMapToPort(destinationPort)
} else {
destinationPort := d.Get(Arg_DestinationPort + ".0").(map[string]interface{})
networkSecurityGroupAddRule.DestinationPort = networkSecurityGroupRuleMapToPort(destinationPort)
}

sourcePort := d.Get(Arg_SourcePorts + ".0").(map[string]interface{})
networkSecurityGroupAddRule.SourcePorts = networkSecurityGroupRuleMapToPort(sourcePort)
if _, ok := d.GetOk(Arg_SourcePorts); ok {
sourcePort := d.Get(Arg_SourcePorts + ".0").(map[string]interface{})
networkSecurityGroupAddRule.SourcePorts = networkSecurityGroupRuleMapToPort(sourcePort)
} else {
sourcePort := d.Get(Arg_SourcePort + ".0").(map[string]interface{})
networkSecurityGroupAddRule.SourcePort = networkSecurityGroupRuleMapToPort(sourcePort)
}

networkSecurityGroup, err := nsgClient.AddRule(nsgID, &networkSecurityGroupAddRule)
ruleID := *networkSecurityGroup.ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ func TestAccIBMPINetworkSecurityGroupRuleTCP(t *testing.T) {
})
}

func TestAccIBMPINetworkSecurityGroupRulePorts(t *testing.T) {
destinationPortBegin := "1200"
sourcePortBegin := "1000"
destinationPortEnd := "2000"
sourcePortEnd := "2000"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworkSecurityGroupRuleConfigPorts(sourcePortBegin, sourcePortEnd, destinationPortBegin, destinationPortEnd),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMPINetworkSecurityGroupRuleExists("ibm_pi_network_security_group_rule.network_security_group_rule"),
resource.TestCheckResourceAttrSet("ibm_pi_network_security_group_rule.network_security_group_rule", power.Arg_NetworkSecurityGroupID),
),
},
},
})
}

func TestAccIBMPINetworkSecurityGroupRuleRemove(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Expand Down Expand Up @@ -103,9 +123,6 @@ func testAccCheckIBMPINetworkSecurityGroupRuleConfigAddRuleTCP() string {
tcp_flags {
flag = "syn"
}
tcp_flags {
flag = "psh"
}
type = "tcp"
}
pi_remote {
Expand All @@ -115,6 +132,30 @@ func testAccCheckIBMPINetworkSecurityGroupRuleConfigAddRuleTCP() string {
}`, acc.Pi_cloud_instance_id, acc.Pi_network_security_group_id, acc.Pi_remote_id, acc.Pi_remote_type)
}

func testAccCheckIBMPINetworkSecurityGroupRuleConfigPorts(sourcePortBegin string, sourcePortEnd string, destinationPortBegin string, destinationPortEnd string) string {
return fmt.Sprintf(`
resource "ibm_pi_network_security_group_rule" "network_security_group_rule" {
pi_cloud_instance_id = "%[1]s"
pi_network_security_group_id = "%[2]s"
pi_action = "allow"
pi_protocol {
type = "tcp"
}
pi_source_port {
minimum = %[5]s
maximum = %[6]s
}
pi_destination_port {
minimum = %[7]s
maximum = %[8]s
}
pi_remote {
id = "%[3]s"
type = "%[4]s"
}
}`, acc.Pi_cloud_instance_id, acc.Pi_network_security_group_id, acc.Pi_remote_id, acc.Pi_remote_type, sourcePortBegin, sourcePortEnd, destinationPortBegin, destinationPortEnd)
}

func testAccCheckIBMPINetworkSecurityGroupRuleConfigRemoveRule() string {
return fmt.Sprintf(`
resource "ibm_pi_network_security_group_rule" "network_security_group_rule" {
Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/pi_network_security_group_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ Review the argument references that you can specify for your resource.
Nested schema for `pi_destination_port`:
- `maximum` - (Optional, Int) The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.
- `minimum` - (Optional, Int) The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.
- `pi_destination_ports` - (Deprecated, Optional, List) The list of destination port. Deprecated, please use `pi_destination_port`.

Nested schema for `pi_destination_ports`:
- `maximum` - (Optional, Int) The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.
- `minimum` - (Optional, Int) The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.
- `pi_network_security_group_id` - (Required, String) The unique identifier of the network security group.
- `pi_network_security_group_rule_id` - (Optional, String) The network security group rule id to remove. Required if none of the other optional fields are provided.
- `pi_protocol` - (Optional, List) The list of protocol. Required if `pi_network_security_group_rule_id` is not provided.
Expand All @@ -96,6 +101,11 @@ Review the argument references that you can specify for your resource.
Nested schema for `pi_source_port`:
- `maximum` - (Optional, Int) The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.
- `minimum` - (Optional, Int) The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.
- `pi_source_ports` - (Deprecated, Optional, List) List of source port. Deprecated, please use `pi_source_port`.

Nested schema for `pi_source_ports`:
- `maximum` - (Optional, Int) The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.
- `minimum` - (Optional, Int) The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.

- `pi_name` - (Optional, String) The name of the network security group rule. Required if `pi_network_security_group_rule_id` is not provided.

Expand Down
Loading