|
| 1 | +package cos |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 11 | + "github.com/tencentyun/cos-go-sdk-v5" |
| 12 | + |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func DataSourceTencentCloudCosObjectSignedUrl() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Read: DataSourceTencentCloudCosObjectSignedUrlRead, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "bucket": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + Description: "Name of the bucket.", |
| 25 | + }, |
| 26 | + "path": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + Description: "The full path to the object inside the bucket.", |
| 30 | + }, |
| 31 | + "method": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Optional: true, |
| 34 | + Default: "GET", |
| 35 | + ValidateFunc: validation.StringInSlice([]string{"GET", "PUT"}, true), |
| 36 | + Description: "Method, GET or PUT. Default value is GET.", |
| 37 | + }, |
| 38 | + "duration": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Default: "1m", |
| 42 | + Description: "Duration of signed url. Default value is 1m.", |
| 43 | + }, |
| 44 | + "headers": { |
| 45 | + Type: schema.TypeMap, |
| 46 | + Optional: true, |
| 47 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 48 | + Description: "Request headers.", |
| 49 | + }, |
| 50 | + "queries": { |
| 51 | + Type: schema.TypeMap, |
| 52 | + Optional: true, |
| 53 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 54 | + Description: "Request query parameters.", |
| 55 | + }, |
| 56 | + "signed_url": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Sensitive: true, |
| 60 | + Description: "Signed URL.", |
| 61 | + }, |
| 62 | + "result_output_file": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + Description: "Used to save results.", |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func DataSourceTencentCloudCosObjectSignedUrlRead(d *schema.ResourceData, meta interface{}) error { |
| 72 | + defer tccommon.LogElapsed("data_source.tencentcloud_cos_object_signed_url.read")() |
| 73 | + |
| 74 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 75 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 76 | + |
| 77 | + bucket := d.Get("bucket").(string) |
| 78 | + path := d.Get("path").(string) |
| 79 | + method := "GET" |
| 80 | + durationString := "1m" |
| 81 | + opt := &cos.PresignedURLOptions{} |
| 82 | + signHost := true |
| 83 | + |
| 84 | + if v, ok := d.GetOk("method"); ok { |
| 85 | + method = v.(string) |
| 86 | + } |
| 87 | + |
| 88 | + if v, ok := d.GetOk("duration"); ok { |
| 89 | + durationString = v.(string) |
| 90 | + } |
| 91 | + |
| 92 | + duration, err := time.ParseDuration(durationString) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + if v, ok := d.GetOk("headers"); ok { |
| 98 | + for key, value := range v.(map[string]string) { |
| 99 | + opt.Header.Set(key, value) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if v, ok := d.GetOk("queries"); ok { |
| 104 | + for key, value := range v.(map[string]string) { |
| 105 | + opt.Query.Set(key, value) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket).Object.GetPresignedURL2(ctx, method, path, duration, opt, signHost) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + signedUrl := result.String() |
| 115 | + |
| 116 | + d.SetId(helper.DataResourceIdHash(path)) |
| 117 | + _ = d.Set("signed_url", signedUrl) |
| 118 | + output, ok := d.GetOk("result_output_file") |
| 119 | + if ok && output.(string) != "" { |
| 120 | + if err := tccommon.WriteToFile(output.(string), signedUrl); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
0 commit comments