Skip to content

Commit 769e259

Browse files
committed
feat(cci): add resource CCI v2 network
1 parent 11cf841 commit 769e259

File tree

4 files changed

+522
-0
lines changed

4 files changed

+522
-0
lines changed

docs/resources/cciv2_network.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
subcategory: "Cloud Container Instance (CCI)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cciv2_network"
5+
description: |-
6+
Manages a CCI Network resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_cciv2_network
10+
11+
Manages a CCI Network resource within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
24+
If omitted, the provider-level region will be used.
25+
Changing this creates a new resource.
26+
27+
* `name` - (Required, String) Specifies the name of the CCI network.
28+
29+
* `namespace` - (Required, String) Specifies the namespace.
30+
31+
* `annotations` - (Optional, Map) Specifies the annotations of the CCI network.
32+
33+
* `ip_families` - (Optional, List) Specifies the IP families of the CCI network.
34+
35+
* `security_group_ids` - (Optional, List) Specifies the security group IDs of the CCI network.
36+
37+
* `subnets` - (Optional, List) Specifies the subnets of the CCI network.
38+
The [subnets](#block--subnets) structure is documented below.
39+
40+
<a name="block--subnets"></a>
41+
The `subnets` block supports:
42+
43+
* `subnet_id` - (Optional, String) Specifies the subnet ID of the CCI network.
44+
45+
## Attribute Reference
46+
47+
In addition to all arguments above, the following attributes are exported:
48+
49+
* `id` - The resource ID.
50+
51+
* `api_version` - The API version of the CCI network.
52+
53+
* `kind` - The kind of the CCI network.
54+
55+
* `resource_version` - The resource version of the namespace.
56+
57+
* `self_link` - The self link of the namespace.
58+
59+
* `status` - The status of the namespace.
60+
61+
* `uid` - The uid of the namespace.
62+
63+
## Timeouts
64+
65+
This resource provides the following timeouts configuration options:
66+
67+
* `create` - Default is 10 minutes.
68+
* `delete` - Default is 10 minutes.
69+
70+
## Import
71+
72+
The xxx can be imported using `id`, e.g.
73+
74+
```bash
75+
$ terraform import huaweicloud_cciv2_network.test <id>
76+
```

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ func Provider() *schema.Provider {
15611561
"huaweicloud_cci_namespace": cci.ResourceCciNamespace(),
15621562
"huaweicloud_cci_network": cci.ResourceCciNetworkV1(),
15631563
"huaweicloud_cci_pvc": cci.ResourcePersistentVolumeClaimV1(),
1564+
"huaweicloud_cciv2_network": cci.ResourceV2Network(),
15641565

15651566
"huaweicloud_ccm_certificate": ccm.ResourceCCMCertificate(),
15661567
"huaweicloud_ccm_certificate_apply": ccm.ResourceCertificateApply(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package cci
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
10+
"github.com/chnsz/golangsdk/openstack/cci/v1/networks"
11+
12+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cci"
16+
)
17+
18+
func getV2NetworkResourceFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
19+
client, err := conf.CciV1BetaClient(acceptance.HW_REGION_NAME)
20+
if err != nil {
21+
return nil, fmt.Errorf("error creating CCI network v2 client: %s", err)
22+
}
23+
return cci.GetNetwork(client, state.Primary.Attributes["namespace"], state.Primary.ID)
24+
}
25+
26+
func TestAccV2Network_basic(t *testing.T) {
27+
var network networks.Network
28+
rName := acceptance.RandomAccResourceNameWithDash()
29+
resourceName := "huaweicloud_cciv2_network.test"
30+
31+
rc := acceptance.InitResourceCheck(
32+
resourceName,
33+
&network,
34+
getV2NetworkResourceFunc,
35+
)
36+
37+
resource.ParallelTest(t, resource.TestCase{
38+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
39+
ProviderFactories: acceptance.TestAccProviderFactories,
40+
CheckDestroy: rc.CheckResourceDestroy(),
41+
Steps: []resource.TestStep{
42+
{
43+
Config: testAccV2Network_basic(rName),
44+
Check: resource.ComposeTestCheckFunc(
45+
rc.CheckResourceExists(),
46+
resource.TestCheckResourceAttr(resourceName, "name", rName),
47+
resource.TestCheckResourceAttr(resourceName, "status", "Active"),
48+
resource.TestCheckResourceAttrSet(resourceName, "status"),
49+
resource.TestCheckResourceAttrSet(resourceName, "cidr"),
50+
resource.TestCheckResourceAttrPair(resourceName, "namespace",
51+
"huaweicloud_cciv2_namespace.test", "name"),
52+
resource.TestCheckResourceAttrPair(resourceName, "network_id",
53+
"huaweicloud_vpc_subnet.test", "id"),
54+
resource.TestCheckResourceAttrPair(resourceName, "subnet_id",
55+
"huaweicloud_vpc_subnet.test", "subnet_id"),
56+
resource.TestCheckResourceAttrPair(resourceName, "security_group_id",
57+
"huaweicloud_networking_secgroup.test", "id"),
58+
resource.TestCheckResourceAttrPair(resourceName, "vpc_id",
59+
"huaweicloud_vpc.test", "id"),
60+
),
61+
},
62+
{
63+
ResourceName: resourceName,
64+
ImportState: true,
65+
ImportStateVerify: true,
66+
ImportStateIdFunc: testAccV2NetworkImportStateFunc(resourceName),
67+
},
68+
},
69+
})
70+
}
71+
72+
func testAccV2NetworkImportStateFunc(name string) resource.ImportStateIdFunc {
73+
return func(s *terraform.State) (string, error) {
74+
rs, ok := s.RootModule().Resources[name]
75+
if !ok {
76+
return "", fmt.Errorf("Resource (%s) not found: %s", name, rs)
77+
}
78+
if rs.Primary.ID == "" || rs.Primary.Attributes["namespace"] == "" {
79+
return "", fmt.Errorf("the namespace name (%s) or network ID (%s) is nil",
80+
rs.Primary.Attributes["namespace"], rs.Primary.ID)
81+
}
82+
return fmt.Sprintf("%s/%s", rs.Primary.Attributes["namespace"], rs.Primary.ID), nil
83+
}
84+
}
85+
86+
func testAccV2Network_base(rName string) string {
87+
return fmt.Sprintf(`
88+
%s
89+
90+
resource "huaweicloud_cci_namespace" "test" {
91+
name = "%s"
92+
type = "general-computing"
93+
}
94+
`, common.TestBaseNetwork(rName), rName)
95+
}
96+
97+
func testAccV2Network_basic(rName string) string {
98+
return fmt.Sprintf(`
99+
%s
100+
101+
resource "huaweicloud_cci_network" "test" {
102+
name = "%s"
103+
namespace = huaweicloud_cci_namespace.test.name
104+
network_id = huaweicloud_vpc_subnet.test.id
105+
security_group_id = huaweicloud_networking_secgroup.test.id
106+
}
107+
`, testAccV2Network_base(rName), rName)
108+
}

0 commit comments

Comments
 (0)