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

feat(VPC): add secgroup_rule_v2 data source #1068

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions docs/data-sources/networking_secgroup_rule_v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
subcategory: "Virtual Private Cloud (VPC)"
---

# flexibleengine_networking_secgroup_rule_v2

Use this data source to get the ID of an available FlexibleEngine security group rule.

## Example Usage

```hcl
data "flexibleengine_networking_secgroup_rule_v2" "secgroup_rule" {
name = "tf_test_secgroup"
}
```

## Argument Reference

* `region` - (Optional) The region in which to obtain the V2 Neutron client.
A Neutron client is needed to retrieve security groups ids. If omitted, the
`region` argument of the provider is used.

* `id` - (Optional) The ID of the security group.

* `description` - (Optional) The description of the security group rule.

* `security_group_id` - (Optional) The security group ID the rule belongs to.

* `ethertype` - (Optional) The layer 3 protocol type.

* `protocol` - (Optional) The layer 4 protocol type.

* `port_range_min` - (Optional) The lower part of the allowed port range.

* `port_range_max` - (Optional) The higher part of the allowd port range.

* `remote_ip_prefix` - (Optional) The remote CICR.

* `remote_group_id` - (Optional) The remote group id.

* `tenant_id` - (Optional) The owner of the security group.

## Attributes Reference

`id` is set to the ID of the found security group rule. In addition, the following
attributes are exported:

* `region` - See Argument Reference above.
* `description` - See Argument Reference above.
* `security_group_id` - See Argument Reference above.
* `ethertype` - See Argument Reference above.
* `protocol` - See Argument Reference above.
* `port_range_min` - See Argument Reference above.
* `port_range_max` - See Argument Reference above.
* `remote_ip_prefix` - See Argument Reference above.
* `remote_group_id` - See Argument Reference above.
* `remote ip_prefix` - See Argument Reference above.

Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package flexibleengine

import (
"fmt"
"log"
"strings"

"github.com/chnsz/golangsdk/openstack/networking/v2/extensions/security/rules"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceNetworkingSecGroupRuleV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkingSecGroupRuleV2Read,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"security_group_id": {
Type: schema.TypeString,
Optional: true,
},
"direction": {
Type: schema.TypeString,
Optional: true,
},
"ethertype": {
Type: schema.TypeString,
Optional: true,
},
"port_range_min": {
Type: schema.TypeInt,
Optional: true,
},
"port_range_max": {
Type: schema.TypeInt,
Optional: true,
},
"protocol": {
Type: schema.TypeString,
Optional: true,
},
"remote_group_id": {
Type: schema.TypeString,
Optional: true,
},
"remote_ip_prefix": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateCIDR,
StateFunc: func(v interface{}) string {
return strings.ToLower(v.(string))
},
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}

func dataSourceNetworkingSecGroupRuleV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating FlexibleEngine networking v2 client: %s", err)
}

listOpts := rules.ListOpts{
ID: d.Get("id").(string),
SecGroupID: d.Get("security_group_id").(string),
RemoteIPPrefix: d.Get("remote_ip_prefix").(string),
RemoteGroupID: d.Get("remote_group_id").(string),
Protocol: d.Get("protocol").(string),
Direction: d.Get("direction").(string),
EtherType: d.Get("ethertype").(string),
PortRangeMax: d.Get("port_range_max").(int),
PortRangeMin: d.Get("port_range_min").(int),
TenantID: d.Get("tenant_id").(string),
}

pages, err := rules.List(networkingClient, listOpts).AllPages()
allSecGroupRules, err := rules.ExtractRules(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve Security Group Rule: %s", err)
}

if len(allSecGroupRules) < 1 {
return fmt.Errorf("No Security Group Rule found")
}

if len(allSecGroupRules) > 1 {
return fmt.Errorf("More than one Security Group Rule found")
}

secGroupRule := allSecGroupRules[0]

log.Printf("[DEBUG] Retrieved Security Group Rule %s: %+v", secGroupRule.ID, secGroupRule)
d.SetId(secGroupRule.ID)
d.Set("description", secGroupRule.Description)
d.Set("tenant_id", secGroupRule.TenantID)
d.Set("region", GetRegion(d, config))
d.Set("direction", secGroupRule.Direction)
d.Set("ethertype", secGroupRule.EtherType)
d.Set("port_range_max", secGroupRule.PortRangeMax)
d.Set("port_range_min", secGroupRule.PortRangeMin)
d.Set("protocol", secGroupRule.Protocol)
d.Set("remote_group_id", secGroupRule.RemoteGroupID)
d.Set("remote_ip_prefix", secGroupRule.RemoteIPPrefix)
d.Set("security_group_id", secGroupRule.SecGroupID)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package flexibleengine

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_group,
},
{
Config: testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_rule,
},
{
Config: testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckNetworkingSecGroupRuleV2DataSourceID("data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "direction", "ingress"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "ethertype", "IPv4"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "port_range_max", "22"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "port_range_min", "22"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "protocol", "tcp"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "remote_ip_prefix", "192.168.0.1/32"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "description", "allow SSH"),
resource.TestCheckResourceAttr(
"data.flexibleengine_networking_secgroup_rule_v2.test_provider_secgroup_rule_1", "protocol", "tcp"),
),
},
},
})
}

