Skip to content

Commit 1d2004f

Browse files
committed
feat(cci): add datasource CCI V2 namespaces
1 parent ce7ffa8 commit 1d2004f

File tree

4 files changed

+285
-1
lines changed

4 files changed

+285
-1
lines changed

docs/incubating/cciv2_namespaces.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
subcategory: "Cloud Container Instance (CCI)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cciv2_namespaces"
5+
description: |-
6+
Use this data source to get the list of CCI namespaces within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_cciv2_namespaces
10+
11+
Use this data source to get the list of CCI namespaces within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "namespace_name" {}
17+
18+
data "huaweicloud_cciv2_namespaces" "test" {
19+
name = var.namespace_name
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `region` - (Optional, String) Specifies the region in which to query the data source.
28+
If omitted, the provider-level region will be used.
29+
30+
* `name` - (Optional, String) Specifies the name of the namespace.
31+
32+
## Attribute Reference
33+
34+
In addition to all arguments above, the following attributes are exported:
35+
36+
* `id` - The data source ID.
37+
38+
* `namespaces` - The CCI namespaces.
39+
The [namespaces](#attrblock_namespaces) structure is documented below.
40+
41+
<a name="attrblock_namespaces"></a>
42+
The `namespaces` block supports:
43+
44+
* `annotations` - The annotations of the namespace.
45+
46+
* `api_version` - The API version of the namespace.
47+
48+
* `creation_timestamp` - The creation timestamp of the namespace.
49+
50+
* `finalizers` - The finalizers of the namespace.
51+
52+
* `kind` - The kind of the namespace.
53+
54+
* `labels` - The labels of the namespace.
55+
56+
* `resource_version` - The resource version of the namespace.
57+
58+
* `status` - The status of the namespace.
59+
60+
* `uid` - The uid of the namespace.

huaweicloud/provider.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,8 @@ func Provider() *schema.Provider {
557557
"huaweicloud_cce_cluster_configurations": cce.DataSourceClusterConfigurations(),
558558
"huaweicloud_cce_addons": cce.DataSourceCceAddons(),
559559

560-
"huaweicloud_cci_namespaces": cci.DataSourceCciNamespaces(),
560+
"huaweicloud_cci_namespaces": cci.DataSourceCciNamespaces(),
561+
"huaweicloud_cciv2_namespaces": cci.DataSourceV2Namespaces(),
561562

562563
"huaweicloud_ccm_certificates": ccm.DataSourceCertificates(),
563564
"huaweicloud_ccm_certificate_export": ccm.DataSourceCertificateExport(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cci
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccV2Namespaces_basic(t *testing.T) {
13+
dataSourceName := "data.huaweicloud_cciv2_namespaces.test"
14+
rName := acceptance.RandomAccResourceNameWithDash()
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
18+
ProviderFactories: acceptance.TestAccProviderFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccV2Namespaces_basic(rName),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr(dataSourceName, "namespaces.#", "1"),
24+
resource.TestCheckResourceAttr(dataSourceName, "namespaces.0.name", rName),
25+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.api_version"),
26+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.kind"),
27+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.annotations.%"),
28+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.labels.%"),
29+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.creation_timestamp"),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.resource_version"),
31+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.uid"),
32+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.finalizers.#"),
33+
resource.TestCheckResourceAttrSet(dataSourceName, "namespaces.0.status"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
func testAccV2Namespaces_basic(rName string) string {
41+
return fmt.Sprintf(`
42+
%[1]s
43+
44+
data "huaweicloud_cciv2_namespaces" "test" {
45+
depends_on = [huaweicloud_cciv2_namespace.test]
46+
47+
name = "%[2]s"
48+
}
49+
`, testAccV2Namespace_basic(rName), rName)
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package cci
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/chnsz/golangsdk"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
16+
)
17+
18+
// @API CCI GET /apis/cci/v2/namespaces
19+
// @API CCI GET /apis/cci/v2/namespaces/{name}
20+
func DataSourceV2Namespaces() *schema.Resource {
21+
return &schema.Resource{
22+
ReadContext: dataSourceV2NamespacesRead,
23+
24+
Schema: map[string]*schema.Schema{
25+
"region": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Computed: true,
29+
},
30+
"name": {
31+
Type: schema.TypeString,
32+
Optional: true,
33+
},
34+
"namespaces": {
35+
Type: schema.TypeList,
36+
Computed: true,
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"name": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: `The name of the namespace.`,
43+
},
44+
"api_version": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: `The API version of the namespace.`,
48+
},
49+
"kind": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: `The kind of the namespace.`,
53+
},
54+
"annotations": {
55+
Type: schema.TypeMap,
56+
Computed: true,
57+
Elem: &schema.Schema{Type: schema.TypeString},
58+
Description: `The annotations of the namespace.`,
59+
},
60+
"labels": {
61+
Type: schema.TypeMap,
62+
Computed: true,
63+
Elem: &schema.Schema{Type: schema.TypeString},
64+
Description: `The labels of the namespace.`,
65+
},
66+
"creation_timestamp": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: `The creation timestamp of the namespace.`,
70+
},
71+
"finalizers": {
72+
Type: schema.TypeList,
73+
Elem: &schema.Schema{Type: schema.TypeString},
74+
Computed: true,
75+
Description: `The finalizers of the namespace.`,
76+
},
77+
"resource_version": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: `The resource version of the namespace.`,
81+
},
82+
"uid": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: `The uid of the namespace.`,
86+
},
87+
"status": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: `The status of the namespace.`,
91+
},
92+
},
93+
},
94+
},
95+
},
96+
}
97+
}
98+
99+
func dataSourceV2NamespacesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
100+
cfg := meta.(*config.Config)
101+
region := cfg.GetRegion(d)
102+
client, err := cfg.NewServiceClient("cci", region)
103+
if err != nil {
104+
return diag.Errorf("error creating CCI client: %s", err)
105+
}
106+
107+
nsList := make([]interface{}, 0)
108+
if ns, ok := d.GetOk("name"); ok {
109+
resp, err := GetNamespaceDetail(client, ns.(string))
110+
if err != nil {
111+
return diag.Errorf("error getting the namespace (%s) from the server: %s", ns.(string), err)
112+
}
113+
nsList = append(nsList, resp)
114+
} else {
115+
resp, err := listNamespaces(client)
116+
if err != nil {
117+
return diag.Errorf("error finding the namespace list from the server: %s", err)
118+
}
119+
nsList = utils.PathSearch("items", resp, make([]interface{}, 0)).([]interface{})
120+
}
121+
122+
uuid, err := uuid.GenerateUUID()
123+
if err != nil {
124+
return diag.Errorf("unable to generate ID: %s", err)
125+
}
126+
d.SetId(uuid)
127+
128+
mErr := multierror.Append(nil,
129+
d.Set("region", region),
130+
d.Set("namespaces", flattenNamespace(nsList)),
131+
)
132+
133+
return diag.FromErr(mErr.ErrorOrNil())
134+
}
135+
136+
func flattenNamespace(nsList []interface{}) []interface{} {
137+
if len(nsList) == 0 {
138+
return nil
139+
}
140+
141+
rst := make([]interface{}, 0, len(nsList))
142+
for _, v := range nsList {
143+
rst = append(rst, map[string]interface{}{
144+
"name": utils.PathSearch("metadata.name", v, nil),
145+
"api_version": utils.PathSearch("apiVersion", v, nil),
146+
"kind": utils.PathSearch("kind", v, nil),
147+
"annotations": utils.PathSearch("metadata.annotations", v, nil),
148+
"labels": utils.PathSearch("metadata.labels", v, nil),
149+
"creation_timestamp": utils.PathSearch("metadata.creationTimestamp", v, nil),
150+
"resource_version": utils.PathSearch("metadata.resourceVersion", v, nil),
151+
"uid": utils.PathSearch("metadata.uid", v, nil),
152+
"finalizers": utils.PathSearch("spec.finalizers", v, nil),
153+
"status": utils.PathSearch("status.phase", v, nil),
154+
})
155+
}
156+
return rst
157+
}
158+
159+
func listNamespaces(client *golangsdk.ServiceClient) (interface{}, error) {
160+
listNamespaceHttpUrl := "apis/cci/v2/namespaces"
161+
listNamespacePath := client.Endpoint + listNamespaceHttpUrl
162+
listNamespaceOpt := golangsdk.RequestOpts{
163+
KeepResponseBody: true,
164+
MoreHeaders: map[string]string{"Content-Type": "application/json"},
165+
}
166+
167+
listNamespacesResp, err := client.Request("GET", listNamespacePath, &listNamespaceOpt)
168+
if err != nil {
169+
return nil, fmt.Errorf("error querying CCI namespaces: %s", err)
170+
}
171+
172+
return utils.FlattenResponse(listNamespacesResp)
173+
}

0 commit comments

Comments
 (0)