Skip to content

Commit

Permalink
refactor(cpts): refactor cpts project resource to general code style (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang authored Jan 20, 2025
1 parent e7b5df1 commit ffa298b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ package cpts

import (
"fmt"
"strconv"
"strings"
"testing"

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

"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cpts/v1/model"
"github.com/chnsz/golangsdk"

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

func getProjectResourceFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := conf.HcCptsV1Client(acceptance.HW_REGION_NAME)
client, err := conf.NewServiceClient("cpts", acceptance.HW_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating CPTS v1 client: %s", err)
return nil, fmt.Errorf("error creating CPTS client: %s", err)
}

id, err := strconv.ParseInt(state.Primary.ID, 10, 32)
if err != nil {
return nil, fmt.Errorf("the project ID must be integer: %s", err)
requestPath := client.Endpoint + "v1/{project_id}/test-suites/{test_suite_id}"
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestPath = strings.ReplaceAll(requestPath, "{test_suite_id}", state.Primary.ID)
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}

request := &model.ShowProjectRequest{
TestSuiteId: int32(id),
resp, err := client.Request("GET", requestPath, &requestOpt)
if err != nil {
return nil, fmt.Errorf("error retrieving CPTS project: %s", err)
}

return client.ShowProject(request)
return utils.FlattenResponse(resp)
}

func TestAccProject_basic(t *testing.T) {
var obj model.CreateProjectResponse
var obj interface{}

rName := acceptance.RandomAccResourceName()
resourceName := "huaweicloud_cpts_project.test"
Expand Down
170 changes: 107 additions & 63 deletions huaweicloud/services/cpts/resource_huaweicloud_cpts_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package cpts
import (
"context"
"strconv"
"time"
"strings"

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

"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cpts/v1/model"
"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
Expand Down Expand Up @@ -61,123 +61,167 @@ func ResourceProject() *schema.Resource {
}
}

func buildCreateProjectBodyParams(d *schema.ResourceData) map[string]interface{} {
return map[string]interface{}{
"name": d.Get("name"),
"description": d.Get("description"),
}
}

func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*config.Config)
region := c.GetRegion(d)
client, err := c.HcCptsV1Client(region)
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/test-suites"
product = "cpts"
)

client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating CPTS v1 client: %s", err)
return diag.Errorf("error creating CPTS client: %s", err)
}

createOpts := &model.CreateProjectRequest{
Body: &model.CreateProjectRequestBody{
Name: d.Get("name").(string),
Description: utils.String(d.Get("description").(string)),
},
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: buildCreateProjectBodyParams(d),
}
response, err := client.CreateProject(createOpts)

resp, err := client.Request("POST", requestPath, &requestOpt)
if err != nil {
return diag.Errorf("error creating CPTS project: %s", err)
}

if response.ProjectId == nil {
return diag.Errorf("error creating CPTS project: id not found in api response")
respBody, err := utils.FlattenResponse(resp)
if err != nil {
return diag.FromErr(err)
}

projectID := utils.PathSearch("project_id", respBody, nil)
if projectID == nil {
return diag.Errorf("error creating CPTS project: ID is not found in API response")
}

d.SetId(strconv.Itoa(int(*response.ProjectId)))
// The `project_id` field is a numeric type.
d.SetId(strconv.Itoa(int(projectID.(float64))))
return resourceProjectRead(ctx, d, meta)
}

func resourceProjectRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*config.Config)
region := c.GetRegion(d)
client, err := c.HcCptsV1Client(region)
if err != nil {
return diag.Errorf("error creating CPTS v1 client: %s", err)
}
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/test-suites/{test_suite_id}"
product = "cpts"
)

id, err := strconv.ParseInt(d.Id(), 10, 32)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("the project ID must be integer: %s", err)
return diag.Errorf("error creating CPTS client: %s", err)
}

response, err := client.ShowProject(&model.ShowProjectRequest{
TestSuiteId: int32(id),
})
if err != nil {
return common.CheckDeletedDiag(d, err, "the project is not found")
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestPath = strings.ReplaceAll(requestPath, "{test_suite_id}", d.Id())
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}