func testAccCheckNetworkingSecGroupRuleV2DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find security group rule data source: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("Security group rule data source ID not set")
}
return nil
}
}

const testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_group = `
resource "flexibleengine_networking_secgroup_v2" "test_provider_secgroup_1" {
name = "flexibleengine_acctest_secgroup"
description = "My neutron security group for flexibleengine acctest"
}
`

var testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_rule = fmt.Sprintf(`
%s

resource "flexibleengine_networking_secgroup_rule_v2" "test_provider_secgroup_rule_1" {
direction = "ingress"
ethertype = "IPv4"
port_range_max = 22
port_range_min = 22
protocol = "tcp"
remote_ip_prefix = "192.168.0.1/32"
security_group_id = flexibleengine_networking_secgroup_v2.test_provider_secgroup_1.id
description = "allow SSH"
}`, testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_group)

var testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_basic = fmt.Sprintf(`
%s
data "flexibleengine_networking_secgroup_rule_v2" "test_provider_secgroup_rule_1" {
security_group_id = flexibleengine_networking_secgroup_v2.test_provider_secgroup_1.id
direction = "ingress"
}`, testAccFlexibleEngineNetworkingSecGroupRuleV2DataSource_rule)
93 changes: 47 additions & 46 deletions flexibleengine/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,52 +227,53 @@ func Provider() *schema.Provider {
},

