|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 6 | + "github.com/ovh/terraform-provider-ovh/ovh/helpers" |
| 7 | + "net/url" |
| 8 | + "strconv" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceCloudProjectRegionStoragePresign() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceCloudProjectRegionStoragePresignCreate, |
| 15 | + Read: schema.Noop, |
| 16 | + Delete: schema.Noop, |
| 17 | + |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "service_name": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + ForceNew: true, |
| 23 | + DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil), |
| 24 | + Description: "Service name of the resource representing the ID of the cloud project.", |
| 25 | + }, |
| 26 | + "region_name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "Region name.", |
| 31 | + }, |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ForceNew: true, |
| 36 | + Description: "The S3 storage container's name.", |
| 37 | + }, |
| 38 | + "expire": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + Description: "How long (in seconds) the URL will be valid.", |
| 43 | + }, |
| 44 | + "method": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { |
| 49 | + err := helpers.ValidateStringEnum(v.(string), []string{"GET", "PUT"}) |
| 50 | + if err != nil { |
| 51 | + errors = append(errors, err) |
| 52 | + } |
| 53 | + return |
| 54 | + }, |
| 55 | + }, |
| 56 | + "object": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + ForceNew: true, |
| 60 | + Description: "Name of the object to download or upload.", |
| 61 | + }, |
| 62 | + |
| 63 | + // Computed |
| 64 | + "url": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "Presigned URL.", |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func resourceCloudProjectRegionStoragePresignCreate(d *schema.ResourceData, meta interface{}) error { |
| 74 | + config := meta.(*Config) |
| 75 | + serviceName := d.Get("service_name").(string) |
| 76 | + regionName := d.Get("region_name").(string) |
| 77 | + name := d.Get("name").(string) |
| 78 | + |
| 79 | + resp := &PresignedURL{} |
| 80 | + opts := (&PresignedURLInput{}).FromResource(d) |
| 81 | + |
| 82 | + endpoint := fmt.Sprintf("/cloud/project/%s/region/%s/storage/%s/presign", url.PathEscape(serviceName), url.PathEscape(regionName), url.PathEscape(name)) |
| 83 | + if err := config.OVHClient.Post(endpoint, opts, resp); err != nil { |
| 84 | + return fmt.Errorf("Error calling post %s:\n\t %q", endpoint, err) |
| 85 | + } |
| 86 | + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) |
| 87 | + err := d.Set("url", resp.URL) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
0 commit comments