Skip to content

Commit

Permalink
feat(cpts): add new datasource to query projects (#6240)
Browse files Browse the repository at this point in the history
deer-hang authored Jan 21, 2025
1 parent 78733e8 commit 9e3dc55
Showing 4 changed files with 253 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/data-sources/cpts_projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
subcategory: "Cloud Performance Test Service (CPTS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cpts_projects"
description: |-
Use this datasource to get a list of CPTS projects within HuaweiCloud.
---

# huaweicloud_cpts_projects

Use this datasource to get a list of CPTS projects within HuaweiCloud.

## Example Usage

```hcl
data "huaweicloud_cpts_projects" "test" {}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `projects` - The project details.

The [projects](#projects_struct) structure is documented below.

<a name="projects_struct"></a>
The `projects` block supports:

* `name` - The project name.

* `source` - The project source.

* `variables_no_file` - The file variable.

* `created_at` - The creation time.

* `updated_at` - The update time.

* `description` - The description.

* `id` - The project ID.
2 changes: 2 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
@@ -1269,6 +1269,8 @@ func Provider() *schema.Provider {
"huaweicloud_workspace_flavors": workspace.DataSourceWorkspaceFlavors(),
"huaweicloud_workspace_service": workspace.DataSourceService(),

"huaweicloud_cpts_projects": cpts.DataSourceCptsProjects(),

// Legacy
"huaweicloud_images_image_v2": ims.DataSourceImagesImageV2(),
"huaweicloud_networking_port_v2": vpc.DataSourceNetworkingPortV2(),
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cpts

import (
"fmt"
"testing"

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

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceCptsProjects_basic(t *testing.T) {
dataSource := "data.huaweicloud_cpts_projects.test"
rName := acceptance.RandomAccResourceName()
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceCptsProjects_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "projects.0.created_at"),
resource.TestCheckResourceAttrSet(dataSource, "projects.0.description"),
resource.TestCheckResourceAttrSet(dataSource, "projects.0.id"),
resource.TestCheckResourceAttrSet(dataSource, "projects.0.name"),
resource.TestCheckResourceAttrSet(dataSource, "projects.0.source"),
resource.TestCheckResourceAttrSet(dataSource, "projects.0.updated_at"),
),
},
},
})
}

func testDataSourceCptsProjects_basic(name string) string {
return fmt.Sprintf(`
%s
data "huaweicloud_cpts_projects" "test" {
depends_on = [huaweicloud_cpts_project.test]
}
`, testProject_basic(name))
}
153 changes: 153 additions & 0 deletions huaweicloud/services/cpts/data_source_huaweicloud_cpts_projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Generated by PMS #538
package cpts

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceCptsProjects() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCptsProjectsRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"projects": {
Type: schema.TypeList,
Computed: true,
Description: `The project details.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The project name.`,
},
"source": {
Type: schema.TypeInt,
Computed: true,
Description: `The project source.`,
},
"variables_no_file": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The file variable.`,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: `The creation time.`,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: `The update time.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The description.`,
},
"id": {
Type: schema.TypeInt,
Computed: true,
Description: `The project ID.`,
},
},
},
},
},
}
}

type ProjectsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newProjectsDSWrapper(d *schema.ResourceData, meta interface{}) *ProjectsDSWrapper {
return &ProjectsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceCptsProjectsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newProjectsDSWrapper(d, meta)
listProjectSetsRst, err := wrapper.ListProjectSets()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listProjectSetsToSchema(listProjectSetsRst)
return diag.FromErr(err)
}

// @API CPTS GET /v1/{project_id}/test-suites
func (w *ProjectsDSWrapper) ListProjectSets() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "cpts")
if err != nil {
return nil, err
}

uri := "/v1/{project_id}/test-suites"
return httphelper.New(client).
Method("GET").
URI(uri).
OffsetPager("projects", "offset", "limit", 0).
Request().
Result()
}

func (w *ProjectsDSWrapper) listProjectSetsToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("projects", schemas.SliceToList(body.Get("projects"),
func(projects gjson.Result) any {
return map[string]any{
"name": projects.Get("name").Value(),
"source": projects.Get("source").Value(),
"variables_no_file": schemas.SliceToStrList(projects.Get("variables_no_file")),
"created_at": w.setCreateTimeAttribute(projects),
"updated_at": w.setUpdateTimeAttribute(projects),
"description": projects.Get("description").Value(),
"id": projects.Get("id").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}

func (*ProjectsDSWrapper) setCreateTimeAttribute(data gjson.Result) string {
createTime := data.Get("CreateTime").String()
return utils.FormatTimeStampRFC3339(utils.ConvertTimeStrToNanoTimestamp(createTime)/1000, false)
}

func (*ProjectsDSWrapper) setUpdateTimeAttribute(data gjson.Result) string {
updateTime := data.Get("UpdateTime").String()
return utils.FormatTimeStampRFC3339(utils.ConvertTimeStrToNanoTimestamp(updateTime)/1000, false)
}

0 comments on commit 9e3dc55

Please sign in to comment.