DataSourcesMap: map[string]*schema.Resource{
"flexibleengine_availability_zones": dataSourceAvailabilityZones(),
"flexibleengine_blockstorage_volume_v2": dataSourceBlockStorageVolumeV2(),
"flexibleengine_compute_instance_v2": dataSourceComputeInstance(),
"flexibleengine_compute_instances": dataSourceComputeInstances(),
"flexibleengine_compute_flavors_v2": dataSourceEcsFlavors(),
"flexibleengine_networking_secgroup_v2": dataSourceNetworkingSecGroupV2(),
"flexibleengine_s3_bucket_object": dataSourceS3BucketObject(),
"flexibleengine_kms_key_v1": dataSourceKmsKeyV1(),
"flexibleengine_kms_data_key_v1": dataSourceKmsDataKeyV1(),
"flexibleengine_vpc_v1": dataSourceVirtualPrivateCloudVpcV1(),
"flexibleengine_vpc_subnet_v1": dataSourceVpcSubnetV1(),
"flexibleengine_vpc_subnet_ids_v1": dataSourceVpcSubnetIdsV1(),
"flexibleengine_vpc_peering_connection_v2": dataSourceVpcPeeringConnectionV2(),
"flexibleengine_vpc_eip": dataSourceVpcEipV1(),
"flexibleengine_nat_gateway_v2": dataSourceNatGatewayV2(),
"flexibleengine_sfs_file_system_v2": dataSourceSFSFileSystemV2(),
"flexibleengine_compute_bms_flavors_v2": dataSourceBMSFlavorV2(),
"flexibleengine_compute_bms_nic_v2": dataSourceBMSNicV2(),
"flexibleengine_compute_bms_server_v2": dataSourceBMSServersV2(),
"flexibleengine_compute_bms_keypairs_v2": dataSourceBMSKeyPairV2(),
"flexibleengine_rts_software_config_v1": dataSourceRtsSoftwareConfigV1(),
"flexibleengine_rts_stack_v1": dataSourceRTSStackV1(),
"flexibleengine_rts_stack_resource_v1": dataSourceRTSStackResourcesV1(),
"flexibleengine_csbs_backup_v1": dataSourceCSBSBackupV1(),
"flexibleengine_csbs_backup_policy_v1": dataSourceCSBSBackupPolicyV1(),
"flexibleengine_vbs_backup_policy_v2": dataSourceVBSBackupPolicyV2(),
"flexibleengine_vbs_backup_v2": dataSourceVBSBackupV2(),
"flexibleengine_cts_tracker_v1": dataSourceCTSTrackerV1(),
"flexibleengine_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(),
"flexibleengine_dcs_product_v1": dataSourceDcsProductV1(),
"flexibleengine_dms_product": dataSourceDmsProduct(),
"flexibleengine_cce_node_v3": dataSourceCceNodesV3(),
"flexibleengine_cce_node_ids_v3": dataSourceCceNodeIdsV3(),
"flexibleengine_cce_cluster_v3": dataSourceCCEClusterV3(),
"flexibleengine_cce_addon_template": dataSourceCCEAddonTemplate(),
"flexibleengine_dns_zone_v2": dataSourceDNSZoneV2(),
"flexibleengine_dds_flavors_v3": dataSourceDDSFlavorsV3(),
"flexibleengine_lb_certificate_v2": dataSourceCertificateV2(),
"flexibleengine_lb_loadbalancer_v2": dataSourceELBV2Loadbalancer(),
"flexibleengine_sdrs_domain_v1": dataSourceSdrsDomainV1(),
"flexibleengine_identity_project_v3": dataSourceIdentityProjectV3(),
"flexibleengine_identity_role_v3": dataSourceIdentityRoleV3(),
"flexibleengine_identity_custom_role_v3": dataSourceIdentityCustomRoleV3(),
"flexibleengine_vpcep_public_services": dataSourceVPCEPPublicServices(),
"flexibleengine_vpcep_endpoints": dataSourceVPCEPEndpoints(),
"flexibleengine_elb_flavors": dataSourceElbFlavorsV3(),
"flexibleengine_availability_zones": dataSourceAvailabilityZones(),
"flexibleengine_blockstorage_volume_v2": dataSourceBlockStorageVolumeV2(),
"flexibleengine_compute_instance_v2": dataSourceComputeInstance(),
"flexibleengine_compute_instances": dataSourceComputeInstances(),
"flexibleengine_compute_flavors_v2": dataSourceEcsFlavors(),
"flexibleengine_networking_secgroup_v2": dataSourceNetworkingSecGroupV2(),
"flexibleengine_networking_secgroup_rule_v2": dataSourceNetworkingSecGroupRuleV2(),
"flexibleengine_s3_bucket_object": dataSourceS3BucketObject(),
"flexibleengine_kms_key_v1": dataSourceKmsKeyV1(),
"flexibleengine_kms_data_key_v1": dataSourceKmsDataKeyV1(),
"flexibleengine_vpc_v1": dataSourceVirtualPrivateCloudVpcV1(),
"flexibleengine_vpc_subnet_v1": dataSourceVpcSubnetV1(),
"flexibleengine_vpc_subnet_ids_v1": dataSourceVpcSubnetIdsV1(),
"flexibleengine_vpc_peering_connection_v2": dataSourceVpcPeeringConnectionV2(),
"flexibleengine_vpc_eip": dataSourceVpcEipV1(),
"flexibleengine_nat_gateway_v2": dataSourceNatGatewayV2(),
"flexibleengine_sfs_file_system_v2": dataSourceSFSFileSystemV2(),
"flexibleengine_compute_bms_flavors_v2": dataSourceBMSFlavorV2(),
"flexibleengine_compute_bms_nic_v2": dataSourceBMSNicV2(),
"flexibleengine_compute_bms_server_v2": dataSourceBMSServersV2(),
"flexibleengine_compute_bms_keypairs_v2": dataSourceBMSKeyPairV2(),
"flexibleengine_rts_software_config_v1": dataSourceRtsSoftwareConfigV1(),
"flexibleengine_rts_stack_v1": dataSourceRTSStackV1(),
"flexibleengine_rts_stack_resource_v1": dataSourceRTSStackResourcesV1(),
"flexibleengine_csbs_backup_v1": dataSourceCSBSBackupV1(),
"flexibleengine_csbs_backup_policy_v1": dataSourceCSBSBackupPolicyV1(),
"flexibleengine_vbs_backup_policy_v2": dataSourceVBSBackupPolicyV2(),
"flexibleengine_vbs_backup_v2": dataSourceVBSBackupV2(),
"flexibleengine_cts_tracker_v1": dataSourceCTSTrackerV1(),
"flexibleengine_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(),
"flexibleengine_dcs_product_v1": dataSourceDcsProductV1(),
"flexibleengine_dms_product": dataSourceDmsProduct(),
"flexibleengine_cce_node_v3": dataSourceCceNodesV3(),
"flexibleengine_cce_node_ids_v3": dataSourceCceNodeIdsV3(),
"flexibleengine_cce_cluster_v3": dataSourceCCEClusterV3(),
"flexibleengine_cce_addon_template": dataSourceCCEAddonTemplate(),
"flexibleengine_dns_zone_v2": dataSourceDNSZoneV2(),
"flexibleengine_dds_flavors_v3": dataSourceDDSFlavorsV3(),
"flexibleengine_lb_certificate_v2": dataSourceCertificateV2(),
"flexibleengine_lb_loadbalancer_v2": dataSourceELBV2Loadbalancer(),
"flexibleengine_sdrs_domain_v1": dataSourceSdrsDomainV1(),
"flexibleengine_identity_project_v3": dataSourceIdentityProjectV3(),
"flexibleengine_identity_role_v3": dataSourceIdentityRoleV3(),
"flexibleengine_identity_custom_role_v3": dataSourceIdentityCustomRoleV3(),
"flexibleengine_vpcep_public_services": dataSourceVPCEPPublicServices(),
"flexibleengine_vpcep_endpoints": dataSourceVPCEPEndpoints(),
"flexibleengine_elb_flavors": dataSourceElbFlavorsV3(),

// importing new data source
"flexibleengine_apig_environments": apig.DataSourceEnvironments(),
Expand Down
Loading