-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NETWORK] port association resource and ids data source (#2493)
[NETWORK] port association resource and ids data source Summary of the Pull Request PR Checklist Refers to: #xxx Tests added/passed. Documentation updated. Schema updated. Release notes added. Acceptance Steps Performed === RUN TestAccNetworkingV2PortAssociate_basic === PAUSE TestAccNetworkingV2PortAssociate_basic === CONT TestAccNetworkingV2PortAssociate_basic --- PASS: TestAccNetworkingV2PortAssociate_basic (66.75s) PASS Process finished with the exit code 0 === RUN TestAccNetworkingV2PortIDsDataSource_basic === PAUSE TestAccNetworkingV2PortIDsDataSource_basic === CONT TestAccNetworkingV2PortIDsDataSource_basic --- PASS: TestAccNetworkingV2PortIDsDataSource_basic (56.40s) PASS Process finished with the exit code 0 Reviewed-by: Aloento Reviewed-by: Artem Lifshits
- Loading branch information
1 parent
53b7257
commit 6c8b5ad
Showing
9 changed files
with
759 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
subcategory: "Virtual Private Cloud (VPC)" | ||
--- | ||
|
||
# opentelekomcloud_networking_port_ids_v2 | ||
|
||
Use this data source to get a list of OpenTelekomCloud Port IDs matching the | ||
specified criteria. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "opentelekomcloud_networking_port_ids_v2" "ports" { | ||
name = "port" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `region` - (Optional, String) The region in which to obtain the V2 Neutron client. | ||
A Neutron client is needed to retrieve port ids. If omitted, the | ||
`region` argument of the provider is used. | ||
|
||
* `project_id` - (Optional, String) The owner of the port. | ||
|
||
* `name` - (Optional, String) The name of the port. | ||
|
||
* `admin_state_up` - (Optional, Bool) The administrative state of the port. | ||
|
||
* `network_id` - (Optional, String) The ID of the network the port belongs to. | ||
|
||
* `device_owner` - (Optional, String) The device owner of the port. | ||
|
||
* `mac_address` - (Optional, String) The MAC address of the port. | ||
|
||
* `device_id` - (Optional, String) The ID of the device the port belongs to. | ||
|
||
* `fixed_ip` - (Optional, String) The port IP address filter. | ||
|
||
* `status` - (Optional, String) The status of the port. | ||
|
||
* `security_group_ids` - (Optional, List) The list of port security group IDs to filter. | ||
|
||
* `sort_key` - (Optional) Sort ports based on a certain key. Defaults to none. | ||
|
||
* `sort_direction` - (Optional) Order the results in either `asc` or `desc`. | ||
Defaults to none. | ||
|
||
## Attributes Reference | ||
|
||
`ids` is set to the list of OpenTelekomCloud Port IDs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
subcategory: "Virtual Private Cloud (VPC)" | ||
--- | ||
|
||
# opentelekomcloud_networking_port_secgroup_associate_v2 | ||
|
||
Manages a V2 port's security groups within OpenTelekomCloud. Useful, when the port was | ||
created not by Terraform (e.g. Manila or LBaaS). It should not be used, when the | ||
port was created directly within Terraform. | ||
|
||
When the resource is deleted, Terraform doesn't delete the port, but unsets the | ||
list of user defined security group IDs. However, if `force` is set to `true` | ||
and the resource is deleted, Terraform will remove all assigned security group | ||
IDs. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "opentelekomcloud_networking_port_v2" "system_port" { | ||
fixed_ip = "10.0.0.10" | ||
} | ||
data "opentelekomcloud_networking_secgroup_v2" "secgroup" { | ||
name = "secgroup" | ||
} | ||
resource "opentelekomcloud_networking_port_secgroup_associate_v2" "port_1" { | ||
port_id = data.opentelekomcloud_networking_port_v2.system_port.id | ||
security_group_ids = [ | ||
data.opentelekomcloud_networking_secgroup_v2.secgroup.id, | ||
] | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional) The region in which to obtain the V2 networking client. | ||
A networking client is needed to manage a port. If omitted, the | ||
`region` argument of the provider is used. Changing this creates a new | ||
resource. | ||
|
||
* `port_id` - (Required) An UUID of the port to apply security groups to. | ||
|
||
* `security_group_ids` - (Required) A list of security group IDs to apply to | ||
the port. The security groups must be specified by ID and not name (as | ||
opposed to how they are configured with the Compute Instance). | ||
|
||
* `force` - (Optional) Whether to replace or append the list of security | ||
groups, specified in the `security_group_ids`. Defaults to `false`. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `all_security_group_ids` - The collection of Security Group IDs on the port | ||
which have been explicitly and implicitly added. | ||
|
||
## Import | ||
|
||
Port security group association can be imported using the `id` of the port, e.g. | ||
|
||
``` | ||
$ terraform import opentelekomcloud_networking_port_secgroup_associate_v2.port_1 eae26a3e-1c33-4cc1-9c31-5ght78rdf12 | ||
lifecycle { | ||
ignore_changes = [ | ||
force, | ||
security_group_ids, | ||
] | ||
} |
74 changes: 74 additions & 0 deletions
74
opentelekomcloud/acceptance/vpc/data_source_opentelekomcloud_networking_port_ids_v2_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package acceptance | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common/quotas" | ||
) | ||
|
||
func TestAccNetworkingV2PortIDsDataSource_basic(t *testing.T) { | ||
dataSourceName := "data.opentelekomcloud_networking_port_ids_v2.ports" | ||
port1Name := "opentelekomcloud_networking_port_v2.port_1" | ||
port2Name := "opentelekomcloud_networking_port_v2.port_2" | ||
t.Parallel() | ||
quotas.BookOne(t, quotas.SecurityGroup) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { common.TestAccPreCheck(t) }, | ||
ProviderFactories: common.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNetworkingV2PortIDsDataSourceBasic, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "2"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "ids.0", port1Name, "id"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "ids.1", port2Name, "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccNetworkingV2PortIDsDataSourceBasic = ` | ||
resource "opentelekomcloud_networking_network_v2" "network_1" { | ||
name = "acc_network_1" | ||
admin_state_up = "true" | ||
} | ||
resource "opentelekomcloud_networking_secgroup_v2" "sg_1" { | ||
name = "acc_secgroup_1" | ||
description = "acc_secgroup_1" | ||
} | ||
resource "opentelekomcloud_networking_port_v2" "port_1" { | ||
name = "port_1" | ||
network_id = opentelekomcloud_networking_network_v2.network_1.id | ||
admin_state_up = "true" | ||
security_group_ids = [ | ||
opentelekomcloud_networking_secgroup_v2.sg_1.id | ||
] | ||
} | ||
resource "opentelekomcloud_networking_port_v2" "port_2" { | ||
name = "port_2" | ||
network_id = opentelekomcloud_networking_network_v2.network_1.id | ||
admin_state_up = "true" | ||
security_group_ids = [ | ||
opentelekomcloud_networking_secgroup_v2.sg_1.id | ||
] | ||
} | ||
data "opentelekomcloud_networking_port_ids_v2" "ports" { | ||
sort_direction = "asc" | ||
sort_key = "name" | ||
network_id = opentelekomcloud_networking_network_v2.network_1.id | ||
depends_on = [ | ||
opentelekomcloud_networking_port_v2.port_1, | ||
opentelekomcloud_networking_port_v2.port_2, | ||
] | ||
} | ||
` |
102 changes: 102 additions & 0 deletions
102
...cloud/acceptance/vpc/resource_opentelekomcloud_networking_port_secgroup_associate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/ports" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env" | ||
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" | ||
) | ||
|
||
const resourcePortAssociateName = "opentelekomcloud_networking_port_secgroup_associate_v2.associate" | ||
|
||
func getPortResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) { | ||
client, err := cfg.NetworkingV2Client(env.OS_REGION_NAME) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating Networking v2 client: %s", err) | ||
} | ||
return ports.Get(client, state.Primary.Attributes["port_id"]).Extract() | ||
} | ||
|
||
func TestAccNetworkingV2PortAssociate_basic(t *testing.T) { | ||
var port ports.Port | ||
rc := common.InitResourceCheck( | ||
resourcePortAssociateName, | ||
&port, | ||
getPortResourceFunc, | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
common.TestAccPreCheck(t) | ||
}, | ||
ProviderFactories: common.TestAccProviderFactories, | ||
CheckDestroy: rc.CheckResourceDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccPortAssociate_basic(), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
testAccCheckNetworkingV2PortSecGroupAssociateCountSecurityGroups(&port, 2), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourcePortAssociateName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"force", "security_group_ids"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckNetworkingV2PortSecGroupAssociateCountSecurityGroups(port *ports.Port, expected int) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if len(port.SecurityGroups) != expected { | ||
return fmt.Errorf("expected %d Security Groups, got %d", expected, len(port.SecurityGroups)) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccNetworkingV2PortSecGroupAssociate = ` | ||
resource "opentelekomcloud_networking_network_v2" "network_1" { | ||
name = "acc_network_1" | ||
admin_state_up = "true" | ||
} | ||
resource "opentelekomcloud_networking_secgroup_v2" "secgroup_1" { | ||
name = "secgroup_1" | ||
description = "terraform security group acceptance test" | ||
} | ||
resource "opentelekomcloud_networking_secgroup_v2" "secgroup_2" { | ||
name = "secgroup_2" | ||
description = "terraform security group acceptance test" | ||
} | ||
resource "opentelekomcloud_networking_port_v2" "port" { | ||
name = "port_1" | ||
network_id = opentelekomcloud_networking_network_v2.network_1.id | ||
admin_state_up = "true" | ||
} | ||
` | ||
|
||
func testAccPortAssociate_basic() string { | ||
return fmt.Sprintf(` | ||
%s | ||
resource "opentelekomcloud_networking_port_secgroup_associate_v2" "associate" { | ||
port_id = opentelekomcloud_networking_port_v2.port.id | ||
force = "false" | ||
security_group_ids = [ | ||
opentelekomcloud_networking_secgroup_v2.secgroup_1.id, | ||
] | ||
} | ||
`, testAccNetworkingV2PortSecGroupAssociate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.