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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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-beta4
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
4 changes: 2 additions & 2 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-beta4 h1:zWgBmbX+OIbnSJRVaumZcI5MF/opzRVIQoQlz0Xm094=
github.com/IBM-Cloud/power-go-client v1.10.0-beta4/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
3 changes: 3 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 @@ -94,6 +95,7 @@ const (
Arg_NetworkSecurityGroupRuleID = "pi_network_security_group_rule_id"
Arg_NetworkType = "pi_network_type"
Arg_OnboardingVolumes = "pi_onboarding_volumes"
Arg_Parameters = "pi_parameters"
Arg_PinPolicy = "pi_pin_policy"
Arg_PlacementGroupID = "pi_placement_group_id"
Arg_PlacementGroupName = "pi_placement_group_name"
Expand Down Expand Up @@ -127,6 +129,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
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 @@ -124,6 +144,30 @@ func testAccCheckIBMPINetworkSecurityGroupRuleConfigRemoveRule() string {
}`, acc.Pi_cloud_instance_id, acc.Pi_network_security_group_id, acc.Pi_network_security_group_rule_id)
}

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 testAccCheckIBMPINetworkSecurityGroupRuleExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
16 changes: 15 additions & 1 deletion ibm/service/power/resource_ibm_pi_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func ResourceIBMPIWorkspace() *schema.Resource {
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_Parameters: {
Description: "Parameters for workspace creation.",
ForceNew: true,
Optional: true,
Type: schema.TypeMap,
},
Arg_Plan: {
Default: Public,
Description: "Plan associated with the offering; Valid values are public or private.",
Expand Down Expand Up @@ -103,9 +109,17 @@ func resourceIBMPIWorkspaceCreate(ctx context.Context, d *schema.ResourceData, m
resourceGroup := d.Get(Arg_ResourceGroupID).(string)
plan := d.Get(Arg_Plan).(string)

parameters := map[string]any{}
if v, ok := d.GetOk(Arg_Parameters); ok {
paramMap := v.(map[string]interface{})
for key, value := range paramMap {
parameters[key] = value
}
}

// No need for cloudInstanceID because we are creating a workspace
client := instance.NewIBMPIWorkspacesClient(ctx, sess, "")
controller, _, err := client.Create(name, datacenter, resourceGroup, plan)
controller, _, err := client.Create(name, datacenter, resourceGroup, plan, parameters)
if err != nil {
log.Printf("[DEBUG] create workspace failed %v", err)
return diag.FromErr(err)
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
1 change: 1 addition & 0 deletions website/docs/r/pi_workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Review the argument references that you can specify for your resource.

- `pi_datacenter` - (Required, String) Target location or environment to create the resource instance.
- `pi_name` - (Required, String) A descriptive name used to identify the workspace.
- `pi_parameters` - (Optional, Map) Extra parameters for workspace creation.
- `pi_plan` - (Optional, String) Plan associated with the offering; Valid values are `public` or `private`. The default value is `public`.
- `pi_resource_group_id` - (Required, String) The ID of the resource group where you want to create the workspace. You can retrieve the value from data source `ibm_resource_group`.
- `pi_user_tags` - (Optional, List) List of user tags attached to the resource.
Expand Down
Loading