|
| 1 | +/* |
| 2 | +Provides a resource to create a scf reserved_concurrency_config |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_scf_reserved_concurrency_config" "reserved_concurrency_config" { |
| 8 | + function_name = "keep-1676351130" |
| 9 | + reserved_concurrency_mem = 128000 |
| 10 | + namespace = "default" |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +scf reserved_concurrency_config can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_scf_reserved_concurrency_config.reserved_concurrency_config reserved_concurrency_config_id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "log" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 32 | + scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416" |
| 33 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudScfReservedConcurrencyConfig() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudScfReservedConcurrencyConfigCreate, |
| 39 | + Read: resourceTencentCloudScfReservedConcurrencyConfigRead, |
| 40 | + Delete: resourceTencentCloudScfReservedConcurrencyConfigDelete, |
| 41 | + Importer: &schema.ResourceImporter{ |
| 42 | + State: schema.ImportStatePassthrough, |
| 43 | + }, |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "function_name": { |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + Type: schema.TypeString, |
| 49 | + Description: "Specifies the function of which you want to configure the reserved quota.", |
| 50 | + }, |
| 51 | + |
| 52 | + "reserved_concurrency_mem": { |
| 53 | + Required: true, |
| 54 | + ForceNew: true, |
| 55 | + Type: schema.TypeInt, |
| 56 | + Description: "Reserved memory quota of the function. Note: the upper limit for the total reserved quota of the function is the user's total concurrency memory minus 12800.", |
| 57 | + }, |
| 58 | + |
| 59 | + "namespace": { |
| 60 | + Optional: true, |
| 61 | + ForceNew: true, |
| 62 | + Default: "default", |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Function namespace. Default value: default.", |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func resourceTencentCloudScfReservedConcurrencyConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 71 | + defer logElapsed("resource.tencentcloud_scf_reserved_concurrency_config.create")() |
| 72 | + defer inconsistentCheck(d, meta)() |
| 73 | + |
| 74 | + logId := getLogId(contextNil) |
| 75 | + |
| 76 | + var ( |
| 77 | + request = scf.NewPutReservedConcurrencyConfigRequest() |
| 78 | + namespace string |
| 79 | + functionName string |
| 80 | + ) |
| 81 | + if v, ok := d.GetOk("function_name"); ok { |
| 82 | + functionName = v.(string) |
| 83 | + request.FunctionName = helper.String(v.(string)) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOkExists("reserved_concurrency_mem"); ok { |
| 87 | + request.ReservedConcurrencyMem = helper.IntUint64(v.(int)) |
| 88 | + } |
| 89 | + |
| 90 | + if v, ok := d.GetOk("namespace"); ok { |
| 91 | + namespace = v.(string) |
| 92 | + request.Namespace = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 96 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseScfClient().PutReservedConcurrencyConfig(request) |
| 97 | + if e != nil { |
| 98 | + return retryError(e) |
| 99 | + } else { |
| 100 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 101 | + } |
| 102 | + return nil |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + log.Printf("[CRITAL]%s create scf ReservedConcurrencyConfig failed, reason:%+v", logId, err) |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + d.SetId(namespace + FILED_SP + functionName) |
| 110 | + |
| 111 | + return resourceTencentCloudScfReservedConcurrencyConfigRead(d, meta) |
| 112 | +} |
| 113 | + |
| 114 | +func resourceTencentCloudScfReservedConcurrencyConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 115 | + defer logElapsed("resource.tencentcloud_scf_reserved_concurrency_config.read")() |
| 116 | + defer inconsistentCheck(d, meta)() |
| 117 | + |
| 118 | + logId := getLogId(contextNil) |
| 119 | + |
| 120 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 121 | + |
| 122 | + service := ScfService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 123 | + |
| 124 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 125 | + if len(idSplit) != 2 { |
| 126 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 127 | + } |
| 128 | + namespace := idSplit[0] |
| 129 | + functionName := idSplit[1] |
| 130 | + |
| 131 | + reservedConcurrencyConfig, err := service.DescribeScfReservedConcurrencyConfigById(ctx, namespace, functionName) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + if reservedConcurrencyConfig == nil { |
| 137 | + d.SetId("") |
| 138 | + log.Printf("[WARN]%s resource `ScfReservedConcurrencyConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + _ = d.Set("namespace", namespace) |
| 143 | + _ = d.Set("function_name", functionName) |
| 144 | + |
| 145 | + if reservedConcurrencyConfig.Response.ReservedMem != nil { |
| 146 | + _ = d.Set("reserved_concurrency_mem", reservedConcurrencyConfig.Response.ReservedMem) |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func resourceTencentCloudScfReservedConcurrencyConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 153 | + defer logElapsed("resource.tencentcloud_scf_reserved_concurrency_config.delete")() |
| 154 | + defer inconsistentCheck(d, meta)() |
| 155 | + |
| 156 | + logId := getLogId(contextNil) |
| 157 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 158 | + |
| 159 | + service := ScfService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 160 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 161 | + if len(idSplit) != 2 { |
| 162 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 163 | + } |
| 164 | + namespace := idSplit[0] |
| 165 | + functionName := idSplit[1] |
| 166 | + |
| 167 | + if err := service.DeleteScfReservedConcurrencyConfigById(ctx, namespace, functionName); err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
0 commit comments