|
| 1 | +package cts |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 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 DataSourceCheckBucket() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + ReadContext: dataSourceCtsCheckBucketRead, |
| 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 | + "bucket_name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: `Specifies the OBS bucket name.`, |
| 34 | + }, |
| 35 | + "bucket_location": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + Description: `Specifies the OBS bucket location.`, |
| 39 | + }, |
| 40 | + "kms_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: `Specifies the Key ID used for encrypting transferred trace files.`, |
| 44 | + }, |
| 45 | + "is_support_trace_files_encryption": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + Description: `Specifies whether trace files are encrypted during transfer to an OBS bucket.`, |
| 49 | + }, |
| 50 | + "buckets": { |
| 51 | + Type: schema.TypeList, |
| 52 | + Computed: true, |
| 53 | + Description: `The OBS bucket information.`, |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "bucket_name": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: `The OBS bucket name.`, |
| 60 | + }, |
| 61 | + "bucket_location": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: `The OBS bucket location.`, |
| 65 | + }, |
| 66 | + "kms_id": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: `The Key ID used for encrypting transferred trace files.`, |
| 70 | + }, |
| 71 | + "is_support_trace_files_encryption": { |
| 72 | + Type: schema.TypeBool, |
| 73 | + Computed: true, |
| 74 | + Description: `Whether trace files are encrypted during transfer to an OBS bucket.`, |
| 75 | + }, |
| 76 | + "check_bucket_response": { |
| 77 | + Type: schema.TypeList, |
| 78 | + Computed: true, |
| 79 | + Description: `The check result of the OBS bucket.`, |
| 80 | + Elem: &schema.Resource{ |
| 81 | + Schema: map[string]*schema.Schema{ |
| 82 | + "error_code": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: `The error code.`, |
| 86 | + }, |
| 87 | + "error_message": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: `The error message.`, |
| 91 | + }, |
| 92 | + "response_code": { |
| 93 | + Type: schema.TypeInt, |
| 94 | + Computed: true, |
| 95 | + Description: `The returned HTTP status code.`, |
| 96 | + }, |
| 97 | + "success": { |
| 98 | + Type: schema.TypeBool, |
| 99 | + Computed: true, |
| 100 | + Description: `Whether the transfer is successful.`, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +type CheckBucketDSWrapper struct { |
| 113 | + *schemas.ResourceDataWrapper |
| 114 | + Config *config.Config |
| 115 | +} |
| 116 | + |
| 117 | +func newCheckBucketDSWrapper(d *schema.ResourceData, meta interface{}) *CheckBucketDSWrapper { |
| 118 | + return &CheckBucketDSWrapper{ |
| 119 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 120 | + Config: meta.(*config.Config), |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func dataSourceCtsCheckBucketRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 125 | + wrapper := newCheckBucketDSWrapper(d, meta) |
| 126 | + checkObsBucketsRst, err := wrapper.CheckObsBuckets() |
| 127 | + if err != nil { |
| 128 | + return diag.FromErr(err) |
| 129 | + } |
| 130 | + |
| 131 | + id, err := uuid.GenerateUUID() |
| 132 | + if err != nil { |
| 133 | + return diag.FromErr(err) |
| 134 | + } |
| 135 | + d.SetId(id) |
| 136 | + |
| 137 | + err = wrapper.checkObsBucketsToSchema(checkObsBucketsRst) |
| 138 | + if err != nil { |
| 139 | + return diag.FromErr(err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +// @API CTS POST /v3/{domain_id}/checkbucket |
| 146 | +func (w *CheckBucketDSWrapper) CheckObsBuckets() (*gjson.Result, error) { |
| 147 | + client, err := w.NewClient(w.Config, "cts") |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + uri := "/v3/{domain_id}/checkbucket" |
| 153 | + uri = strings.ReplaceAll(uri, "{domain_id}", w.Config.DomainID) |
| 154 | + params := map[string]any{ |
| 155 | + "bucket_location": w.Get("bucket_location"), |
| 156 | + "bucket_name": w.Get("bucket_name"), |
| 157 | + "is_support_trace_files_encryption": w.Get("is_support_trace_files_encryption"), |
| 158 | + "kms_id": w.Get("kms_id"), |
| 159 | + } |
| 160 | + params = utils.RemoveNil(params) |
| 161 | + reqBody := map[string]any{ |
| 162 | + "buckets": []map[string]any{params}, |
| 163 | + } |
| 164 | + return httphelper.New(client). |
| 165 | + Method("POST"). |
| 166 | + URI(uri). |
| 167 | + Body(reqBody). |
| 168 | + Request(). |
| 169 | + Result() |
| 170 | +} |
| 171 | + |
| 172 | +func (w *CheckBucketDSWrapper) checkObsBucketsToSchema(body *gjson.Result) error { |
| 173 | + d := w.ResourceData |
| 174 | + mErr := multierror.Append(nil, |
| 175 | + d.Set("region", w.Config.GetRegion(w.ResourceData)), |
| 176 | + d.Set("buckets", schemas.SliceToList(body.Get("buckets"), |
| 177 | + func(buckets gjson.Result) any { |
| 178 | + return map[string]any{ |
| 179 | + "bucket_name": buckets.Get("bucket_name").Value(), |
| 180 | + "bucket_location": buckets.Get("bucket_location").Value(), |
| 181 | + "kms_id": buckets.Get("kms_id").Value(), |
| 182 | + "is_support_trace_files_encryption": buckets.Get("is_support_trace_files_encryption").Value(), |
| 183 | + "check_bucket_response": schemas.SliceToList(buckets.Get("check_bucket_response"), |
| 184 | + func(checkBucketResponse gjson.Result) any { |
| 185 | + return map[string]any{ |
| 186 | + "error_code": checkBucketResponse.Get("error_code").Value(), |
| 187 | + "error_message": checkBucketResponse.Get("error_message").Value(), |
| 188 | + "response_code": checkBucketResponse.Get("response_code").Value(), |
| 189 | + "success": checkBucketResponse.Get("success").Value(), |
| 190 | + } |
| 191 | + }, |
| 192 | + ), |
| 193 | + } |
| 194 | + }, |
| 195 | + )), |
| 196 | + ) |
| 197 | + return mErr.ErrorOrNil() |
| 198 | +} |
0 commit comments