|
| 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