|
| 1 | +// Generated by PMS #202 |
| 2 | +package css |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/go-multierror" |
| 9 | + "github.com/hashicorp/go-uuid" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/tidwall/gjson" |
| 13 | + |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" |
| 17 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" |
| 18 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 19 | +) |
| 20 | + |
| 21 | +func DataSourceCssLogBackupRecords() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + ReadContext: dataSourceCssLogBackupRecordsRead, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "region": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Computed: true, |
| 30 | + Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, |
| 31 | + }, |
| 32 | + "cluster_id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + Description: `Specifies the ID of the CSS cluster.`, |
| 36 | + }, |
| 37 | + "job_id": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Description: `Specifies the ID of the log backup job.`, |
| 41 | + }, |
| 42 | + "type": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Description: `Specifies the type of the log backup job.`, |
| 46 | + }, |
| 47 | + "status": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Description: `Specifies the status of the log backup job.`, |
| 51 | + }, |
| 52 | + "records": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Description: `The list of the CSS cluster log backup records.`, |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "id": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: `The ID of the log backup job.`, |
| 62 | + }, |
| 63 | + "type": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: `The type of the log backup job.`, |
| 67 | + }, |
| 68 | + "status": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: `The status of the log backup job.`, |
| 72 | + }, |
| 73 | + "cluster_id": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: `The ID of the CSS cluster.`, |
| 77 | + }, |
| 78 | + "log_path": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: `The storage path of backed up logs in the OBS bucket.`, |
| 82 | + }, |
| 83 | + "create_at": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: `The creation time.`, |
| 87 | + }, |
| 88 | + "finished_at": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: `The end time.`, |
| 92 | + }, |
| 93 | + "failed_msg": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: `The error information.`, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +type LogBackupRecordsDSWrapper struct { |
| 106 | + *schemas.ResourceDataWrapper |
| 107 | + Config *config.Config |
| 108 | +} |
| 109 | + |
| 110 | +func newLogBackupRecordsDSWrapper(d *schema.ResourceData, meta interface{}) *LogBackupRecordsDSWrapper { |
| 111 | + return &LogBackupRecordsDSWrapper{ |
| 112 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 113 | + Config: meta.(*config.Config), |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func dataSourceCssLogBackupRecordsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 118 | + wrapper := newLogBackupRecordsDSWrapper(d, meta) |
| 119 | + listLogsJobRst, err := wrapper.ListLogsJob() |
| 120 | + if err != nil { |
| 121 | + return diag.FromErr(err) |
| 122 | + } |
| 123 | + |
| 124 | + id, err := uuid.GenerateUUID() |
| 125 | + if err != nil { |
| 126 | + return diag.FromErr(err) |
| 127 | + } |
| 128 | + d.SetId(id) |
| 129 | + |
| 130 | + err = wrapper.listLogsJobToSchema(listLogsJobRst) |
| 131 | + if err != nil { |
| 132 | + return diag.FromErr(err) |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// @API CSS GET /v1.0/{project_id}/clusters/{cluster_id}/logs/records |
| 139 | +func (w *LogBackupRecordsDSWrapper) ListLogsJob() (*gjson.Result, error) { |
| 140 | + client, err := w.NewClient(w.Config, "css") |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + uri := "/v1.0/{project_id}/clusters/{cluster_id}/logs/records" |
| 146 | + uri = strings.ReplaceAll(uri, "{cluster_id}", w.Get("cluster_id").(string)) |
| 147 | + return httphelper.New(client). |
| 148 | + Method("GET"). |
| 149 | + URI(uri). |
| 150 | + OffsetStart(1). |
| 151 | + OffsetPager("clusterLogRecord", "start", "limit", 10). |
| 152 | + Filter( |
| 153 | + filters.New().From("clusterLogRecord"). |
| 154 | + Where("jobId", "=", w.Get("job_id")). |
| 155 | + Where("jobTypes", "=", w.Get("type")). |
| 156 | + Where("status", "=", w.Get("status")), |
| 157 | + ). |
| 158 | + Request(). |
| 159 | + Result() |
| 160 | +} |
| 161 | + |
| 162 | +func (w *LogBackupRecordsDSWrapper) listLogsJobToSchema(body *gjson.Result) error { |
| 163 | + d := w.ResourceData |
| 164 | + mErr := multierror.Append(nil, |
| 165 | + d.Set("region", w.Config.GetRegion(w.ResourceData)), |
| 166 | + d.Set("records", schemas.SliceToList(body.Get("clusterLogRecord"), |
| 167 | + func(records gjson.Result) any { |
| 168 | + return map[string]any{ |
| 169 | + "id": records.Get("jobId").Value(), |
| 170 | + "type": records.Get("jobTypes").Value(), |
| 171 | + "status": records.Get("status").Value(), |
| 172 | + "cluster_id": records.Get("clusterId").Value(), |
| 173 | + "log_path": records.Get("logPath").Value(), |
| 174 | + "create_at": w.setCluLogRecCreAt(records), |
| 175 | + "finished_at": w.setCluLogRecFinAt(records), |
| 176 | + "failed_msg": records.Get("failedMsg").Value(), |
| 177 | + } |
| 178 | + }, |
| 179 | + )), |
| 180 | + ) |
| 181 | + return mErr.ErrorOrNil() |
| 182 | +} |
| 183 | + |
| 184 | +func (*LogBackupRecordsDSWrapper) setCluLogRecCreAt(data gjson.Result) string { |
| 185 | + return utils.FormatTimeStampRFC3339(data.Get("createAt").Int()/1000, false) |
| 186 | +} |
| 187 | + |
| 188 | +func (*LogBackupRecordsDSWrapper) setCluLogRecFinAt(data gjson.Result) string { |
| 189 | + finishedAt := data.Get("finishedAt").Value() |
| 190 | + if finishedAt != nil { |
| 191 | + return utils.FormatTimeStampRFC3339(int64(finishedAt.(float64))/1000, false) |
| 192 | + } |
| 193 | + |
| 194 | + return "" |
| 195 | +} |
0 commit comments