|
| 1 | +package wedata |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + wedatav20250806 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata/v20250806" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudWedataResourceFile() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudWedataResourceFileCreate, |
| 20 | + Read: resourceTencentCloudWedataResourceFileRead, |
| 21 | + Update: resourceTencentCloudWedataResourceFileUpdate, |
| 22 | + Delete: resourceTencentCloudWedataResourceFileDelete, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "project_id": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + Description: "Project id.", |
| 29 | + }, |
| 30 | + |
| 31 | + "resource_name": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + Description: "The resource file name should be consistent with the uploaded file name as much as possible.", |
| 35 | + }, |
| 36 | + |
| 37 | + "bucket_name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + Description: "cos bucket name, which can be obtained from the GetResourceCosPath interface.", |
| 41 | + }, |
| 42 | + |
| 43 | + "cos_region": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + Description: "The cos bucket area corresponding to the BucketName bucket.", |
| 47 | + }, |
| 48 | + |
| 49 | + "parent_folder_path": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + Description: "The path to upload resource files in the project, example value: /wedata/qxxxm/, root directory, please use/.", |
| 53 | + }, |
| 54 | + |
| 55 | + "resource_file": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Required: true, |
| 58 | + Description: "- You can only choose one of the two methods of uploading a file and manually filling. If both are provided, the order of values is file> manual filling value\n-the manual filling value must be the existing cos path, /datastudio/resource/is a fixed prefix, projectId is the project ID, and a specific value needs to be passed in, parentFolderPath is the parent folder path, name is the file name, and examples of manual filling value values are: /datastudio/resource/projectId/parentFolderPath/name \n.", |
| 59 | + }, |
| 60 | + |
| 61 | + "bundle_id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + Description: "bundle client ID.", |
| 65 | + }, |
| 66 | + |
| 67 | + "bundle_info": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Optional: true, |
| 70 | + Description: "bundle client information.", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func resourceTencentCloudWedataResourceFileCreate(d *schema.ResourceData, meta interface{}) error { |
| 77 | + defer tccommon.LogElapsed("resource.tencentcloud_wedata_resource_file.create")() |
| 78 | + defer tccommon.InconsistentCheck(d, meta)() |
| 79 | + |
| 80 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 81 | + |
| 82 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 83 | + |
| 84 | + var ( |
| 85 | + projectId string |
| 86 | + resourceId string |
| 87 | + ) |
| 88 | + var ( |
| 89 | + request = wedatav20250806.NewCreateResourceFileRequest() |
| 90 | + response = wedatav20250806.NewCreateResourceFileResponse() |
| 91 | + ) |
| 92 | + |
| 93 | + if v, ok := d.GetOk("project_id"); ok { |
| 94 | + projectId = v.(string) |
| 95 | + request.ProjectId = helper.String(projectId) |
| 96 | + } |
| 97 | + |
| 98 | + if v, ok := d.GetOk("resource_name"); ok { |
| 99 | + request.ResourceName = helper.String(v.(string)) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := d.GetOk("bucket_name"); ok { |
| 103 | + request.BucketName = helper.String(v.(string)) |
| 104 | + } |
| 105 | + |
| 106 | + if v, ok := d.GetOk("cos_region"); ok { |
| 107 | + request.CosRegion = helper.String(v.(string)) |
| 108 | + } |
| 109 | + |
| 110 | + if v, ok := d.GetOk("parent_folder_path"); ok { |
| 111 | + request.ParentFolderPath = helper.String(v.(string)) |
| 112 | + } |
| 113 | + |
| 114 | + if v, ok := d.GetOk("resource_file"); ok { |
| 115 | + request.ResourceFile = helper.String(v.(string)) |
| 116 | + } |
| 117 | + |
| 118 | + if v, ok := d.GetOk("bundle_id"); ok { |
| 119 | + request.BundleId = helper.String(v.(string)) |
| 120 | + } |
| 121 | + |
| 122 | + if v, ok := d.GetOk("bundle_info"); ok { |
| 123 | + request.BundleInfo = helper.String(v.(string)) |
| 124 | + } |
| 125 | + |
| 126 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 127 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseWedataV20250806Client().CreateResourceFileWithContext(ctx, request) |
| 128 | + if e != nil { |
| 129 | + return tccommon.RetryError(e) |
| 130 | + } else { |
| 131 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 132 | + } |
| 133 | + response = result |
| 134 | + return nil |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + log.Printf("[CRITAL]%s create wedata resource file failed, reason:%+v", logId, err) |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + if response.Response.Data != nil && response.Response.Data.ResourceId != nil { |
| 142 | + resourceId = *response.Response.Data.ResourceId |
| 143 | + d.SetId(strings.Join([]string{projectId, resourceId}, tccommon.FILED_SP)) |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + return resourceTencentCloudWedataResourceFileRead(d, meta) |
| 148 | +} |
| 149 | + |
| 150 | +func resourceTencentCloudWedataResourceFileRead(d *schema.ResourceData, meta interface{}) error { |
| 151 | + defer tccommon.LogElapsed("resource.tencentcloud_wedata_resource_file.read")() |
| 152 | + defer tccommon.InconsistentCheck(d, meta)() |
| 153 | + |
| 154 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 155 | + |
| 156 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 157 | + |
| 158 | + service := WedataService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 159 | + |
| 160 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 161 | + if len(idSplit) != 2 { |
| 162 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 163 | + } |
| 164 | + projectId := idSplit[0] |
| 165 | + resourceId := idSplit[1] |
| 166 | + |
| 167 | + respData, err := service.DescribeWedataResourceFileById(ctx, projectId, resourceId) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + if respData == nil { |
| 173 | + d.SetId("") |
| 174 | + log.Printf("[WARN]%s resource `wedata_resource_file` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 175 | + return nil |
| 176 | + } |
| 177 | + |
| 178 | + _ = d.Set("project_id", projectId) |
| 179 | + if respData.ResourceName != nil { |
| 180 | + _ = d.Set("resource_name", respData.ResourceName) |
| 181 | + } |
| 182 | + |
| 183 | + if respData.BucketName != nil { |
| 184 | + _ = d.Set("bucket_name", respData.BucketName) |
| 185 | + } |
| 186 | + |
| 187 | + if respData.CosRegion != nil { |
| 188 | + _ = d.Set("cos_region", respData.CosRegion) |
| 189 | + } |
| 190 | + |
| 191 | + if respData.BundleId != nil { |
| 192 | + _ = d.Set("bundle_id", respData.BundleId) |
| 193 | + } |
| 194 | + |
| 195 | + if respData.BundleInfo != nil { |
| 196 | + _ = d.Set("bundle_info", respData.BundleInfo) |
| 197 | + } |
| 198 | + |
| 199 | + _ = projectId |
| 200 | + _ = resourceId |
| 201 | + return nil |
| 202 | +} |
| 203 | + |
| 204 | +func resourceTencentCloudWedataResourceFileUpdate(d *schema.ResourceData, meta interface{}) error { |
| 205 | + defer tccommon.LogElapsed("resource.tencentcloud_wedata_resource_file.update")() |
| 206 | + defer tccommon.InconsistentCheck(d, meta)() |
| 207 | + |
| 208 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 209 | + |
| 210 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 211 | + |
| 212 | + immutableArgs := []string{"bucket_name", "cos_region", "parent_folder_path"} |
| 213 | + for _, v := range immutableArgs { |
| 214 | + if d.HasChange(v) { |
| 215 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 216 | + } |
| 217 | + } |
| 218 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 219 | + if len(idSplit) != 2 { |
| 220 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 221 | + } |
| 222 | + projectId := idSplit[0] |
| 223 | + resourceId := idSplit[1] |
| 224 | + |
| 225 | + needChange := false |
| 226 | + mutableArgs := []string{"resource_file", "resource_name", "bundle_id", "bundle_info"} |
| 227 | + for _, v := range mutableArgs { |
| 228 | + if d.HasChange(v) { |
| 229 | + needChange = true |
| 230 | + break |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if needChange { |
| 235 | + request := wedatav20250806.NewUpdateResourceFileRequest() |
| 236 | + request.ProjectId = helper.String(projectId) |
| 237 | + request.ResourceId = helper.String(resourceId) |
| 238 | + |
| 239 | + if v, ok := d.GetOk("resource_file"); ok { |
| 240 | + request.ResourceFile = helper.String(v.(string)) |
| 241 | + } |
| 242 | + |
| 243 | + if v, ok := d.GetOk("resource_name"); ok { |
| 244 | + request.ResourceName = helper.String(v.(string)) |
| 245 | + } |
| 246 | + |
| 247 | + if v, ok := d.GetOk("bundle_id"); ok { |
| 248 | + request.BundleId = helper.String(v.(string)) |
| 249 | + } |
| 250 | + |
| 251 | + if v, ok := d.GetOk("bundle_info"); ok { |
| 252 | + request.BundleInfo = helper.String(v.(string)) |
| 253 | + } |
| 254 | + |
| 255 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 256 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseWedataV20250806Client().UpdateResourceFileWithContext(ctx, request) |
| 257 | + if e != nil { |
| 258 | + return tccommon.RetryError(e) |
| 259 | + } else { |
| 260 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 261 | + } |
| 262 | + return nil |
| 263 | + }) |
| 264 | + if err != nil { |
| 265 | + log.Printf("[CRITAL]%s update wedata resource file failed, reason:%+v", logId, err) |
| 266 | + return err |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + _ = projectId |
| 271 | + _ = resourceId |
| 272 | + return resourceTencentCloudWedataResourceFileRead(d, meta) |
| 273 | +} |
| 274 | + |
| 275 | +func resourceTencentCloudWedataResourceFileDelete(d *schema.ResourceData, meta interface{}) error { |
| 276 | + defer tccommon.LogElapsed("resource.tencentcloud_wedata_resource_file.delete")() |
| 277 | + defer tccommon.InconsistentCheck(d, meta)() |
| 278 | + |
| 279 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 280 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 281 | + |
| 282 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 283 | + if len(idSplit) != 2 { |
| 284 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 285 | + } |
| 286 | + projectId := idSplit[0] |
| 287 | + resourceId := idSplit[1] |
| 288 | + |
| 289 | + var ( |
| 290 | + request = wedatav20250806.NewDeleteResourceFileRequest() |
| 291 | + response = wedatav20250806.NewDeleteResourceFileResponse() |
| 292 | + ) |
| 293 | + |
| 294 | + request.ProjectId = helper.String(projectId) |
| 295 | + request.ResourceId = helper.String(resourceId) |
| 296 | + |
| 297 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 298 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseWedataV20250806Client().DeleteResourceFileWithContext(ctx, request) |
| 299 | + if e != nil { |
| 300 | + return tccommon.RetryError(e) |
| 301 | + } else { |
| 302 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 303 | + } |
| 304 | + response = result |
| 305 | + return nil |
| 306 | + }) |
| 307 | + if err != nil { |
| 308 | + log.Printf("[CRITAL]%s delete wedata resource file failed, reason:%+v", logId, err) |
| 309 | + return err |
| 310 | + } |
| 311 | + |
| 312 | + _ = response |
| 313 | + _ = projectId |
| 314 | + _ = resourceId |
| 315 | + return nil |
| 316 | +} |
0 commit comments