|
| 1 | +// Generated by PMS #215 |
| 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/httphelper" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" |
| 17 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 18 | +) |
| 19 | + |
| 20 | +func DataSourceCssClusterLogs() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + ReadContext: dataSourceCssClusterLogsRead, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "region": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Computed: true, |
| 29 | + Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, |
| 30 | + }, |
| 31 | + "cluster_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + Description: `Specifies the ID of the cluster.`, |
| 35 | + }, |
| 36 | + "instance_name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + Description: `Specifies the node name.`, |
| 40 | + }, |
| 41 | + "log_type": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + Description: `Specifies the log type.`, |
| 45 | + }, |
| 46 | + "level": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + Description: `Specifies the log level.`, |
| 50 | + }, |
| 51 | + "logs": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Computed: true, |
| 54 | + Description: `The log list.`, |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + "level": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: `The log level.`, |
| 61 | + }, |
| 62 | + "date": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: `The log date.`, |
| 66 | + }, |
| 67 | + "content": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: `The log content.`, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +type ClusterLogsDSWrapper struct { |
| 80 | + *schemas.ResourceDataWrapper |
| 81 | + Config *config.Config |
| 82 | +} |
| 83 | + |
| 84 | +func newClusterLogsDSWrapper(d *schema.ResourceData, meta interface{}) *ClusterLogsDSWrapper { |
| 85 | + return &ClusterLogsDSWrapper{ |
| 86 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 87 | + Config: meta.(*config.Config), |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func dataSourceCssClusterLogsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 92 | + wrapper := newClusterLogsDSWrapper(d, meta) |
| 93 | + showLogBackupRst, err := wrapper.ShowLogBackup() |
| 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.showLogBackupToSchema(showLogBackupRst) |
| 105 | + if err != nil { |
| 106 | + return diag.FromErr(err) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +// @API CSS POST /v1.0/{project_id}/clusters/{cluster_id}/logs/search |
| 113 | +func (w *ClusterLogsDSWrapper) ShowLogBackup() (*gjson.Result, error) { |
| 114 | + client, err := w.NewClient(w.Config, "css") |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + uri := "/v1.0/{project_id}/clusters/{cluster_id}/logs/search" |
| 120 | + uri = strings.ReplaceAll(uri, "{cluster_id}", w.Get("cluster_id").(string)) |
| 121 | + params := map[string]any{ |
| 122 | + "instanceName": w.Get("instance_name"), |
| 123 | + "level": w.Get("level"), |
| 124 | + "logType": w.Get("log_type"), |
| 125 | + "limit": 100, |
| 126 | + } |
| 127 | + params = utils.RemoveNil(params) |
| 128 | + return httphelper.New(client). |
| 129 | + Method("POST"). |
| 130 | + URI(uri). |
| 131 | + Body(params). |
| 132 | + OkCode(200). |
| 133 | + Request(). |
| 134 | + Result() |
| 135 | +} |
| 136 | + |
| 137 | +func (w *ClusterLogsDSWrapper) showLogBackupToSchema(body *gjson.Result) error { |
| 138 | + d := w.ResourceData |
| 139 | + mErr := multierror.Append(nil, |
| 140 | + d.Set("region", w.Config.GetRegion(w.ResourceData)), |
| 141 | + d.Set("logs", schemas.SliceToList(body.Get("logList"), |
| 142 | + func(logs gjson.Result) any { |
| 143 | + return map[string]any{ |
| 144 | + "level": logs.Get("level").Value(), |
| 145 | + "date": logs.Get("date").Value(), |
| 146 | + "content": logs.Get("content").Value(), |
| 147 | + } |
| 148 | + }, |
| 149 | + )), |
| 150 | + ) |
| 151 | + return mErr.ErrorOrNil() |
| 152 | +} |
0 commit comments