Skip to content

Commit 9e3dc55

Browse files
authored
feat(cpts): add new datasource to query projects (#6240)
1 parent 78733e8 commit 9e3dc55

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed

docs/data-sources/cpts_projects.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
subcategory: "Cloud Performance Test Service (CPTS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cpts_projects"
5+
description: |-
6+
Use this datasource to get a list of CPTS projects within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_cpts_projects
10+
11+
Use this datasource to get a list of CPTS projects within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_cpts_projects" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the resource.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `projects` - The project details.
33+
34+
The [projects](#projects_struct) structure is documented below.
35+
36+
<a name="projects_struct"></a>
37+
The `projects` block supports:
38+
39+
* `name` - The project name.
40+
41+
* `source` - The project source.
42+
43+
* `variables_no_file` - The file variable.
44+
45+
* `created_at` - The creation time.
46+
47+
* `updated_at` - The update time.
48+
49+
* `description` - The description.
50+
51+
* `id` - The project ID.

huaweicloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,8 @@ func Provider() *schema.Provider {
12691269
"huaweicloud_workspace_flavors": workspace.DataSourceWorkspaceFlavors(),
12701270
"huaweicloud_workspace_service": workspace.DataSourceService(),
12711271

1272+
"huaweicloud_cpts_projects": cpts.DataSourceCptsProjects(),
1273+
12721274
// Legacy
12731275
"huaweicloud_images_image_v2": ims.DataSourceImagesImageV2(),
12741276
"huaweicloud_networking_port_v2": vpc.DataSourceNetworkingPortV2(),
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cpts
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 TestAccDataSourceCptsProjects_basic(t *testing.T) {
13+
dataSource := "data.huaweicloud_cpts_projects.test"
14+
rName := acceptance.RandomAccResourceName()
15+
dc := acceptance.InitDataSourceCheck(dataSource)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
acceptance.TestAccPreCheck(t)
20+
},
21+
ProviderFactories: acceptance.TestAccProviderFactories,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testDataSourceCptsProjects_basic(rName),
25+
Check: resource.ComposeTestCheckFunc(
26+
dc.CheckResourceExists(),
27+
resource.TestCheckResourceAttrSet(dataSource, "projects.0.created_at"),
28+
resource.TestCheckResourceAttrSet(dataSource, "projects.0.description"),
29+
resource.TestCheckResourceAttrSet(dataSource, "projects.0.id"),
30+
resource.TestCheckResourceAttrSet(dataSource, "projects.0.name"),
31+
resource.TestCheckResourceAttrSet(dataSource, "projects.0.source"),
32+
resource.TestCheckResourceAttrSet(dataSource, "projects.0.updated_at"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testDataSourceCptsProjects_basic(name string) string {
40+
return fmt.Sprintf(`
41+
%s
42+
43+
data "huaweicloud_cpts_projects" "test" {
44+
depends_on = [huaweicloud_cpts_project.test]
45+
}
46+
`, testProject_basic(name))
47+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Generated by PMS #538
2+
package cpts
3+
4+
import (
5+
"context"
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+
"github.com/tidwall/gjson"
12+
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
17+
)
18+
19+
func DataSourceCptsProjects() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceCptsProjectsRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
29+
},
30+
"projects": {
31+
Type: schema.TypeList,
32+
Computed: true,
33+
Description: `The project details.`,
34+
Elem: &schema.Resource{
35+
Schema: map[string]*schema.Schema{
36+
"name": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
Description: `The project name.`,
40+
},
41+
"source": {
42+
Type: schema.TypeInt,
43+
Computed: true,
44+
Description: `The project source.`,
45+
},
46+
"variables_no_file": {
47+
Type: schema.TypeList,
48+
Computed: true,
49+
Elem: &schema.Schema{Type: schema.TypeString},
50+
Description: `The file variable.`,
51+
},
52+
"created_at": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: `The creation time.`,
56+
},
57+
"updated_at": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: `The update time.`,
61+
},
62+
"description": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Description: `The description.`,
66+
},
67+
"id": {
68+
Type: schema.TypeInt,
69+
Computed: true,
70+
Description: `The project ID.`,
71+
},
72+
},
73+
},
74+
},
75+
},
76+
}
77+
}
78+
79+
type ProjectsDSWrapper struct {
80+
*schemas.ResourceDataWrapper
81+
Config *config.Config
82+
}
83+
84+
func newProjectsDSWrapper(d *schema.ResourceData, meta interface{}) *ProjectsDSWrapper {
85+
return &ProjectsDSWrapper{
86+
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
87+
Config: meta.(*config.Config),
88+
}
89+
}
90+
91+
func dataSourceCptsProjectsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
92+
wrapper := newProjectsDSWrapper(d, meta)
93+
listProjectSetsRst, err := wrapper.ListProjectSets()
94+
if err != nil {
95+
return diag.FromErr(err)
96+
}
97+
98+
id, err := uuid.GenerateUUID()
99+
if err != nil {
100+
return diag.FromErr(err)
101+
}
102+
d.SetId(id)
103+
104+
err = wrapper.listProjectSetsToSchema(listProjectSetsRst)
105+
return diag.FromErr(err)
106+
}
107+
108+
// @API CPTS GET /v1/{project_id}/test-suites
109+
func (w *ProjectsDSWrapper) ListProjectSets() (*gjson.Result, error) {
110+
client, err := w.NewClient(w.Config, "cpts")
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
uri := "/v1/{project_id}/test-suites"
116+
return httphelper.New(client).
117+
Method("GET").
118+
URI(uri).
119+
OffsetPager("projects", "offset", "limit", 0).
120+
Request().
121+
Result()
122+
}
123+
124+
func (w *ProjectsDSWrapper) listProjectSetsToSchema(body *gjson.Result) error {
125+
d := w.ResourceData
126+
mErr := multierror.Append(nil,
127+
d.Set("region", w.Config.GetRegion(w.ResourceData)),
128+
d.Set("projects", schemas.SliceToList(body.Get("projects"),
129+
func(projects gjson.Result) any {
130+
return map[string]any{
131+
"name": projects.Get("name").Value(),
132+
"source": projects.Get("source").Value(),
133+
"variables_no_file": schemas.SliceToStrList(projects.Get("variables_no_file")),
134+
"created_at": w.setCreateTimeAttribute(projects),
135+
"updated_at": w.setUpdateTimeAttribute(projects),
136+
"description": projects.Get("description").Value(),
137+
"id": projects.Get("id").Value(),
138+
}
139+
},
140+
)),
141+
)
142+
return mErr.ErrorOrNil()
143+
}
144+
145+
func (*ProjectsDSWrapper) setCreateTimeAttribute(data gjson.Result) string {
146+
createTime := data.Get("CreateTime").String()
147+
return utils.FormatTimeStampRFC3339(utils.ConvertTimeStrToNanoTimestamp(createTime)/1000, false)
148+
}
149+
150+
func (*ProjectsDSWrapper) setUpdateTimeAttribute(data gjson.Result) string {
151+
updateTime := data.Get("UpdateTime").String()
152+
return utils.FormatTimeStampRFC3339(utils.ConvertTimeStrToNanoTimestamp(updateTime)/1000, false)
153+
}

0 commit comments

Comments
 (0)