layout := "2006-01-02T15:04:05-07:00"
createTime, err := time.Parse(layout, *response.Project.CreateTime)
resp, err := client.Request("GET", requestPath, &requestOpt)
if err != nil {
return diag.Errorf("error parsing the time: %s", err)
return common.CheckDeletedDiag(d,
common.ConvertExpected403ErrInto404Err(err, "code", "SVCSTG.CPTS.4032002"),
"error retrieving CPTS project")
}
updateTime, err := time.Parse(layout, *response.Project.UpdateTime)

respBody, err := utils.FlattenResponse(resp)
if err != nil {
return diag.Errorf("error parsing the time: %s", err)
return diag.FromErr(err)
}

createTime := utils.PathSearch("project.create_time", respBody, "").(string)
updateTime := utils.PathSearch("project.update_time", respBody, "").(string)

mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("name", response.Project.Name),
d.Set("description", response.Project.Description),
d.Set("created_at", utils.FormatTimeStampUTC(createTime.Unix())),
d.Set("updated_at", utils.FormatTimeStampUTC(updateTime.Unix())),
d.Set("name", utils.PathSearch("project.name", respBody, nil)),
d.Set("description", utils.PathSearch("project.description", respBody, nil)),
d.Set("created_at", utils.FormatTimeStampUTC(utils.ConvertTimeStrToNanoTimestamp(createTime)/1000)),
d.Set("updated_at", utils.FormatTimeStampUTC(utils.ConvertTimeStrToNanoTimestamp(updateTime)/1000)),
)

return diag.FromErr(mErr.ErrorOrNil())
}

func buildUpdateProjectBodyParams(d *schema.ResourceData, idInt64 int64) map[string]interface{} {
return map[string]interface{}{
"id": idInt64,
"name": d.Get("name"),
"description": d.Get("description"),
}
}

func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*config.Config)
region := c.GetRegion(d)
client, err := c.HcCptsV1Client(region)
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/test-suites/{test_suite_id}"
product = "cpts"
)

client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating CPTS v1 client: %s", err)
return diag.Errorf("error creating CPTS client: %s", err)
}

id, err := strconv.ParseInt(d.Id(), 10, 32)
idInt64, err := strconv.ParseInt(d.Id(), 10, 32)
if err != nil {
return diag.Errorf("the project ID must be integer: %s", err)
}

_, err = client.UpdateProject(&model.UpdateProjectRequest{
TestSuiteId: int32(id),
Body: &model.UpdateProjectRequestBody{
Id: int32(id),
Name: d.Get("name").(string),
Description: utils.String(d.Get("description").(string)),
},
})
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestPath = strings.ReplaceAll(requestPath, "{test_suite_id}", d.Id())
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{200, 201, 204},
JSONBody: buildUpdateProjectBodyParams(d, idInt64),
}

_, err = client.Request("PUT", requestPath, &requestOpt)
if err != nil {
return diag.Errorf("error updating the project %q: %s", id, err)
return diag.Errorf("error updating CPTS project: %s", err)
}

return resourceProjectRead(ctx, d, meta)
}

func resourceProjectDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*config.Config)
region := c.GetRegion(d)
client, err := c.HcCptsV1Client(region)
if err != nil {
return diag.Errorf("error creating CPTS v1 client: %s", err)
}
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/test-suites/{test_suite_id}"
product = "cpts"
)

id, err := strconv.ParseInt(d.Id(), 10, 32)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("the project ID must be integer: %s", err)
return diag.Errorf("error creating CPTS client: %s", err)
}

deleteOpts := &model.DeleteProjectRequest{
TestSuiteId: int32(id),
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestPath = strings.ReplaceAll(requestPath, "{test_suite_id}", d.Id())
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}

_, err = client.DeleteProject(deleteOpts)
_, err = client.Request("DELETE", requestPath, &requestOpt)
if err != nil {
return diag.Errorf("error deleting CPTS project %q: %s", id, err)
return common.CheckDeletedDiag(d,
common.ConvertExpected403ErrInto404Err(err, "code", "SVCSTG.CPTS.4032002"),
"error deleting CPTS project")
}

return nil
Expand Down

0 comments on commit ffa298b

Please sign in to comment.